-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathalmost_sorted_sequence.cpp
66 lines (55 loc) · 1.74 KB
/
almost_sorted_sequence.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 mod=1e9+7;
#define F(a,b,var) for(int var=a;var<b;var++)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
void almost_sorted(vector<int> &sequence){
//edgecases
if(sequence.size() < 2){
cout<<"Yes\n"; //since less than 2 means it can be reversed as well as swapped
return;
}
if(is_sorted(sequence.begin(), sequence.end())){
cout<<"Yes\n"; //already sorted
return;
}
//vector is partially sorted..
int N = sequence.size();
int i = 0, j = sequence.size() - 1;
while(i != N && sequence[i+1] > sequence[i])
i++; //find start of partial array where it breaks the sorted sequence
while(j >= 0 && sequence[j-1] < sequence[j])
j--; //find end of partial array where it breaks the sorted sequence
//start and end of unsorted - subarray
swap(sequence[i], sequence[j]);
if(is_sorted(sequence.begin(), sequence.end())){
cout<<"yes\n"; //partial is now sorted
cout<<"swap "<<(i+1)<<" "<<(j+1)<<"\n";
return;
}
int k = i + 1, l = j - 1;
while(k < l){
swap(sequence[k], sequence[l]); //keep swapping the entire partial array .. basically reversing the array
k++, l--;
}
if(is_sorted(sequence.begin(), sequence.end())){
cout<<"yes\n"; //partial is now sorted
cout<<"reverse "<<(i+1)<<" "<<(j+1)<<"\n";
return;
}
else{
cout<<"no\n"; // it is not possible to make the array properly sorted..
return;
}
}
int main() {
//code
FAST_INP;
int n;
cin>>n;
vector<int> seq(n);
for(int i = 0; i < n; i++)
cin>>seq[i];
almost_sorted(seq);
return 0;
}