求时间复杂度
查看原帖
求时间复杂度
836448
stylus楼主2025/1/3 18:58

老师说这个复杂度是 O(n2)O(n^2) ,过不了,是题目水了

#include<bits/stdc++.h>
#define int long long
using namespace std;
void read(int &x){
	x=0;bool f=0;char ch=getchar();
	while(ch>'9'||ch<'0'){
		if(ch=='-')f=1;
		ch=getchar();
	}do{x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}while(ch>='0'&&ch<='9');
	x=f?-x:x;
}
int n,t,a[100001],s;
signed main(){
	read(n),read(t),a[0]=114514;
	for(int i=1;i<=n;i++)read(a[i]);
	sort(a+1,a+1+n,greater<int>());
	for(int i=1;i<=n;i++){
		if(i-a[i]<1)s=i;
		else if(i-a[i]==1&&t){
			t--;
			int j=i-1;
			while(i-a[j--]==1){
				if(t)t--;
				else{t=-114514;break;}
			}if(t==-114514)break;
			s=i;
		}else break;
	}cout<<s;
	return 0;
}
2025/1/3 18:58
加载中...