求问,我这做法为什么不行?
查看原帖
求问,我这做法为什么不行?
808773
forever516楼主2024/10/27 15:52

rt,按理来说是 O(n)O(n) 的做法,为什么只对了一个点?

求大佬讲解。

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n,a[N],maxn=-0x3f,lasti,ans;
int main(){
	cin>>n;
	ans=n;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		maxn=max(maxn,x);
		a[x]++;
	}
	for(int i=1;i<=maxn;i++){
		if(a[i]>0){
			lasti=i;
			break;
		}
	}
	for(int i=lasti;i<=maxn;i++){
		if(a[i]>0){
			if(a[i]>a[lasti]){
				a[i]-=a[lasti];
				lasti=i;
				ans--;
			}else {
				a[lasti]-=a[i];
				ans--;
			}
		}
	}
	cout<<ans;
	return 0;
} 
2024/10/27 15:52
加载中...