-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task756.java
61 lines (59 loc) · 2.03 KB
/
Task756.java
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
import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
public class Task756 {
private static int BUFFER_SIZE;
private static DataInputStream dataInputStream;
private static byte[] buffer;
private static int bufferPointer, bytesRead;
public static void main(String[] args) throws IOException {
BUFFER_SIZE = 1 << 16;
dataInputStream = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = 0;
bytesRead = 0;
int n = nextInt();
int k = nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
Deque<Integer> deque = new ArrayDeque<>();
StringBuilder result = new StringBuilder((n - k + 1) * 6);
for (int i = 0; i < n; i++) {
if (!deque.isEmpty() && deque.getFirst() < i - k + 1) {
deque.removeFirst();
}
while (!deque.isEmpty() && array[deque.getLast()] > array[i]) {
deque.removeLast();
}
deque.addLast(i);
if (i >= k - 1) {
result.append(array[deque.getFirst()]).append('\n');
}
}
System.out.write(result.toString().getBytes());
}
private static int nextInt() throws IOException {
int result = 0;
byte currentByte = read();
boolean isNegative = (currentByte == '-');
if (isNegative) {
currentByte = read();
}
do {
result = result * 10 + currentByte - '0';
} while ((currentByte = read()) >= '0' && currentByte <= '9');
return isNegative ? -result : result;
}
private static byte read() throws IOException {
if (bufferPointer == bytesRead) {
bytesRead = dataInputStream.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
return buffer[bufferPointer++];
}
}