• B1816 扑克牌 二分答案 + 贪心


    这个题我一开始想到了二分答案,但是去写了另一个算法,用优先队列直接模拟,最后GG了。。。因为我没考虑每个套牌只能有一个joker。。。尴尬。

    后来二分答案,然后暴力验证就行了。

    题干:

    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

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<ctime>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    #define duke(i,a,n) for(int i = a;i <= n;i++)
    #define lv(i,a,n) for(int i = a;i >= n;i--)
    #define clean(a) memset(a,0,sizeof(a))
    const int INF = 1 << 30;
    typedef long long ll;
    typedef double db;
    template <class T>
    void read(T &x)
    {
        char c;
        bool op = 0;
        while(c = getchar(), c < '0' || c > '9')
            if(c == '-') op = 1;
        x = c - '0';
        while(c = getchar(), c >= '0' && c <= '9')
            x = x * 10 + c - '0';
        if(op) x = -x;
    }
    template <class T>
    void write(T x)
    {
        if(x < 0) putchar('-'), x = -x;
        if(x >= 10) write(x / 10);
        putchar('0' + x % 10);
    }
    //priority_queue <int,vector<int>,greater<int> > qu;
    int x,y,m,n;
    ll f[200];
    ll l = 0,r = 0;
    ll g[200];
    ll ans = 0;
    bool work(int x)
    {
        ll t = min(x,m);
        duke(i,1,n)
        {
            if(f[i] < x)
            {
                t -= (x - f[i]);
                if(t < 0)
                {
                    return 0;
                }
            }
        }
        return 1;
    }
    int mid;
    int main()
    {
        read(n);read(m);
        duke(i,1,n)
        {
            read(f[i]);
        }
    //    sort(f + 1,f + n + 1);
        l = 1;r = 1000000001;
        while(l != r)
        {
            mid = (l + r) >> 1;
            if(work(mid))
            {
                ans = mid;
                l = mid + 1;
            }
            else
            {
                r = mid;
            }
        }
        printf("%lld
    ",ans);
        return 0;
    }
    /*
    3 4
    1 2 3
    */
  • 相关阅读:
    斐波那契数列的两种解法:1.递归2.字典
    逆向工程,调试Hello World !程序(更新中)
    python计算和媳妇在一起天数的小程序,最后绘制成花.
    Python 3.10 第二个 alpha 版来了!一些可以关注的新特性
    pandas:使用函数批量处理数据(map、apply、applymap)
    机器学习 --- k-means
    自定义注解!绝对是程序员装逼的利器!!
    某网站出现自己电脑的搜索记录,这是怎么回事呢?
    《Linux操作系统》——教学进度表20140218——张同光
    Java Web整合开发(27) -- Spring的Core模块
  • 原文地址:https://www.cnblogs.com/DukeLv/p/9500686.html
Copyright © 2020-2023  润新知