bzoj3410[Usaco2009 Dec]Selfish Grazing 自私的食草者*
生活随笔
收集整理的這篇文章主要介紹了
bzoj3410[Usaco2009 Dec]Selfish Grazing 自私的食草者*
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
bzoj3410[Usaco2009 Dec]Selfish Grazing 自私的食草者
題意:
n個區(qū)間,求最多的區(qū)間集合使其互不覆蓋。n≤50000
題解:
好像是第三次出現(xiàn)這種題了~但是區(qū)間范圍可達10^9,不能dp了QAQ膜了一發(fā)題解發(fā)現(xiàn)只要按區(qū)間右端點排序然后貪心取即可。
代碼:
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
轉(zhuǎn)載于:https://www.cnblogs.com/YuanZiming/p/5769462.html
總結(jié)
以上是生活随笔為你收集整理的bzoj3410[Usaco2009 Dec]Selfish Grazing 自私的食草者*的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: juqery 获取radio选中的值
- 下一篇: 日常五乐事