#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[300005];
struct node{
int x,id;
}b[300005];
bool cmp(node a,node b){
return a.x<b.x;
}
bool check(int x){
if(b[n].id==x){
return true;
}
if(x==1){
if(b[n].x<=a[x]+n){
return true;
}else{
return false;
}
}
if(b[n].x-1<=a[x]+n){
return true;
}
return false;
}
signed main(){
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
b[i].x=a[i]+n-i+1;
b[i].id=i;
}
sort(b+1,b+n+1,cmp);
for(int i=1;i<=n;i++){
if(check(i)){
printf("%lld",n-i+1);
return 0;
}
}
cout<<0;
return 0;
}
数据:
10
104
101
110
109
110
110
110
110
104
104
输出:
6
我的代码输出:
9