NBUT 1651 - Red packet (求运气王的钱数)(二分法)
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
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
Sample Input
3
3 5 2
2 1
4 10 2
2 3
4 15 2
3 5
Sample Output
Impossible
4
6
Hint
无 求出可能的钱数区间,在区间内用二分法找到一个值使它大于k人的钱数,并大于剩下n-k-1人的最大钱数。
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,k,sum,ans,a[+],ri,le,mid,max0;
bool f(int x)
{
if(x <= max0)
return ;
if(x > (m-sum-x-(n-k-)))
return ;
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
sum=;
max0=;
scanf("%d %d %d",&n,&m,&k);
for(int i = ; i < k ; i++)
{
scanf("%d",&a[i]);
max0=max(max0,a[i]);
sum+=a[i];
}
if((m-sum-(n-k-))<= max0)
{
printf("Impossible\n");
continue;
}
le=max0;
ri=m-sum-(n-k-);
while(le <= ri)
{
mid=( le + ri)/;
if(f(mid))
{
ans=mid;
ri=mid-;
}
else
{
le=mid+;
}
}
printf("%d\n",ans);
}
}
NBUT 1651 - Red packet (求运气王的钱数)(二分法)的更多相关文章
- NOJ 1651 Red packet(二分)
[1651] Red packet 时间限制: 1000 ms 内存限制: 65535 K 问题描述 New Year is coming! Our big boss Wine93 will dist ...
- 新年抢红包效果(New Year Red Packet)
新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...
- 1. while循环(当循环) 2. do{}while()循环 3. switch cose(多选一) 例子:当选循环下求百鸡百钱 用 switch cose人机剪刀石头布
1. while循环: 当选循环下求百鸡百钱:如下: 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
- 【优先队列-求第Ki大的数】Black Box
Black Box Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8637 Accepted: 3542 Descrip ...
- 《数据结构与算法分析:C语言描述》读书笔记------练习1.1 求第K大的数
求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保 ...
- hdu1568&&hdu3117 求斐波那契数前四位和后四位
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=2 ...
- 省赛i题/求1~n内全部数对(x,y),满足最大公约数是质数的对数
求1~n内全部数对(x,y),gcd(x,y)=质数,的对数. 思路:用f[n]求出,含n的对数.最后用sum[n]求和. 对于gcd(x,y)=a(设x<=y,a是质数),则必有gcd(x/a ...
- [LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)
传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the m ...
随机推荐
- 关于使用IQKeyBoardManager键盘还是被遮挡的问题解决方案
今天在做一个登录界面的时候发现使用了IQKeyBoardManager键盘还是被遮挡,解决方案如下 解决方案一:在所有视图的最外层添加一个UIView作为容器即可,但在有导航栏的情况下导航栏会跟着向上 ...
- iOS UITextView自适应高度UITextContainerView抖动问题
在打造一个类似于微信朋友圈评论输入框的时候,需要动态调整输入框的高度, 但是,在调整了UITextView的高度之后,继续输入会导致内容(UITextContainerView里的文字)抖动. scr ...
- @ConfigurationProperties和@EnableConfigurationProperties配合使用
https://blog.csdn.net/u010502101/article/details/78758330 @ConfigurationProperties注解主要用来把properties配 ...
- python之类的相关名词解释
变量:在类里面定义的变量,不必实例化即可调用 实例变量:在类里面定义的变量,必须实例化之后才可以调用 比如: 属性方法:调用时看起来像是一个变量,方法没有入参,可以变成一个属性方法 在方法上添加@pr ...
- UWP 剪贴板
一:剪贴板 一般的复制,用户自己光标选中文本,crtl +c复制就可以了.但是有时候也需求有一个复制的按钮,当用户点击复制按钮,就可以把当前的某些内容复制到剪贴板里. 这里就用到了DataPackag ...
- Alt+数字 输入特殊字符
前言: 按住Alt键不放,再按(小键盘的)数字键,然后放开就可以输入特殊字符. 起始 终止 字符类别 0 255 基本与ASCII 码表对应 42657 42680 大写希腊字母 ...
- Android-apk文件反编译
一:工具介绍及下载 1:apktool 作用:资源文件获取,可以提取出图片文件和布局文件进行使用查看 2:dex2jar 作用:将apk反编译成java源码(classes ...
- list的一些功能
x = [1,5,2,3,4] 1.列表反转序: 函数法: x.reverse()该方法没有返回值但会对列表进行反向排序. 注意 不能y=x.reverse(),会得到None 如果要的话要y=rev ...
- jsapi4加载的首个图层的范围被默认作为地图范围,且不能修改的解决
在map加载的第一个图层的图层范围(fullExtent),会被默认设置为map的全图范围,且不能更改,从一般地图控件角度来说,应该有fullExtent属性,作为地图的全图范围,但很遗憾jsapi4 ...
- xutils3批量上传文件
前几天开发安卓要用到文件批量上传,就是上传图片,视频,文件之类的用到Xutil3框架,用 RequestParams params = new RequestParams(url); params.a ...