ACdream 1224 Robbers (贪心)

Robbers

Special Judge
Time Limit: 2000/1000MS (Java/Others)
Memory Limit: 128000/64000KB (Java/Others)

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 4
1 1 2

Sample Output

2 3 5

Source

Andrew Stankevich Contest 2

Manager

mathlover

解析:先对每个人都按比例向下取整得到k[i],之后再将剩下的金币每次都分给当前“不公平值”最大的那个人,这样就会让他的“不公平值”减小,直到所有的金币分配完为止。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int k[1002], x[1002];
bool flag[1002];

int main(){
    #ifdef sxk
        freopen("in.txt", "r", stdin);
    //    freopen("out.txt", "w", stdout);
    #endif // sxk

    int n, m, y, sum;
    while(scanf("%d%d%d", &n, &m, &y)!=EOF){
        sum = 0;
        for(int i=0; i<n; i++){
            scanf("%d", &x[i]);
            k[i] = m * x[i] / y;
            if(m * x[i] % y == 0) flag[i] = true;
            sum += k[i];
        }

        int tx = m - sum;
        while(tx--){
            double t = 0;
            int j = 0;
            for(int i=0; i<n; i++){
                if(flag[i]) continue;
                double foo = (double)x[i]/y - (double)k[i]/m;
                if(t < foo){
                    t = foo;
                    j = i;
                }
            }
            k[j] ++;
        }
        for(int i=0; i<n-1; i++) printf("%d ", k[i]);
        printf("%d\n", k[n-1]);
    }
    return 0;
}
时间: 2024-11-05 12:26:03

ACdream 1224 Robbers (贪心)的相关文章

AC_Dream 1224 Robbers(贪心)

题意:n个抢劫犯分别抢到的金钱是k1, k2, k3,...,一共得到的金钱是m, 但是在分钱的时候是按照x1/y, x2/y, x3/y,....的比例进行分配的!这样的话 一些抢劫犯就会觉得不公平,不公平度为|xi/y - ki/m|(浮点运算), 输出一个序列ki,使得 总的不公平度最小..... 思路:很明显的贪心! 首先按照 [xi/y](取整)的比例将每一个人得到的钱求出来(ni),然后会得到 剩下的钱数, 最后在所有人中找到谁分配的相对比例少了,也就是xi/y*m - ni的最大值

acdream 1224(贪心)

题意:有n个抢劫者抢劫了m块金子,然后第i个人平分xi/y块金子,但是会有除不尽的情况而金子不可再分,那么每个人都有一个不满意度fabs(xi / y - ki/m),ki是每个人实际分得的金子数量,要保证所有人的不满意度和最小,问ki应如何分配. 题解:如果可以除尽,ki就是xi * m / y,否则要把不满意度和再多分一块金子的不满意度的差值存起来,按从大到小排序,把多出来的金子数量num给前num个人多分一块. #include <cstdio> #include <cstring

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, i

acdream 1735 输油管道 贪心

输油管道 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acdream.info/problem?pid=1735 Description 平面上有n个油井,现在要建立一条主干线,用来把所有的油井产出的原油都输送出去,主干线是平行于x轴的一条直线,每个油井通过一条支线把原油输送到主干线上,现在给定n个油井在平面上的坐标,那么应该把主干线建在什么地方才能让所有的支干线的总长度最小呢? A的某一段完全重合,或者能够经过上下左右平移与折线A的

UVa 1616 Caravan Robbers (二分+贪心)

题意:给定 n 个区间,然后把它们变成等长的,并且不相交,问最大长度. 析:首先是二分最大长度,这个地方精度卡的太厉害了,都卡到1e-9了,平时一般的1e-8就行,二分后判断是不是满足不相交,找出最长的.这个题并不难, 就是精度可能控制不好,再就是把小数化成分数时,可能有点麻烦. 代码如下: #include <iostream> #include <cmath> #include <cstdlib> #include <set> #include <

acdream 1717(贪心)

题意: Problem Description ACdream王国有一条贯穿整个王国的高速公路,一天,你要驾驶着一辆油箱容量为P的车从高速公路的一头驶向另一头,总路程为L千米,每单位体积的汽油可维持行驶W千米,显然路途遥远~总有不够油的情况,所以就要加油~! 再高速公路上总共有N个加油站,但是由于是不同人开的,因此定价也参差不齐. 现在你知道每个加油站的位置,以及每个加油站的单价,问你最少需要多少钱才能到达另一头? Input 多组数据,每组数据首先是四个整数,P(1<=P<=100),L(1

acdream 1716(贪心)

题意: Problem Description 在ACdream王国中,有一条母亲河,这条母亲河为王国人民提供了各种生活用水.在河边共住着n户人家,每户人家的位置为x[i].由于经济发展需要,ACdream决定在这条母亲河上建立一个水力发电站,经过勘测,这个水力发电站只可以建立在区间[a,b]的某一个地方x0.为了减少大家受到来此发电站的影响,希望min{|x[i]-x0| |0<=i<=n-1}最大,即使得距离发电站最近的人家与发电站的距离最大.若有多组答案,输出坐标较小的即可. Input

ACDream 1734 Can you make a water problem?(贪心)

Can you make a water problem? Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Losanto want to make a water problem. But he have no idea….Then he thought a problem: A b

acdream 1212(贪心)

题意:有n个人,从1到n,编号越大职位越低,然后给出了第2到第n个人的上司的编号,每个人可以有一堆小弟但只能有一个上司.年底发奖金,每个人可以从上司那得到1000元或发给某个小弟1000元,问所有人能发的奖金和最大是多少,那些人得到了奖金. 题解:直接倒着遍历一遍,把人和他的上司标记掉算作一组,看有几组. #include <stdio.h> #include <string.h> const int N = 500005; int pa[N], n, flag[N]; int m