今年暑假不ACTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 79365 Accepted Submission(s): 42409 Problem Description “今年暑假不AC?”
Input 输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。
Output 对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。
Sample Input 12 1 3 3 4 0 7 3 8 15 19 15 20 10 15 8 18 6 12 5 10 4 14 2 9 0
Sample Output 5 |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n != 0){
Process[] pros = new Process[n];
for (int i = 0; i < n; i++) {
pros[i] = new Process(sc.nextInt(), sc.nextInt());
}
Arrays.sort(pros);
int res = 1;
int beginTime = pros[0].begin;
for (int i = 1; i < n; i++) {
if(beginTime <= pros[i].start) {
res++;
beginTime = pros[i].begin;
}
}
System.out.println(res);
n = sc.nextInt();
}
}
}
class Process implements Comparable<Process> {
int start;
int begin;
Process(int start, int begin) {
this.start = start;
this.begin = begin;
}
@Override
public int compareTo(Process o) {
return this.begin - o.begin;
}
}
AC代码。