hdu_2037_今年暑假不AC(贪心算法)

来源:互联网 发布:四度空间软件 编辑:程序博客网 时间:2024/05/17 02:44
http://acm.hdu.edu.cn/showproblem.php?pid=2037
#include <iostream>#include <algorithm>using namespace std;typedef struct//结构体排序,对节目结束时间早晚排序{    int s,e;}tv;int compare(tv a,tv b){    return a.e < b.e;}int main(){    int n;    tv a[1001];    while(cin >> n)    {        if(n == 0)            return 0;        for(int i = 0;i < n;i++)        {            cin >> a[i].s >> a[i].e;        }        sort(a,a+n,compare);        int finish = a[0].e;        int cnt = 1;        for(int i = 1;i < n;i++)        {            if(finish <= a[i].s)            {                cnt++;                finish = a[i].e;            }        }        cout << cnt << "\n";    }    return 0;}

0 0