HDU - 2037 this summer without AC (greedy)

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90034 Accepted Submission(s): 48081

Problem Description

"This summer without AC?"
"Yes." "What
are you doing?"
"Yeah watching the World Cup, stupid!"
"@ # $% ^ & *% ..."
Indeed, the World Cup to the fans of the festival also it came, estimated that many ACMer will put aside the computer, toward the television.
As fans, we want to see as much of the full game, of course, as a new era of good young people, you must also look at some other programs, such as news network (Never forget that concerned about national affairs), is 6 + 7, Super girls, and Wang Xiaoya of "happy dictionary" and so on, assuming you already know you like to watch the broadcast schedule of all TV programs, you'll arrange it? (Goal is to see as much of the full program)

Input

Input data comprising a plurality of test example, the first line of each test case only one integer n (n <= 100), represents the total number of programs you like, then n rows, each row comprising two data Ti_s, Ti_e (1 <= i <= n), represent the i-th program start and end times, in order to simplify the problem, each time with a positive integer. n = 0 represents the input end without processing.

Output

For each test case, the number of outputs can see the complete TV program, the output of each test case in a separate line.

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

Solve

First Sort ending time of the program then traverse from front to back, each time watching a program will be changed to the current end time for this program, if the start time of the next program is more than equal to the current time, we look at a program, or else continue to the next iteration.

Code
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[101];
bool cmp(pair<int, int> lef, pair<int, int> rhs)
{
    return lef.second < rhs.second;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int n;
    while (cin >> n && n)
    {
        for (int i = 0; i < n; i++)
            cin >> a[i].first >> a[i].second;

        sort(a, a + n, cmp);
        int time = a[0].second;
        int cnt = 1;
        for (int i = 1; i < n; i++)
        {
            if (a[i].first >= time)
            {
                time = a[i].second;
                cnt++;
            }
        }
        cout << cnt << endl;
    }
}

Guess you like

Origin www.cnblogs.com/YY666/p/11346758.html