bzoj1651[Usaco2006 Feb]Stall Reservations 专用牛棚*

bzoj1651[Usaco2006 Feb]Stall Reservations 专用牛棚

题意:

有N头牛,每头牛有个喝水时间段,这段时间它将专用一个棚。现在给出每头牛的喝水时间段,问至少要多少个棚才能满足它们的要求。n≤50000,时刻≤1000000。

题解:

时间段左端点对应的sum元素++,右端点+1对应的sum元素--,最后从左到右加一遍就可以得到每个时刻的喝水牛数,找个最大值就可以了。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 #define maxn 1000010
 7 using namespace std;
 8 
 9 inline int read(){
10     char ch=getchar(); int f=1,x=0;
11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
12     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
13     return f*x;
14 }
15 int sm[maxn],n,mx,ans;
16 int main(){
17     n=read(); inc(i,1,n){int x=read(),y=read(); sm[x]++; sm[y+1]--; mx=max(mx,y);}
18     inc(i,1,mx)sm[i]+=sm[i-1]; inc(i,1,mx)ans=max(ans,sm[i]); printf("%d",ans); return 0;
19     return 0;
20 }

20160919

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