会场安排问题(贪心)

来源:互联网 发布:剑灵for mac 编辑:程序博客网 时间:2024/04/29 04:57
/*日期:2011-10-17
  作者:xiaosi
  题目:会场安排问题(贪心)
  题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=14
*/
#include<iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
struct Activity
{
    int begin;
    int end;
}A[10000];
int cmp(const void *a,const void *b)
{
    struct Activity *c = (Activity *)a;
    struct Activity *d = (Activity *)b;
    return c->end - d->end;
}
int Selector(Activity A[],int n)
{
    int i,j=0,count=1;
    for(i=1;i<n;i++)
    {
        if(A[i].begin > A[j].end)
        {
            count++;
            j=i;
        }
    }
    return count;
}
int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        while(N--)
        {
            int n,i;
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                scanf("%d %d",&A[i].begin,&A[i].end);
            }
            qsort(A,n,sizeof(A[0]),cmp);
            printf("%d\n",Selector(A,n));
        }
    }
}
原创粉丝点击