首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【NBUT】1651 - Red packet(二分)

【NBUT】1651 - Red packet(二分)

作者头像
FishWang
发布2025-08-27 09:11:33
发布2025-08-27 09:11:33
11000
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

  • [1651] Red packet
  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • 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
  • 提示
  • 来源
  • 本站或者转载

刚开始二分的条件控制有错,应该是剩下的人无论怎么分这个人都是运气王。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
	int n,m,k;
	int maxx,sum;		//前面人钱最多的,总和 
	int rec;		//剩余分钱的人数 
	int money;		//剩余钱数 
	int u;
	scanf ("%d",&u);
	while (u--)
	{
		scanf ("%d %d %d",&n,&m,&k);
		maxx = sum = 0;
		for (int i = 1 ; i <= k ; i++)
		{
			int t;
			scanf ("%d",&t);
			maxx = max (maxx , t);
			sum += t;
		}
		rec = n-k-1;
		money = m - sum;
		if (money-rec <= maxx)
		{
			printf ("Impossible\n");
			continue;
		}
		if (rec == 0)		//注意这个 
		{
			printf ("%d\n",money);
			continue;
		}
		int l,r,mid;
		l = maxx + 1;
		r = money - rec;
		while (r >= l)
		{
			mid = (l + r) >> 1;
			if (money - mid - rec + 1 < mid)
				r = mid - 1;
			else
				l = mid + 1;
		}
		printf ("%d\n",l);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档