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.
- 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.
- 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
//
N个人一起凑钱给一个人买了一本《仙剑奇侠传V》,这本书的价格是P,现在告诉你每个人最多能付的钱price[i].val,现在要求在每个人付的钱尽量接近平均值的情况下,求出每个人应该付多少钱
按照每个人最多能支付的金额排序
For (i=0....N-1)
Contribute[i]=min(max[i].Price/(N-i)
Price-=Contribute[i]
注意Tie 时的处理,按照列表中的位置
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=11000;
struct Node
{
int val;
int ret;
int idx;
};
Node a[maxn];
bool cmp(Node h,Node k)
{
if(h.val!=k.val) return h.val<k.val;
return h.idx>k.idx;
}
bool comp(Node h,Node k)
{
return h.idx<k.idx;
}
int main()
{
int ci;scanf("%d",&ci);
while(ci--)
{
int p,n;scanf("%d%d",&p,&n);
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i].val);
sum+=a[i].val;
a[i].idx=i;
}
if(sum<p)
{
printf("IMPOSSIBLE\n");
continue;
}
sum=p;
sort(a,a+n,cmp);
for(int i=0;i<n;i++)
{
int cont=min(a[i].val,sum/(n-i));
a[i].ret=cont;
sum-=cont;
}
sort(a,a+n,comp);
printf("%d",a[0].ret);
for(int i=1;i<n;i++) printf(" %d",a[i].ret);printf("\n");
}
return 0;
}