Indi(E)

 avatar
unknown
c_cpp
12 days ago
1.5 kB
2
Indexable
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define endl "\n"

// const ll N = 1e7;

void solve(){
    int n; cin>>n;
    vector<int> v(n), t(n);
    for(int i = 0; i<n; i++){
        cin>>v[i]; t[i] = v[i];
    }
    sort(t.begin(), t.end());
    int p = -1, q = -1;
    for(int i = 0; i<n; i++){
        if(v[i]!=t[i]){
            p = i; break;
        }
    }
    if(p==-1){ 
        cout<<"0\n"; return;
    }
    for(int i = n-1; i>=0; i--){
        if(v[i]!=t[i]){
            q = i; break;
        }
    }
    cout<<p<<" "<<q<<" "<<v[p]<<endl;
    int ltor = 1;
    if(v[p]>v[q]) ltor = 0;
    cout<<ltor<<endl;
    if(!ltor){
        int j = q-1;
        int op = 0;
        while(q>=0 && j>=0 && v[q]<v[j]){
            while(j-1>=0 && v[j]==v[j-1]){
                j--;
            }
            swap(v[q], v[j]);
            q = j; j--;
            op++;
        }
        cout<<op<<endl;
    }
    else{
        int j = p+1;
        int op = 0;
        while(p<n && j<n && v[p]>v[j]){
            while(j+1<n && v[j]==v[j+1]){
                j++;
            }
            swap(v[p], v[j]);
            p = j; j++;
            op++;
        }
        cout<<op<<endl;
    }

}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);

    // freopen("outofplace.in", "r", stdin);
    // freopen("outofplace.out", "w", stdout);
    ll tcase = 1;
    // cin>>tcase;
    for(ll tc = 1; tc<=tcase; tc++){
        solve();
    }
}
Leave a Comment