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.
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
给出t组数据,每组数据包括要发红包总人数n,总钱数m,已发人数,问自己至少得多少钱,才能成为幸运者,幸运者的钱数是最多的
#include<stdio.h> #include<algorithm> #include<iostream> using namespace std; int main() { int n,m,t,k,a; scanf("%d",&t); while(t--) {int sum=0,sum1=0; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<k;i++) {scanf("%d",&a); sum=max(sum,a); sum1+=a;} int t1=m-sum1;//发了k人还剩多少钱 int t2=n-k;//还有多少人没发 if(t1<=n-k||t1-t2+1<=sum)//不可能成为幸运者的条件 { printf("Impossible\n"); continue; } int r=t1,l=sum+1,mid,ans=0; while(r>=l)//二分 { mid=(r+l)/2; if(mid>sum&&(t1-mid-t2+2)<mid) { r=mid-1; ans=mid; } else l=mid+1; } printf("%d\n",ans); } }