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

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 }

时间: 2024-10-10 22:11:17

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

HDU - 5190 - Go to movies &amp;&amp; 5191 - Building Blocks (BC#34 A,B)

Go to movies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 99    Accepted Submission(s): 67 Problem Description Winter holiday is coming!As the monitor, LeLe plans to go to the movies. Becaus

BC#34 1002 hdu 5192 Building Blocks

Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 60    Accepted Submission(s): 6 Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks.

Building Blocks (hdu 5191)

Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 281    Accepted Submission(s): 59 Problem Description After enjoying the movie,LeLe went home alone. LeLe decided to build blocks

EF6 SQL Logging – Part 3: Interception building blocks | One Unicorn

In parts 1 and 2 of this series we looked at how to use DbContext.Database.Log to log the SQL generated by EF. But this code is actually a relatively thin fa?ade over some low-level building blocks for interception in general and, in this case, DbCom

DTD - XML Building Blocks

The main building blocks of both XML and HTML documents are elements. The Building Blocks of XML Documents Seen from a DTD point of view(从dtd的角度来看), all XML documents (and HTML documents) are made up by the following building blocks: Elements Attribu

Intel&#174; Threading Building Blocks (Intel&#174; TBB) Developer Guide 中文 Parallelizing Data Flow and Dependence Graphs并行化data flow和依赖图

https://www.threadingbuildingblocks.org/docs/help/index.htm Parallelizing Data Flow and Dependency Graphs In addition to loop parallelism, the Intel® Threading Building Blocks (Intel® TBB) library also supports graph parallelism. It's possible to cre

UVA 11039 Building designing 贪心

题目链接:UVA - 11039 题意描述:建筑师设计房子有两条要求:第一,每一层楼的大小一定比此层楼以上的房子尺寸要大:第二,用蓝色和红色为建筑染色,每相邻的两层楼不能染同一种颜色.现在给出楼层数量和每层楼的尺寸(楼层尺寸的大小没有按照顺序给出),求出满足这样要求的最大楼层数. 算法分析:把楼层尺寸按照从大到小排序,然后遍历一次的同时记录相邻楼层所染颜色不同,把不满足要求的楼层去掉即可. 1 #include<iostream> 2 #include<cstdio> 3 #inc

01-02 Introducing the Building Blocks of the .NET Platform (the CLR, CTS, and CLS)

The primary role of the CLR is to locate, load, and manage .NET objects on your behalf. CLR主要的角色就是为你定位,加载和管理.Net对象. The CLR also takes care of a number of low-level details such as memory management, application hosting, coordinating threads, and per

Android GUI Building Blocks

说明:此笔记为“Android开发”学习视频的笔记,链接如下:http://open.163.com/movie/2010/1/8/D/M79HE97C3_M79HEQJ8D.html 一, Activity: A represnetation of a user doing something, 例子:显示联系人列表并且选中一个联系人 Activity的三种状态:Stopped, Paused,Active 二, Service: an application or an applicaton