Main topic:
N Robbers Rob the bank to get a total of M gold coins, before the robbery they determined the distribution plan, everyone proportional xi/y distribution, x1+x2+ ... +xn=y,m is not necessarily divisible by Y, assuming that the first robber is assigned ki coins, then injustice is | xi/y-ki/m |, now input n,m,y,x, find Ki, make the least injustice.
Problem Solving Ideas:
First of all, our proportion [xi/y*m] to each allocation, and then from the bottom to take the whole, so the allocation of a certain amount of less than or equal to m, assuming that the rest of G, then we put each person's gold coins plus 1 after the injustice from small to large sort, and then the first G small people of gold number plus 1, then the
AC Code:
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <iostream > #include <algorithm> #define MAX (a) > (b)? ( A):(B) #define MIN (a) > (b) ( b):(a)) using namespace Std;int n,m,y;int x[1010]={0};d ouble t[1010]={0};int a[1010]={0};int rank[1010]={0};bool CMP ( int A1,int A2) {Double K1=fabs (t[a1]-(double) (a[a1]+1) *1.0/m);d ouble k2=fabs (t[a2]-(double) (a[a2]+1) *1.0/m); if (k1 <K2) return True;return false;} int main () {int g;scanf ("%d%d%d", &n,&m,&y), g=m;for (int i=1;i<=n;i++) {scanf ("%d", &x[i]); t[i]= ( Double) x[i]*1.0/y;a[i]=m*x[i]/y;rank[i]=i;g-=a[i];} Sort (rank+1,rank+n+1,cmp), for (int i=1;i<=g;i++) a[rank[i]]++;for (int i=1;i<=n;i++) printf ("%d", A[i]); return 0 ;}
sgu-207 Robbers