1. 程式人生 > 實用技巧 >Educational Codeforces Round 91 (Rated for Div. 2) A. Three Indices (模擬)

Educational Codeforces Round 91 (Rated for Div. 2) A. Three Indices (模擬)

  • 題意:有一長度為\(n\)的序列,問是否能找到\(a_{i}<a_{j},a_{j}>a_{k},(i<j<k)\),如果滿足,輸出其位置.

  • 題解:直接暴力兩頭找即可,最壞複雜度:\(O(n^2)\).

  • 程式碼:

    int t;
    int n;
    int a[N];
     
    int main() {
        ios::sync_with_stdio(false);cin.tie(0);
      	cin>>t;
      	 while(t--){
      	 	cin>>n;
      	 	 for(int i=1;i<=n;++i) cin>>a[i];
     
      	 	 bool ok=0;
      	 	 for(int j=1;j<=n;++j){
      	 	 	int pos1=-1;
      	 	 	int pos2=-1;
      	 	 	for(int i=1;i<j;++i){
      	 	 		if(a[i]<a[j]){
      	 	 			pos1=i;
      	 	 			break;
      	 	 		}
      	 	 	}
      	 	 	if(pos1==-1) continue;
      	 	 	for(int k=j+1;k<=n;++k){
      	 	 		if(a[k]<a[j]){
      	 	 			pos2=k;
      	 	 			break;
      	 	 		}
      	 	 	}
      	 	 	if(pos1!=pos2 && pos1!=-1 && pos2!=-1){
      	 	 		ok=1;
      	 	 		cout<<"YES"<<endl;
      	 	 		cout<<pos1<<" "<<j<<" "<<pos2<<endl;
      	 	 		break;  
      	 	 	}
      	 	 }
      	 	 if(!ok) cout<<"NO"<<endl;
      	 }
    
        return 0;
    }