2620: [Usaco2012 Mar]Haybale Restacking
2620: [Usaco2012 Mar]Haybale Restacking
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 201 Solved: 111
Description
Farmer John has just ordered a large number of bales of hay. He would like to organize these into N piles (1 <= N <= 100,000) arranged in a circle, where pile i contains B_i bales of hay. Unfortunately, the truck driver delivering the hay was not listening carefully when Farmer John provided this information, and only remembered to leave the hay in N piles arranged in a circle. After delivery, Farmer John notes that pile i contains A_i bales of hay. Of course, the A_i's and the B_i's have the same sum. Farmer John would like to move the bales of hay from their current configuration (described by the A_i's) into his desired target configuration (described by the B_i's). It takes him x units of work to move one hay bale from one pile to a pile that is x steps away around the circle. Please help him compute the minimum amount of work he will need to spend.
给出n块土地,现有泥土A[i],需要改造成B[i],但这次土地排列成环,且不可买进买出,只能运,且∑A[i]=∑B[i],问最小花费。
Input
INPUT FORMAT: * Line 1: The single integer N. * Lines 2..1+N: Line i+1 contains the two integers A_i and B_i (1 <= A_i, B_i <= 1000).
Output
Sample Input
4 7 1 3 4 9 2 1 13 INPUT DETAILS: There are 4 piles around a circle. Initially, the piles contain 7, 3, 9, and 1 bales of hay. Farmer John would like to move them so the piles contain 1, 4, 2, and 13 bales of hay.
Sample Output
13 OUTPUT DETAILS: A minimum of 13 units of work is required (move 6 bales from pile 1 to pile 4, move 1 bale from pile 3 to pile 2, and move 6 bales from pile 3 to pile 4).
HINT
Source
题解:显然不用多说,在最优方案中,两个堆之间的移动必然是单向的,不可能出现甲挪到乙,然后又白费力气挪回去的情况,然后算出每个堆最终的移动情况,然后求出前缀和,然后弄出相对于中位数的绝对值之差的和即可
然后我壮烈的交了上去,然后壮烈的WA,然后杯具地把longint改成int64,然后杯具地AC。。。数据类型又坑爹了不解释
1 var
2 i,j,k,l,m,n:longint;
3 a1,a2,ans:int64;
4 a,b:array[0..200000] of int64;
5 procedure swap(var x,y:int64);
6 var z:int64;
7 begin
8 z:=x;x:=y;y:=z;
9 end;
10 procedure sort(l,r:longint);inline;
11 var i,j:longint;x:int64;
12 begin
13 i:=l;j:=r;x:=b[(l+r) div 2];
14 repeat
15 while b[i]<x do inc(i);
16 while b[j]>x do dec(j);
17 if i<=j then
18 begin
19 swap(b[i],b[j]);
20 inc(i);dec(j);
21 end;
22 until i>j;
23 if i<r then sort(i,r);
24 if l<j then sort(l,j);
25 end;
26 begin
27 readln(n);m:=(n+1) div 2;
28 for i:=1 to n do
29 begin
30 readln(a1,a2);
31 a[i]:=a1-a2;
32 end;
33 for i:=2 to n do b[i]:=a[i-1]+b[i-1];
34 b[1]:=a[n]+b[n]; //A掉后才发现这句话其实完全可以删掉,想想为什么^_^
35 sort(1,n);ans:=0;
36 for i:=1 to n do ans:=ans+abs(b[m]-b[i]);
37 writeln(ans);
38 end.
- 11-移动端开发教程-zepto.js入门教程
- 【OpenCV学习笔记之一】图像加载,修改及保存
- 【干货】一种直观的方法认识梯度下降
- 漫谈Java IO之普通IO流与BIO服务器
- 浅谈强化学习的方法及学习路线
- 【亲测有效】Win10家庭版Microsoft Edge页面出现乱码的两种解决方案及gpedit.msc命令无法使用的解决策略
- Fiddler抓包7-post请求(json)
- Selenium2+python自动化56-unittest之断言(assert)
- 长文 | 手把手教你如何使用python进行数据分析(最好将文章代码自己码一遍)
- 回归与梯度下降法及实现原理
- 【宅男宅女们的福音】电影天堂最新电影爬取及搜索脚本
- 把模块有关联的放在一个文件夹中 在python2中调用文件夹名会直接失败 在python3中调用会成功,但是调用不能成功的解决方案
- numpy用法小结
- 凯撒密码加解密及破解实现原理
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 初识Spark
- 初识SpringBoot
- MyBatis结果集映射
- Hadoop分布式集群环境搭建
- 使用Hadoop统计日志数据
- Mybatis在接口上使用注解配置SQL语句以及接口与xml一起使用
- 分布式计算框架MapReduce
- 安装webpack后,执行webpack -v命令时报错:SyntaxError: Block-sc
- SpringMVC数据类型转换器与国际化配置
- 分布式资源调度——YARN框架
- 在SpringMVC中使用数据验证组件——hibernate-validator
- 我的 2020 iOS BAT 面试心得
- Java操作HDFS开发环境搭建以及HDFS的读写流程
- HDFS伪分布式环境搭建
- 初识Hadoop