-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathG.java
116 lines (104 loc) · 2.52 KB
/
G.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
class OCHEREDNYARA {
int ind;
OCHEREDNYARA prev;
OCHEREDNYARA next;
}
class DOTA {
OCHEREDNYARA top;
OCHEREDNYARA bot;
OCHEREDNYARA mid;
int len;
DOTA() {
top = null;
bot = null;
mid = null;
}
void addLast(int id) {
OCHEREDNYARA tmp = new OCHEREDNYARA();
tmp.ind = id;
tmp.next = null;
if (bot != null) {
bot.next = tmp;
tmp.prev = bot;
bot = tmp;
if (len % 2 == 0) {
mid = mid.next;
}
} else {
top = tmp;
bot = tmp;
mid = tmp;
tmp.prev = null;
}
len++;
}
void addSoloMid(int id) {
if (top == null) {
addLast(id);
} else {
OCHEREDNYARA tmp = new OCHEREDNYARA();
tmp.ind = id;
tmp.prev = mid;
tmp.next = mid.next;
if (mid.next != null) {
mid.next.prev = tmp;
} else {
bot = tmp;
}
mid.next = tmp;
if (len % 2 == 0) {
mid = tmp;
}
len++;
}
}
int KILL() {
if (top == null) {
return 0;
}
int res = top.ind;
if (len == 1) {
top = null;
bot = null;
mid = null;
len = 0;
return res;
}
OCHEREDNYARA tmp = top;
top = top.next;
top.prev = null;
if (len % 2 == 0) {
mid = mid.next;
}
len--;
return res;
}
}
public class G {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("hospital.in"));
FileWriter fileWriter = new FileWriter(new File("hospital.out"));
int n = scanner.nextInt();
DOTA dota = new DOTA();
for (int i = 0; i < n; i++) {
String cur = scanner.next();
switch (cur) {
case "-":
fileWriter.write(dota.KILL() + "\n");
break;
case "+":
dota.addLast(scanner.nextInt());
break;
case "*":
dota.addSoloMid(scanner.nextInt());
break;
}
}
fileWriter.flush();
}
}