NYOJ 14 会场安排问题

地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=14

思路:贪心算法

主要问题是时间的问题,这就需要用到ACM中常用的algorithm库函数

 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 struct  node
 5 {
 6     int first;//开始时间
 7     int last; //结束时间
 8 }w[10001];
 9 bool cmp(node x,node y)
10 {
11     if(x.last<y.last)  return true;  //升序
12     return false;
13 }
14 int main()
15 {
16     int m,n,i,count,p;
17     scanf("%d",&m);
18     while(m--)
19     {
20         scanf("%d",&n);
21         for(i=0;i<n;i++)
22         scanf("%d %d",&w[i].first,&w[i].last);
23         sort(w,w+n,cmp);
24         p=0;count=1; //因为是从第二个开始比较的,所以count初始为1
25         for(i=1;i<n;i++)
26         {
27             if(w[i].first>w[p].last)
28             {
29                 count++;
30                 p=i;  //比较后修改下标,进行下一次比较 
31             }
32         }
33         printf("%d\n",count);
34     }
35     return 0;
36 } 

 

 

posted on 2012-08-14 21:04  mycapple  阅读(324)  评论(0)    收藏  举报

导航