阿里2018实习生校招直播!看直播,投简历.... |
今年暑假不ACTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) 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.Scanner;
public class P2037 {//贪心
public static void GreedySelector(int n,int s[], int e[]){
int j=0;
int count=1;
for(int i=1;i<n;i++){
if(s[i]>=e[j]){
j=i;
count++;
}
}
System.out.println(count);
}
public static void sort(int n,int s[],int e[]){
for(int i=0;i<e.length-1;i++){
int temp=e[i+1];
int j=i;
int temps;
while(e[j]>temp){
e[j+1]=e[j];
temps=s[j+1];
s[j+1]=s[j];
s[j]=temps;
j--;
if(j<0){
break;
}
}
e[j+1]=temp;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int s[]=new int[n];
int e[]=new int[n];
for(int i=0;i<n;i++){
s[i]=sc.nextInt();
e[i]=sc.nextInt();
}
if(n==0){
break;
}
sort(n,s,e);
GreedySelector(n, s, e);
}
}
}