admin管理员组

文章数量:1664350

Description
Functioning of a scientific conference is usually divided into several simultaneous sections. For example, there may be a section on parallel computing, a section on visualization, a section on data compression, and so on.
Obviously, simultaneous work of several sections is necessary in order to reduce the time for scientific program of the conference and to have more time for the banquet, tea-drinking, and informal discussions. However, it is possible that interesting reports are given simultaneously at different sections.
A participant has written out the time-table of all the reports which are interesting for him. He asks you to determine the maximal number of reports he will be able to attend.

Input
The first line contains the number 1 ≤ N ≤ 100000 of interesting reports. Each of the next N lines contains two integers Ts and Te separated with a space (1 ≤ Ts < Te ≤ 30000). These numbers are the times a corresponding report starts and ends. Time is measured in minutes from the beginning of the conference.

Output
You should output the maximal number of reports which the participant can attend. The participant can attend no two reports simultaneously and any two reports he attends must be separated by at least one minute. For example, if a report ends at 15, the next report which can be attended must begin at 16 or later.

Sample Input
5
3 4
1 5
6 7
4 5
1 3
Sample Output
3
HINT
Source
CSU Monthly 2012 May.

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
#include<bitset>
using namespace std;

const int MAX=100005;

struct Node
{
    int s,e;
    bool operator<(const Node& a) const
    {
        if (e==a.e)
            return s>a.s;
        return e<a.e;
    }
};

Node S[MAX];
int N;

int main()
{
    cin.sync_with_stdio(false);
    while (cin>>N)
    {
        for (int i=0;i<N;i++)
            cin>>S[i].s>>S[i].e;
        sort(S,S+N);
        int now=0,Ans=0;
        for (int i=0;i<N;i++)
            if (S[i].s>now)
                Ans++,now=S[i].e;
        cout<<Ans<<endl;
    }
    return 0;
}

本文标签: 贪心CSUConferenceScientific