Robbers(贪心)

C - Robbers

Time Limit:  2000/1000MS (Java/Others)     Memory Limit:  128000/64000KB (Java/Others)      Special Judge
Submit  Status

Problem Description

      N robbers have robbed the bank. As the result of their crime they chanced to get M golden coins. Before the robbery the band has made an agreement that after the robbery i-th gangster would get Xi/Y of all money gained. However, it turned out that M may be not divisible by Y.

The problem which now should be solved by robbers is what to do with the coins. They would like to share them fairly. Let us suppose that i-th robber would get Ki coins. In this case unfairness of this fact is |Xi/Y - Ki/M|. The total unfairness is the sum of all particular unfairnesses. Your task as the leader of the gang is to spread money among robbers in such a way that the total unfairness is minimized.

Input

      The first line of the input file contains numbers N, M and Y (1 ≤ N ≤ 1000, 1 ≤ M, Y ≤ 10000). N integer numbers follow - X i (1 ≤ X i ≤ 10000, sum of all X i is Y).

Output

      Output N integer numbers - K i (sum of all K i must be M), so that the total unfairness is minimal.

Sample Input

3 10 4
1 1 2

Sample Output

2 3 5




题目大意:N个强盗抢银行,抢了M个金币,他们按事先的比例分金币,但可能会出现半个金币之类的情况~那么这样就不公平啦~问怎样分配使得不公平度之和最小。


分析:这里的贪心策略是,先向下取整着分给每个人。然后,多余的金币,每次分给不公平度最大的人。


代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int maxn = 1111;

int x[maxn], k[maxn];
int flag[maxn];
int n, m, y;

int main() {
    while(~scanf("%d%d%d", &n, &m, &y)) {
        for(int i = 0; i < n; i++)
            scanf("%d", &x[i]);
        int sum = 0;
        memset(flag, 0, sizeof(flag));
        for(int i = 0; i < n; i++) {
            k[i] = m*x[i]/y;
            if(m*x[i]%y == 0) flag[i] = 1;
            sum += k[i];
        }
        int t = m-sum;     //多余的金币
        while(t--) {
            double tmp = 0.0;
            int j;
            for(int i = 0; i < n; i++) {
                if(flag[i]) continue;
                double p = x[i]*1.0/y-k[i]*1.0/m;
                if(p > tmp) tmp = p, j = i;
            }
            k[j]++;
        }
        for(int i = 0; i < n; i++)
            printf("%d%c", k[i], i == n-1 ? '\n' : ' ');
    }
    return 0;
}


你可能感兴趣的:(Robbers(贪心))