NOJ 1651 Red packet(二分)
[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
以前红包专场的题目,当时实在是水平低下,天真的以为可以拿点钱,结果被爆0血虐(Orz各位学长学姐老司机们)。
于是现在又回来噜一下这题,发现挺简单的……就是二分的时候剩下的除我之外其他人的钱忘记减1,WA两次才发现……
主要思路:记录剩余的钱为res,出现过的最高红包额度记为maxm,特判掉impossib的情况之后,二分的左端点肯定是maxm+1,右端点显然要奢侈一点取最好情况即剩下的人均只分到1元,那么我就有res-(n-k-1)元,然后唯一的竞争对手就是另外一个人,他有res-(n-k-2)-mid元,条件是不能让他大于等于我,然后二分就可以了……,以前还傻乎乎地看不懂二分的条件还跑论坛里问别人这是什么意思…………Naive。
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef __int64 LL;
const double PI=acos(-1.0);
int main(void)
{
int n,m,l,r,mid,ans,temp;
int i,k;
int tcase;
scanf("%d",&tcase);
while (tcase--)
{
scanf("%d%d%d",&n,&m,&k);
int maxm=-1;
int sum=0;
int res=0;
for (i=0; i<k; ++i)
{
scanf("%d",&temp);
sum+=temp;
if(temp>maxm)
maxm=temp;
}
res=m-sum;
if(res<n-k)//不够分
puts("Impossible");
else if(res-(n-k-1)<=maxm)//无法超越最大值
puts("Impossible");
else
{
int oth=res-(n-k-2);
l=maxm+1;
r=res-(n-k-1);
while (l<=r)
{
mid=(l+r)>>1;
if(oth-mid<mid)
{
r=mid-1;
ans=mid;
}
else
l=mid+1;
}
printf("%d\n",ans);
}
}
return 0;
}
NOJ 1651 Red packet(二分)的更多相关文章
- NBUT 1651 - Red packet (求运气王的钱数)(二分法)
Description New Year is coming! Our big boss Wine93 will distribute some “Red Package”, just like Al ...
- 新年抢红包效果(New Year Red Packet)
新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...
- NOJ——1649Find Sum(二分查找)
[1649] Find Sum 时间限制: 1000 ms 内存限制: 65535 K 问题描述 This problem is really boring. You are given a numb ...
- TCC细读 - 1 例子流程
http://www.iocoder.cn/categories/TCC-Transaction/ https://github.com/changmingxie/tcc-transaction 细读 ...
- python中的*arg和**kwargs
arg对应多出来的位置参数,把它们解析成tuple;kwargs把关键字参数解析成dict. def example(pram): print(pram) def example2(param, *a ...
- php导出csv并保存在服务器,返回csv的文件路径
<?php namespace app\common\controller; use think\Controller; use think\Db; class Csv extends Cont ...
- daily plan
想了想自己留的坑有点多了,写个计划提醒自己 一些没做出来的题 csp-s模拟测试54 z csp-s模拟测试b层加餐 string 平衡树+并查集 因为某杰的**安排,没时间改了csp-s模拟测试47 ...
- noj二分查找
二分查找: 要么左边,要么右边,哈哈哈哈 描述 给定一个单调递增的整数序列,问某个整数是否在序列中. 输入 第一行为一个整数n,表示序列中整数的个数:第二行为n(n不超过10000)个整数:第三行 ...
- NOJ——1274The battle of Red Cliff(并查集按秩合并)
[1274] The battle of Red Cliff 时间限制: 1000 ms 内存限制: 65535 K 问题描述 Zero loves palying Sanguo game very ...
随机推荐
- 引水入城(codevs 1066)
题目描述 Description 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好构成一个N行M列的矩形,如上图所示,其中每个格子都代表一座城 市,每座 ...
- android 兼容性测试 CTS 测试过程(实践测试验证通过)
source: http://blog.csdn.net/jianguo_liao19840726/article/details/7222814 写这个博客的时候是为了记忆,建议大家还是看官方的说明 ...
- 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))
解决办法:在项目属性里设置“生成”=>“目标平台”为x86而不是默认的ANY CPU.
- jstack命令(Java Stack Trace)
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...
- wp8
请问如何在应用内跳转到 显示 来自XX的更多应用? Windows.System.Launcher.LaunchUriAsync(new Uri("zune:search?pu ...
- CDT
Eclipse CDT 是 Eclipse 插件,它将把 Eclipse 转换为功能强大的 C/C++ IDE.
- C/C++函数使用
1 memset 将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASCII值, 块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作, 其返回值为指向s的指针. 函数介绍 ...
- [转]Android的Handler总结
一.Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线程更新UI. 解释: 当应用程序启动时,Android首先会开启一个主线程 (也就是UI线 ...
- Android 利用Gson生成或解析json
目前手机端和服务端数据交流格式一般是json,而谷歌提供了Gson来解析json.下载Gson:https://code.google.com/p/google-gson/ 下载的放在lib并导入,若 ...
- Android 电池电量进度条,上下滚动图片的进度条(battery)
最近,制作一个app,需要模拟一个电池电量的进度条,根据电量多少来设置百分比,进度条不断上下滚动,就像平时手机充电一样的电池电量进度条.我就自定义view实现了电量进度条.修改图片就可以达到自己想要的 ...