标签:des style blog http color java os io
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27586 Accepted Submission(s): 14578
典型贪心.
最大线段覆盖区间问题.
按照区间结束时间进行升序排序.
因为结束的越早,那么剩下的时间也就越多,可选择的节目就越多.
代码如下:
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 #define MAX 101 7 struct node 8 { 9 int begin; 10 int end; 11 }; 12 struct node p[MAX]; 13 int n; 14 bool cmp(struct node x,struct node y) 15 { 16 if(x.end==y.end) return x.begin<y.begin; 17 return x.end<y.end; 18 } 19 void init() 20 { 21 memset(p,0,sizeof(p)); 22 } 23 void read() 24 { 25 int i; 26 for(i=0;i<n;i++) 27 scanf("%d %d",&p[i].begin,&p[i].end); 28 sort(p,p+n,cmp); 29 } 30 void cal() 31 { 32 int ans=1; 33 int end=p[0].end; 34 int i; 35 for(i=1;i<n;i++) 36 { 37 if(p[i].begin>=end) 38 { 39 end=p[i].end; 40 ans++; 41 //cout<<p[i].begin<<" "<<p[i].end<<endl; 42 } 43 } 44 printf("%d\n",ans); 45 } 46 void solve() 47 { 48 init(); 49 read(); 50 cal(); 51 } 52 53 int main() 54 { 55 while(scanf("%d",&n)!=EOF&&n) 56 { 57 solve(); 58 } 59 return 0; 60 }
Hdu 2037 今年暑假不AC,布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/By-ruoyu/p/3905551.html