BestCoder Round #34 1002 Building Blocks

1 篇文章 0 订阅

Building Blocks

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


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
 


题意:给你n根木堆,你可以移动木堆或者新建木堆,问你如何用最少步数实现m堆连续长度为h的木堆。

做法:首先新建木堆可以在前面或者后面,又因为数据范围都在50000以内,所以一开始保存数据以50000开始,然后算出所有木堆的总高度sum,判断sum是否大于m*h如果不是则直接输出-1,如果是定义duo为当前木棒和比m*h多的值,shao为当前木棒和比m*h少的值,然后第一个循环for(i,50000,50000+m)里面对duo和shao进行更改,如果当前木堆高度比h高则duo+=num[i]-h,如不是则shao-=h-num[i],然后判断num[i-m]是否大于h,如果成立则duo-=num[i-m]-h,如果不成立则shao-=h-num[i-m],判断答案则为ans=min(ans,max(duo,shao)),然后for(i,50000+m,50000+n)也是如上的规则,最后for(i,50000+n,50000+n+m)也是如上。


#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#define LL long long
using namespace std;
long long num[200055];
int main()
{
    long long n,m,h;
    long long i,j,sum;
    long long duo,shao,ans;
    while(scanf("%I64d%I64d%I64d",&n,&m,&h)!=EOF)
    {
        memset(num,0,sizeof(num));
        sum=0;
        for(i=50000;i<50000+n;i++)
        {
            scanf("%I64d",&num[i]);
            sum+=num[i];
        }
        if(m*h>sum)
        {
            printf("-1\n");
            continue;
        }
        duo=0;
        shao=(LL)m*h;
        ans=max(duo,shao);
        for(i=50000;i<50000+m;i++)
        {
            if(num[i]<h)
                shao+=(h-num[i]);
            else
                duo+=(num[i]-h);
            shao-=h;
            ans=min(ans,max(duo,shao));
        }
        for(i=50000+m;i<50000+n;i++)
        {
            if(num[i]<h)
                shao+=(h-num[i]);
            else
                duo+=(num[i]-h);
            if(num[i-m]<h)
                shao-=(h-num[i-m]);
            else
                duo-=(num[i-m]-h);
            ans=min(ans,max(duo,shao));
        }
        for(i=50000+n;i<50000+n+m;i++)
        {
            shao+=h;
            if(num[i-m]<h)
                shao-=(h-num[i-m]);
            else
                duo-=(num[i-m]-h);
            ans=min(ans,max(duo,shao));
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值