2620: [Usaco2012 Mar]Haybale Restacking

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

 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.      
时间: 2024-10-26 03:00:14

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

BZOJ2620 [Usaco2012 Mar]Haybale Restacking

恩,非常好的题...至少思路非常巧妙 首先可以得到性质:对于相邻的两堆A & B,A给B然后B再给A是完全没有意义的...也就是说只能单向传递 然后我们记下每个点要给(被给)多少堆干草a[i] 同时可以计算出del[i],表示若第i堆只向右传且第n堆不向第1堆运任何干草的情况下i - 1向i传递干草的数量 del[i] = del[i - 1] + a[i - 1](其实就是前缀和) 现在1可以向右移了,设向右移x堆,则ans = Σabs(del[i] - x) 故x = mid(del +

BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper

首先比较容易想到是状态压缩DP 令$f[S]$表示选取了集合$S$以后,已经送了最少次数cnt且当前电梯剩下的体积rest最大(即$f[S]$是一个二元组(cnt, rest)) 于是$f[S] = min_{i \in S} f[S - {i}] + v[i]$ $<$和$+$运算详情就请看程序好了,反正就是一个贪心思想,总复杂度$O(n * 2 ^ {n - 1})$ 1 /***********************************************************

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

https://www.luogu.org/problemnew/show/P2698#sub 老板需要你帮忙浇花.给出N滴水的坐标,y表示水滴的高度,x表示它下落到x轴的位置. 每滴水以每秒1个单位长度的速度下落.你需要把花盆放在x轴上的某个位置,使得从被花盆接着的第1滴水开始,到被花盆接着的最后1滴水结束,之间的时间差至少为D. 我们认为,只要水滴落到x轴上,与花盆的边沿对齐,就认为被接住.给出N滴水的坐标和D的大小,请算出最小的花盆的宽度W. 单调队列好题,参考洛谷题解. emm……显然是

1639: [Usaco2007 Mar]Monthly Expense 月度开支

1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 593  Solved: 295[Submit][Status] Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N<=100,000)个工作日中每一天的花费moneyi(1<=money

[BZOJ] 1639: [Usaco2007 Mar]Monthly Expense 月度开支

1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1077  Solved: 533[Submit][Status][Discuss] Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N<=100,000)个工作日中每一天的花费moneyi(1

[BZOJ1639][Usaco2007 Mar]Monthly Expense 月度开支

1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1069  Solved: 530 [Submit][Status][Discuss] Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N<=100,000)个工作日中每一天的花费moneyi

[BZOJ1617][Usaco2008 Mar]River Crossing渡河问题

1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1102  Solved: 801 [Submit][Status][Discuss] Description Farmer John以及他的N(1 <= N <= 2,500)头奶牛打算过一条河,但他们所有的渡河工具,仅仅是一个木筏. 由于奶牛不会划船,在整个渡河过程中,FJ必须始终在木筏上.在这个基础上,木筏上的奶牛数

bzoj 1637: [Usaco2007 Mar]Balanced Lineup

1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,000)和种族(0或1). 一直以来 Farmer John 总是喜欢做一些非凡的事,当然这次照相也不例外.他只给一部分牛照相,并且这一组牛的阵容必须是"

Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved: 238[Submit][Status][Discuss] Description 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序.    约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一