結果
問題 |
No.1028 闇討ち
|
ユーザー |
|
提出日時 | 2020-08-06 11:28:01 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 1,315 ms / 2,000 ms |
コード長 | 1,944 bytes |
コンパイル時間 | 2,522 ms |
コンパイル使用メモリ | 78,280 KB |
実行使用メモリ | 171,100 KB |
最終ジャッジ日時 | 2024-09-19 11:31:52 |
合計ジャッジ時間 | 17,803 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
import java.io.*; import java.util.ArrayList; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); ArrayList<Integer> arr[][] = new ArrayList[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ arr[i][j] = new ArrayList<Integer>(); } } for(int i = 0; i < n; i++){ String[] line = br.readLine().split(" "); for(int j = 0; j < n; j++){ arr[Integer.parseInt(line[j])-1][i].add(j); } } int ans = 0; for(int i = 0; i < n; i++){ int sum[] = new int[n]; for(int j = 0; j < n; j++){ for(int k : arr[i][j]){ sum[j] += k; } } int down[] = new int[n], tmp[] = new int[n]; int val = 0, acc = 0; for(int j = 0; j < n; j++){ for(int k : arr[i][j]){ if(j+k+1 < n) tmp[j+k+1]++; } val += sum[j]; acc += tmp[j]; val += acc; down[j] = val; } int up[] = new int[n]; Arrays.fill(tmp, 0); val = 0; acc = 0; for(int j = n-1; j >= 0; j--){ for(int k : arr[i][j]){ if(j-k-1 >= 0) tmp[j-k-1]++; } val += sum[j]; acc += tmp[j]; val += acc; up[j] = val; } int score = 1<<30; for(int j = 0; j < n; j++){ score = Math.min(score, down[j]+up[j]-sum[j]); } ans += score; } System.out.println(ans); } }