File tree Expand file tree Collapse file tree 1 file changed +107
-0
lines changed
Expand file tree Collapse file tree 1 file changed +107
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.util.* ;
3+ import java.io.* ;
4+
5+ public class Main {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
8+
9+ public static void main (String [] args ) throws Exception {
10+
11+ int T = Integer . parseInt(br. readLine());
12+ while (T -- > 0 ) {
13+
14+ int N = Integer . parseInt(br. readLine());
15+
16+ if (N <= 47 ) naive(N );
17+ else window(N );
18+
19+ }
20+
21+ }
22+
23+ static void naive (int N ) throws Exception {
24+ int a = - 1 , b = - 1 ;
25+ for (int i= 1 ;i<= N ;i++ ) {
26+ if (shout(i,i)) {
27+ if (a == - 1 ) a = i;
28+ else b = i;
29+ }
30+ }
31+ bw. write(" ! " + a + " " + b + " \n " );
32+ bw. flush();
33+ }
34+
35+ static void window (int N ) throws Exception {
36+ int s = 2 , e = (N + 2 )/ 2 + 1 ;
37+ int start = s- 1 , end = e- 1 ;
38+
39+ // 맨 처음이 O인 경우
40+ if (shout(s- 1 ,e- 1 )) {
41+ bw. write(" ! " + find(1 ,e- 1 ) + " " + find(e,N ) + " \n " );
42+ bw. flush();
43+ return ;
44+ }
45+
46+ while (e <= N ) {
47+ if (shout(s,e)) {
48+ if (s == 45 && e == 91 ) {
49+ if (shout(46 ,92 )) {
50+ if (shout(46 ,46 )) {
51+ bw. write(" ! 44 46\n " );
52+ bw. flush();
53+ }
54+ else {
55+ bw. write(" ! 44 47\n " );
56+ bw. flush();
57+ }
58+ }
59+ else {
60+ if (shout(91 ,91 )) {
61+ bw. write(" ! 91 92\n " );
62+ bw. flush();
63+ }
64+ else {
65+ bw. write(" ! 44 45\n " );
66+ bw. flush();
67+ }
68+ }
69+ }
70+ else if (e < N && shout(e,e)) {
71+ bw. write(" ! " + e + " " + find(e+ 1 ,N ) + " \n " );
72+ bw. flush();
73+ }
74+ else {
75+ bw. write(" ! " + (s- 1 ) + " " + find(s,end) + " \n " );
76+ bw. flush();
77+ }
78+ return ;
79+ }
80+ s++ ;
81+ e++ ;
82+ }
83+ bw. write(" ! " + end + " " + (end- 1 ) + " \n " );
84+ bw. flush();
85+ }
86+
87+ static int find (int s , int e ) throws Exception {
88+ int m = (s+ e)>> 1 ;
89+ while (s< e) {
90+ if (shout(s,m)) e = m;
91+ else {
92+ s = m+ 1 ;
93+ if (s == e) return s;
94+ }
95+ m = (s+ e)>> 1 ;
96+ }
97+ return m;
98+ }
99+
100+ static boolean shout (int l , int r ) throws Exception {
101+ bw. write(" AK " + l + " " + r + " \n " );
102+ bw. flush();
103+ return br. readLine(). charAt(0 ) == ' O' ;
104+ }
105+
106+ }
107+ ```
You can’t perform that action at this time.
0 commit comments