Building BlocksTime

** limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)**

Total submission (s): 281 Accepted Submission (s): 59

Problem Descriptionafter 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 him 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 both piles already exists. For Instance,after one Move, "3 2 3" can become "2 2 4" or "3 2 2 1", and not "3 1 1 3".

You is request to calculate the minimum blocks should LeLe move.

Inputthere is multiple test cases, about - 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 is n Integers A 1 , A 2 , A 3 ,... ..... .., A n Indicate the height of each piles. (1≤A i ≤ 50000 )

The height of a block is 1.

Outputoutput the minimum number of blocks should LeLe move.

If There is no solution, output "1" (without quotes).

Sample Input

4 3 21 2 3 54 4 41 2 3 4

Sample Output

1-1*Hint*in first case, LeLe move one block from third pile to first pile.

Sourcebestcoder Round #34

Recommendhujie | We have carefully selected several similar problems for you:5193 5192 5189 5188 5185

Test instructions: There are n heaps of bricks piled up by 1*1 small wooden blocks, now you have to move several times so that the continuous length of the height of W is h, can only move one at a time, and can be added on both sides of the new heap (only on both sides, cannot be inserted in the middle), ask at least how many times to move.

Idea: Because the heap can be added on both sides, so I first on each side of the array with W length, move array in each place into H to move in or move out of the number of steps, a array of records I is to move in or move out. Then maintain a continuous interval of W (i-w+1 ~ i), calculate the total number of moves in this interval z and the number of moves out and F, so that the interval to meet the conditions will be moved M=z+f-min (z,f), (here minus min (Z, f) is because the move in and out can offset a portion). The match with the g++, the result of the final sentence timeout hung! Later in C + + turn over!!!

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set > #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 150010#define MAXN 2005# Define mod 1000000009#define INF 0x3f3f3f3f#define pi ACOs ( -1.0) #define EPS 1e-6#define Lson rt<<1,l,mid#define RSO N rt<<1|1,mid+1,r#define FRE (i,a,b) for (i = A, I <= b; i++) #define FREE (i,a,b) for (i = A; I >= b; i--) #define FRL (i,a,b) for (i = A; I < b; i++) #define FRLL (i,a,b) for (i = A; i > b; i--) #define MEM (T, v) memset ((t), V, si Zeof (t)) #define SF (n) scanf ("%d", &n) #define SFF (A, b) scanf ("%d%d", &a, &b) #define SFFF (a,b,c) scanf ("%d%d%d", &a, &b, &c) #define PF printf#define DBG pf ("hi\n") typedef long Long ll;using Nam Espace std;ll n,w,h;ll Move[maxn];bool a[maxn];intMain () {ll i,j,x; while (~SCANF ("%lld%lld%lld", &n,&w,&h)) {ll sum=0,m; FRL (i,0,n+2*w) A[i]=false; FRE (i,w+1,n+w) {scanf ("%lld", &x); Move[i]=abs (h-x); if (h<x) a[i]=true; Sum+=x; } if (w*h>sum) {pf (" -1\n"); Continue } ll Z=0,f=0; FRE (I,1,W)//before and after the expansion of move[i]=h; FRE (i,n+w+1,n+2*w) move[i]=h; Z=w*h; f=0; M=z+f-min (Z,F); ll Ans=m; FRE (I,W+1,N+2*W)///enumeration interval {if (a[i-w])//To remove the first number of the previous interval (move it back) f-=move[i-w]; else z-=move[i-w]; if (A[i])//plus new incoming f+=move[i]; else Z+=move[i]; M=z+f-min (Z,F); Ans=min (ANS,M); } PF ("%lld\n", ans); } return 0;}

Building Blocks (Hdu 5191)