結果
| 問題 |
No.1060 素敵な宝箱
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-06-08 08:42:39 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 187 ms / 2,000 ms |
| コード長 | 3,131 bytes |
| コンパイル時間 | 4,165 ms |
| コンパイル使用メモリ | 83,888 KB |
| 実行使用メモリ | 44,828 KB |
| 最終ジャッジ日時 | 2024-12-30 15:22:55 |
| 合計ジャッジ時間 | 7,940 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int m = sc.nextInt();
Box[] boxes = new Box[n];
int[] sums = new int[m];
for (int i = 0; i < n; i++) {
int[] values = new int[m];
for (int j = 0; j < m; j++) {
values[j] = sc.nextInt();
sums[j] += values[j];
}
boxes[i] = new Box(values);
}
Box.values = sums;
for (Box b : boxes) {
b.calc();
}
Arrays.sort(boxes);
long[] ev = new long[m];
long[] od = new long[m];
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
boxes[i].add(ev);
} else {
boxes[i].add(od);
}
}
long ans = 0;
for (int i = 0; i < m; i++) {
ans -= ev[i] * ev[i] - od[i] * od[i];
}
System.out.println(ans);
}
static class Box implements Comparable<Box> {
static int[] values;
int[] counts;
long score;
public Box(int[] counts) {
this.counts = counts;
}
public void calc() {
for (int i = 0; i < counts.length; i++) {
score += values[i] * counts[i];
}
}
public int compareTo(Box another) {
if (score == another.score) {
return 0;
} else if (score < another.score) {
return -1;
} else {
return 1;
}
}
public void add(long[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] += counts[i];
}
}
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten