[Greedy] card sharing issues

Solitaire ring sharing

 

Sharing Solitaire noip2002   

There N stacks of cards, each with a stack of several sheets, the total number of cards must be N multiples. A plurality of cards can take on either a pile and then moved.

Card moving rules: In No. 1 card heap taken, only moved to No. 2 on the heap; numbered heap take the N card, only moved to No. N - heap 1; Other take the heap of cards, can be moved left or right adjacent to the heap.

Now asked to find a mobile method with the least number of moves both the number of cards per stack as many

The first bunch of cards differ cards can only assume (give or ask for) by a second stack cards

After considering only deal with piles pile (pile has to deal with before it causes a waste reprocessing

#include<bits/stdc++.h>
using namespace std;
#define rg register
const int N=1e6+5,M=1e6+5,inf=0x3f3f3f3f,P=9999973;
int n,a[N],x=0,ans=0;
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

int main(){
    rd(n);
    for(int i=1;i<=n;++i) rd(a[i]),x+=a[i];
    x/=n;
    for(int i=1;i<=n;++i)
    if(a[i]-x) ++ans,a[i+1]+=(a[i]-x);
    printf("%d",ans);
    return 0;
}

 

[HAOI2008] Confectionery transfer

There are n children sitting in a circle, each have a candy ai. Only about two per person to deliver candy. Each person passes a candy price for 1. Seeking to give all individuals equal the minimum cost of candy.

Suppose designated i children began Ai pieces of candy, Xi indicates the i-th child to the first i-1 th child Xi pieces of candy, if Xi <0, described the first i-1 th child to the i-th child Xi particles candy, X1 represents a number of children to the n-th children's candy. So the final answer is ans = | X1 | + | X2 | + | X3 | + ...... + | Xn |. For the first child, he gave n-th child X1 candy pieces, candy pieces left A1-X1; however, because the first two children gave him X2 candy pieces, the final remaining A1-X1 + X2 candy pieces. According meaning of the questions, the last equals the number of candy ave, obtain an equation of: A1-X1 + X2 = ave.

1 for the first child, A1-X1 + X2 = ave -> X2 = ave-A1 + X1 = X1-C1 (assuming C1 = A1-ave, similar to the following)

For the first two children, A2-X2 + X3 = ave -> X3 = ave-A2 + X2 = 2ave-A1-A2 + X1 = X1-C2

For the first three children, A3-X3 + X4 = ave -> X4 = ave-A3 + X3 = 3ave-A1-A2-A3 + X1 = X1-C3

For the n-th child ......, An-Xn + X1 = ave

Xi hoped the absolute value of the sum as small as possible, that | X1 | + | X1-C1 | + | X1-C2 | + ...... + | X1-Cn-1 | as small as possible, noting that | X1-Ci | geometry significance is Ci from point X1 to the number line, so the question becomes: given n points set on the number line, to find a distance and try their small point, but the point is that these numbers are in Los digits from the first to the Valley ( problem solution

Probably push it again to understand Liao

#include<bits/stdc++.h>
using namespace std;
#define Max(x,y) (x)<(y)?(y):(x)
#define Min(x,y) (x)<(y)?(x):(y)
#define ll long long
#define Abs(x) (x)<0?-x:x;
#define rg register
const int N=1e6+5,M=1e6+5,inf=0x3f3f3f3f,P=9999973;
int n;
ll ans=0,a[N],sum[N],b[N],ave=0;
template <class t>void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}

int main(){
//    freopen("in.txt","r",stdin);
    rd(n);
    for(int i=1;i<=n;++i) rd(a[i]),sum[i]=sum[i-1]+a[i];
    ave=sum[n]/n;
    for(int i=1;i<=n;++i) b[i]=b[i-1]-a[i]+ave;
    sort(b+1,b+n+1);
    ll mid;
    if(n%2) mid=b[n+1>>1];
    else mid=(b[n>>1]+b[n>>1|1])/2;
    for(int i=1;i<=n;++i) ans+=abs(mid-b[i]);
    printf("%lld",ans); 
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lxyyyy/p/11285684.html