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;
    }
  • 相关阅读:
    编译duilib遇到问题:Error C2371 "IDispatchEx重定义;不同的基类型"
    初尝DirectUI
    ms-onlinetest-question02
    ms-onlinetest-question3
    都是申请空间后不赋初值惹的祸..
    CString接受返回的char*字符串后成为乱码
    CL.exe @C:Users upAppDataLocalTemp mpc8fc399365e34f739eff6191a0c9acde.rsp”。存储控制块地址无效
    Visual Studio Ultimate 2012 静态激活密钥
    如何写入和获取软件的版本信息(VS环境下)
    mt.exe : general error c101008a: Failed to save the updated manifest to the file "DebugResource.dll.embed.manifest". Bpo
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766258.html
Copyright © 2011-2022 走看看