题解:P17136 [KOI 2026 #1] 数列排序
题意
一种特殊的排序方式:对于
分析
考虑一对逆序对
那我们就把问题转化为了值域上的区间覆盖问题。
但是逆序对最大可以达到
从小到大枚举阈值
实现
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
int fir[N],lst[N];
int main()
{
int n;
cin>>n;
for(int i = 1,x; i <= n; i ++)
{
cin>>x;
if(!fir[x]) fir[x] = i;
lst[x] = i;
}
int res = 0,p = 0;
for(int i = 1; i < n; i ++)
{
p = max(p,lst[i]);
if(fir[i + 1] && p > fir[i + 1]) res ++,p = 0;
}
cout<<res<<'\n';
return 0;
}