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\n",ans);
    return 0;
}
/*
3 4
1 2 3
*/

猜你喜欢

转载自www.cnblogs.com/DukeLv/p/9500686.html