40pts 求调
查看原帖
40pts 求调
1028403
OIerWu_829楼主2025/6/22 13:07

rt,大致思路就是用一个集合记录所有数的出现位置,然后遍历每个数 xx,对于位置 ii,在 x+1x+1 的集合中找到 >i>i 的最小位置 jj,如果找到就配对并删除。

能否给个 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;
}
2025/6/22 13:07
加载中...