今年暑假不AC--贪心算法
2017-06-08 16:19
281 查看
[align=left]Problem Description[/align]
“今年暑假不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
java实现:
运行结果:
C++实现代码:
“今年暑假不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
java实现:
package com.company; import java.util.*; public class AC { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int count = 1; while(scanner.hasNext()){ int n = scanner.nextInt(); if(n==0){ break; } List<TV> list = new ArrayList<TV>(); for(int i=0;i<n;i++){ int start = scanner.nextInt(); int end = scanner.nextInt(); TV tv = new TV(start,end); list.add(tv); } Collections.sort(list,new MyComparator()); int m = list.get(0).end; for(int i=1;i<n;i++){ if(list.get(i).start>=m){ count++; m=list.get(i).end; } } } scanner.close(); System.out.println(count); } } class TV{ int start; int end; public TV(int start,int end){ this.start = start; this.end = end; } } class MyComparator implements Comparator<TV>{ @Override public int compare(TV o1, TV o2) { if(o1.end != o2.end){ return o1.end - o2.end; }else{ return o1.start - o2.start; } } }
运行结果:
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 5 Process finished with exit code 0
C++实现代码:
#include <iostream> #include <algorithm> using namespace std; struct node { int f; int e; }a[500]; bool cmp(node x,node y) { if(x.e<y.e) return true; else if(x.e==y.e) { if(x.f<y.f) return true; else return false; } else return false; } int main() { int n,i,j,s; while(cin>>n) { if(n==0) break; for(i=0;i<n;i++) cin>>a[i].f>>a[i].e; sort(a,a+n,cmp); s=1; for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[j].f>=a[i].e) { i=j-1; s++; break; } } } cout<<s<<endl; } return 0; }
相关文章推荐
- hdu_2037_今年暑假不AC(贪心算法)
- 杭电 2037 今年暑假不AC 贪心算法 附解题思路
- hdu 1.3.1今年暑假不AC(贪心算法)
- hdu 2037 今年暑假不AC (贪心算法)
- 【贪心算法】今年暑假不AC
- Hdu2037 - 今年暑假不AC - 贪心算法
- 今年暑假不AC(贪心算法)
- 贪心算法感悟之区间问题(白书例题和HDU2037 今年暑假不AC)
- HDU 2037 今年暑假不AC (简单贪心算法)
- ACM HDOJ 今年暑假不AC(贪心算法)
- 贪心算法初识——对今年暑假不AC问题的两种理解
- HDU-2037今年暑假不AC(贪心算法)
- 【练习12】 贪心算法 1002 今年暑假不AC
- 贪心算法小结。今年暑假不AC&&Radar Installation&&Y2K Accounting Bug&&Minimal Coverage
- HDOJ,杭电 2037 ,今年暑假不AC。贪心算法入门题目
- hud——2037今年暑假不AC(贪心算法)
- 杭电-2037-今年暑假不AC【贪心算法】
- WUST OJ 1187 今年暑假不AC(贪心算法)
- hdn2037今年暑假不AC(结构体+贪心算法)
- 【hdu2037】今年暑假不AC——贪心算法