2620: [Usaco2012 Mar]Haybale Restacking

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 201  Solved: 111
[Submit][Status][Discuss]

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。。。数据类型又坑爹了不解释

 var
i,j,k,l,m,n:longint;
a1,a2,ans:int64;
a,b:array[..] of int64;
procedure swap(var x,y:int64);
var z:int64;
begin
z:=x;x:=y;y:=z;
end;
procedure sort(l,r:longint);inline;
var i,j:longint;x:int64;
begin
i:=l;j:=r;x:=b[(l+r) div ];
repeat
while b[i]<x do inc(i);
while b[j]>x do dec(j);
if i<=j then
begin
swap(b[i],b[j]);
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if l<j then sort(l,j);
end;
begin
readln(n);m:=(n+) div ;
for i:= to n do
begin
readln(a1,a2);
a[i]:=a1-a2;
end;
for i:= to n do b[i]:=a[i-]+b[i-];
b[]:=a[n]+b[n]; //A掉后才发现这句话其实完全可以删掉,想想为什么^_^
sort(,n);ans:=;
for i:= to n do ans:=ans+abs(b[m]-b[i]);
writeln(ans);
end.

2620: [Usaco2012 Mar]Haybale Restacking的更多相关文章

  1. BZOJ2620 [Usaco2012 Mar]Haybale Restacking

    恩,非常好的题...至少思路非常巧妙 首先可以得到性质:对于相邻的两堆A & B,A给B然后B再给A是完全没有意义的...也就是说只能单向传递 然后我们记下每个点要给(被给)多少堆干草a[i] ...

  2. 动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper

      2621: [Usaco2012 Mar]Cows in a Skyscraper Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 303  Sol ...

  3. BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper

    首先比较容易想到是状态压缩DP 令$f[S]$表示选取了集合$S$以后,已经送了最少次数$cnt$且当前电梯剩下的体积$rest$最大(即$f[S]$是一个二元组$(cnt, rest)$) 于是$f ...

  4. bzoj2621: [Usaco2012 Mar]Cows in a Skyscraper(状压DP)

    第一眼是3^n*n的做法...然而并不可行T T 后来发现对于奶牛的一个状态i,最优情况下剩下那个可以装奶牛的电梯剩下的可用重量是一定的,于是我们设f[i]表示奶牛状态为i的最小电梯数,g[i]为奶牛 ...

  5. 洛谷3258:[USACO2012 MAR]Flowerpot 花盆——题解

    https://www.luogu.org/problemnew/show/P2698#sub 老板需要你帮忙浇花.给出N滴水的坐标,y表示水滴的高度,x表示它下落到x轴的位置. 每滴水以每秒1个单位 ...

  6. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  7. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  8. USACO March. 2012

    Connect the Cows Times17 水题 Landscaping Flowerpot Tractor 广搜 搜到边界就可以终止了 没什么难度 #include <stdio.h&g ...

  9. USACO2012 Haybale stacking /// 区间表示法 oj21556

    题目大意:N个方块 标号1~N  K个操作 操作a b 表示标号a~b区间每位多加一个方块 Input * Line 1: Two space-separated integers, N  K. * ...

随机推荐

  1. oracle取字符串长度的函数length()和hengthb()

    http://blog.itpub.net/161195/viewspace-613263/ lengthb(string)计算string所占的字节长度 :返回字符串的长度,单位是字节 length ...

  2. loadrunner:web services接口测试

    本文以实例讲解web services接口测试操作,内容包括:脚本生成.参数化和接口与接口间的取值关联操作. 网站"http://www.webxml.com.cn/zh_cn/web_se ...

  3. boneCP的连接管理

    boneCP连接的实现 boneCP自己实现了标准的java.sql.Connection接口,除了会持有Connection对象之外,还会拥有一些属性用于标记连接的创建时间,空闲时间等. 比较重要的 ...

  4. CSS3中字体平滑处理和抗锯齿渲染

    在围观Drupal官方主题的时候,发现了一个有意思的非标准CSS选择器-webkit-font-smoothing,于是上手把玩了一番.如何使用css3字体平滑显示呢 要知道,W3C对CSS中字体的抗 ...

  5. 《JAVASCRIPT高级程序设计》第五章(2)

    一.Date类型 Date类型类型用于保存日期,有以下几种创建方式: //获取当前时间 var now = new Date(); //获取当前时间的毫秒数 var nowSecond = Date. ...

  6. API网关Ocelot 使用Polly 处理部分失败问题

    在实现API Gateway过程中,另外一个需要考虑的问题就是部分失败.这个问题发生在分布式系统中当一个服务调用另外一个服务超时或者不可用的情况.API Gateway不应该被阻断并处于无限期等待下游 ...

  7. SQL 数据库基本知识

    SQL:Structured Quety Language SQL SERVER是一个以客户/服务器(c/s)模式访问.使用Transact-SQL语言的关系型数据库管理子系统(RDBMS) DBMS ...

  8. js详解之作用域-实例

    函数如下大家可以做做看 function aa(a,b,c){ function a(){} console.log(a); console.log(aa); console.log(argument ...

  9. 1activiti认识和数据库和插件配置

    工作流介绍 工作流(Workflow),就是通过计算机对业务流程自动化执行管理.它主要解决的是"使在多个参与者之间按照某种预定义的规则自动进行传递文档.信息或任务的过程, 从而实现某个预期的 ...

  10. 【福利大放送】不止是Android,Github超高影响力开源大放送,学习开发必备教科书

    一.写在前面 最近项目重构,时间贼多,也没什么时间更新博客,个人的开源项目也是多时没有更新了:github地址,然而没有更新不代表我不在乎,后面一有空还是会继续提交的. 还是来冒个泡,给大家献上一些福 ...