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

时间: 2024-10-17 23:12:08

bzoj1651[Usaco2006 Feb]Stall Reservations 专用牛棚*的相关文章

BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚( 线段树 )

线段树.. -------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define rep( i , n ) for( int i = 0 ; i < n ; i++ ) #define c

BZOJ 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚

题目 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 553  Solved: 307[Submit][Status] Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some preci

bzoj 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚【贪心+堆||差分】

这个题方法还挺多的,不过洛谷上要输出方案所以用堆最方便 先按起始时间从小到大排序. 我用的是greater重定义优先队列(小根堆).用pair存牛棚用完时间(first)和牛棚编号(second),每次查看队首的first是否比当前牛的起始时间早,是则弹出队首记录当前牛的答案,再把新的pair放进去,否则增加牛棚,同样要塞进队里 #include<iostream> #include<cstdio> #include<algorithm> #include<que

【差分】BZOJ 1651 [Usaco2006 Feb]Stall Reservations 专用牛棚

显而易见的是我们要求的时间段重叠最多的次数. 用线段树也可以,不过既然是学习和练习前缀和与差分那么就用差分的思想. 在num[a]的位置+1 在num[y+1]位置-1 表示[a,b]区间有一个时间段. 最后统计一下num[i]最多的次数(i是1~n) /************************************************************** Problem: 1651 User: LYFer Language: C++ Result: Accepted Ti

bzoj1651:专用牛棚

1651: [Usaco2006 Feb]Stall Reservations 专用牛棚 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 638  Solved: 359[Submit][Status][Discuss] Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some

POJ 3190 Stall Reservations (优先队列)C++

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7646   Accepted: 2710   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved: 161[Submit][Status][Discuss] Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a cer

POJ 3190 Stall Reservations(贪心)

Stall Reservations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3590   Accepted: 1284   Special Judge Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time in

BZOJ 1652: [Usaco2006 Feb]Treats for the Cows( dp )

dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边界 : dp( i , i ) = V[ i ] * n -------------------------------------------------------------------------------------------- #include<cstdio> #include&l