-
[1651] Red packet
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
-
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 starts with an integer T (T <= 50) denoting the number of test case.
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.
- 输出
-
Ouput the least money that you need to become the “lucky man”, if it is impossible, output “Impossible” (no quote).
- 样例输入
-
3 3 5 2 2 1 4 10 2 2 3 4 15 2 3 5
- 样例输出
-
Impossible 4 6
- 提示
-
无
- 来源
-
本站或者转载
今天一天做二分都有一种要死的感觉_(:з」∠)_
总之各种不会分。
这题的大意一开始还没看懂,WA了几次后百度翻译了一下才发现不对……
题意:新年到了发红包,老板叫你帮他发红包,现在给你了m元钱,要把红包发给n个人(包括你自己),有k人对红包的金额有最低要求。问你发红包的时候要最少给自己发多少才能让自己成为幸运的人(也叫运气王(弱弱的说一句这个翻译比我直译好啊))(幸运的人成为条件是得到的红包金额最大)。注意所有钱都要发完。
题解:不用二分直接滚会TLE的OTZ……这题二分具体看代码里的注释吧,难点在二分上。
#include<stdio.h>
int n,m,k,sum,tmp;
int OK(int x)
{
if(m-x-sum-tmp+1<x) //抽出来一个人(钱数仅次于运气王),除了这个人以外所有没有要求的人发一块,有要求的发最低要求。
return 1; //减出来的金额(m-x-sum-tmp)就是抽出来的那个人能得到的钱,如果你的钱(x)比那个多,r移动。
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,a;
int max=0;
scanf("%d%d%d",&n,&m,&k);
tmp=n-k-1;sum=0;
for(i=0;i<k;i++)
{
scanf("%d",&a);
if(a>max)
max=a;
sum+=a;
}
if(m-sum-tmp<max+1)
printf("Impossible\n");
else
{
int l=max+1,r=m-sum-tmp;
int mid;
while(l<=r)
{
mid=(l+r)/2;
if(OK(mid)) r=mid-1;
else l=mid+1;
}
printf("%d\n",l);
}
}
return 0;
}