​https://www.lydsy.com/JudgeOnline/problem.php?id=4391​

想到了分别从前从后扫两边 但是觉得有的数会被重复使用 就放弃了。。

看了别人博客才知 如果一个数a被用了两次 那一定有一个数b没被用 不管a>b还是a<b 那b都能为a分担一个数

 

#include <bits/stdc++.h>
using namespace std;
const int maxn=5e4+10;

set <int> st;
int ary[maxn],pre[maxn],suf[maxn],book[2*maxn];
int n;

int main()
{
set <int> ::iterator it;
int i,ans;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&ary[i]);
book[ary[i]]=1;
}

for(i=1;i<=2*n;i++){
if(!book[i]) st.insert(i);
}
for(i=1;i<=n;i++){
pre[i]=pre[i-1];
if(st.empty()) continue;
it=st.upper_bound(ary[i]);
if(it!=st.end()){
pre[i]++;
st.erase(it);
}
}

st.clear();
for(i=1;i<=2*n;i++){
if(!book[i]) st.insert(i);
}
for(i=n;i>=1;i--){
suf[i]=suf[i+1];
if(st.empty()) continue;
it=st.lower_bound(ary[i]);
if(it!=st.begin()){
it--;
suf[i]++;
st.erase(it);
}
}
ans=0;
for(i=1;i<=n+1;i++){
ans=max(ans,pre[i-1]+suf[i]);
}
printf("%d\n",ans);
return 0;
}