forked from indy256/codejam-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateMT.java
More file actions
68 lines (59 loc) · 1.66 KB
/
TemplateMT.java
File metadata and controls
68 lines (59 loc) · 1.66 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
import java.io.File;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TemplateMT {
public static void main(String[] args) throws Exception {
//long start = System.currentTimeMillis();
String name = "A-small";
String path = "";
Locale.setDefault(Locale.US);
Scanner sc = new Scanner(new File(path + name + ".in"));
PrintWriter pw = new PrintWriter(path + name + ".out");
// Scanner sc = new Scanner(System.in);
// PrintWriter pw = new PrintWriter(System.out);
ExecutorService executor = Executors.newFixedThreadPool(16);
int testCases = sc.nextInt();
Future<Double>[] futures = new Future[testCases];
for (int testCase = 1; testCase <= testCases; testCase++) {
String cmd = sc.next();
int n = sc.nextInt();
double[] a = new double[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextDouble();
}
futures[testCase - 1] = executor.submit(() -> {
final double res;
switch (cmd) {
case "median":
Arrays.sort(a);
res = a[n / 2];
break;
case "mean":
double sum = 0;
for (double v : a) {
sum += v;
}
res = sum / n;
break;
default:
throw new RuntimeException();
}
return res;
});
}
for (int testCase = 1; testCase <= testCases; testCase++) {
Double res = futures[testCase - 1].get();
pw.printf("Case #" + testCase + ": %.10f\n", res);
pw.flush();
}
pw.close();
sc.close();
executor.shutdown();
//System.err.println(System.currentTimeMillis() - start);
}
}