HDU—— 5159 Building 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.
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.
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的更多相关文章
- HDU 5191 Building Blocks
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5191 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
- hdu 5190 Building Blocks
问题描述 看完电影后,乐乐回家玩起了积木. 他已经搭好了n堆积木,他想通过调整积木,使得其中有连续W堆积木具有相同的高度,同时他希望高度恰好为H. 乐乐的积木都这了,也就是说不能添加新的积木,只能移动 ...
- 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 ...
- bc.34.B.Building Blocks(贪心)
Building Blocks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- DTD - XML Building Blocks
The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Doc ...
- 企业架构研究总结(35)——TOGAF架构内容框架之构建块(Building Blocks)
之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并无太多能力和机会对TOGAF进行理论和实际的联系,仅可对标准的文本 ...
- TOGAF架构内容框架之构建块(Building Blocks)
TOGAF架构内容框架之构建块(Building Blocks) 之前忙于搬家移居,无暇顾及博客,今天终于得闲继续我的“政治课”了,希望之后至少能够补完TOGAF方面的内容.从前面文章可以看出,笔者并 ...
- HDU 5033 Building(单调栈)
HDU 5033 Building(单调栈) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5033 Description Once upon a ti ...
- [翻译]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 ...
随机推荐
- [CEOI2017]Palindromic Partitions
[CEOI2017]Palindromic Partitions 题目大意: 给出一个长度为\(n(n\le10^6)\)的只包含小写字母字符串,要求你将它划分成尽可能多的小块,使得这些小块构成回文串 ...
- Javascript 严格模式use strict详解
1.概述 除了正常运行模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).顾名思义,这种模式使得Javascript在更严格的条件下运行. ...
- 用AB对Webservice做压力测试
AB工具下载 AB工具介绍见 web压力测试工具 下载ab工具后放到任意目录下,从命令行打开ab.exe 使用以下命令测试Webservice ab -c 100 -n 1000 -p zip.txt ...
- STM32F4XX devices vector table for EWARM toolchain.
;/******************** (C) COPYRIGHT 2015 STMicroelectronics ******************** ;* File Name : sta ...
- excel中文转成拼音字母(包括首字母大写)
参考文献: 1.首字母大写:http://www.excelpx.com/thread-168029-1-1.html(里面下载一个excel,里面有宏) 中文转拼音: 2.http://blog.s ...
- Introduction to the Optimizer --cbo
http://docs.oracle.com/cd/B10500_01/server.920/a96533/optimops.htm
- Revit API取得变量的内参名称
与取得元素变量的内参名称类别有个BuiltInParameter //取得内参名称 [Transaction(TransactionMode.Manual)] [Regeneration(Regene ...
- 解决winform中mdi子窗体加载时显示最大化最小化按钮的方法
场景:在mid加载子窗体的时候如果指定WindowState为Maximized,加载完成后主窗体会显示最大化.最小化.关闭的按钮图标. 解决方法: 1.更改主窗体FormMain的属性.制定Main ...
- DOM对象之document对象
DOM对象:当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. 打开网页后,首先看到的是浏览器窗口,即顶层的win ...
- 创建 HelloWorld 项目
在 Eclipse 的导航栏中点击 File →New →Android Application Project ,此时会弹出创建 Android 项目的对话框.其中 Application Name ...