BC#34 1002 hdu 5192 Building Blocks

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

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

BestCoder Round #34

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;
        }
    }
}

时间: 2024-10-20 13:40:09

BC#34 1002 hdu 5192 Building Blocks的相关文章

CA Loves GCD (BC#78 1002) (hdu 5656)

CA Loves GCD Accepts: 135 Submissions: 586 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) 问题描述 CA喜欢是一个热爱党和人民的优秀同♂志,所以他也非常喜欢GCD(请在输入法中输入GCD得到CA喜欢GCD的原因). 现在他有N个不同的数,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去. 为了使自己不会无聊,CA会把每

HDU - 5190 - Go to movies &amp;&amp; 5191 - Building Blocks (BC#34 A,B)

Go to movies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 99    Accepted Submission(s): 67 Problem Description Winter holiday is coming!As the monitor, LeLe plans to go to the movies. Becaus

bc.34.B.Building Blocks(贪心)

Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 751    Accepted Submission(s): 164 Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build block

Building Blocks (hdu 5191)

Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 281    Accepted Submission(s): 59 Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks

【HDU】4908 (杭电 BC #3 1002题)BestCoder Sequence ——哈希

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 573    Accepted Submission(s): 201 Problem Description Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing s

HDU 4908 (杭电 BC #3 1002题)BestCoder Sequence(DP)

题目地址:HDU 4908 这个题是从m开始,分别往前DP和往后DP,如果比m大,就比前面+1,反之-1.这样的话,为0的点就可以与m这个数匹配成一个子串,然后左边和右边的相反数的也可以互相匹配成一个子串,然后互相的乘积最后再加上就行了.因为加入最终两边的互相匹配了,那就说明左右两边一定是偶数个,加上m就一定是奇数个,这奇数个的问题就不用担心了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h&g

hdu 5365 (bc #50 1002 )Run

Run Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 594    Accepted Submission(s): 265 Problem Description AFA is a girl who like runing.Today,he download an app about runing .The app can record

EF6 SQL Logging – Part 3: Interception building blocks | One Unicorn

In parts 1 and 2 of this series we looked at how to use DbContext.Database.Log to log the SQL generated by EF. But this code is actually a relatively thin fa?ade over some low-level building blocks for interception in general and, in this case, DbCom

BestCoder Round #1 1001 &amp;&amp; 1002 hdu 4857 4858

hdu 4857 逃生 第一题是拓扑排序,不是按照字典序最小输出,而是要使较小的数排在最前面..赛后弄了好久,才比较明白,我一直以为 反向建图,i从1到n,开始深搜dfs( i ),对i点的边,由小到大继续搜一下,同时标记搜过的数,搜过之后就不再搜,搜到底之后ans[cnt++] = u;这样顺序输出就是答案,后来经过超哥指点,才明白深搜贪心是错的.只有 反向建图,用优先队列把较大的数尽量排在前面,然后反序输出才是正解.. 1 #include<iostream> 2 #include<