首页 技术 正文
技术 2022年11月15日
0 收藏 976 点赞 3,418 浏览 1858 个字

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 DescriptionAfter 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. InputThere 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. OutputOutput the minimum number of blocks should LeLe move.
If there is no solution, output “-1” (without quotes). Sample Input4 3 2
1 2 3 5
4 4 4
1 2 3 4 Sample Output1
-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 }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903