首页 > 代码库 > hust 1540 Yy’s celebration

hust 1540 Yy’s celebration

题目描述

It‘s Yy‘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 

本题是到排序题,不过需要优化一下,第一次按date排序,在循环过程中,若碰到date==0;k值就直接回到1;
要不本题会超时
#include<map>#include<set>#include<stack>#include<queue>#include<cmath>#include<vector>#include<cstdio>#include<string>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#define  inf 0x0f0f0f0fusing namespace std;const int maxn=10000+10;struct node{          int date,num;}a[maxn];int b[maxn];bool cmp1(node x,node y){          if(x.date==y.date) return x.num<y.num;          else return x.date>y.date;}bool cmp2(node x,node y){          return x.num<y.num;}int main(){          int n,p,t;          scanf("%d",&t);          while(t--)          {                    scanf("%d%d",&p,&n);                    int sum=0;                    int minx=inf;                    for (int i=1;i<=n;i++)                    {                              scanf("%d",&a[i].date);                              a[i].num=i;                              sum+=a[i].date;                              minx=min(minx,a[i].date);                              b[i]=a[i].date;                    }                    if (sum<p)                    {                              printf("IMPOSSIBLE\n");                              continue;                    }                    sort(a+1,a+n+1,cmp1);                    while(minx*n>=p) minx--;                    for (int i=1;i<=n;i++) a[i].date-=minx;                    p-=minx*n;                    int k=1;                    while(p)                    {                              while(a[k].date==0)                              {                                        k=1;                              }                              a[k].date--;                              p--;                              k++;                    }                    sort(a+1,a+n+1,cmp2);                    printf("%d",b[1]-a[1].date);                    for (int i=2;i<=n;i++)                    printf(" %d",b[i]-a[i].date);                    printf("\n");          }          return 0;}

作者 chensunrise