You are viewing a single comment's thread. Return to all comments →
C++ solution
/*Prodip sarker(nothing36)*/ #include<bits/stdc++.h> using namespace std; #define bn begin() #define en end() #define nl "\n" #define mx 2000100 #define mod 1000000007 #define pb push_back #define LB lower_bound #define PI (2.0*acos(0.0)) #define al(a) a.begin(),a.end() #define fast ios_base::sync_with_stdio(false),cin.tie(NULL) typedef long long int ll; typedef vector<int> vi; typedef vector<ll> vll; int main() { fast; ll tc,n,i,j,k,a,b; cin>>n; vll v; for(i=0;i<n;i++) { cin>>a; v.pb(a); } if(is_sorted(al(v))) cout<<"yes\n"; else{ i=0,j=n-1; ll d1=0,d2=n-1; for(i=1;i<n;i++) { if(v[i-1]>v[i]) { d1=i-1; break; } } for(i=n-1;i>=1;i--) { if(v[i]<v[i-1]) { d2=i; break; } } swap(v[d1],v[d2]); if(is_sorted(al(v))) cout<<"yes\nswap "<<d1+1<<" "<<d2+1<<endl; else{ swap(v[d1],v[d2]); reverse(v.bn+d1,v.bn+d2+1); if(is_sorted(al(v))) cout<<"yes\nreverse "<<d1+1<<" "<<d2+1<<endl; else cout<<"no\n"; } } return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Almost Sorted
You are viewing a single comment's thread. Return to all comments →
C++ solution