您的位置:首页 > 编程语言 > Java开发

HDOJ 今年暑假不AC JAVA 2037

2017-11-22 19:57 225 查看
简单贪心

很简单的一道题,要多看电视,就按结束时间排序,然后过一遍就可以了。

不多说,看代码

/**
*
*/
/**
* @author Enron
*
*/
package _2037;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
class Node implements Comparable<Node>{
int s,e;
Node(int a,int b){
s=a;e=b;
}
@Override
public int compareTo(Node o) {
if(this.e!=o.e) return this.e-o.e;
return this.s-o.s;
}
}
public class Main{

public static void main(String[] args){
Scanner sc= new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
if(n==0) break;
List<Node> list = new ArrayList<Node>();
for (int i = 0; i < n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
list.add(new Node(a,b));
}
Collections.sort(list);
int ans = 0,e=0;
for (int i = 0; i < list.size(); i++) {
if(list.get(i).s>=e){
e = list.get(i).e;
ans++;
}
}
System.out.println(ans);

}
}
}
////1 3
////3 4
//0 7
//3 8
//2 9
////5 10
//6 12
//4 14
////10 15
//8 18
////15 19
//15 20



今年暑假不AC

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 63520    Accepted Submission(s): 34265


Problem Description

“今年暑假不AC?”

“是的。”

“那你干什么呢?”

“看世界杯呀,笨蛋!”

“@#$%^&*%...”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。

作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

 

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

 

Author

lcy

 

Source

ACM程序设计期末考试(2006/06/07)

 

Recommend

lcy
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA HDOJ 贪心