Building Blocks

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

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.

 
这道题唯一让我有些欣慰的是我找到了一种蛮清楚的判断一个w大小的区间 的 最小移动数。(这几次的bc.B都有功亏一篑的感觉)
但是未考虑到遍历区间总长度为 1 ~ n + w + w (改了一下就AC了)
还有一点是long long , orz
 1 #include<stdio.h>
2 #include<string.h>
3 #include<math.h>
4 #include<algorithm>
5 using namespace std;
6 typedef long long ll ;
7 const int inf = 0x3f3f3f3f ;
8 int n , w , h ;
9
10 ll a[50010 * 3] ;
11
12 int main ()
13 {
14 //freopen ("a.txt" , "r" , stdin ) ;
15 while (~ scanf ("%d%d%d" , &n , &w , &h) ) {
16 int sum = 0 ;
17 memset (a , 0 , sizeof(a)) ;
18 for (int i = w + 1; i <= n + w ; i++) {
19 scanf ("%d" , &a[i]) ;
20 sum += a[i] ;
21 }
22 int blog = w * h ;
23 if (sum < blog ) {
24 puts ("-1") ;
25 continue ;
26 }
27 int minn = inf ;
28 ll l = 0 , r = 0 ;
29 for (int i = 1 ; i <= n + w + w ; i++ ) {
30 a[i] - h < 0 ? l += a[i] - h : r += a[i] - h ;
31
32 if (i >= w) {
33 l = -l ;
34 int temp = max (l , r) ;
35 if (minn > temp ) {
36 minn = temp ;
37 }
38 l = -l ;
39 a[i - w + 1] - h < 0 ? l -= a[i - w + 1] - h : r -= a[i - w + 1] - h ;
40 }
41 }
42
43 // printf ("index = %d\n" , index) ;
44 printf ("%d\n" , minn ) ;
45 }
46 return 0 ;
47 }

bc.34.B.Building Blocks(贪心)的更多相关文章

  1. [翻译]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 ...

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

  3. DTD - XML Building Blocks

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

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

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

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

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

  6. HDU—— 5159 Building Blocks

    Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks. LeL ...

  7. 四、Implementation: The Building Blocks 实现:构件

    四.Implementation: The Building Blocks 实现:构件 This is the essential part of this guide. We will introd ...

  8. 2.3 Core Building Blocks 核心构件

    Core Building Blocks 核心构件 DDD mostly focuses on the Domain & Application Layers and ignores the ...

  9. HDU 5191 Building Blocks

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

随机推荐

  1. Win10开发究竟能实现哪些牛逼的功能

    经Win10开发者群(53078485)大咖Aran童鞋授权,这次先Show一下他通过vs2015做的跨端APP一些高级功能的GIF图,大家可以回帖想要哪个功能的DEMO,我和Aran说一下,会把DE ...

  2. 【MPI学习4】MPI并行程序设计模式:非阻塞通信MPI程序设计

    这一章讲了MPI非阻塞通信的原理和一些函数接口,最后再用非阻塞通信方式实现Jacobi迭代,记录学习中的一些知识. (1)阻塞通信与非阻塞通信 阻塞通信调用时,整个程序只能执行通信相关的内容,而无法执 ...

  3. jQuery Easy UI 开发笔记

    1.jQuery Easy UI主要的运行原理是通过核心的代码调用插件来实现UI效果的 2.jQuery Easy UI插件与插件之间的关系是: 一.独立式插件: 独立式插件是指:不与其他的插件具有相 ...

  4. lei

    upstream pap_backend_conf { keepalive 128; server 127.0.0.1:9221 max_fails=2 fail_timeout=5 weight=1 ...

  5. 11.C#迭代器(六章6.1)

    今天我们说下C#中的迭代器,首先引出一些关于迭代的概念,后面举出代码供大家讨论. 迭代器模式是行为模式的一种范例,行为模式是一种简化对象之间通信的一种设计模式.在.NET中使用IEnumerator和 ...

  6. Bootstrap3.0学习第九轮(CSS补充)

    详情请查看http://aehyok.com/Blog/Detail/15.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  7. C基础--结构体

    C语言,结构体语法: 1.定义结构体类型: struct 结构体名称 { 成员类型 成员名称1; 成员类型 成员名称2; ... }; 例:struct Date { int year ; int m ...

  8. 小菜鸟学 MQ(三)

    创建程序测试MQ 1,创建生产者 package com.robert; import java.util.Hashtable; import java.util.Map; import javax. ...

  9. Java基础-CGLIB动态代理

    JDK的动态代理机制只能代理实现了接口的类,而不能实现接口的类就不能实现JDK的动态代理,cglib是针对类来实现代理的,他的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继 ...

  10. Yii2分页

    Yii中的分页功能主要由yii\web: Linkable接口.yii\widgets: LinkPager类和yii\data: Pagination类三个组成 yii\data: Paginati ...