2620: [Usaco2012 Mar]Haybale Restacking
2015-04-03 22:08
399 查看
2620: [Usaco2012 Mar]Haybale Restacking
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 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 13INPUT 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
13OUTPUT 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[0..200000] 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 2]; 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+1) div 2; for i:=1 to n do begin readln(a1,a2); a[i]:=a1-a2; end; for i:=2 to n do b[i]:=a[i-1]+b[i-1]; b[1]:=a +b ; //A掉后才发现这句话其实完全可以删掉,想想为什么^_^ sort(1,n);ans:=0; for i:=1 to n do ans:=ans+abs(b[m]-b[i]); writeln(ans); end.
相关文章推荐
- BZOJ2620 [Usaco2012 Mar]Haybale Restacking
- bzoj2620[Usaco2012 Mar]Haybale Restacking
- USACO 2012 Mar Haybale Restacking 重排干草
- bzoj 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机
- BZOJ 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机
- bzoj 1682: [Usaco2005 Mar]Out of Hay 干草危机(最小生成树)
- 洛谷3258:[USACO2012 MAR]Flowerpot 花盆——题解
- BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
- 动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
- 【最小生成树】BZOJ1682[Usaco2005 Mar]-Out of Hay 干草危机
- bzoj1615 【Usaco2008 Mar】The Loathsome Hay Baler 麻烦的干草打包机
- bzoj1682 [Usaco2005 Mar]Out of Hay 干草危机(kruskal裸题)
- 【POJ3657】【USACO 2008 Jan Gold】 1.Haybale Guessing 二分答案,并查集check
- BZOJ_P1682 [Usaco2005 Mar]Out of Hay 干草危机(最小生成树)
- BFS-BZOJ-1615-[Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机
- BZOJ 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机 bfs
- 【BZOJ】1682: [Usaco2005 Mar]Out of Hay 干草危机(kruskal)
- 【BZOJ】1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机(模拟+bfs)
- 1682: [Usaco2005 Mar]Out of Hay 干草危机
- [USACO 08JAN]Haybale Guessing