您的位置:首页 > 其它

杭电acm2037:尽可能多的看电视节目

2017-09-23 10:17 288 查看
思路:将电视节目按结束时间从小到大排列,越早结束,越能看更多的节目;

以结束时间顺序找上个节目结束后能开始的节目,统计个数;

额,好像是贪心算法的一个简单题,没到那个层次,作为小白的我任重而道远啊- -!。

#include <iostream>
using namespace std;
struct program{
int start_time;
int end_time;
};
int main(){
int n;
while (cin >> n && n != 0){
program a[100];
int b[100] = { 0 };
program temp;
for (int i = 0; i < n; i++)
cin >> a[i].start_time >> a[i].end_time;
//按结束时间从小到大排列节目
for (int i = 0; i < n; i++){
for (int j = i ; j < n; j++){
if (a[i].end_time > a[j].end_time){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
//排列完的第一个节目为起始节目
//找最近的(结束最早的),上个节目结束后能看的
int cont = 1;
temp = a[0];
for (int i = 1; i < n; i++){
if (a[i].start_time >= temp.end_time){
cont++;
temp = a[i];
}
}
cout << cont << endl;
}
return 1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: