HDU 5191 Building Blocks

来源:互联网 发布:便携式数据采集器pt800 编辑:程序博客网 时间:2024/05/16 23:59
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.
 

Input
There are multiple test cases, about 100 cases.

The first line of input contains three integers n,W,H(1n,W,H50000).n indicate n piles blocks.

For the next line ,there are n integers A1,A2,A3,,An indicate the height of each piles. (1Ai50000)

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).
 

Sample Input
4 3 21 2 3 54 4 41 2 3 4
 

Sample Output
1

-1

前后补上w,然后扫描一遍即可。

#include<stdio.h>#include<cstring>#include<vector>#include<algorithm>#include<iostream>using namespace std;const int maxn = 150005;int n, W, H, a[maxn], f[maxn][2];long long sum, ans;int main(){    while (~scanf("%d%d%d", &n, &W, &H))    {        f[0][0] = f[0][1] = sum = 0;        for (int i = 1; i <= W + n + W; i++)        {            if (W < i && i <= n + W) scanf("%d", &a[i]); else a[i] = 0;            sum += a[i];            f[i][0] = f[i - 1][0];        f[i][1] = f[i - 1][1];            if (a[i] < H) f[i][0] +=H - a[i]; else f[i][1]+= a[i] - H;        }        if ((long long)W*H > sum) printf("-1\n");        else        {            ans = 0x7FFFFFFF;            for (int i = W; i <= n + W + W; i++)                ans = min(ans, (long long)max(f[i][0] - f[i - W][0], f[i][1] - f[i - W][1]));            cout << ans << endl;        }    }}


0 0
原创粉丝点击