Building Blocks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2209    Accepted Submission(s): 509

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
 
题意:在一个区间里面进行块的增删,让某段长度为W的区间高度都为H,每次移动一块,问最少要移动多少次?
题解:刚开始漏看了每次移动一块这个条件,打死也做不出来..这个题目的解法就是区间的移动,左端点删除以及右端点的添加来维护所有长度为W的区间,但是要在前面和后面添W个0块,因为我们有可能是往两边补。维护两个变量,一个是把高变矮 t,一个是把矮变高 s,最开始的区间是[1,W],这个区间我们要将矮的变高,所以最开始我们 s = w*h ,ans =w*h
,然后我们的区间右移到[2,W+1],这时我们要把1删除,然后将w+1添加进去,这样的话对 s,t进行加减,然后取个大值就行了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int N = ;
LL n,w,h;
LL high[N];
int main()
{
while(scanf("%lld%lld%lld",&n,&w,&h)!=EOF){
LL sum = ;
memset(high,,sizeof(high));
for(int i=;i<w+;i++){
high[i]-=h;
}
for(int i=w+;i<w++n;i++){
scanf("%lld",&high[i]);
sum+=high[i];
high[i]-=h;
}
for(int i=w++n;i<=w+w+n;i++){
high[i]-=h;
}
if(sum<h*w){
printf("-1\n");
continue;
}
LL s=w*h,t=,ans = w*h; ///s维护将高的拿走,t维护将矮的补上,最开始[1,w]要补w*h进去,所以ans初始化w*h
for(int i=w+;i<=w+w+n;i++){
if(high[i-w]>) t-=high[i-w]; ///删除第 i-w 块
else s+=high[i-w];
if(high[i]>) t+=high[i]; ///添加第 i 块
else s-=high[i];
ans = min(ans,max(t,s));
}
printf("%lld\n",ans);
}
return ;
}

hdu 5191(思路题)的更多相关文章

  1. hdu 4908(思路题)

    BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  3. hdu 5101(思路题)

    Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. hdu 5063(思路题-反向操作数组)

    Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu 4859(思路题)

    Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. hdu 4956(思路题)

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 5400(思路题)

    Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. HDU 1173 思路题

    题目大意 有n个地点(坐标为实数)需要挖矿,让选择一个地点,使得在这个地方建造基地,到n个地点的距离和最短,输出基地的坐标. 题解+代码: 1 /* 2 把这个二维分开看(即把所有点投影到x轴上,再把 ...

  9. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

随机推荐

  1. OpenCV中的按钮问题

    在HighGUI中,没有显示提供任何形式的按钮.一般有两种方法替代: 1.用只有两个状态的滑动条来替代按钮.开关(switch)事实上就是只有两个状态的滑动条,这两个状态是on和off.然后通过回调函 ...

  2. windows curl 命令

    windows 64 curl 命令的使用 https://blog.csdn.net/qq_27093465/article/details/53545693 curl命令可以通过命令行的方式,执行 ...

  3. java 使用ByteArrayOutputStream和ByteArrayInputStream实现深拷贝

    首先介绍Java中的浅拷贝(浅克隆)和深拷贝(深克隆)的基本概念: 浅拷贝: 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.浅复制仅仅复制所考虑的对象,而 ...

  4. 利用FFT来进行字符串匹配

    给定串A和串B,A由26个小写字母构成,B由?和26个小写字母构成 ?可以和任意字符匹配 求A中出现了多少次B 这里可以使用fft做法,定义向量A和向量B 然后求A和rev(B)的卷积结果C C的第i ...

  5. 【题解】JSOI2009游戏

    真的没想到...果然反应太迟钝,看到题目毫无思路,一点联想都没有. 按照网上博客的说法:一眼棋盘染色二分->二分图->最大匹配->BINGO?果然我还是太弱了…… 我们将棋盘黑白染色 ...

  6. 【BZOJ 3925】[Zjoi2015]地震后的幻想乡 期望概率dp+状态压缩+图论知识+组合数学

    神™题........ 这道题的提示......(用本苣蒻并不会的积分积出来的)并没有 没有什么卵用 ,所以你发现没有那个东西并不会 不影响你做题 ,然后你就可以推断出来你要求的是我们最晚挑到第几大的 ...

  7. Eclipse中的引用项目报Could not find *.apk!解决办法

    百度上很多关于Could not find *.apk!这种编译报错的解决帖子,但是笔主在这里主要说一下在 引用工程项目的场景 下报这个错误消息的问题(不影响本项目的正常编译运行!). 笔主刚从谷歌上 ...

  8. JQuery中的each()的使用

    each()函数是基本上所有的框架都提供了的一个工具类函数,通过它,你可以遍历对象.数组的属性值并进行处理. jQuery和jQuery对象都实现了该方法,对于jQuery对象,只是把each方法简单 ...

  9. cookie 是存储于访问者的计算机中的变量

    今天把javascript如何用来创建及存储cookie复习了一下,其中的一点体会拿出来和大家讨论,首先看一下基础知识: 什么是cookie cookie 是存储于访问者的计算机中的变量.每当同一台计 ...

  10. HDU 2844 二进制优化的多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...