Skip to content

Commit bc32ca8

Browse files
authored
[20250325] BOJ / P2 / 백설공주와 난쟁이 / 권혁준
1 parent 4ba77e7 commit bc32ca8

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Edge{
7+
int s, e;
8+
double c;
9+
Edge(int s, int e, double c){
10+
this.s = s;
11+
this.e = e;
12+
this.c = c;
13+
}
14+
}
15+
16+
class Main {
17+
18+
// IO field
19+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
21+
static StringTokenizer st = new StringTokenizer("");
22+
23+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
24+
static String nextToken() throws Exception {
25+
while(!st.hasMoreTokens()) nextLine();
26+
return st.nextToken();
27+
}
28+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
29+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
30+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
31+
static void bwEnd() throws Exception {bw.flush();bw.close();}
32+
33+
// Additional field
34+
35+
static int N, C, M;
36+
static int[] A;
37+
static List<Integer>[] Idx;
38+
static Random rd = new Random();
39+
40+
public static void main(String[] args) throws Exception {
41+
42+
ready();
43+
solve();
44+
45+
bwEnd();
46+
47+
}
48+
49+
static void ready() throws Exception{
50+
51+
N = nextInt();
52+
C = nextInt();
53+
A = new int[N+1];
54+
Idx = new List[C+1];
55+
for(int i=1;i<=C;i++) Idx[i] = new ArrayList<>();
56+
57+
for(int i=1;i<=N;i++) {
58+
A[i] = nextInt();
59+
Idx[A[i]].add(i);
60+
}
61+
62+
}
63+
64+
static void solve() throws Exception{
65+
66+
for(M=nextInt();M-->0;) {
67+
int a = nextInt(), b = nextInt();
68+
69+
int res = -1;
70+
for(int i=0;i<100;i++) {
71+
int x = A[a + rd.nextInt(b-a+1)];
72+
73+
int cnt = lowerBound(x,b) - lowerBound(x,a-1);
74+
if(cnt > (b-a+1)/2) {
75+
res = x;
76+
break;
77+
}
78+
79+
}
80+
if(res == -1) bw.write("no\n");
81+
else bw.write("yes " + res + "\n");
82+
}
83+
84+
}
85+
86+
static int lowerBound(int k, int x) {
87+
int s = 0, e = Idx[k].size(), m = (s+e)>>1;
88+
while(s<e) {
89+
if(Idx[k].get(m) <= x) s = m+1;
90+
else e = m;
91+
m = (s+e)>>1;
92+
}
93+
return m;
94+
}
95+
96+
}
97+
98+
```

0 commit comments

Comments
 (0)