zoukankan      html  css  js  c++  java
  • 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
    ",ans);
    		}
    	}
    	return 0;
    }
  • 相关阅读:
    Linux(实操篇)--- 实用指令-运行级别和找回root密码
    Python使用文件操作实现一个XX信息管理系统的示例
    python解释器安装教程的方法步骤
    python如何使用代码运行助手
    python 识别登录验证码图片功能的实现代码(完整代码)
    python线性插值解析
    python协程 详解
    maxcompute mapjoin
    spark split节点笔记
    安装和配置hadoop
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766258.html
Copyright © 2011-2022 走看看