码迷,mamicode.com
首页 > Windows程序 > 详细

BC#34 1002 hdu 5192 Building Blocks

时间:2015-03-22 09:15:00      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:hdu 5192 building bl   bc#34   1002   

Building Blocks

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


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 5190 5189 5188 
 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define INF 1010010101000
#define ll long long

using namespace std;

int n,m,h;
ll a[50020*3];

ll mmin(ll a,ll b) {
    if(a>b)return b;
    return a;
}
ll mmax(ll a,ll b) {
    if(a<b)return b;
    return a;
}

int main() {
    while(cin>>n>>m>>h) {
        ll sum=0;
        for(int i=1; i<=m; i++)
            a[i]=0;
        for(int i=m+1; i<=m+n; i++) {
            scanf("%I64d",&a[i]);
            sum+=a[i];
        }
        for(int i=m+n+1; i<=n+m+m; i++)
            a[i]=0;
        ll ans=0;
        if(sum<(ll)h*m)printf("-1\n");
        else {
            int first=1;
            ll Min=INF;
            ll ans1=0;
            ll ans2=0;
            for(int i=1; i<=m; i++) {
                if(a[i]>h)
                    ans1+=a[i]-h;
                else
                    ans2+=h-a[i];
            }
            ans=max(ans1,ans2);
            Min=ans;
            for(int i=m+1; i<=m+m+n; i++) {
                if(a[first]>h)
                    ans1-=a[first]-h;
                else
                    ans2-=h-a[first];
                if(a[i]>h)
                    ans1+=a[i]-h;
                else
                    ans2+=h-a[i];
                first++;
                ans=mmax(ans1,ans2);
                Min=mmin(ans,Min);
            }
            cout<<Min<<endl;
        }
    }
}

BC#34 1002 hdu 5192 Building Blocks

标签:hdu 5192 building bl   bc#34   1002   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/44523117

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!