Luogu P1031 Split Solitaire

Source: Internet
Author: User
Tags split
P1031 the cards evenly Description

See link Analysis

This problem is categorized in mock and greedy, initially want to use the violent simulation, write very complex and finally re very awkward.
Later looked at the puzzle, it became a number, very concise a little abstract. Code

Big guy, right.

#include <iostream>
#include <cstdio>
using namespace std; 
int a[10010];
int n;
int sum=0,x=0,to1=0;
int main ()
{
    cin>>n;
    for (int i=1;i<=n;i++)
    {
       cin>>a[i];
       Sum+=a[i];
    }
    x=sum/n;
    for (int i=1;i<=n;i++) a[i]=a[i]-x;
    for (int i=1;i<=n;i++)
    {
        if (a[i]==0) continue;
        A[I+1]=A[I]+A[I+1];
        to1++;
    }
    cout<<to1;
    return 0;
}

Own souvenir ...

#include <iostream> #include <cstdio> #include <cstdlib> using namespace std;
    int big (int cot[],int n) {int cur=-1,ret=0;
            for (int i=0;i<n;i++) {if (cot[i]>cur) {cur = cot[i];
        ret = i;
}} return ret;
    } bool EQL (int cot[],int n) {for (int i=1;i<n;i++) if (Cot[i]!=cot[i+1]) return false;
return true;
    } int ACU (int cot[], int beg, int en) {int sum = 0;

    for (int i=beg;i<=en;i++) sum+=cot[i];
return sum;
    } int main () {int n,sum=0;
    cin>>n;

    int cot[102];
        for (int i=1;i<=n;i++) {cin>>cot[i];
    Sum+=cot[i];

    } int avg = sum/n;
    int Times,bar,lloss,rloss;
    times=0;
        while (!EQL (cot,n)) {bar = big (cot,n+1);
        cout<< "bar:" <<bar<< "<<cot[bar" <<endl;
            if (bar!=1) {Lloss = avg* (bar-1)-acu (cot,1,bar-1);
if (lloss>0) {cot[bar-1]+=lloss;                Cot[bar]-=lloss;
            times++;
            }} if (bar!=n) {Rloss = avg* (N-bar)-acu (cot,bar+1,n);
                if (rloss>0) {times++;
                Cot[bar]-= Rloss;
            Cot[bar+1]+=rloss;
    }}} cout<<times;
return 0;
 }

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.