Problem Description
After enjoying the movie,LeLe went home alone. LeLe decided to build blocks.
LeLe has already built  piles.
He wants to move some blocks to make  consecutive
piles with exactly the same height .

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  cases.

The first line of input contains three integers . indicate  piles
blocks.

For the next line ,there are  integers  indicate
the height of each piles.

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

 
问题描述

看完电影后,乐乐回家玩起了积木。
他已经搭好了堆积木,他想通过调整积木,使得其中有连续堆积木具有相同的高度,同时他希望高度恰好为
乐乐的积木都这了,也就是说不能添加新的积木,只能移动现有的积木。
他可以把一个积木从一堆移动到另一堆或者新的一堆,但是不能移动到两堆之间。比如,一次移动之后,"3 2 3" 可以变成 "2 2 4" 或者 "3 2 2 1",但是不能变成"3 1 1 3".
请你帮他算算,需要移动的最少积木数。 输入描述 有多组测试数据,大约组。
第一行三个整数,
表示有多少堆积木。
第二行个元素,表示这座积木的高度。
所有数据的范围 ; 输出描述 输出最少需要移动的次数,如果无法完成输出-。 输入样例 输出样例 - Hint 样例解释:把第3座积木上的一个积木移动到第1座上,前3座积木的高度就变成了2 ,就得到了一个3*(积木必须是连续的W堆)。

解题思路:首先记录n个数值的总和,然后每次以w长度对数组进行遍历,切每次对w区间长度内的数值进行处理,将每个大于H的A[i]与H的差值累加赋给t1,将每个小于H的A[i]与H的差值累加赋给t2,然后将t1,t2中较大的值与上次的ans比较取出最小值赋给ans,ans表示该w长度内满足条件时所需移动的次数,然后往下移动即t1或t2中移除一个a[i-w],然后添加一个a[i],最后输出ans即可。

分析:

首先看对于一个连续的W堆,若要将其高度全变为h,我们可以计算出各个堆与h的差值,用s1累加低于h的部分,s2累加超出h的部分。则最少需要移动的次数为max(s1,s2),这是因为:使低于h的部分达到h需要s1个积木,这s1个可以从s2中补得,也可以从其他积木中获得;而使得超出h的积木达到h,需要移走s2个,这s2个可以移动到低于h的积木上,也可以移动到其他积木或者新堆中。于是取s1,s2的较大值,这样一定可以使得这连续的W堆高度均为h。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define INT_MAX 1e9+10
typedef long long LL;
int n, w, h;
int a[]; int main()
{
freopen("ini.txt","r",stdin);
while (~scanf("%d%d%d", &n, &w, &h))
{
fill(a, a + , );
LL sum = , t1 = , t2 = ;//t1代表高度超过H的总和,t2高度小于H的总和,sum表示所有输入a[i]值的总和
int i = ;
for (; i < w; i++) //数组a[i]的前端增加w个0
{
a[i] = a[i] - h; //a[i]-h表示每个a[i]值与h的差值,并将该差值赋给a[i]
t2 += -a[i]; //将差值小于0的累加赋给t2保存
}
for (; i < n + w; i++)
{
scanf("%d", &a[i]);
sum += a[i];
a[i] = a[i] - h; //将每个a[i]替换成a[i]-h的差值
}
for (; i < n + * w; i++)
a[i] = a[i] - h; LL ans = INT_MAX;
ans = max(t1, t2); //将t1、t2中的最大值赋给ans,ans表示每次移动的次数,
if (sum < w*h)
printf("-1\n"); //sum小于w*h直接输出-1
else //否则遍历后边的区间
{
for (int j = w; j<n + * w; j++)
{
if (a[j - w] > )
t1 -= a[j - w];//t1移除前面一个a[i]值
else
t2 += a[j - w];//t2移除后面一个a[i]值 if (a[j] > )
t1 += a[j];//t1往后添加一个a[i]值
else
t2 += -a[j];//t2往后添加一个a[i]值 ans = min(ans, max(t1, t2));//每次找出最小值赋给ans
}
printf("%I64d\n", ans);
}
}
return ;
}

HDU—— 5159 Building Blocks的更多相关文章

  1. HDU 5191 Building Blocks

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5191 bc(中文):http://bestcoder.hdu.edu.cn/contests ...

  2. hdu 5190 Building Blocks

    问题描述 看完电影后,乐乐回家玩起了积木. 他已经搭好了n堆积木,他想通过调整积木,使得其中有连续W堆积木具有相同的高度,同时他希望高度恰好为H. 乐乐的积木都这了,也就是说不能添加新的积木,只能移动 ...

  3. Intel® Threading Building Blocks (Intel® TBB) Developer Guide 中文 Parallelizing Data Flow and Dependence Graphs并行化data flow和依赖图

    https://www.threadingbuildingblocks.org/docs/help/index.htm Parallelizing Data Flow and Dependency G ...

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

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. DTD - XML Building Blocks

    The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Doc ...

  6. 企业架构研究总结(35)——TOGAF架构内容框架之构建块(Building Blocks)

    之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并无太多能力和机会对TOGAF进行理论和实际的联系,仅可对标准的文本 ...

  7. TOGAF架构内容框架之构建块(Building Blocks)

    TOGAF架构内容框架之构建块(Building Blocks) 之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并 ...

  8. HDU 5033 Building(单调栈)

    HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...

  9. [翻译]Review——How JavaScript works:The building blocks of Web Workers

    原文地址:https://blog.sessionstack.com/how-javascript-works-the-building-blocks-of-web-workers-5-cases-w ...

随机推荐

  1. codevs 水过 骑马修栅栏

    [问题描述] 农民John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个一个栅栏.你必须编一个程序,读入栅 ...

  2. BZOJ4268 : 小强的书架

    首先将所有高度乘上10,设f[i]为将前i本书放入书架的最小高度,则 \[\begin{eqnarray*}f[i]&=&\min(f[j-1]+first(j,i)+second(j ...

  3. bzoj 3285 离散对数解指数方程

    /************************************************************** Problem: 3285 User: idy002 Language: ...

  4. Java 中的“implements Runnable” 和“extends Thread”

    知识点 “implements Runnable” 和“extends Thread”的不同 具体分析 最近在学习Android中的Handler消息传递机制时,创建新线程有两种方式:一种是实现Run ...

  5. 有强大的cURL,忘掉httpclient的吧!

    这段时间想做一个网页采集的程序,由于一网站采用了防采集的办法,我的httpclient总是在登录后无法获取到我想要过去的链接.在无数次的跟踪过后发现原来人家给返回的是javascript拼成的页面,而 ...

  6. Lodop客户端本地和集中打印 [是否安装][操作系统]

    前面有些博文或图片介绍了这两种方式,可能文字太多,耐心看完的人不多,这里简略描述+图片的方式解释一下. 客户端本地打印: 用户访问某网站,用自己本地的打印机打出来. 集中打印方式: 用户访问某网页,用 ...

  7. Android 解压zip文件

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  8. BootstrapClassloader ExtClassloader AppClassloader

    http://www.importnew.com/26269.html   import java.net.URL; class test9 { public static void main(Str ...

  9. Android Studio 怎样打JAR包

    Android Studio 怎样打JAR包 在eclipse中我们知道怎样将一个项目导出为jar包,供其他项目使用. 在AS中能够通过改动gradle才处理. 我们新建一个项目MakeJar,在项目 ...

  10. Win7电脑开启局域网连接和共享过程中出现的"您可能没有权限使用网络资源"的解决办法

    Win7电脑开启局域网连接和共享 http://bbs.ithome.com/thread-334567-1-1.html http://jingyan.baidu.com/article/6dad5 ...