这几天心态有点崩,不会的忒多了,翻翻学长们的博客,同时期的他们学的算法比我多,题量也是!而且我很多学的算法和数据结构都只是做过模板题。。。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;
}