bc.34.B.Building Blocks(贪心)
Building Blocks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 751 Accepted Submission(s): 164
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 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.
If there is no solution, output "-1" (without quotes).
1 2 3 5
4 4 4
1 2 3 4
-1
In first case, LeLe move one block from third pile to first pile.
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(贪心)的更多相关文章
- [翻译]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 ...
- 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 ...
- 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—— 5159 Building Blocks
Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks. LeL ...
- 四、Implementation: The Building Blocks 实现:构件
四.Implementation: The Building Blocks 实现:构件 This is the essential part of this guide. We will introd ...
- 2.3 Core Building Blocks 核心构件
Core Building Blocks 核心构件 DDD mostly focuses on the Domain & Application Layers and ignores the ...
- HDU 5191 Building Blocks
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5191 bc(中文):http://bestcoder.hdu.edu.cn/contests ...
随机推荐
- Java:静态代理 and 动态代理
代理模式是常用的设计模式,其特征是代理类与委托类具有相同的接口,在具体实现上,有静态代理和动态代理之分.代理类与委托类之间通常会存在关联关系,一个代理类的对象与一个委托类的对象关联,代理类的对象本身并 ...
- [codevs 1051]接龙游戏(栈)
题目:http://codevs.cn/problem/1051/ 分析: 当然单词查找树是可以的,但这题有更为简便的方法.可以先按字典序排序,然后弄一个栈,如果当前字串可以接到栈顶元素的后面,那么当 ...
- C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)
前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...
- AngularJS开发指南1:AngularJS简介
什么是 AngularJS? AngularJS 是一个为动态WEB应用设计的结构框架.它能让你使用HTML作为模板语言,通过扩展HTML的语法,让你能更清楚.简洁地构建你的应用组件.它的创新点在于, ...
- ORA-12737: Instant Client Light: unsupported server character set CHS16GBK/ZHS16GBK解决方案
二.Navicat for Oracle的配置 1.启动该工具,出现如下的开始界面,单击“连接”选项,进行连接数据库,如图所示: 6.在“新建连接”对话框中,输入任意的连接名,选择默认的连接类型,输入 ...
- Jetty+json-lib库抛异常的问题解决过程(java.lang.NoClassDefFoundError: net/sf/json/JSONObject)
一.之前抛异常是将json库改成了fastjson解决的,参见: http://www.cnblogs.com/gossip/p/5369670.html 异常信息: 二.解决步骤 ...
- 关于 Maven 的插件maven-war-plugin
在进行项目发布的时候,可能会碰到这样的情况, 希望在保持项目源代码不变的前提下,希望能够针对不同的运行环境获得相应的运行包.(比如war包) 基本配置 :(包括排除 不想打进war包的jar 的配置) ...
- Spring对Hibernate的session生效期(事物提交管理)介绍
在Hibernate中我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据操作,然后提交事务,关闭事务,这样做的原因是因为Hibernate默认的事务自动提交是false,它是需要人为的手动 ...
- iframe标签用法详解(属性、透明、自适应高度)
1.iframe 定义和用法 iframe 元素会创建包含另外一个文档的内联框架(即行内框架). HTML 与 XHTML 之间的差异 在 HTML 4.1 Strict DTD 和 XHTML 1. ...
- iOS之单例
今天在看多线程同步时,突然想到了单例的同步问题.自从dispatch_once出现后,我们创建单例非常简单且安全: static dispatch_once_t pred; static Single ...