Building Blocks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2209 Accepted Submission(s): 509
LeLe has already built n piles. He wants to move some blocks to make W consecutive piles with exactly the same height H.
LeLe already put all of his blocks in these piles, which means he can not add any blocks into them. Besides, he can move a block from one pile to another or a new one,but not the position betweens two piles already exists.For instance,after one move,"3 2 3" can become "2 2 4" or "3 2 2 1",but not "3 1 1 3".
You are request to calculate the minimum blocks should LeLe move.
The first line of input contains three integers n,W,H(1≤n,W,H≤50000).n indicate n piles blocks.
For the next line ,there are n integers A1,A2,A3,……,An indicate the height of each piles. (1≤Ai≤50000)
The height of a block is 1.
If there is no solution, output "-1" (without quotes).
,然后我们的区间右移到[2,W+1],这时我们要把1删除,然后将w+1添加进去,这样的话对 s,t进行加减,然后取个大值就行了。
#include<iostream> #include<cstdio> #include<cstring> #include <algorithm> #include <math.h> using namespace std; typedef long long LL; const int N = 200005; LL n,w,h; LL high[N]; int main() { while(scanf("%lld%lld%lld",&n,&w,&h)!=EOF){ LL sum = 0; memset(high,0,sizeof(high)); for(int i=1;i<w+1;i++){ high[i]-=h; } for(int i=w+1;i<w+1+n;i++){ scanf("%lld",&high[i]); sum+=high[i]; high[i]-=h; } for(int i=w+1+n;i<=w+w+n;i++){ high[i]-=h; } if(sum<h*w){ printf("-1\n"); continue; } LL s=w*h,t=0,ans = w*h; ///s维护将高的拿走,t维护将矮的补上,最开始[1,w]要补w*h进去,所以ans初始化w*h for(int i=w+1;i<=w+w+n;i++){ if(high[i-w]>0) t-=high[i-w]; ///删除第 i-w 块 else s+=high[i-w]; if(high[i]>0) t+=high[i]; ///添加第 i 块 else s-=high[i]; ans = min(ans,max(t,s)); } printf("%lld\n",ans); } return 0; }