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
题意:把m元分给n个人,已经分给了k个人,轮到我,问至少分给我多少,就一定是n个人中最幸运的人,即得到的钱最多。(每人至少一元)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41#include<cstdio> #include<cmath> #include<algorithm> using namespace std; int a[100010]; int main() { int t,l,r,n,m,k; scanf("%d",&t); while(t--) { int sum=0; scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=k;i++) { scanf("%d",&a[i]); sum=sum+a[i]; } sort(a+1,a+k+1); m-=sum;//分给k人之后剩余的钱 int h=n-k-1;//剩下的人除去一个人分得最大红包,其余每人一元 int s=m-h;//在后面n-k人中最多可以得到的钱 if(a[k]>=s)//a[k] 为前k人中得到最多的钱 printf("Impossible\n"); else { l=a[k]+1,r=s; while(l<=r) { int mid=(l+r)/2; if(mid<=s-mid+1)//把s-mid元给n-k-1中的一个人,如果得到的mid元小于等于那个人,说明偏小了 l=mid+1; else r=mid-1; } printf("%d\n",l); } } return 0; }