SGU 207. Robbers
207. Robbers
time limit per test: 0.5 sec.
memory limit per test: 65536 KB
input: standard
output: standard
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.
Sample test(s)
Input
3 10 4
1 1 2
Output
2 3 5
题意
n个强盗去抢劫银行得到m个金币,抢劫前他们先确定好了分配方案,每个人按比例\(X_i/Y\)分配,\(X_1+X_2+..X_n = Y\),m可能不能被Y整除,所以实际分配可能会有不公平,假设第i个强盗分配到\(K_i\)个金币,定义不公平度为\(|X_i/Y-K_i/m|\),给出了\(n,m,Y,X_i\),求出\(K_i\),使得不公平度的总和最小。
sb题,没啥好说的。。
#include <bits/stdc++.h>
#define rep(_i, _j) for(int _i = 1; _i <= _j; ++_i)
const int inf = 0x3f3f3f3f;
typedef long long LL;
typedef double DB;
using namespace std;
const int maxn = 1000 + 10;
int n, m, y;
int Akane[maxn], Ranma[maxn], id[maxn];
DB Ukyo[maxn];
bool cmp(int a, int b) {
return Ukyo[a] < Ukyo[b];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("207.in", "r", stdin); freopen("207.out", "w", stdout);
#endif
scanf("%d%d%d", &n, &m, &y);
int cnt = 0;
for(int i = 1; i <= n; id[i] = i, ++i) {
scanf("%d", &Akane[i]);
Ranma[i] = Akane[i] * m / y;
cnt += Ranma[i];
}
if(m % y != 0) {
for(int i = 1; i <= n; ++i) {
Ukyo[i] = fabs((DB)Akane[i] / y - (DB)(Ranma[i] + 1) / m);
}
sort(id + 1, id + n + 1, cmp);
int res = m - cnt;
for(int i = 1; i <= res; ++i) {
++Ranma[id[i]];
}
}
for(int i = 1; i < n; ++i) {
printf("%d ", Ranma[i]);
}
printf("%d\n", Ranma[n]);
return 0;
}