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 ...
随机推荐
- JSP | 基础 | 新建Hello world 的三种方式
第一种: 直接写一个 test.jsp 文件到ROOT文件目录下,内容如下,访问 “http://localhost:8080/test.jsp” <%-- Licensed to the Ap ...
- 洛谷p1955[NOI2015]程序自动分析
题目: 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3...代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变量 ...
- Codeforces Round #295 (Div. 2) B. Two Buttons
B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Linux、Windows 下分割、合并rar文件
1.分割rar 1.1 linux下分割压缩rar 安装rar和unrar和序 $sudo aptitude install rar unrar 示例,分割压缩temp文件,每个包为1MB $rar ...
- STM32HAL库学习之前言
HAL库:HAL 的全称是: Hardware Abstraction Layer (硬件抽象层) ,是ST最新推荐的库.包括基本库和扩展库(功能外展):三种编程模型(轮询.中断和 DMA) 灵活的回 ...
- qconbeijing2015
http://2015.qconbeijing.com/schedule 大会日程 2015年4月23日,星期四 地点 2号厅 203AB 201AB 9:15 开场致辞 专题 主题演讲 互联网金融背 ...
- PHP连接数据操作步骤
数据库的操作步骤: 端口号:0到65535 3306:mysql数据库的默认端口号(可修改) mysql_connect(“本机地址”,“用户名”,“密码”,); new_link:如果用同样的参数第 ...
- RabbitMQ九:远程过程调用RPC
定义 RPC(Remote Procedure Call Protocol)——远程过程调用协议:它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议 ...
- TextView、EditText
1.TextView 显示文本信息 常用属性: layout_width/height 控件的宽/高 width/heigth 文本区域的宽/高 text 显示的文本 textSi ...
- [ POI 2017 ] Sabota?
Description 题目链接 Solution 因为一个节点染黑了子树就都被染黑了,所以最后染黑的点集必然是一棵子树. 可以得出的结论是,如果被染黑的节点在节点 \(a\) 的子树中,而 \(a\ ...