I - Red packet

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


  1. 题意:给你m元钱,分给n个人,已经有k个人分过了钱,将这k个人所分的的钱数数据给你,问你在剩下的人中的人分的的钱数最多的最小值存不存在,若存在是多少。(每人最少分一元)
  2. 思路:找出k个人中的最大值,判断剩余钱数满足剩余人的最大值,两者相比,判断存不存在最大值;如果存在,在用二分法在区间内找出最小化的情况。
  3. 失误:刚开始用cin输入,加过cin.sycn_with_stdio(false)后依然超时,而且只有一个输入的循环,输入数据10^6,改成c之后不超时了,还是用c的输入输出吧。
  4. 代码如下:
#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;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值