100pts,subtask1--TLE了,求调!!!
查看原帖
100pts,subtask1--TLE了,求调!!!
1062212
xuzb楼主2024/6/28 21:10

源代码,复杂度有一点点超,求改进,能不能把那个从n到2的for循环改一改,因为有check数组,所以大概两重循环,时间复杂度O(N^2)级别:

#include <bits/stdc++.h>
using namespace std;
int n;
int qzh[100005];
bool check(int x){
	for(int i=1;i<=n-x+1;i++){
		if(qzh[i+x-1]-qzh[i-1]==0){
			return true;
		}
	}
	return false;
}
int ans;
int main(){
	cin>>n;
	int opt;
	for(int i=1;i<=n;i++){
		cin>>opt;
		if(!opt){
			qzh[i]=qzh[i-1]-1;
		}
		else{
			qzh[i]=qzh[i-1]+1;
		}
	}
	for(int i=n;i>=2;i--){
		if(check(i)){
			cout<<i;
			return 0;
		}
	}
	cout<<0;
	return 0;
}
2024/6/28 21:10
加载中...