现在的位置: 首页 > 综合 > 正文

nyoj 344 Moonfang’s Birthday(贪心,注意思想)

2018年04月26日 ⁄ 综合 ⁄ 共 2285字 ⁄ 字号 评论关闭
难度:3
描述

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.

输入
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.
输出
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.
样例输入
        3
        20 4
        10 10 4 4
        7 3
        1 1 4
        34 5
        9 8 9 9 4
样例输出:
       6 6 4 4
       IMPOSSIBLE
       8 7 8 7 4
 
 
 
 
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#define min(x,y) ((x)>(y)?(y):(x))
struct node
{
    int p,x;
};
node nodes[10010];
int cmp(node a,node b)
//If there is still ambiguity, those who come first in the list pay more.”
//关键点:由于总体是按money升序排列的,而money相同时先到者要多付  
//按照这个思想多付者因该排到后头,故应该按num的降序排列  
{
    if(a.p!=b.p)
    return a.p<b.p;
    return a.x>b.x;
}
int cmp1(node a,node b)
{
    return a.x<b.x;
}
int main()
{
    int cases,sum,avg,price,num,i;
    scanf("%d",&cases);
    while(cases--)
    {
        sum=0;
        scanf("%d%d",&price,&num);
        for(i=0;i<num;i++)
        {
            scanf("%d",&nodes[i].p);
            sum+=nodes[i].p;
            nodes[i].x=i;
        }
        if(sum<price)
        {printf("IMPOSSIBLE\n");continue;}
        sum=price;
        sort(nodes,nodes+num,cmp);
        for(i=0;i<num;i++)
        {
            int t=min(nodes[i].p,sum/(num-i));
            nodes[i].p=t;
            sum-=t;
        }
        sort(nodes,nodes+num,cmp1);
        for(i=0;i<num-1;i++)
        {
            printf("%d ",nodes[i].p);
        }
        printf("%d\n",nodes[num-1].p);
    }
    return 0;
}
                


抱歉!评论已关闭.