ACdream 1224(贪心处理)

来源:互联网 发布:ubuntu 硬盘大小 编辑:程序博客网 时间:2024/05/29 08:33

题目链接:http://acdream.info/problem?pid=1224

Robbers

Special JudgeTime Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)
SubmitStatisticNext Problem

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.

Sample Input

3 10 41 1 2

Sample Output

2 3 5

Source

Andrew Stankevich Contest 2
思路:先求出 ki=[xi*m/y] (即取下整),然后sum+=ki;  那么还会有一部分钱没有分配完left=m-sum;

接下来贪心排序即可

#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;}

0 0