首页 > 其他 > 详细

bzoj3410[Usaco2009 Dec]Selfish Grazing 自私的食草者*

时间:2016-08-14 09:03:58      阅读:317      评论:0      收藏:0      [点我收藏+]

bzoj3410[Usaco2009 Dec]Selfish Grazing 自私的食草者

题意:

n个区间,求最多的区间集合使其互不覆盖。n≤50000

题解:

好像是第三次出现这种题了~但是区间范围可达10^9,不能dp了QAQ膜了一发题解发现只要按区间右端点排序然后贪心取即可。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 50100
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
11     while(ch>=0&&ch<=9)x=x*10+ch-0,ch=getchar();
12     return f*x;
13 }
14 struct nd{int l,r;} nds[maxn];
15 inline bool cmp(const nd &a,const nd &b){return a.r<b.r;}
16 int n,r,ans;
17 int main(){
18     n=read(); inc(i,1,n){int a=read(),b=read(); nds[i]=(nd){a,b};}
19     sort(nds+1,nds+1+n,cmp);
20     inc(i,1,n){
21         if(nds[i].l>=r)ans++,r=nds[i].r;
22     }
23     printf("%d",ans); return 0;
24 }

 

20160810

bzoj3410[Usaco2009 Dec]Selfish Grazing 自私的食草者*

原文:http://www.cnblogs.com/YuanZiming/p/5769462.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!