結果

問題 No.5014 セクスタプル (reactive)
ユーザー EvbCFfp1XBEvbCFfp1XB
提出日時 2022-12-29 16:00:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 14,070 bytes
コンパイル時間 1,774 ms
実行使用メモリ 67,096 KB
スコア 0
平均クエリ数 7.81
最終ジャッジ日時 2022-12-29 16:02:39
合計ジャッジ時間 19,731 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 WA -
testcase_80 WA -
testcase_81 WA -
testcase_82 WA -
testcase_83 WA -
testcase_84 WA -
testcase_85 WA -
testcase_86 WA -
testcase_87 WA -
testcase_88 WA -
testcase_89 WA -
testcase_90 WA -
testcase_91 WA -
testcase_92 WA -
testcase_93 WA -
testcase_94 WA -
testcase_95 WA -
testcase_96 WA -
testcase_97 WA -
testcase_98 WA -
testcase_99 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    private int[][] dice;
    private int[][] table;
    private int[] counts = new int[6];

    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    private void run() {
        solve();
    }

    private void solve() {
        try (final Scanner in = new Scanner(System.in)) {
            table = new int[6][6];
            for (int r = 0; r < 6; r++) {
                Arrays.fill(table[r], -1);
            }

            dice = new int[35][6];
            for (int index = 0; index < 35; index++) {
                for (int j = 0; j < 6; j++) {
                    dice[index][j] = in.nextInt() - 1;
                    if (index == 0 && j == 0) {
                        Constants.watch.init();
                    }
                }

                int[] rank = rank(dice[index]);

                int best = (int) -1e9;
                int bestR = -1;
                int bestC = -1;
                for (int r = 0; r < 6; r++) {
                    if (!(rank[r] == 0 || rank[r] == 1)) {
                        continue;
                    }
                    for (int c = 0; c < 6; c++) {
                        int[][] copyTable = copy(table);
                        copyTable[r][c] = index;
                        int score = calculateScore(copyTable);
                        if (score > best) {
                            best = score;
                            bestR = r;
                            bestC = c;
                        }
                    }
                }
                table[bestR][bestC] = index;

                System.out.println((bestR + 1) + " " + (bestC + 1));
                System.out.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private int[][] copy(int[][] table) {
        int[][] copy = new int[6][6];
        for (int r = 0; r < 6; r++) {
            for (int c = 0; c < 6; c++) {
                copy[r][c] = table[r][c];
            }
        }
        return copy;
    }

    private int[] rank(int[] dice) {
        int[] counts = new int[6];
        for (int i = 0; i < dice.length; i++) {
            counts[dice[i]]++;
        }
        int[] rank = new int[6];
        for (int i = 0; i < counts.length; i++) {
            for (int j = 0; j < counts.length; j++) {
                if (j == i) {
                    continue;
                }
                if (counts[i] < counts[j]) {
                    rank[i]++;
                }
            }
        }
        return rank;
    }

//    private void solve_() {
//        greedy();
////        multiSA();
//    }

//    private void greedy() {
//        table = new int[6][6];
//        bestTable = new int[6][6];
//        for (int i = 0; i < 6; i++) {
//            for (int j = 0; j < 6; j++) {
//                table[i][j] = i * 6 + j;
//            }
//        }
//        score = calculateScore();
//        saveBest();
//    }

//    private void saveBest() {
//        if (score > bestScore) {
//            bestScore = score;
//            for (int r = 0; r < 6; r++) {
//                for (int c = 0; c < 6; c++) {
//                    bestTable[r][c] = table[r][c];
//                }
//            }
//        }
//    }

//    private void multiSA() {
//        int numRestart = 1;
//        double startTime = Constants.watch.getSecond();
//        double endTime = 1 * 1.75;
//        double remainTime = endTime - startTime;
//        double startStartTemperature = 1;
//        double endStartTemperature = 1e-9;
//        for (double restart = 0; restart < numRestart; restart++) {
//            sa.startTime = startTime + remainTime * restart / numRestart;
//            sa.endTime = startTime + remainTime * (restart + 1) / numRestart;
//            sa.startTemperature = endStartTemperature + (startStartTemperature - endStartTemperature) * ((numRestart - restart) / numRestart);
//            sa.endTemperature = 1e-9;
//            SA();
//        }
//    }
//
//    private void SA() {
//        sa.init();
//        for (;; ++sa.numIterations) {
//            if ((sa.numIterations & ((1 << 10) - 1)) == 0) {
//                sa.update();
//                if (sa.isTLE()) {
//                    break;
//                }
//            }
//            mutate();
//        }
//    }
//
//    private void mutate() {
//        change();
//    }
//
//    private void change() {
////        int v1 = sa.numIterations % 36;
////        int r1 = v1 / 6;
////        int c1 = v1 % 6;
////        int r2 = Constants.RNG.nextInt(6);
////        int c2 = Constants.RNG.nextInt(6);
////        int current1 = table[r1][c1];
////        int current2 = table[r2][c2];
////        int before = 0;
////        before += part_score_row[r1];
////        before += part_score_row[r2];
////        before += part_score_column[c1];
////        before += part_score_column[c2];
////        table[r1][c1] = current2;
////        table[r2][c2] = current1;
////        int after_r1 = partScoreRow(counts_row, r1);
////        int after_r2 = partScoreRow(counts_row, r2);
////        int after_c1 = partScoreColumn(counts_column, c1);
////        int after_c2 = partScoreColumn(counts_column, c2);
////        int after = 0;
////        after += after_r1;
////        after += after_r2;
////        after += after_c1;
////        after += after_c2;
////        int deltaScore = after - before;
////        if (sa.accept(deltaScore)) {
////            score += deltaScore;
////            part_score_row[r1] = after_r1;
////            part_score_row[r2] = after_r2;
////            part_score_column[c1] = after_c1;
////            part_score_column[c2] = after_c2;
////            saveBest();
////        } else {
////            table[r1][c1] = current1;
////            table[r2][c2] = current2;
////        }
//    }

    private int calculateScore(int[][] table) {
        int score = 0;
        for (int r = 0; r < 6; r++) {
            score += partScoreRow(table, r);
        }
        for (int c = 0; c < 6; c++) {
            score += partScoreColumn(table, c);
        }
        return score;
    }

    private int partScoreColumn(int[][] table, int c) {
        Arrays.fill(counts, 0);
        int mask = (1 << 6) - 1;
        int count_r = 0;
        for (int r = 0; r < 6; r++) {
            int index = table[r][c];
            if (index < 0) {
                continue;
            }
            count_r++;
            int mask2 = 0;
            for (int i = 0; i < 6; i++) {
                counts[dice[index][i]]++;
                mask2 |= (1 << dice[index][i]);
            }
            mask &= mask2;
        }
        int part_score = 0;
        for (int i = 0; i < 6; i++) {
            if ((mask & (1 << i)) == 0) {
                continue;
            }
            if (counts[i] == 0) {
                continue;
            }
            part_score += 3;
            part_score += counts[i] - count_r;
        }
        return part_score;
    }

    private int partScoreRow(int[][] table, int r) {
        Arrays.fill(counts, 0);
        int mask = (1 << 6) - 1;
        int count_c = 0;
        for (int c = 0; c < 6; c++) {
            int index = table[r][c];
            if (index < 0) {
                continue;
            }
            count_c++;
            int mask2 = 0;
            for (int i = 0; i < 6; i++) {
                counts[dice[index][i]]++;
                mask2 |= (1 << dice[index][i]);
            }
            mask &= mask2;
        }
        int part_score = 0;
        for (int i = 0; i < 6; i++) {
            if ((mask & (1 << i)) == 0) {
                continue;
            }
            if (counts[i] == 0) {
                continue;
            }
            part_score += 3;
            part_score += counts[i] - count_c;
        }
        return part_score;
    }

//    private void write() {
//        String[] s = new String[36];
//        for (int i = 0; i < s.length; i++) {
//            s[i] = "";
//        }
//        for (int r = 0; r < 6; r++) {
//            for (int c = 0; c < 6; c++) {
//                s[bestTable[r][c]] = (r + 1) + " " + (c + 1);
//            }
//        }
//        StringBuilder sb = new StringBuilder();
//        for (int i = 0; i < 36; i++) {
//            sb.append(s[i]).append('\n');
//        }
//        System.out.print(sb.toString());
//        System.out.flush();
//    }
}

class SAState {
    public static final boolean useTime = true;
    public double startTime;
    public double endTime;
    public double time;
    public double startTemperature;
    public double endTemperature;
    public double inverseTemperature;
    public double lastAcceptTemperature;
    public double startRange;
    public double endRange;
    public double range;
    public int numIterations;
    public int validIterations;
    public int acceptIterations;
    private double[] log = new double[32768];

    public SAState() {
        for (int i = 0; i < log.length; i++) {
            log[i] = Math.log((i + 0.5) / log.length);
        }
    }

    public void init() {
        numIterations = 0;
        validIterations = 0;
        acceptIterations = 0;
        startTime = useTime ? Constants.watch.getSecond() : numIterations;
        update();
        lastAcceptTemperature = inverseTemperature;
    }

    public void update() {
        updateTime();
        updateTemperature();
    }

    public boolean useExp = !true;

    public void updateTemperature() {
        if (useExp) {
            double time0to1 = elapsedPercentage(startTime, endTime, time);
            double startY = startTemperature;
            double endY = endTemperature;
            double startX = Math.log(startY);
            double endX = Math.log(endY);
            double xStartToEnd = interpolate(startX, endX, time0to1);
            double temperature = Math.exp(xStartToEnd);
            inverseTemperature = 1.0 / temperature;
        } else {
            double time0to1 = elapsedPercentage(startTime, endTime, time);
            double startY = startTemperature;
            double endY = endTemperature;
            double temperature = interpolate(startY, endY, time0to1);
            inverseTemperature = 1.0 / temperature;
        }
    }

    private double elapsedPercentage(double min, double max, double v) {
        return (v - min) / (max - min);
    }

    private double interpolate(double v0, double v1, double d0to1) {
        return v0 + (v1 - v0) * d0to1;
    }

    public void updateRange() {
        range = endRange + (startRange - endRange) * Math.pow((endTime - time) / (endTime - startTime), 1.0);
    }

    public void updateTime() {
        time = useTime ? Constants.watch.getSecond() : numIterations;
    }

    public boolean isTLE() {
        return time >= endTime;
    }

    public boolean accept(double deltaScore) {
        return acceptB(deltaScore);
    }

    public boolean acceptB(double deltaScore) {
        validIterations++;
        if (deltaScore > -1e-9) {
            acceptIterations++;
            return true;
        }
        double d = deltaScore * inverseTemperature;
        if (d < -10) {
            return false;
        }
        if (log[Constants.RNG.nextInt() & 32767] < d) {
            acceptIterations++;
            lastAcceptTemperature = inverseTemperature;
            return true;
        }
        return false;
    }

    public boolean acceptS(double deltaScore) {
        validIterations++;
        if (deltaScore < 1e-9) {
            acceptIterations++;
            return true;
        }
        double d = -deltaScore * inverseTemperature;
        if (d < -10) {
            return false;
        }
        if (log[Constants.RNG.nextInt() & 32767] < d) {
            acceptIterations++;
            lastAcceptTemperature = inverseTemperature;
            return true;
        }
        return false;
    }
}

final class Utils {
    private Utils() {
    }

    public static final void debug(Object... o) {
        System.err.println(toString(o));
        System.err.flush();
    }

    public static final String toString(Object... o) {
        return Arrays.deepToString(o);
    }

    public static boolean isValid(int v, int min, int minUpper) {
        return v >= min && v < minUpper;
    }
}

class Watch {
    private long start;

    public Watch() {
        init();
    }

    public double getSecond() {
        return (System.nanoTime() - start) * 1e-9;
    }

    public void init() {
        init(System.nanoTime());
    }

    private void init(long start) {
        this.start = start;
    }

    public String getSecondString() {
        return toString(getSecond());
    }

    public static final String toString(double second) {
        if (second < 60) {
            return String.format("%5.2fs", second);
        } else if (second < 60 * 60) {
            int minute = (int) (second / 60);
            return String.format("%2dm%2ds", minute, (int) (second % 60));
        } else {
            int hour = (int) (second / (60 * 60));
            int minute = (int) (second / 60);
            return String.format("%2dh%2dm%2ds", hour, minute % (60), (int) (second % 60));
        }
    }
}

interface Constants {
    Watch watch = new Watch();
    PCG_XSH_RR RNG = new PCG_XSH_RR(System.nanoTime());
}

final class PCG_XSH_RR {
    private long state = 5342;

    public PCG_XSH_RR(final long state) {
        this.state = state;
    }

    public int nextInt() {
        final long oldstate = state;
        state = oldstate * 6364136223846793005L + 521L;
        final int xorshift = (int) (((oldstate >>> 18) ^ oldstate) >>> 27);
        final int rotation = (int) (oldstate >>> 59);
        return (xorshift >>> rotation) | (xorshift << (-rotation & 31));
    }

    public int nextInt(int n) {
        return (int) (n * nextDouble());
    }

    public double nextDouble() {
        return (nextInt() >>> 1) * 4.6566128730773926E-10;
    }
}
0