rt,大致思路就是用一个集合记录所有数的出现位置,然后遍历每个数 x,对于位置 i,在 x+1 的集合中找到 >i 的最小位置 j,如果找到就配对并删除。
能否给个 hack qwq
#include <iostream>
#include <set>
using namespace std;
const int N = 1e6 + 5;
int a[N];
set<int> pos[N];
int main()
{
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
int mx = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
mx = max(mx, a[i]);
pos[a[i]].insert(i);
}
int ans = 0;
for (int x = 1; x < mx; x++)
{
auto i = pos[x].begin();
while (i != pos[x].end())
{
auto j = pos[x + 1].upper_bound(*i);
if (j != pos[x + 1].end())
{
ans++;
i = pos[x].erase(i);
pos[x + 1].erase(j);
}
else i++;
}
}
cout << ans;
return 0;
}