4412: [Usaco2016 feb]circular BarnDescription
There is a ring with n points, and the distance between the two points is 1. The point clockwise label is 1. N.
Each point has CI cow, guaranteed ∑ci=n.
Every cow can walk clockwise. Set a cow to go away D units stop, will consume d^2 energy.
Please design a way of cattle, so that each point has exactly one cow, and minimize the energy consumed.
Input
The first row is a number n. N <= 100000
Next n lines, one number per line CI.
Output
Output a number indicating the minimum cost of energy
Sample Input10
1
0
0
2
0
0
1
2
2
2Sample Output -The following:all cows can only move clockwise, so there should be a starting point. so how to find the starting point?? first take the ownership value-1, thenWe consider the question of if all cows from x->y will not come out of this range when and only if there is not a k, so that the k->y weight is positive, the brain to mend it ... then this starting point is the maximum substring of this ring and the starting point, prove not to speak, and then turn the ring into a chain, the end is the beginning of the first one. The last is to find the minimum value, this is also difficult. because (x+y) 2>x2+y2so to some point will definitely be in the middle of the shift, the specific implementation of a queue is good.
#include <stdio.h>#include<iostream>using namespacestd;Const intn=100005;intN,i,s,l,x,t,w,a[n],g[n];Long Longans;intMain () {scanf ("%d",&N); for(i=1; i<=n;i++) {scanf ("%d",&A[i]); A[n+i]=A[i]; } ans=0; x=1; for(i=1; i<=2*n-1; i++) { if(s<0) s=0, x=i; S+=a[i]-1; if(s>ans) {ans=s; L=x; }} t=1; w=0; ans=0; for(i=l;i<=l+n-1; i++) { while(a[i]--) g[++w]=i; Ans+=1ll* (I-g[t]) * (I-g[t]); T++; } cout<<ans; return 0;}
Bzoj 4412: [Usaco2016 feb]circular Barn