NUBT 1651 Red packet(红包问题,二分,宁波工程学院在线评测)
2016-07-27 11:03
393 查看
[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
思路:
发红包有成为幸运儿的说法即你要比每个人得到的红包都要多,你就是幸运儿。
输入数据:
测试数据个数
人数,红包总额,已经分发的人数
已经分发的人数的红包
思路:
代码注释部分。
如果描述不清晰:
博主参考自:Classmata_Wang Yinggang
代码:
#include<cstdio> #include<cstdlib> #define MYDD 1103 int MAX(int x,int y) { return x>y? x:y; } int main() { int t; scanf("%d",&t); while(t--) { int n,m,k;//人数,红包总额,已经分发的人数 int max_get,distri_money,money; scanf("%d%d%d",&n,&m,&k); max_get=0;//当前已分发红包的最高金额 distri_money=0;//已经分发红包的总额 for(int j=0; j<k; j++) { scanf("%d",&money); distri_money+=money; max_get=MAX(max_get,money);//已经得到红包的最大金额 } int remaining_money=m-distri_money;//剩下的金额 if(remaining_money<=max_get) { puts("Impossible");//剩下的红包不大于已经得到红包的最大金额 continue; } int remaining_people=n-k-1;//没发到红包的人数(不包含幸运儿) if(remaining_people==0) {//此时没有剩下没发的红包 printf("%d\n",remaining_money); } int left=max_get+1;//二分求解满足条件 int right=remaining_money; while(left<=right) { int middle=(left+right)/2; int k=remaining_money-middle-remaining_people+1; if(k<middle) right=middle-1; else left=middle+1; } printf("%d\n",left); } return 0; }
相关文章推荐
- 网络中的基本概念
- SQL基础语句
- hd 1969 Pie (二分)
- Parcelable接口
- 1088. Rational Arithmetic (20)
- 浅解:iOS应用程序中的性能优化
- FloatingActionButton 完全解析[Design Support Library(2)]
- Linux 学习笔记之 CentOS7 安装
- 如何理解闭包(摘录网友的文章)
- 事务路线图
- Linux中命令历史history及man帮助手册的相关理解和使用
- Html-布局基础记录
- 如何下载高德电子地图
- OpenCV Core模块
- 杭电ACM1049的解题报告
- 数据结构实验之链表七:单链表中重复元素的删除 sdutoj2122
- 查看系统端口占用
- [置顶] 关于微信公众号开发的Token验证失败该如何解决?
- C#向线程中传递多个参数的解决方法(两种)
- 1089 A+B for Input-Output Practice (I)