-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZip.java
More file actions
89 lines (78 loc) · 3.2 KB
/
Copy pathZip.java
File metadata and controls
89 lines (78 loc) · 3.2 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
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
package algorithms.sprint0;
import static common.SafeParse.parseInt;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import static algorithms.sprint0.Utils.printList;
public class Zip {
private static final int MAX_LIST_SIZE = 100_000;
private static final int MAX_INPUT_LINE_LENGTH = 1_200_001;
static List<Integer> zip(List<Integer> a, List<Integer> b, int n) {
if (n < 0) {
throw new IllegalArgumentException("n >= 0 required");
}
int min = Math.min(n, Math.min(a.size(), b.size()));
ArrayList<Integer> integers = new ArrayList<>(min * 2);
for (int i = 0; i < min; i++) {
integers.add(a.get(i));
integers.add(b.get(i));
}
return integers;
}
public static void main(String[] args) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out, StandardCharsets.UTF_8))) {
try {
process(reader, writer);
} catch (IllegalArgumentException | EOFException exception) {
System.err.println("Invalid input: " + exception.getMessage());
}
}
}
static void process(BufferedReader reader, BufferedWriter writer) throws IOException {
String sizeLine = readBoundedLine(reader);
if (sizeLine == null) {
throw new EOFException("Missing list size");
}
int n = parseInt(sizeLine.trim());
if (n < 0 || n > MAX_LIST_SIZE) {
throw new IllegalArgumentException("List size must be between 0 and " + MAX_LIST_SIZE);
}
List<Integer> a = parseList(readBoundedLine(reader));
List<Integer> b = parseList(readBoundedLine(reader));
if (a.size() < n || b.size() < n) {
throw new IllegalArgumentException("Each list must contain at least n integers");
}
printList(zip(a, b, n), writer);
}
private static String readBoundedLine(BufferedReader reader) throws IOException {
StringBuilder line = new StringBuilder();
int character;
while ((character = reader.read()) != -1 && character != '\n' && character != '\r') {
if (line.length() == MAX_INPUT_LINE_LENGTH) {
throw new IllegalArgumentException("Input line is too long");
}
line.append((char) character);
}
if (character == '\r') {
reader.mark(1);
if (reader.read() != '\n') {
reader.reset();
}
}
return character == -1 && line.length() == 0 ? null : line.toString();
}
private static List<Integer> parseList(String line) throws IOException {
if (line == null) {
throw new EOFException("Missing integer list");
}
return Utils.readList(new BufferedReader(new StringReader(line)));
}
}