Description
New Year is coming! Our big boss Wine93 will distribute some “Red Package”, just like Alipay and Wechat.
Wine93 has m yuan, he decides to distribute them to n people and everyone can get some money(0 yuan is not allowed and everyone’s money is an integer), Now k people has gotten money, it’s your turn to get “Red Package”, you want to know, at least how much money to give you, then you can must become the “lucky man”. and the m yuan must be used out.
Noting that if someone’s money is strictly much than others’, than he is “lucky man”.
Input
For each test case, three integers n, m, k (1 <= k < n <= 100000, 0< m <= 100000000) will be given.
Next line contains k integers, denoting the money that k people get. You can assume that the k integers’ summation is no more than m.
Output
Sample Input
3 3 5 2 2 1 4 10 2 2 3 4 15 2 3 5
Sample Output
Impossible 4 6
就是发红包,给出总钱数n,总人数m,已经k个已经拿到钱的人和他们拿到的钱,问把钱全分完,每个人最少拿1块钱,所有人拿到的钱都比这个人少,这个人最少拿多少钱
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
int s, e, m, m1, v, n, k, u, ans, last, cnt, t;
scanf("%d", &t);
while(t--){
scanf("%d%d%d", &n, &m1, &k);
ans = u = 0;
for(int i = 0; i < k; i++){
scanf("%d", &v);
u = u > v ? u : v;
ans += v;
}
s = u+1;//必须比前k个人拿钱最多的那个多
e = cnt = m1 - ans - n + k + 1;//剩下的每人先按1块钱算,除掉自己
if(u >= e){
printf("Impossible\n");
continue;
}
else{
while(s <= e){
m = (e+s) >> 1;
if(m <= cnt - m + 1){
s = m+1;
}
else{
e = m-1;
}
}
printf("%d\n", s);
}
}
return 0;
}
本文介绍了一个红包分配问题的解决方法,核心在于确保每个人至少获得1元且一人成为幸运者,即获得的钱比其他人多。通过输入总金额、总人数及部分已分配情况,算法输出成为幸运者的最小金额。
655

被折叠的 条评论
为什么被折叠?



