您的位置:首页 > 其它

1004 problem E

2016-03-15 17:47 253 查看

1004 problem E

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

最简单的那个贪心问题

// 1004.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>
using namespace std;
struct program
{
int x;
int y;
}b;
bool cmp(program &a, program &b)
{
return a.y < b.y;
return a.x < a.x;
}
int main()
{
//fstream cin("E:\C++\IN\aaa.txt");
vector<program> p;
//fstream cin("E:/C++/IN/aaa.txt");
int a=0;
//cin >> a;

int j = 1;
//if (a == 0) return 0;
while (cin >> a&&a!=0) {
j = 1;
p.clear();
int n = a;
while (a--)
{
cin >> b.x >> b.y;
p.push_back(b);
}
sort(p.begin(), p.end(), cmp);
int m = p[0].y;
for (int i = 1;i < n;i++)
{
if (p[i].x >= m) { m = p[i].y;j++; }
//cout << "j" << j << "i" << i << endl;
}
cout << j << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: