HDU-2037-今年暑假不AC

題目點這裏

題目大意

給定n(n <= 100)個節目的開始時間和結束時間,求能看的最多的完整節目個數。

解題思想

貪心,以結束時間排序,結束時間越靠前,就看這個節目,這樣貪心保證能夠看的節目最多

代碼


#include<iostream>
#include<algorithm>

using namespace std;

struct Node
{
    int start;
    int end;
};

bool cmp(Node A,Node B)
{
    return A.end < B.end;  //以結束時間排序
}

int main()
{
    int n,cnt,t;
    Node a[105];
    while(cin >> n && n)
    {
        cnt = 0;
        t = 0;
        for(int i = 0; i < n; ++i)
            cin >> a[i].start >> a[i].end;
        sort(a,a+n,cmp);
        for(int i = 0; i < n; ++i)
        {
            if(a[i].start >= t)
            {
                cnt++;
                t = a[i].end;   //更新結束時間
            }
        }
        cout << cnt << endl;
    }
    return 0;
}
發佈了48 篇原創文章 · 獲贊 17 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章