您的位置:首页 > 其它

I - Red packet

2016-07-27 11:08 921 查看

I - Red packet

Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu

Submit

Status

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



题意:给你m元钱,分给n个人,已经有k个人分过了钱,将这k个人所分的的钱数数据给你,问你在剩下的人中的人分的的钱数最多的最小值存不存在,若存在是多少。(每人最少分一元)

思路:找出k个人中的最大值,判断剩余钱数满足剩余人的最大值,两者相比,判断存不存在最大值;如果存在,在用二分法在区间内找出最小化的情况。

失误:刚开始用cin输入,加过cin.sycn_with_stdio(false)后依然超时,而且只有一个输入的循环,输入数据10^6,改成c之后不超时了,还是用c的输入输出吧。

代码如下:

#include<cstdio>//c++用G++ 用G会编译错误

const int MAXN=1e7+10;
long long l,n,m,left,right,MAX,k,mid,s;
int a[MAXN];
bool judge(long long mid)
{
return mid>MAX&&mid>m-s-mid-(n-k-2);//我分到钱比分过的人的最大值大,比我之后分的人的最大值大
}

long long max(long long a,long long b)//这个在c里面有 在c++里没有
{
return a>b?a:b;
}

int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&n,&m,&k);
s=0;
MAX=0; //
for(i=1;i<=k;++i)
{
scanf("%d",&a[i]);
MAX=max(a[i],MAX);
s+=a[i];
}

if(m-s-n+k+1<=MAX)//公式应写出来 明白其所代表的意义
printf("Impossible\n");
else
{
left=1;right=m-s-n+k+1;//初始二分区间
while(right>=left)
{
mid=(left+right)>>1;
if(judge(mid))//用二分应该清楚函数随自变量的变化是递增,还是递减
{
right=mid-1;
}
else
{
left=mid+1;
}
}
printf("%lld\n",mid);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: