The money that the n robbers snatched was K1, K2, K3,.... The total money was M,
However, in the case of money sharding, it is allocated according to the ratio of x1/y, x2/y, X3/y! In this case
Some robbers may feel unfair. The unfair level is | XI/Y-ki/M | (floating point operation), and a sequence Ki is output to make
The minimum injustice .....
Idea: greedy! First, calculate the money (NI) that each person receives according to the [XI/y] (rounded up) ratio, and then get
The remaining amount of money is found to be less than the relative proportion allocated to everyone, that is, the maximum value of XI/y * m-ni! Find this person
Then, increase the amount of money he gets to 1!
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define N 1006 5 using namespace std; 6 7 int num[N]; 8 int x[N]; 9 bool flag[N];10 11 int main(){12 int n, m, y;13 while(scanf("%d%d%d", &n, &m, &y) != EOF){14 memset(flag, 0, sizeof(flag));15 int left = 0;16 for(int i=1; i<=n; ++i){17 scanf("%d", &x[i]);18 num[i] = x[i]*m/y;19 left += num[i];20 if( x[i]%y == 0 ) flag[i] = true;21 }22 left = m - left;23 while(left > 0){24 double tmp = 0.0;25 int p = 0;26 for(int i=1; i<=n; ++i)27 if(tmp < x[i]*1.0/y * m - num[i]){28 tmp = x[i]*1.0/y * m - num[i];29 p = i;30 } 31 --left;32 ++num[p];33 }34 printf("%d", num[1]);35 for(int i=2; i<=n; ++i)36 printf(" %d", num[i]);37 printf("\n");38 }39 return 0;40 } View code
Ac_dream 1224 robbers (Greedy)