• [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(二分)的更多相关文章

  1. NBUT 1651 - Red packet (求运气王的钱数)(二分法)

    Description New Year is coming! Our big boss Wine93 will distribute some “Red Package”, just like Al ...

  2. 新年抢红包效果(New Year Red Packet)

    新年抢红包效果(New Year Red Packet) 晓娜的文章(微信公众号:migufe) 2016即将过去,我们将迎来新的一年2017,这里小编提前祝大家新年快乐!万事如意!那我们新年最开心的 ...

  3. NOJ——1649Find Sum(二分查找)

    [1649] Find Sum 时间限制: 1000 ms 内存限制: 65535 K 问题描述 This problem is really boring. You are given a numb ...

  4. TCC细读 - 1 例子流程

    http://www.iocoder.cn/categories/TCC-Transaction/ https://github.com/changmingxie/tcc-transaction 细读 ...

  5. python中的*arg和**kwargs

    arg对应多出来的位置参数,把它们解析成tuple;kwargs把关键字参数解析成dict. def example(pram): print(pram) def example2(param, *a ...

  6. php导出csv并保存在服务器,返回csv的文件路径

    <?php namespace app\common\controller; use think\Controller; use think\Db; class Csv extends Cont ...

  7. daily plan

    想了想自己留的坑有点多了,写个计划提醒自己 一些没做出来的题 csp-s模拟测试54 z csp-s模拟测试b层加餐 string 平衡树+并查集 因为某杰的**安排,没时间改了csp-s模拟测试47 ...

  8. noj二分查找

    二分查找: 要么左边,要么右边,哈哈哈哈 描述 给定一个单调递增的整数序列,问某个整数是否在序列中.   输入 第一行为一个整数n,表示序列中整数的个数:第二行为n(n不超过10000)个整数:第三行 ...

  9. NOJ——1274The battle of Red Cliff(并查集按秩合并)

    [1274] The battle of Red Cliff 时间限制: 1000 ms 内存限制: 65535 K 问题描述 Zero loves palying Sanguo game very ...

随机推荐

  1. (转)JAVA AJAX教程第一章-初始AJAX

    既然是认识AJAX,理论和实践相结合,这样让自己学的更快,理解更深入,我分一下几点: 1.  认识传统的同步交互方式和AJAX解决方案 2.  AJAX使用到的技术 3.  实例体验AJAX 一.同步 ...

  2. HDU 5754 Life Winner Bo (各种博弈) 2016杭电多校联合第三场

    题目:传送门 题意:一个国际象棋棋盘,有四种棋子,从(n,m)走到(1,1),走到(1,1)的人赢,先手赢输出B,后手赢输出G,平局输出D. 题解:先把从(n,m)走到(1,1)看做是从(1,1)走到 ...

  3. java 执行command

    StringBuffer buf = new StringBuffer(1000); try { Process pos = Runtime.getRuntime().exec("sh &q ...

  4. 第一章 用记事本搭建C#程序

    1.新建记事本:using System;class Text{ Console.WriteLine("你好如鹏网"); Console.WriteLine("www.r ...

  5. LINUX下安装TeamViewer

    https://www.teamviewer.com/zhcn/download/linux/ [root@ok teamviewer]# yum install -y teamviewer_11.0 ...

  6. 微信api退款操作

    状况:证书加载进去,本地调试退款成功,然而发不到iis上却是不成功. 分析:定然是iis配置问题. 问题一:证书加载不进去,出现“内部错误” 解决:在iis中找到对应的应用连接池,右键高级设置,找到“ ...

  7. CSS选择器以及优先级与匹配原理

    最常用的五类CSS选择器 准确而简洁的运用CSS选择器会达到非常好的效果.我们不必通篇给每一个元素定义类(class)或ID,通过合适的组织,可以用最简单的方法实现同样的效果.在实际工作中,最常用的选 ...

  8. Android源码编译

    参考百度文章: http://jingyan.baidu.com/article/a501d80ce61ad0ec630f5e0b.html

  9. Codeforces Round #Pi (Div. 2) A. Lineland Mail 水

    A. Lineland MailTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/proble ...

  10. BZOJ2783: [JLOI2012]树 dfs+set

    2783: [JLOI2012]树 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 588  Solved: 347 Description 数列 提交文 ...