zoukankan      html  css  js  c++  java
  • BZOJ1816 [Cqoi2010]扑克牌

    Description

    你有n种牌,第i种牌的数目为ci。另外有一种特殊的 牌:joker,它的数目是m。你可以用每种牌各一张来组成一套牌,也可以用一张joker和除了某一种牌以外的其他牌各一张组成1套牌。比如,当n=3 时,一共有4种合法的套牌:{1,2,3}, {J,2,3}, {1,J,3}, {1,2,J}。 给出n, m和ci,你的任务是组成尽量多的套牌。每张牌最多只能用在一副套牌里(可以有牌不使用)。

    Input

    第一行包含两个整数n, m,即牌的种数和joker的个数。第二行包含n个整数ci,即每种牌的张数。

    Output

    输出仅一个整数,即最多组成的套牌数目。

    Sample Input

    3 4
    1 2 3

    Sample Output

    3

    样例解释
    输入数据表明:一共有1个1,2个2,3个3,4个joker。最多可以组成三副套牌:{1,J,3}, {J,2,3}, {J,2,3},joker还剩一个,其余牌全部用完。

    数据范围
    50%的数据满足:2 < = n < = 5, 0 < = m < = 10^ 6, 0< = ci < = 200
    100%的数据满足:2 < = n < = 50, 0 < = m, ci < = 500,000,000。

    正解:二分答案+贪心

    解题报告:

      今天考试的T2,因为二分边界没设得好,炸成50分,错失AK良机。

      我考场上面写的算法太复杂了,事实上并不需要。直接二分一个答案x,表示套牌数目,判定的时候,如果不到x的牌就必须要用J代替,相当于有x-a[i]张J要打出。最后判断一下是否J足够即可。当然J要和x取一个min,因为不能超过套牌数(每局只能代替一次、一张)。

     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #include <set>
    13 using namespace std;
    14 typedef long long LL;
    15 const int MAXN = 101;
    16 const int inf = 2000000000; 
    17 int n,m;
    18 LL l,r,ans;
    19 LL c[MAXN],a[MAXN];
    20 
    21 inline int getint()
    22 {
    23        int w=0,q=0; char c=getchar();
    24        while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
    25        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
    26 }
    27 
    28 inline LL max(LL x,LL y){ if(x<y) return y; return x; }
    29 inline LL min(LL x,LL y){ if(x<y) return x; return y; }
    30 
    31 inline bool check(LL x){
    32     for(int i=1;i<=n;i++) a[i]=c[i];
    33     sort(a+1,a+n+1); LL now=min(m,x),add;    
    34     a[n+1]=a[n];
    35     for(int i=1;i<n;i++) {
    36     add=a[i+1]-a[i]; 
    37     if(add*i<=now)   for(int j=1;j<=i;j++) a[j]+=add,now-=add; 
    38     else {
    39         now/=i; for(int j=1;j<=i;j++) a[j]+=now;
    40         now=0;
    41         break;
    42     }
    43     }
    44     sort(a+1,a+n+1); if(now>0) { now/=n;for(int i=1;i<=n;i++) a[i]+=now; }
    45     if(a[1]>=x) return true;
    46     return false;
    47 }
    48 
    49 inline void work(){
    50     n=getint(); m=getint(); for(int i=1;i<=n;i++) c[i]=getint(),r=max(c[i],r);
    51     LL mid; l=1;  r=1LL<<60;//m=min(m,r);
    52     while(l<=r) {
    53     mid=(l+r)/2;
    54     if(check(mid)) ans=mid,l=mid+1;
    55     else r=mid-1;
    56     }
    57     printf("%lld",ans);
    58 }
    59 
    60 int main()
    61 {
    62   work();
    63   return 0;
    64 }
  • 相关阅读:
    项目开发中的注意点和技巧
    addslashes — 使用反斜线引用字符串
    PHP error_reporting() 错误控制函数功能详解
    零碎收集cocos知识
    LeetCode:二叉树的前序遍历【144】
    SpringBoot学习笔记:SpringBootAdmin
    LeetCode:简化路径【71】
    LeetCode:组合总数II【40】
    LeetCode:全排列II【47】
    LeetCode:全排列【46】
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5864756.html
Copyright © 2011-2022 走看看