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

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. 

Output

Ouput the least money that you need to become the “lucky man”, if it is impossible, output “Impossible” (no quote). 

Sample Input

3
3 5 2
2 1
4 10 2
2 3
4 15 2
3 5

Sample Output

Impossible
4
6

Hint

无

求出可能的钱数区间,在区间内用二分法找到一个值使它大于k人的钱数,并大于剩下n-k-1人的最大钱数。
 #include<cstdio>
#include<algorithm>
using namespace std;
int n,m,k,sum,ans,a[+],ri,le,mid,max0;
bool f(int x)
{
if(x <= max0)
return ;
if(x > (m-sum-x-(n-k-)))
return ;
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
sum=;
max0=;
scanf("%d %d %d",&n,&m,&k);
for(int i = ; i < k ; i++)
{
scanf("%d",&a[i]);
max0=max(max0,a[i]);
sum+=a[i];
}
if((m-sum-(n-k-))<= max0)
{
printf("Impossible\n");
continue;
}
le=max0;
ri=m-sum-(n-k-);
while(le <= ri)
{
mid=( le + ri)/;
if(f(mid))
{
ans=mid;
ri=mid-;
}
else
{
le=mid+;
}
}
printf("%d\n",ans);
}
}
 

NBUT 1651 - Red packet (求运气王的钱数)(二分法)的更多相关文章

  1. NOJ 1651 Red packet(二分)

    [1651] Red packet 时间限制: 1000 ms 内存限制: 65535 K 问题描述 New Year is coming! Our big boss Wine93 will dist ...

  2. 新年抢红包效果(New Year Red Packet)

    新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...

  3. 1. while循环(当循环) 2. do{}while()循环 3. switch cose(多选一) 例子:当选循环下求百鸡百钱 用 switch cose人机剪刀石头布

    1. while循环: 当选循环下求百鸡百钱:如下: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...

  4. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  5. 【优先队列-求第Ki大的数】Black Box

    Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8637   Accepted: 3542 Descrip ...

  6. 《数据结构与算法分析:C语言描述》读书笔记------练习1.1 求第K大的数

    求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保 ...

  7. hdu1568&&hdu3117 求斐波那契数前四位和后四位

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=2 ...

  8. 省赛i题/求1~n内全部数对(x,y),满足最大公约数是质数的对数

    求1~n内全部数对(x,y),gcd(x,y)=质数,的对数. 思路:用f[n]求出,含n的对数.最后用sum[n]求和. 对于gcd(x,y)=a(设x<=y,a是质数),则必有gcd(x/a ...

  9. [LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)

    传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the m ...

随机推荐

  1. 多线程 线程间通信 wait,notify

    1. 方法wait锁释放,notify()锁不释放

  2. bzoj 4860 [BeiJing2017]树的难题

    题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4860 题解 点分治 设当前重心为v 假设已经把所有边按照出发点第一关键字, 颜色第二关键字排 ...

  3. 【模板】c++动态数组vector

    相信大家都知道$C$++里有一个流弊的$STL$模板库.. 今天我们就要谈一谈这里面的一个容器:动态数组$vector$. $vector$实际上类似于$a[]$这个东西,也就是说它重载了$[]$运算 ...

  4. 数学 2015百度之星初赛2 HDOJ 5255 魔法因子

    题目传送门 /* 数学:不会写,学习一下这种解题方式:) 思路:设符合条件的数的最高位是h,最低位是l,中间不变的部分为mid,由题意可得到下面的公式(这里对X乘上1e6用a表示,b表示1e6) (h ...

  5. #219. 【NOI2016】优秀的拆分

      如果一个字符串可以被拆分为 AABBAABB 的形式,其中 AA 和 BB 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 aabaabaa,如果令 A=aabA=aab ...

  6. 【学习笔记】深入理解js原型和闭包系列学习笔记——精华

    深入理解js原型和闭包笔记: 1.“一切皆是对象”,对象是属性的集合. 丨 函数也是对象,但是使用typeof时为什么函数返回function而 丨  不是object呢,js为何要对函数做这样的区分 ...

  7. RxJava的map方法与flatMap方法

    简单讲,map和flatMap都是来完成Observable构造的数据到Observer接收数据的一个转换,这么说有点绕

  8. IOS访问webserver接口

    接口调用参数只能是字符串格式,返回格式支持3种(字符串,数组,DataSet) 需要引用第三方库,包含(DataSet,PlatServinceDataParser,WebserviceCommon, ...

  9. Android里的 ART、JIT、AOT、Dalvik之间有什么关系?

    ART.JIT.AOT.Dalvik之间有什么关系? JIT与Dalvik JIT是"Just In Time Compiler"的缩写,就是"即时编译技术", ...

  10. The lion king 经典句型摘录

    What am I going to do with him? Everything the light touches is our kingdom. But I thought a king ca ...