Red packet 二分算法
2016-07-27 21:17
330 查看
Red packet
Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u
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
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.
Output
Ouput the least money that you need to become the “lucky man”, if it is impossible, output “Impossible” (no quote).
Sample Input
3
3 5 2
2 1
4 10 2
2 3
4 15 2
3 5
Sample Output
Impossible
4
6
这个题就是抢红包呗,二分直接算,但是注意在二分之前的各种判断。
代码如下:
Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u
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
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.
Output
Ouput the least money that you need to become the “lucky man”, if it is impossible, output “Impossible” (no quote).
Sample Input
3
3 5 2
2 1
4 10 2
2 3
4 15 2
3 5
Sample Output
Impossible
4
6
这个题就是抢红包呗,二分直接算,但是注意在二分之前的各种判断。
代码如下:
#include<cstdio> #include<algorithm> using namespace std; int n,m,k,sum,ans,a[100000+11],ri,le,mid,max0; bool f(int x) { if(x <= max0) return 0; if(x > (m-sum-x-(n-k-2))) return 1; return 0; } int main() { int t; scanf("%d",&t); while(t--) { sum=0; max0=0; scanf("%d %d %d",&n,&m,&k); for(int i = 0 ; i < k ; i++) { scanf("%d",&a[i]); max0=max(max0,a[i]); sum+=a[i]; } if((m-sum-(n-k-1))<= max0) { printf("Impossible\n"); continue; } le=max0; ri=m-sum-(n-k-1); while(le <= ri) { mid=( le + ri)/2; if(f(mid)) { ans=mid; ri=mid-1; } else { le=mid+1; } } printf("%d\n",ans); } }
相关文章推荐
- masm汇编伪指令
- css中具有继承性的属性
- 从头学android_minSdkVersion、targetSdkVersion、maxSdkVersion、target API level的区别
- ASP.NET--Razor-model-compare属性用法
- 第三方---百度地图的使用
- JQuery学习之旅(5)
- 南阳理工acm 1236 挑战密室
- cookie小知识点儿总结
- LINUX入门
- 版本更新
- 【字符串4】-第一个只出现一次的字符
- **YII** 系列一理论知识
- (二)Spring框架——框架结构
- android 版本更新
- Apache与Nginx的优缺点比较
- 利用iis虚拟目录实现文件服务器功能(分布式存储)
- Storm nimbus 单节点宕机的处理
- (一)Spring框架——概览
- CodeForces 372A Counting Kangaroos is Fun
- Expanding Rods(二分+几何)