練習賽15.1.活動選擇

活動選擇

Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 5

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

學校在最近幾天有n個活動,這些活動都需要使用學校的大禮堂,在同一時間,禮堂只能被一個活動使。由於有些活動時間上有衝突,學校辦公室人員只好讓一些活動放棄使用禮堂而使用其他教室。現在給出n個活動使用禮堂的起始時間begini和結束時間endi(begini<endi),請你幫助辦公室人員安排一些活動來使用禮堂,要求安排的活動儘量多。

Input

輸入有多組數據,每組數據的第一行一個整數n(n<=1000);接下來的n行,每行兩個整數,第一個begini,第二個是endi(begini<endi<=32767)

Output

對於每組數據輸出最多能安排的活動個數。

Sample Input

11
3 5
1 4
12 14
8 12
0 6
8 11
6 10
5 7
3 8
5 9
2 13

Sample Output

4
思路分析:簡單的排序問題;
代碼:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct  tmp
{
    int a,b;
}s[1300];
int f(tmp n,tmp m)
{
    if(n.b!=m.b) return n.b<m.b;//按B排序
    else if(n.a!=m.a) return n.a<m.a;
}
int main()
{
    int t,i,j,l;
    while(scanf("%d",&t)!=EOF)
    {
        for(i=0;i<t;i++)
        {
             scanf("%d%d",&s[i].a,&s[i].b);
        }
        sort(s,s+t,f);
        int a=s[0].b,cnt=1;
        for(i=0;i<t;i++)
        {
            if(a<=s[i].a)
            {
                cnt++;
              a=s[i].b;
            }


        }
        printf("%d\n",cnt);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章