历届试题 对局匹配

这几天心态有点崩,不会的忒多了,翻翻学长们的博客,同时期的他们学的算法比我多,题量也是!而且我很多学的算法和数据结构都只是做过模板题。。。md下学期逃课吧,我爱ACM!万幸今天下午查机组考得还算可以hhhh,算是一件值得高兴的事呢。

这个题呢,不好想,看了题解可以总结一下方法是贪心和转换对象。题目是说最多多少人同时在线&&都匹配不上,那么我们可以先求所有人都在线能最多匹配多少组假设为p组,那么每一组都有选择地去掉一个人(但是不用考虑具体去掉谁),那么剩余的人肯定都无法再配对了。

如果对于给定的k为0,那么我们让每一个积分为x的组的人数都只剩一个(假如积分为x的组的人数>=2)就好。

参考的这位大佬的代码,附上他的题解吧https://blog.csdn.net/u013852115/article/details/83384802

代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
int dis[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
const int maxn = 100000 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int cnt[maxn];
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, k, x;
    int maxv = 0;
    int ans = 0;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        cnt[x]++;
        maxv = max(maxv, x);
    }
    for (int i = 0; i + k <= maxv; i++)
    {
        while (k && cnt[i] && cnt[i + k])
        {
            ans++;
            cnt[i]--;
            cnt[i + k]--;
        }
        if (!k && cnt[i] >= 2)
        {
            ans += cnt[i] - 1;
        }
    }
    cout << n - ans;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值