bzoj3375[Usaco2004 Mar]Paranoid Cows 发疯的奶牛*
生活随笔
收集整理的這篇文章主要介紹了
bzoj3375[Usaco2004 Mar]Paranoid Cows 发疯的奶牛*
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
bzoj3375[Usaco2004 Mar]Paranoid Cows 發瘋的奶牛
題意:
依次給出n只奶牛的產奶時間段,求最大的k使得前k只奶牛不存在一個時間段被另一個時間段完全覆蓋的情況。n≤100000。
題解:
設當前在處理第i只奶牛,前i-1只奶牛都合法。那么如果前i-1只奶牛中時間段左端點小于且最接近第i只奶牛時間段左端點的奶牛右端點大于當前奶牛則不合法,且如果前i-1只奶牛中時間段左端點大于且最接近第i只奶牛時間段左端點的奶牛右端點小于第i只奶牛則不合法,這是一個貪心的過程,可以用STLset維護。注意set要先插入一個+INF和負INF以防邊界炸。
代碼:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <set> 5 #define inc(i,j,k) for(int i=j;i<=k;i++) 6 #define INF 0x3fffffff 7 #define sit set<nd>::iterator 8 using namespace std; 9 10 inline int read(){ 11 char ch=getchar(); int f=1,x=0; 12 while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} 13 while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); 14 return f*x; 15 } 16 struct nd{int l,r; bool operator <(const nd&a)const{return l==a.l?r<a.r:l<a.l;}}; 17 set<nd>s; int n; 18 int main(){ 19 n=read(); s.insert((nd){INF,0}); s.insert((nd){-INF,0}); 20 inc(i,1,n){ 21 int x=read(),y=read(); 22 s.insert((nd){x,y}); sit a=s.find((nd){x,y}); 23 sit b=--a; a++; sit c=++a; a--; 24 if(b->r>=y&&b->l!=-INF){printf("%d",i-1); return 0;} 25 if(c->r<=y&&c->l!=INF){printf("%d",i-1); return 0;} 26 } 27 printf("%d",n); return 0; 28 }?
20160909
轉載于:https://www.cnblogs.com/YuanZiming/p/5876464.html
總結
以上是生活随笔為你收集整理的bzoj3375[Usaco2004 Mar]Paranoid Cows 发疯的奶牛*的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ps aux|grep xxx详解
- 下一篇: angular解析json数据