-
Notifications
You must be signed in to change notification settings - Fork 0
/
Almost Sorted.cpp
67 lines (58 loc) · 1.4 KB
/
Almost Sorted.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
int a[1000001];
vector< int >v;
map< int , int >mm;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
for(int u=1;u<=n;u++) {cin>>a[u];
mm[a[u]]=u;
v.push_back(a[u]);}
bool fl=true;
int st ,en;
sort(v.begin(), v.end());
int kk=0;
for(int u=1;u<=n;u++){
if(a[u]!=v[u-1]) kk++;
if(kk>2){
fl=false;break;
}
}
if(!fl){
bool ff=true;
vector< int >ch ;
// logic for second case!!!!!!!!!!!!!!!!
for(int u=1;u<=n;u++){
if(a[u]!=v[u-1]){
ch.push_back(a[u]);
}
}
//st=ch[0];
for(int u=1;u<ch.size();u++){
if(ch[u]< ch[u-1]) continue;
else{
puts("no");ff=false;break;
}
}
if(ff){
cout<<"yes"<<endl;
cout<<"reverse ";
cout<<mm[ch[0]]<<" "<<mm[ch[ch.size()-1]]<<endl;
}
}
else{
puts("yes");
if(kk>0){
cout<<"swap ";
vector< int >dd;
for(int u=1;u<=n;u++){
if(a[u]!=v[u-1]){
dd.push_back(u);
}
}
cout<< dd[0]<<" "<<dd[1]<<endl;}
}
return 0;
}