Ac_dream 1224 robbers (Greedy)

Source: Internet
Author: User

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)

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.