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. Develop with asyncio部分的翻译

    Develop with asyncio 异步程序和普通的连续程序(也就是同步程序)是很不一样的,这里会列出一些常见的陷阱,并介绍如何去避开他们. Debug mode of asyncio 我们用a ...

  2. 2016-2017 CT S03E06: Codeforces Trainings Season 3 Episode 6(8/13)

    2016-2017 CT S03E06: Codeforces Trainings Season 3 Episode 6 比赛连接: http://codeforces.com/gym/101124/ ...

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

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

  4. 使用 IntraWeb (5) - 页面布局之 TFrame

    IW 对 TFrame(还是之前那个), 这在页面布局中很有用. 如果多个页面都有一个共同的部分(譬如页眉.页脚.菜单.边栏等), 可以将这些共同的部分放在一个 TFrame 中, 从而方便统一与修改 ...

  5. MikroTik RouterOS 5.x使用HunterTik 2.3.1进行破解

    一.加载光驱: 二.一路回车: 三.说明: 1.可以不安装Debian内核,但如果在无缝升级到6.6的版本,此项就一定要选择. 2.6版本的破解必须小于等于1G的空间,不然无法破解成功,亲测有效,如果 ...

  6. 強大的jQuery Chart组件-Highcharts

    Highcharts是一个制作图表的纯Javascript类库,主要特性如下: 兼容性:兼容当今所有的浏览器,包括iPhone.IE和火狐等等: 对个人用户完全免费: 纯JS,无BS: 支持大部分的图 ...

  7. 设置eclipse不同的workspace共享配置

    有很多的项目,每个项目使用一个workspace,结果每新建一个workspace重新配置一下,但是配置的东西都是一样的, 总结一下,复制工作空间配置步骤如下: 1 使用eclipse新建worksp ...

  8. SGU 200. Cracking RSA (高斯消元求自由变元个数)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=200 200. Cracking RSA time limit per test: ...

  9. SQL 查询逻辑处理顺序

    http://www.cnblogs.com/lyhabc/articles/3912608.html http://blog.csdn.net/lanxu_yy/article/details/62 ...

  10. How do I debug a published XBAP file in VS2010?

    I need to debug a full-trust application either by specifying a URL or, ideally, from within the web ...