贪心: hdu 3866 Moonfang's Birthday

Description
It’s Moonfang’s birthday,and his friends decided to buy him a copy of XianJianQiXiaZhuan V.

Since some of friends have more money available than others, nobody has to pay more than he can afford. Every contribution will be a multiple of 1 cent,i.e.,nobody can pay fractions of a cent.

Everybody writes down the maxumum amount he is able to contribute. Taking into account these maximum amounts from everybody, your task is to share the cost of the present as fairly as possible. That means, you minimize the largest distance of the contributions to 1/n-th of the total cost.

In case of a tie, minimize the second largest distance, and so on. Since the smallest unit of contribution is 1 cent, there might be more than one possible division of the cost. In that case, persons with a higher maximum amount pay more. If there is still ambiguity, those who come first in the list pay more.

Since you bought the present, it is your task to figure out how much everybody has to pay.

Input
On the first line a positive integer: the number of test cases, at most 200. After that per test case:
One line with two integers p and n: the price of the present in cents (1 ≤ p ≤ 1 000 000) and the number of people (2 ≤ n ≤ 10000) who contribute to the present (including you).

One line with n integers ai (1 ≤ ai ≤ 1 000 000), where ai is the maximum amount, in cents, that the i-th person on the list is able to contribute.

Output
Per test case:
One line with n integers: the amounts each person has to contribute according to the scheme. If the total cost cannot be divided according to the above rules, the line must contain “IMPOSSIBLE” instead.

Sample Input
3
20 4
10 10 4 4
7 3
1 1 4
34 5
9 8 9 9 4

Sample Output
6 6 4 4
IMPOSSIBLE
8 7 8 7 4

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
    int pos,val,ret;
}p[10008];
int cmp1(node a,node b)
{
    if(a.val!=b.val)
        return a.val<b.val;
    return a.pos>b.pos;
}
int cmp2(node a,node b)
{
    return a.pos<b.pos;
}
int t,n,price;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&price,&n);
        int sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&p[i].val);
            sum+=p[i].val;
            p[i].pos=i;
        }
        if(sum<price)
        {
            printf("IMPOSSIBLE\n");
            continue;
        }
        sum=price;
        sort(p,p+n,cmp1);
        for(int i=0;i<n;i++)
        {
            int tt=min(p[i].val,sum/(n-i));
             p[i].ret=tt;
             sum-=tt;
        }
        sort(p,p+n,cmp2);
        for(int i=0;i<n-1;i++)
            printf("%d ",p[i].ret);
        printf("%d\n",p[n-1].ret);
    }
    return 0;
}

Reflect:
1,按照val从小到大排序,pos从大到小排序;
2,每个人要出自己能出的最大值;
3,从钱数最小者开始,出完自己需要出的最大值,然后钱数由后面的人分配;

出处:http://blog.csdn.net/kongming_acm/article/details/6641217

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值