結果

問題 No.43 野球の試合
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-02-24 16:51:57
言語 Java21
(openjdk 21)
結果
AC  
実行時間 568 ms / 5,000 ms
コード長 2,721 bytes
コンパイル時間 3,649 ms
コンパイル使用メモリ 81,472 KB
実行使用メモリ 73,712 KB
最終ジャッジ日時 2023-08-24 20:33:43
合計ジャッジ時間 6,631 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 147 ms
56,676 KB
testcase_01 AC 146 ms
56,776 KB
testcase_02 AC 146 ms
56,328 KB
testcase_03 AC 148 ms
56,232 KB
testcase_04 AC 147 ms
56,376 KB
testcase_05 AC 146 ms
56,280 KB
testcase_06 AC 182 ms
56,620 KB
testcase_07 AC 568 ms
73,712 KB
testcase_08 AC 146 ms
56,648 KB
testcase_09 AC 144 ms
55,960 KB
testcase_10 AC 144 ms
55,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map.Entry;
import java.util.OptionalInt;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        
        int n = Integer.parseInt(stdin.nextLine());
        char[] s = new char[n * n];
        for (int i = 0; i < n; i++) {
            String t = stdin.nextLine();
            for (int j = 0; j < n; j++) s[i * n + j] = t.charAt(j);
        }
        
        List<Integer> games = new ArrayList<>();
        for (int i = 0; i < n * n; i++) {
            int x = i / n;
            int y = i % n;
            if (x < y && s[i] == '-') games.add(i);
        }

        
        int ans = Integer.MAX_VALUE;
        
        for (int bit = 0, len = games.size(); bit < (1 << len); bit++) {
            for (int i = 0; i < len; i++) {
                int x = games.get(i) / n;
                int y = games.get(i) % n;
                if ((bit & (1 << i)) == 0) {
                    s[n * x + y] = 'o';
                    s[n * y + x] = 'x';
                } else {
                    s[n * x + y] = 'x';
                    s[n * y + x] = 'o';                    
                }
            }
            
            int[] counter = new int[n];
            for (int x = 0; x < n; x++) {
                for (int y = 0; y < n; y++) {
                    if (x >= y) continue;
                    if (s[n * x + y] == 'o') {
                        counter[x]++;
                    } else {
                        counter[y]++;
                    }
                }
            }
            
            List<List<Integer>> standings = IntStream.range(0, n)
                                                     .boxed()
                                                     .collect(Collectors.groupingBy(i -> counter[i]))
                                                     .entrySet()
                                                     .stream()
                                                     .sorted(Comparator.comparing(Entry::getKey, Comparator.reverseOrder()))
                                                     .map(Entry::getValue)
                                                     .collect(Collectors.toList());
            OptionalInt rank = IntStream.range(0, n) 
                                        .filter(i -> standings.get(i).contains(0))
                                        .findFirst();
            if (rank.isPresent()) ans = Math.min(ans, rank.getAsInt() + 1);
        }

        System.out.println(ans);
    }
}
0