Question A: watching TV
Time limit: 1 Sec memory limit: 32 MB
Flower offering: 142 solution: 86
[flower offering] [wreath] [TK question bank]
Title Description
Summer vacation arrived, Xiao Ming can watch TV happily finally. But Xiao Ming likes too many programs. He hopes to see as many complete programs as possible.
Now he gives you the broadcasting schedule of his favorite TV program. Can you help him to make a reasonable arrangement?
input
The input contains multiple sets of test data. The first line of each input is an integer n (n < = 100), which represents the total number of programs Xiaoming likes.
Next N lines, input two integers si and ei (1 < = i < = n) for each line to indicate the start and end time of the ith program. In order to simplify the problem, each time is represented by a positive integer.
When n=0, the input ends.
output
For each group of input, output the number of TV programs that can be seen completely.
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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef struct
{
int x;
int y;
}Interval;
bool cmp(Interval I1, Interval I2)
{
if (I1.x != I2.x) return I1.x > I2.x;
return I1.y < I2.y;
}
int main()
{
#ifdef _DEBUG
//freopen("data.txt", "r+", stdin);
fstream cin("data.txt");
#endif // _DEBUG
const int MaxN = 100;
int N;
Interval Itv[MaxN];
while (cin >> N, N)
{
for (int i = 0; i < N; ++i)
cin >> Itv[i].x >> Itv[i].y;
sort(Itv, Itv + N,cmp); //Sort by left endpoint from large to small, or right endpoint from small to large if left endpoints are the same
int count = 1, tmpX = Itv[0].x;
for (int i = 1; i < N; ++i)
{
if(Itv[i].y <= tmpX)
{
++count;
tmpX = Itv[i].x;
}
}
cout << count << endl;
}
#ifdef _DEBUG
cin.close();
#ifndef _CODEBLOCKS
system("pause");
#endif // !_CODEBLOCKS
#endif // _DEBUG
return 0;
}