poj 2726 Holiday Hotel
2007/10/22
java
poj
2726 -- Holiday Hotel
参考:
ACM PKU 2726 Holiday Hotel - AClayton's ACM/ICPC Life 只切菜题 菜鸟乱飞 - C++博客
けこーん - 危ないRiSKのブ・ロ・グ
参考サイトを参考にしました。
ひっかかったのは、0が入力されるまで繰り返しテストケースを実行するというところでした。
わかってしまえばどうってことないのですが...
package p2726_not_solved;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static class Node {
int D;
int C;
Node(int D, int C) {
this.D = D;
this.C = C;
}
public String toString() {
return "D:" + D + " C:" + C;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
while ((n = scanner.nextInt()) != 0) {
Node[] nodes = new Node[n];
for (int i = 0; i < n; i++) {
int D = scanner.nextInt();
int C = scanner.nextInt();
nodes[i] = new Node(D, C);
}
Comparator comparator = new Comparator() {
public int compare(Node o1, Node o2) {
if (o1.D == o2.D)
return o1.C - o2.C;
return o1.D - o2.D;
}
};
Arrays.sort(nodes, comparator);
for (Node node : nodes) {
// System.out.println(node);
}
int total = 1;
int min = nodes[0].C;
// System.out.println(nodes[0]);
for (int i = 1; i <= n - 1; i++) {
if (min > nodes[i].C) {
min = nodes[i].C;
// System.out.println(nodes[i]);
total++;
}
}
System.out.println(total);
}
}
}
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
static class Node {
int D;
int C;
Node(int D, int C) {
this.D = D;
this.C = C;
}
public String toString() {
return "D:" + D + " C:" + C;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
while ((n = scanner.nextInt()) != 0) {
Node[] nodes = new Node[n];
for (int i = 0; i < n; i++) {
int D = scanner.nextInt();
int C = scanner.nextInt();
nodes[i] = new Node(D, C);
}
Comparator
public int compare(Node o1, Node o2) {
if (o1.D == o2.D)
return o1.C - o2.C;
return o1.D - o2.D;
}
};
Arrays.sort(nodes, comparator);
for (Node node : nodes) {
// System.out.println(node);
}
int total = 1;
int min = nodes[0].C;
// System.out.println(nodes[0]);
for (int i = 1; i <= n - 1; i++) {
if (min > nodes[i].C) {
min = nodes[i].C;
// System.out.println(nodes[i]);
total++;
}
}
System.out.println(total);
}
}
}
: