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.

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

题意:有n堆由1*1小木块堆起来的积木,现在要你移动若干次使其中连续的长度为w的高度都为h,一次只能移动一个,而且可以在两边增加新堆(只能在两边,不能在中间插入),问最少得移动多少次。

思路:因为可以在两边增加堆,所以我先在数组两边各加上w长度,Move数组存 每个地方变成h要移进来或者移出去的步数,a数组记录i位子是要移进来还是移出去。然后维护一个长度为w的连续区间(i-w+1 ~ i),计算出这个区间总共得移进来的数目z和移出去的数目和f,那么要使这个区间满足条件就要移动m=z+f-min(z,f),(这里减去min(z,f)是因为移进来和移出去可以抵消一部分)。比赛时用的G++交的,结果终判超时挂了!后来用C++交就过了!!!

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 150010
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

ll n,w,h;
ll Move[maxn];
bool a[maxn];

int main()
{
    ll i,j,x;
    while (~scanf("%lld%lld%lld",&n,&w,&h))
    {
        ll sum=0,m;
        FRL(i,0,n+2*w)
            a[i]=false;
        FRE(i,w+1,n+w)
        {
            scanf("%lld",&x);
            Move[i]=abs(h-x);
            if (h<x) a[i]=true;
            sum+=x;
        }
        if (w*h>sum)
        {
            pf("-1\n");
            continue;
        }
        ll z=0,f=0;
        FRE(i,1,w)      //前后扩大
            Move[i]=h;
        FRE(i,n+w+1,n+2*w)
            Move[i]=h;
        z=w*h;
        f=0;
        m=z+f-min(z,f);
        ll ans=m;
        FRE(i,w+1,n+2*w)    //枚举区间
        {
            if (a[i-w]) //要除去前面一个区间的第一个数(往后移动它就出了嘛)
                f-=Move[i-w];
            else
                z-=Move[i-w];
            if (a[i])       //加上新进来的
                f+=Move[i];
            else
                z+=Move[i];

            m=z+f-min(z,f);
            ans=min(ans,m);
        }
        pf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-10 04:19:19

Building Blocks (hdu 5191)的相关文章

Valentine&#39;s Day Round 1001.Ferries Wheel(hdu 5174)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5174 题目意思:给出 n 个人坐的缆车值,假设有 k 个缆车,缆车值 A[i] 需要满足:A[i−1]<A[i]<A[i+1](1<i<K).现在要求的是,有多少人满足,(他坐的缆车的值 + 他左边缆车的值) % INT_MAX == 他右边缆车的值. 首先好感谢出题者的样例三,否则真的会坑下不少人.即同一部缆车可以坐多个人.由于缆车的值是唯一的,所以可以通过排序先排出缆车的位置.求出

最短路 (HDU 2544)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28836    Accepted Submission(s): 12480 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找

BestCoder Round #70 Jam&#39;s math problem(hdu 5615)

Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ax^2+bx+cax?2??+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m)pqx?2??+(qk+mp)x+km=(px+k)(qx+m). He could only solve the problem in which p,q,m,

RPG的错排 (HDU 2068)

RPG的错排 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6746    Accepted Submission(s): 2738 Problem Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R是

BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]

传送门 GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 264    Accepted Submission(s): 57 Problem Description GTY has n gay friends. To manage them conveniently, every morning he o

字典树 Trie (HDU 1671)

Problem Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers: 1. Emergency 911 2. Alice 97 625 999 3. Bob 91 12 54 26 In this

BestCoder Round #1 1002 项目管理 (HDU 4858)

项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 738    Accepted Submission(s): 260 Problem Description 我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的!两个节点间可能有多条边,不过一条边的两端必然是不同的节点.每个节点都有一个能量值. 现在我

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

【Code::Blocks】windows 环境下编译 Code::Blocks(已修正)

Code::Blocks 在2012-11-25发布了最新的12.11版本,相比上一个版本(10.05),Code::Blocks 进行了许多改进和更新(Change log). 引用 Wikipedia:Code::Blocks: Code::Blocks是一个免费.开源.跨平台的IDE,使用C++开发,并且使用wxWidgets做为GUI函式库.Code::Blocks使用了插件架构,其功能可以使用插件自由地扩充.目前, Code::Blocks主要针对开发C/C++程式而设计. Code: