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

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

原文地址:https://www.cnblogs.com/YuanZiming/p/5769462.html