码迷,mamicode.com
首页 > 其他好文 > 详细

bc.34.B.Building Blocks(贪心)

时间:2015-03-22 10:34:30      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

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(1n,W,H50000).n indicate n piles blocks.
For the next line ,there are n integers A1,A2,A3,,An indicate the height of each piles. (1Ai50000)
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 }
View Code

bc.34.B.Building Blocks(贪心)

标签:

原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4356762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!