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. BZOJ3500 : PA2008 Cliquers

    设g[i]表示n=i时的答案,则OEIS上可以找到如下递推式: g[i]=g[i-1]+g[i-2]-g[i-5]-g[i-7]+... 其中符号为++--交替,第i项为f[i],f[1]=1,f[2 ...

  2. 20172319 2018.10.12《Java程序设计教程》第6周课堂实践(补写博客)

    20172319 2018.10.12 <Java程序设计教程>第6周课堂测验 课程:<程序设计与数据结构> 班级:1723 学生:唐才铭 学号:20172319 指导老师:王 ...

  3. Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心

    C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...

  4. C# 高级编程9 介绍篇

    对等网络 在日常软件环境中,解决了以下问题: 不断增加的客户端通讯负载放在服务器上,服务器必须与每个客户端进行通讯,导致站点崩溃.大流量消耗.服务器无法响应等问题. 因此产生了P2B网络技术. 使用P ...

  5. AtomicReference,AtomicStampedReference与AtomicMarkableReference的区别

    AtomicReference 通过volatile和Unsafe提供的CAS函数实现原子操作. 自旋+CAS的无锁操作保证共享变量的线程安全 value是volatile类型,这保证了:当某线程修改 ...

  6. 在SpringMVC中使用@RequestBody注解处理json时,报出HTTP Status 415的解决方案

    Spring的@RequestBody非常牛x,可以将提交的json直接转换成POJO对象. 正好今天有这样的需求,使用一下,结果一直报415,十分头疼. HTTP 415 错误 – 不支持的媒体类型 ...

  7. Serial Wire Debug (SWD) Interface -- PSoc5

    PSoC 5 supports programming through the serial wire debug (SWD) interface. There are two signals in ...

  8. 依赖注入(DI)和控制反转(IOC)的理解,写的太好了。

    学习过spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  9. C#网络编程技术SuperSocket实战项目演练

    一.SuperSocket课程介绍 1.1.本期<C#网络编程技术SuperSocket实战项目演练>课程阿笨给大家带来三个基于SuperSocket通讯组件的实战项目演示实例: ● 基于 ...

  10. 大不列颠百科全书Encyclopaedia Britannica Ultimate 2014光盘镜像

    大不列颠百科全书又名大英百科全书,是目前最古老的百科全书之一.大英百科全书每10余年出一个版本,如今已经推出到Encyclopaedia Britannica Ultimate 2014.此次推荐的是 ...