NBUT 1651 Red packet (思路)

  • 问题描述
  • 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 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.
  • 输出
  • Ouput the least money that you need to become the “lucky man”, if it is impossible, output “Impossible” (no quote).
  • 样例输入
  • 3
    3 5 2
    2 1
    4 10 2
    2 3
    4 15 2
    3 5
    
  • 样例输出
  • Impossible
    4
    6

思路:

设运气王为x,运气第二为y。

如果x最多可得钱数 小于 当前最大值输出 Impossible

然后分两种情况讨论

还剩2人及以上人数未抢红包,欲使x最小,除了x,y其余人皆为1,x占剩余总资产一半以上,并且 x 大于 前面出现过的最大值

只剩一人至少要抢 maxn+1即可,虽然会抢完但要求最小值。


网上还有二分写法这里不再讨论。

代码:

#include<stdio.h>

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n, m ,k, a, maxn = 0;
        scanf("%d%d%d",&n,&m,&k);

        for(int i = 0; i < k; i++)
        {
            scanf("%d",&a);
            if(a > maxn) maxn = a;
            m -= a;
        }
        n -= k;
        if(m - n + 1 <= maxn )
        {
            printf("Impossible\n");
            continue;
        }

        if(n > 1)
        {
            m = m - n + 2;
            m = m/2 + 1;
            printf("%d\n",m > maxn ? m : maxn + 1);
        }
        else printf("%d\n",m);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值