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;
}