SGU 207.Robbers

题意:

  有m(m<=10^4)个金币分给n(n<=1000)个人,第i个人期望得到所有金币的xi/y,现在给分给每个人一些金币ki使得∑|xi/y-ki/m|最小。

Solution:

  首先要算出所有人的期望金币,向下取整.如此一来,所有人期望金币的和s一定小于等于m.这个时候我们需要将剩下的m-s个金币分给m-s个人,对于xi/y-ki/m,将其乘上y*m 得到 xi*m-ki*y,n个人中这个值最大的m-s个人就是我们需要分的人.

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;
struct node {
    int p, val;
    bool operator < (const node &a) const {
        return a.val > val;
    }
} tem;
priority_queue<node> ql;
int ans[1009];
int n, m, y, s;
int main() {
    ios::sync_with_stdio (0);
    cin >> n >> m >> y;
    for (int i = 0, x; i < n; i++) {
        cin >> x;
        ans[i] = m * x / y;
        s += ans[i];
        tem.p = i;
        tem.val = abs (x * m - ans[i] * y);
        ql.push (tem);
    }
    s = m - s;
    while (s--) {
        tem = ql.top(); ql.pop();
        ans[tem.p]++;
    }
    for (int i = 0; i < n; i++)
        cout << ans[i] << ' ';
}
Code
原文地址:https://www.cnblogs.com/keam37/p/4319979.html