题目链接:http://acdream.info/problem?pid=1224
Robbers
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
Output
Sample Input
3 10 4 1 1 2
Sample Output
2 3 5
Source
接下来贪心排序即可
#include <iostream>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
const int N=1100;
using namespace std;
struct node
{
int id;
double inf;
int p;
}a[N];
bool cmp(node a,node b)
{
return a.inf<b.inf;
}
int b[N],q[N];
int cnt[N];
int main()
{
int n,m,y;
while(scanf("%d%d%d",&n,&m,&y)!=EOF)
{
int sum=0,left=0;
for(int i=1;i<=n;i++)
{
int temp;
scanf("%d",&temp);
q[i]=temp;
b[i]=temp*m/y;
sum+=b[i];
}
left=m-sum;
for(int i=1;i<=n;i++)
{
a[i].id=i;
a[i].inf=abs((b[i]+1.0)/m-(q[i]*1.0)/y)-abs((b[i]*1.0)/m-(q[i]*1.0)/y);
a[i].p=b[i];
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
{
cnt[a[i].id]=a[i].p;
}
for(int i=1;i<=left;i++)
{
cnt[a[i].id]++;
}
for(int i=1;i<=n;i++)
if(i==1)
printf("%d",cnt[i]);
else
printf(" %d",cnt[i]);
printf("\n");
}
return 0;
}