"bzoj-3293&1465&1045" cent coins & Candy pass x2 median + wtf

Source: Internet
Author: User

3293: [Cqoi2011] cent coins time limit:10 Sec Memory limit:128 MB
submit:854 solved:476
[Submit] [Status] [Discuss] Description

The Round table Sat n Individuals, each with a certain number of gold coins, the total number of coins can be evenly divisible by N . Each person can give him some gold coins around the neighboring people, eventually making each person's gold number equal. Your task is to find the minimum value of the number of coins that have changed hands.

Input

The first behavior is an integer n(n>=3), and the following n rows have a positive integer per line, giving the number of coins each has in counter-clockwise order.

Output

The minimum value of the number of coins to be transferred.

Sample Input4
1
2
5
4
Sample Output4
Sample explanation
Set four person number to 1,2,3,4. The 3rd person gives 2nd person 2 gold coins (becomes 1,4,3,4), 2nd person and 4th person gives 1th person 1 gold coins respectively.
HINT

n<=<=100000, total gold number <=10^9

Source1045: [HAOI2008] Candy pass time limit:10 Sec Memory limit:162 MB
submit:3115 solved:1419
[Submit] [Status] [Discuss] Description

There are N children sitting in a circle, each with Ai a candy. Each person can only pass candy to the left and right. Each person passes a candy price of 1.

Input

Number of children n below N line ai

Output

The minimum cost of obtaining equal candy for all people.

Sample Input4
1
2
5
4Sample Output4HINT

100% n<=987654321

Source 1465: Candy Pass time limit:2 Sec Memory limit:64 MB
submit:376 solved:177
[Submit] [Status] [Discuss] Description Teacher prepared a bunch of candy, just n a child can be divided into the number of sweets. The teacher wants n children to get candy, and then sit around the round table, the 1th child's left is the nth child, the other first child left is the first i-1 children. After everyone sat down, the teacher found that some children robbed a lot of sweets, some children only got a little candy, even a no?, set the first child has AI candy. Children can choose to give some candy to his left or right child, through the "Candy Pass" finally make each child get the number of sweets is as much, suppose a candy from a child to another child's price is 1, ask how to pass the total cost of the least. Input first line a positive integer n, indicating the number of children. n rows, one integer AI per line, representing the number of candies that the children of the first child get. Output outputs have only one number, indicating the minimum cost. Sample Input4
1
2
5
4
Sample Output4HINT

Data range
30% of the test data, n<=1000.
100% of the test data, n<=1000000.
Ai>=0, the AI is guaranteed to be within the Longint/int range, and the sum of the AI is within the Int64/long long range.

Source solution First, the number of sweets in the end of each child can be calculated, equal to the total number of candies divided by N, expressed in Ave. Assuming that the child labeled I began to have AI candy, Xi said that the first child gave the first I-1 children XI Candy, if xi<0, the I-1 children gave the first child XI Candy, X1 said the number of sweets to the nth children. So the final answer is ans=|. V12 + | x2| + | x3| + ... + | xn|. For the first child, he gave the nth child X1 candy, but also left a1-x1 candy, but because the 2nd child gave him X2 candy, so finally left a1-x1+x2 candy. According to test instructions, the last number of candies equals Ave, that is to get an equation: A1-x1+x2=ave. In the same vein, for a 2nd child, there is a2-x2+x3=ave. In the end, we can get n equations, there are n variables, but because the last equation can be deduced from the former n-1 equation, it is actually only n-1 that the equation is useful. Although the answer cannot be solved directly, but the other XI can be expressed with X1, the problem becomes the extremum of the single variable. For the 1th child, a1-x1+x2=ave-x2=ave-a1+x1 = X1-C1 (assuming c1=a1-ave, similar below) for the 2nd child, A2-x2+x3=ave-x3=ave-a2+x2= 2AVE-A1-A2+X1=X1-C2 for the 3rd child, A3-x3+x4=ave-x4=ave-a3+x3=3ave-a1-a2-a3+x1=x1-c3 ...  For nth children, An-xn+x1=ave. We want the sum of the absolute value of Xi to be as small as possible, i.e. | V12 + | x1-c1| + | x1-c2| + ... + | X1-cn-1| to be as small as possible. Notice that | The geometric meaning of x1-ci| is the distance from the point X1 to Ci on the axis, so the question becomes: N points on the given axis, find a point to their distance and try to be small, and this point is the median of these numbers, proving a little bit. Transfer from HzwerCode
#include <iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>using namespacestd;intn,a[1000010],b[1000010];Long LongSum,ans;intMain () {scanf ("%d\n",&N);  for(intI=1; i<=n; i++) scanf ("%d", &a[i]), sum+=A[i]; intpj=sum/N;  for(intI=1; i<=n; i++) B[i]=b[i-1]+a[i]-PJ; Sort (b+1, b+n+1); intmid=b[n/2+1];  for(intI=1; i<=n; i++) ans+=abs (mid-B[i]); printf ("%lld\n", ans); return 0;}

...... A lunatic ...

"bzoj-3293&1465&1045" cent coins & Candy pass x2 median + wtf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.