首页 > 其他 > 详细

hdu 5191

时间:2015-03-23 16:13:59      阅读:217      评论:0      收藏:0      [点我收藏+]

Building Blocks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1323    Accepted Submission(s): 282


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 2 1 2 3 5 4 4 4 1 2 3 4
 

Sample Output
1 -1
Hint
In first case, LeLe move one block from third pile to first pile.
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5193 5192 5191 5190 5189 
 


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
using namespace std;
#define ll long long
int n;
ll W,H;
int a[200010];
ll max(ll t1,ll t2){
    if(t1>=t2){
        return t1;
    }
    return t2;
}
ll min(ll t1,ll t2){
    if(t1<=t2){
        return t1;
    }
    return t2;
}
int main(){
    while(scanf("%d%lld%lld",&n,&W,&H)!=EOF){
        ll s=W*H;
        ll ans=s;
        for(int i=0;i<W;i++){
            a[i]=0;
        }
        int len=W+n;
        for(int i=W;i<len;i++){
            scanf("%d",&a[i]);
            s-=a[i];
        }
        len+=W;
        for(int i=W+n;i<len;i++){
            a[i]=0;
        }
        if(s>0){
            printf("-1\n");
        }
        else{
            ll mn,mx;
            mn=ans;
            mx=0;
            for(int i=W;i<len;i++){
                int j=i-W;
                if(a[j]<=H){
                    mn-=H-a[j];
                }
                else{
                    mx-=a[j]-H;
                }

                if(a[i]<=H){
                    mn+=H-a[i];
                }
                else{
                    mx+=a[i]-H;
                }
                ans=min(ans,max(mx,mn));
            }
            printf("%lld\n",ans);
        }

    }
    return 0;
}


hdu 5191

原文:http://blog.csdn.net/my_acm/article/details/44564433

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!