题意:有n个抢劫者抢劫了m块金子,然后第i个人平分xi/y块金子,但是会有除不尽的情况而金子不可再分,那么每个人都有一个不满意度fabs(xi / y - ki/m),ki是每个人实际分得的金子数量,要保证所有人的不满意度和最小,问ki应如何分配。
题解:如果可以除尽,ki就是xi * m / y,否则要把不满意度和再多分一块金子的不满意度的差值存起来,按从大到小排序,把多出来的金子数量num给前num个人多分一块。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1005;
struct P {
int id;
double re;
}p[N];
int n, m, y, x[N], res[N], flag[N], cnt;
bool cmp(P a, P b) {
return a.re > b.re;
}
double Count(int i) {
return fabs(x[i] * 1.0 / y - res[i] * 1.0 / m) - fabs(x[i] * 1.0 / y - (res[i] + 1) * 1.0 / m);
}
void solve(int num) {
cnt = 0;
for (int i = 0; i < n; i++) {
if (!flag[i]) {
p[cnt].re = Count(i);
p[cnt++].id = i;
}
}
sort(p, p + cnt, cmp);
for (int i = 0, j = num; j > 0; i++, j--)
res[p[i].id]++;
}
int main() {
while (scanf("%d%d%d", &n, &m, &y) == 3) {
for (int i = 0; i < n; i++)
flag[i] = 0;
int num = m;
for (int i = 0; i < n; i++) {
scanf("%d", &x[i]);
if ((m * x[i]) % y == 0) {
res[i] = (m * x[i]) / y;
flag[i] = 1;
}
else {
res[i] = (m * x[i]) / y;
}
num -= res[i];
}
solve(num);
printf("%d", res[0]);
for (int i = 1; i < n; i++)
printf(" %d", res[i]);
printf("\n");
}
return 0;
}