Description
给出 n n 个人在旅行中的花费,现在这个人要通过最少的钱的交换使得 n n 个任的花费尽可能平均,花费由美元和美分组成,交换过程中不会出现比美分更小面额的钱币
Input
多组用例,每组用例首先输入一整数表示人数,之后 n n 个两位小数表示这个人的花费,整数部分表示美元数,小数部分表示美分数,人数不超过 103 10 3 ,花费不超过 106 10 6
Output
输出使得 n n 个人的花费尽可能平均所需要的最少的钱币交换面额
Sample Input
10.00
20.00
30.00
4
15.00
15.01
3.00
3.01
0
Sample Output
11.99
Solution
把每个人的花费乘 100 100 转化成整数并排序得到一个不减序列 a1,...,an a 1 , . . . , a n ,为使这 n n 个数尽可能平均,假设和为,那么要有 sum%n s u m % n 个数变成 x+1=⌊sumn⌋+1 x + 1 = ⌊ s u m n ⌋ + 1 ,剩下 n−sum%n n − s u m % n 个数变成 x x ,贪心的先把大的数变成,如果比 x+1 x + 1 大的数不超过 sum%n s u m % n 那么只需要把比 x+1 x + 1 大的数变成 x+1 x + 1 即可,否则要把超过的数字变成 x x <script type="math/tex" id="MathJax-Element-24">x</script>,累加答案即可
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=1005;
int n,a[maxn];
int main()
{
while(~scanf("%d",&n),n)
{
int sum=0;
for(int i=1;i<=n;i++)
{
double temp;
scanf("%lf",&temp);
a[i]=(int)(temp*100+0.5);
sum+=a[i];
}
sort(a+1,a+n+1);
int res=sum%n,ans=0;
for(int i=n;i>=1;i--)
if(a[i]*n>sum)
{
if(res)
{
if((a[i]-1)*n>sum)ans+=a[i]-(sum/n+1),res--;
else break;
}
else ans+=a[i]-sum/n;
}
else break;
printf("$%d.%02d\n",ans/100,ans%100);
}
return 0;
}