-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalmost_sorted.cpp
More file actions
executable file
·46 lines (38 loc) · 935 Bytes
/
almost_sorted.cpp
File metadata and controls
executable file
·46 lines (38 loc) · 935 Bytes
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i,t=0,a[100002],d=0,u=0,di,ui=-1;
scanf("%d",&n);
a[0]=0;
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
a[n+1]=1000001;
for(i=1;i<=n;i++){
if(a[i]>a[i-1]&&a[i]>a[i+1]){
u++;
if(ui==-1)
ui=i;
}
else if(a[i]<a[i-1]&&a[i]<a[i+1]){
d++;
di=i;
}
}
if(d==1&&u==1&&di-ui>2){
if((di<n&&a[ui]>a[di+1])||(ui>1&&a[di]<a[ui-1]))
printf("no");
else
printf("yes\nreverse %d %d",ui,di);
}
else if(d==2&&u==2||(d==1&&u==1&&di-ui<3))
if(di-ui<3&&(di<n&&a[ui]>a[di+1])||(ui>1&&a[di]<a[ui-1]))
printf("no");
else
printf("yes\nswap %d %d",ui,di);
else
printf("no");
return 0;
}