結果

問題 No.1060 素敵な宝箱
ユーザー tentententen
提出日時 2023-06-08 08:42:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 3,131 bytes
コンパイル時間 2,378 ms
コンパイル使用メモリ 80,348 KB
実行使用メモリ 57,172 KB
最終ジャッジ日時 2023-08-28 23:48:58
合計ジャッジ時間 6,507 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,840 KB
testcase_01 AC 42 ms
50,088 KB
testcase_02 AC 42 ms
49,744 KB
testcase_03 AC 42 ms
49,692 KB
testcase_04 AC 42 ms
49,780 KB
testcase_05 AC 42 ms
49,664 KB
testcase_06 AC 43 ms
49,680 KB
testcase_07 AC 42 ms
49,688 KB
testcase_08 AC 62 ms
50,764 KB
testcase_09 AC 60 ms
50,724 KB
testcase_10 AC 58 ms
50,688 KB
testcase_11 AC 57 ms
50,712 KB
testcase_12 AC 110 ms
52,972 KB
testcase_13 AC 119 ms
55,060 KB
testcase_14 AC 121 ms
54,264 KB
testcase_15 AC 138 ms
56,432 KB
testcase_16 AC 131 ms
55,996 KB
testcase_17 AC 110 ms
52,272 KB
testcase_18 AC 118 ms
55,204 KB
testcase_19 AC 111 ms
54,944 KB
testcase_20 AC 132 ms
56,024 KB
testcase_21 AC 128 ms
56,060 KB
testcase_22 AC 134 ms
56,028 KB
testcase_23 AC 118 ms
55,392 KB
testcase_24 AC 151 ms
57,028 KB
testcase_25 AC 152 ms
57,172 KB
testcase_26 AC 151 ms
56,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0