import java.util.*;
import java.io.*;
public class ho_t2 {
final static int MOD = 1000000007;
final static int intMax = 1000000000;
final static int intMin = -1000000000;
final static int[] dx = { 0, 0, -1, 1 };
final static int[] dy = { -1, 1, 0, 0 };
static int T;
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[360]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
static int N, K;
static String s;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
s = br.readLine();
ArrayList<Integer> Js = new ArrayList<Integer>();
ArrayList<Integer> Os = new ArrayList<Integer>();
ArrayList<Integer> Is = new ArrayList<Integer>();
for(int i = 0; i < N; ++i) {
if(s.charAt(i) == 'J') {
Js.add(i);
}
else if(s.charAt(i) == 'O') {
Os.add(i);
}
else {
Is.add(i);
}
}
int ans = 1000000000;
for(int i = K - 1; i < Js.size(); ++i) {
int curpos = Js.get(i);
int l = 0, r = Os.size();
while(l < r) {
int mid = l + (r - l) / 2;
if(mid == Os.size()) break;
if(Os.get(mid) > curpos) {
r = mid;
}
else {
l = mid + 1;
}
}
if(l + K - 1 >= Os.size()) break;
int opos = Os.get(l + K - 1);
l = 0; r = Is.size();
while(l < r) {
int mid = l + (r - l) / 2;
if(mid == Is.size()) break;
if(Is.get(mid) > opos) {
r = mid;
}
else {
l = mid + 1;
}
}
if(l + K - 1 >= Is.size()) break;
ans = Math.min(ans, Is.get(l + K - 1) - Js.get(i - K + 1) + 1);
}
if(ans == 1000000000) System.out.println(-1);
else System.out.println(ans - K * 3);
// in.close();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
10340 KB |
Output is correct |
2 |
Correct |
87 ms |
10316 KB |
Output is correct |
3 |
Correct |
75 ms |
10212 KB |
Output is correct |
4 |
Correct |
79 ms |
10240 KB |
Output is correct |
5 |
Correct |
93 ms |
10216 KB |
Output is correct |
6 |
Correct |
86 ms |
10240 KB |
Output is correct |
7 |
Correct |
94 ms |
10160 KB |
Output is correct |
8 |
Correct |
89 ms |
10220 KB |
Output is correct |
9 |
Correct |
77 ms |
10232 KB |
Output is correct |
10 |
Correct |
77 ms |
10236 KB |
Output is correct |
11 |
Correct |
79 ms |
10380 KB |
Output is correct |
12 |
Correct |
79 ms |
10304 KB |
Output is correct |
13 |
Correct |
78 ms |
10168 KB |
Output is correct |
14 |
Correct |
82 ms |
10344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
10340 KB |
Output is correct |
2 |
Correct |
87 ms |
10316 KB |
Output is correct |
3 |
Correct |
75 ms |
10212 KB |
Output is correct |
4 |
Correct |
79 ms |
10240 KB |
Output is correct |
5 |
Correct |
93 ms |
10216 KB |
Output is correct |
6 |
Correct |
86 ms |
10240 KB |
Output is correct |
7 |
Correct |
94 ms |
10160 KB |
Output is correct |
8 |
Correct |
89 ms |
10220 KB |
Output is correct |
9 |
Correct |
77 ms |
10232 KB |
Output is correct |
10 |
Correct |
77 ms |
10236 KB |
Output is correct |
11 |
Correct |
79 ms |
10380 KB |
Output is correct |
12 |
Correct |
79 ms |
10304 KB |
Output is correct |
13 |
Correct |
78 ms |
10168 KB |
Output is correct |
14 |
Correct |
82 ms |
10344 KB |
Output is correct |
15 |
Correct |
92 ms |
10264 KB |
Output is correct |
16 |
Correct |
91 ms |
10464 KB |
Output is correct |
17 |
Correct |
99 ms |
10324 KB |
Output is correct |
18 |
Correct |
88 ms |
10596 KB |
Output is correct |
19 |
Correct |
93 ms |
10364 KB |
Output is correct |
20 |
Correct |
91 ms |
10344 KB |
Output is correct |
21 |
Correct |
93 ms |
10344 KB |
Output is correct |
22 |
Correct |
84 ms |
10464 KB |
Output is correct |
23 |
Correct |
86 ms |
10268 KB |
Output is correct |
24 |
Correct |
100 ms |
10340 KB |
Output is correct |
25 |
Correct |
85 ms |
10364 KB |
Output is correct |
26 |
Correct |
84 ms |
10364 KB |
Output is correct |
27 |
Correct |
90 ms |
10252 KB |
Output is correct |
28 |
Correct |
87 ms |
10604 KB |
Output is correct |
29 |
Correct |
81 ms |
10228 KB |
Output is correct |
30 |
Correct |
95 ms |
10348 KB |
Output is correct |
31 |
Correct |
83 ms |
10348 KB |
Output is correct |
32 |
Correct |
86 ms |
10704 KB |
Output is correct |
33 |
Correct |
85 ms |
10496 KB |
Output is correct |
34 |
Correct |
89 ms |
10356 KB |
Output is correct |
35 |
Correct |
79 ms |
10416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
10340 KB |
Output is correct |
2 |
Correct |
87 ms |
10316 KB |
Output is correct |
3 |
Correct |
75 ms |
10212 KB |
Output is correct |
4 |
Correct |
79 ms |
10240 KB |
Output is correct |
5 |
Correct |
93 ms |
10216 KB |
Output is correct |
6 |
Correct |
86 ms |
10240 KB |
Output is correct |
7 |
Correct |
94 ms |
10160 KB |
Output is correct |
8 |
Correct |
89 ms |
10220 KB |
Output is correct |
9 |
Correct |
77 ms |
10232 KB |
Output is correct |
10 |
Correct |
77 ms |
10236 KB |
Output is correct |
11 |
Correct |
79 ms |
10380 KB |
Output is correct |
12 |
Correct |
79 ms |
10304 KB |
Output is correct |
13 |
Correct |
78 ms |
10168 KB |
Output is correct |
14 |
Correct |
82 ms |
10344 KB |
Output is correct |
15 |
Correct |
92 ms |
10264 KB |
Output is correct |
16 |
Correct |
91 ms |
10464 KB |
Output is correct |
17 |
Correct |
99 ms |
10324 KB |
Output is correct |
18 |
Correct |
88 ms |
10596 KB |
Output is correct |
19 |
Correct |
93 ms |
10364 KB |
Output is correct |
20 |
Correct |
91 ms |
10344 KB |
Output is correct |
21 |
Correct |
93 ms |
10344 KB |
Output is correct |
22 |
Correct |
84 ms |
10464 KB |
Output is correct |
23 |
Correct |
86 ms |
10268 KB |
Output is correct |
24 |
Correct |
100 ms |
10340 KB |
Output is correct |
25 |
Correct |
85 ms |
10364 KB |
Output is correct |
26 |
Correct |
84 ms |
10364 KB |
Output is correct |
27 |
Correct |
90 ms |
10252 KB |
Output is correct |
28 |
Correct |
87 ms |
10604 KB |
Output is correct |
29 |
Correct |
81 ms |
10228 KB |
Output is correct |
30 |
Correct |
95 ms |
10348 KB |
Output is correct |
31 |
Correct |
83 ms |
10348 KB |
Output is correct |
32 |
Correct |
86 ms |
10704 KB |
Output is correct |
33 |
Correct |
85 ms |
10496 KB |
Output is correct |
34 |
Correct |
89 ms |
10356 KB |
Output is correct |
35 |
Correct |
79 ms |
10416 KB |
Output is correct |
36 |
Correct |
209 ms |
19596 KB |
Output is correct |
37 |
Correct |
236 ms |
19616 KB |
Output is correct |
38 |
Correct |
201 ms |
19888 KB |
Output is correct |
39 |
Correct |
239 ms |
20400 KB |
Output is correct |
40 |
Correct |
211 ms |
19952 KB |
Output is correct |
41 |
Correct |
194 ms |
19780 KB |
Output is correct |
42 |
Correct |
228 ms |
19960 KB |
Output is correct |
43 |
Correct |
137 ms |
16684 KB |
Output is correct |
44 |
Correct |
144 ms |
18628 KB |
Output is correct |
45 |
Correct |
165 ms |
19772 KB |
Output is correct |
46 |
Correct |
152 ms |
19704 KB |
Output is correct |
47 |
Correct |
153 ms |
19704 KB |
Output is correct |
48 |
Correct |
172 ms |
20804 KB |
Output is correct |
49 |
Correct |
135 ms |
17812 KB |
Output is correct |
50 |
Correct |
150 ms |
19704 KB |
Output is correct |
51 |
Correct |
163 ms |
19848 KB |
Output is correct |
52 |
Correct |
158 ms |
19420 KB |
Output is correct |
53 |
Correct |
158 ms |
19456 KB |
Output is correct |
54 |
Correct |
206 ms |
19928 KB |
Output is correct |
55 |
Correct |
185 ms |
20656 KB |
Output is correct |
56 |
Correct |
168 ms |
19764 KB |
Output is correct |