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 ...
随机推荐
- 【WEB API项目实战干货系列】- API登录与身份验证(三)
上一篇: [WEB API项目实战干货系列]- 接口文档与在线测试(二) 这篇我们主要来介绍我们如何在API项目中完成API的登录及身份认证. 所以这篇会分为两部分, 登录API, API身份验证. ...
- Android Studio修改项目的包名
android studio的修改包名,没有有Eclipse环境中那么好操作.也可能你对Eclipse的操作比较熟悉,对Andoid Studio的操作还不太熟悉.在项目的开发中,你可能遇到需要更改模 ...
- Bootstrap系列 -- 14. 表单控件输入框input
每一个表单都是由表单控件组成.离开了控件,表单就失去了意义.接下来的我们简单的来了解Bootstrap框架中表单控件的相关知识. 单行输入框,常见的文本输入框,也就是input的type属性值为tex ...
- [bzoj 1027][JSOI2007]合金(解析几何+最小环)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1027 分析: 首先因为一个合金的和为1,所以考虑2个材料合金能否合成一个需求合金的时候 ...
- AngularJS开发指南5:AngularJS表达式详解
AngularJS表达式类似Javascript的代码片段,通常在数据绑定中用到,写在双大括号中,如:{{表达式}}.表达式是用$parse方法来处理的. 下面是一些合法的AngularJS表达式 1 ...
- 2016 版 Laravel 系列入门教程(五)【最适合中国人的 Laravel 教程】
本教程示例代码见: https://github.com/johnlui/Learn-Laravel-5 在任何地方卡住,最快的办法就是去看示例代码. 本文是本系列教程的完结篇,我们将一起给 Arti ...
- MVC4 code first 增加属性,对应自动修改列的方法笔记
VS工具>库程序包管理器>程序包管理控制台,然后输入以下命令 enable-migrations -contexttypename Mvc4Application1.Models.Movi ...
- .Net MVC中访问PC网页时,自动切换到移动端对应页面
随着移动端的流行,越来越的网站,除了提供PC网页之外,也提供了移动端的H5页面,手机在访问www.xxx.com的时候,能自动跳转到mobile.xxx.com.网上很多在实现时也能使用JS直接进行跳 ...
- struts2中把action中的值传递到jsp页面的例子
例子: RegistAction的代码: package com.wss.action; import javax.servlet.http.HttpServletRequest; import or ...
- Oracle修改数据表
(1)修改数据表中某个字段值为空: update 表明 别名 set 别名.字段名 例子:update JWT_JYWF t set t.jdsbh=''(2)设置数据表中某个字段值和rownu ...