码迷,mamicode.com
首页 > 其他好文 > 详细

1224 Robbers

时间:2015-05-09 16:40:13      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

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 - Xi (1 ≤ Xi ≤ 10000, sum of all Xi is Y).

Output 

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

代码

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

struct K {
    double V;
    int P;
} k[1005];

int cmp(K a, K b) {
    return a.V > b.V;
}

int main() {
    int N, M, Y, A[1005];
    while (scanf("%d%d%d", &N, &M, &Y) != EOF) {
        double tmp;
        int sum = 0;
        for (int i = 0; i < N; i++) {
            scanf("%lf", &tmp);
            A[i] = tmp * M / Y;
            sum += A[i];

            k[i].V = tmp * M / Y - A[i];
            k[i].P = i;
        }
        sort(k, k + N, cmp);

        for (int i = 0; i < M - sum; i++)
            A[k[i].P]++;

        for (int i = 0; i < N - 1; i++)
            printf("%d ", A[i]);
        printf("%d\n", A[N - 1]);
    }
    return 0;
}

1224 Robbers

标签:

原文地址:http://blog.csdn.net/kl28978113/article/details/45601563

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!