-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTeque.java
More file actions
60 lines (56 loc) · 1.72 KB
/
Copy pathTeque.java
File metadata and controls
60 lines (56 loc) · 1.72 KB
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
//Name: Nguyen Minh Hieu
//https://open.kattis.com/problems/teque
import java.util.*;
import java.io.*;
public class Teque {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
HashMap<Integer, Integer> first = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> second = new HashMap<Integer, Integer>();
int firstHead = 0;
int firstTail = 0;
int secondHead = 0;
int secondTail = 0;
for (int i = 0; i < n; i ++) {
String[] command = br.readLine().split(" ");
int num = Integer.parseInt(command[1]);
if (command[0].equals("push_back")) {
second.put(secondTail, num);
secondTail++;
if (second.size() > first.size() + 1) {
first.put(firstTail,second.get(secondHead));
firstTail++;
secondHead++;
second.remove(secondHead-1);
}
} else if (command[0].equals("push_front")) {
firstHead--;
first.put(firstHead,num);
if (first.size() > second.size()) {
firstTail--;
secondHead--;
second.put(secondHead,first.get(firstTail));
first.remove(firstTail);
}
} else if (command[0].equals("push_middle")) {
if (first.size() == second.size()) {
secondHead--;
second.put(secondHead,num);
} else {
first.put(firstTail,second.get(secondHead));
firstTail++;
second.put(secondHead, num);
}
} else if (command[0].equals("get")) {
if (num < firstTail - firstHead) {
pw.println(first.get(firstHead + num));
} else {
pw.println(second.get(secondHead + num - (firstTail -firstHead)));
}
}
}
pw.flush();
}
}