hdu 2491 Priest John's Busiest Day(动态规划之贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=2491

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 100008
 4 struct cere
 5 {
 6     int x,y;
 7     int mid,end;
 8 }a[N];
 9 int cmp(const void *a,const void *b)
10 {
11     struct cere *c,*d;
12     c=(struct cere *)a;
13     d=(struct cere *)b;
14     if(c->y!=d->y)
15         return c->y-d->y;
16     else
17         return c->x-d->x;
18 }
19 int main()
20 {
21     int n,i,x,y;
22     while(scanf("%d",&n),n!=0)
23     {
24         for(i=0;i<n;i++)
25         {
26             scanf("%d%d",&x,&y);
27             a[i].x=x;
28             a[i].mid=(y-x)/2+1;
29             a[i].y=x+a[i].mid;
30             a[i].end=y;
31         }
32         qsort(a,n,sizeof(a[0]),cmp);
33         int flag=0,time;
34         time=0;
35         for(i=0;i<n;i++)
36         {
37             if(time>(a[i].end-a[i].mid))
38                 break;
39             if(time>=a[i].x)
40                 time+=a[i].mid;
41             else
42             time=a[i].y;
43         }
44         if(i>=n)
45             printf("YES\n");
46         else
47             printf("NO\n");
48     }
49     return 0;
50 }
51 
52 
53 
54     

 

posted @ 2013-05-04 11:22  zlyblog  阅读(151)  评论(0编辑  收藏  举报