[BOJ] 2505 두 번 뒤집기

Time Lapse :44min 38sec

2505.c

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <memory.h>
short buf[10001];
int ans[4]= {1,1,1,1};
int swap(int from, int end,int last){
short _buf[10001], f = from, e = end;
memcpy(_buf,buf,sizeof(_buf));
while(f<e){
_buf[f]^=_buf[e]^=_buf[f]^=_buf[e];
++f, --e;
}
for(int i = from; i <= last; ++i){
if(_buf[i]!=i) break;
if(i==last) return -1;
}
f = from;
int ret;
while(_buf[f]==f) ++f;
ret = f;
e = last;
while(_buf[e]==e) --e;
int _ans = e;
while(f<e){
_buf[f]^=_buf[e]^=_buf[f]^=_buf[e];
++f, --e;
}
for(int i = from; i <= last; ++i)
if(_buf[i]!=i) return 0;
ans[2] = ret,ans[3] = _ans;
return ret;
}
int swap2(int from, int end, int first){
short _buf[10001], f = from, e = end;
memcpy(_buf,buf,sizeof(_buf));
while(f<e){
_buf[f]^=_buf[e]^=_buf[f]^=_buf[e];
++f, --e;
}
for(int i = first; i <= end; ++i){
if(_buf[i]!=i) break;
if(i==end) return -1;
}
e = end;
int ret;
while(_buf[e]==e) --e;
ret = e;
f = first;
while(_buf[f]==f) ++f;
int _ans = f;
while(f<e){
_buf[f]^=_buf[e]^=_buf[f]^=_buf[e];
++f, --e;
}
for(int i = first; i <= end; ++i)
if(_buf[i]!=i) return 0;
ans[2] = _ans, ans[3] = ret;
return ret;
}
int main(void) {
int N;
scanf("%d",&N);
for(int i = 1; i <= N; ++i)
scanf("%d",&buf[i]);
int s = 1, e = N;
while(buf[s]==s) ++s;
while(buf[e]==e) --e;
if(e==-1){
printf("1 1\n1 1");
return 0;
}
int minv = s, maxv = e;
while(buf[minv]!=s) ++minv;
while(buf[maxv]!=e) --maxv;
int ret = swap(s,minv,e);
if(ret){
if(ret==-1)
printf("%d %d\n1 1",s,minv);
else
printf("%d %d\n%d %d",s,minv,ans[2],ans[3]);
}
else{
ret = swap2(maxv,e,s);
if(ret==-1)
printf("%d %d\n1 1",s,minv);
else
printf("%d %d\n%d %d",maxv,e,ans[2],ans[3]);
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2505/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.