Building Blocks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2165 Accepted Submission(s): 495
Problem Description
After enjoying the movie,LeLe went home alone. LeLe decided to build blocks.
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.
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.
Input
There are multiple test cases, about
100
cases.
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.
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.
Output
Output the minimum number of blocks should LeLe move.
If there is no solution, output "-1" (without quotes).
If there is no solution, output "-1" (without quotes).
Sample Input
4 3 2 1 2 3 5 4 4 4 1 2 3 4
Sample Output
1 -1HintIn first case, LeLe move one block from third pile to first pile.
枚举最终的W堆积木在哪,确定了区间,那么就需要把高于H的拿走,低于H的补上,高处的积木放到矮的上面,这样最优。因此把这个区间变成W*H的代价就是max(∑(Hi−H),∑(H−Hj))(Hi>H,Hj≤H)即在把高的变矮和把矮的变高需要的移动的积木数 取较大的。从第一个区间[1,W]到第二区间[2,W+1]只是改变了2堆积木,可以直接对这两堆积木进行删除和添加来维护∑(Hi−H)和∑(H−Hj)。
需要注意的是,最终选取的W堆积木中,有可能有几堆原本不存在。如 9 8 7 形成3*3,可把3堆积木变成5堆 3 3 3 8 7,最少移动6个积木。因此需要在n堆积木两端补上W个0。整个问题的复杂度是O(n+W).
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn=2000010;
int num[maxn];
int main()
{
int n,i,j,k,w,h;
while(scanf("%d%d%d",&n,&w,&h)!=EOF){
memset(num,0,sizeof(num));
int sum=0;
for(i=50000+1;i<=50000+n;++i){
scanf("%d",&num[i]);
sum+=num[i];
}
if(sum<w*h){
printf("-1\n");
}
else {
int ans=w*h,cnt1=w*h,cnt2=0;
for(i=50001-w;i<=50000+n;++i){
if(num[i-1]<=h){
cnt1=cnt1-(h-num[i-1]);
}
else {
cnt2=cnt2-(num[i-1]-h);
}
if(num[i+w-1]<=h){
cnt1=cnt1+(h-num[i+w-1]);
}
else {
cnt2=cnt2+(num[i+w-1]-h);
}
ans=min(ans,max(cnt1,cnt2));
}
printf("%d\n",ans);
}
}
return 0;
}