题解:P12880 [蓝桥杯 2025 国 C] 数字配对
题意简述
给定
思路
拿到题第一时间可能会想到枚举每个数,只要前面有对应的数就直接匹配。比如:
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (cnt[a[i]-1])
cnt[a[i]-1]--, ans++;
else
cnt[a[i]]++;
}
但这样是错误的。当输入为 4 2 3 4 3 时,
对每个数 vector 记录它在原序列中出现的每个位置。对于相邻的两个 vector,使用双指针统计答案,每个位置都尽量和最靠后的位置匹配。这样这道题就做完了。
感性证明:假设
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+5;
vector<int> cnt[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, mx = 0, ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
cnt[x].push_back(i);
mx = max(mx, x);
}
n = mx;
for (int i = 2; i <= n; i++) {
int j = cnt[i-1].size()-1, k = cnt[i].size()-1;
while (j >= 0 && k >= 0) {
while (j > 0 && cnt[i-1][j] > cnt[i][k])
j--;
if (cnt[i-1][j] > cnt[i][k]) break; // 此时cnt[i-1]枚举完了
while (k > 0 && j > 0 && cnt[i-1][j] < cnt[i][k])
ans++, k--, j--, cnt[i].pop_back(); // 删除对应位置,避免下次循环重复计算
if (cnt[i-1][j] < cnt[i][k]) { // 此时cnt[i]枚举完了
ans++, cnt[i].pop_back();
break;
}
}
}
cout << ans << endl;
return 0;
}