結果

問題 No.505 カードの数式2
ユーザー kokatsukokatsu
提出日時 2022-12-22 21:17:13
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 1,099 bytes
コンパイル時間 2,079 ms
コンパイル使用メモリ 206,720 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 19:24:44
合計ジャッジ時間 4,130 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,376 KB
testcase_09 WA -
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 WA -
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 WA -
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 RE -
testcase_20 AC 1 ms
4,376 KB
testcase_21 RE -
testcase_22 WA -
testcase_23 WA -
testcase_24 RE -
testcase_25 WA -
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;
void main() {
    long N;
    readf("%d\n", N);

    auto a = readln.chomp.split.to!(long[]);

    auto dp = new long[][][](N, 4, 2);
    foreach (i; 0 .. 4) dp[0][i][] = a.front;
    a.popFront;

    foreach (i; 1 .. N) {
        foreach (j; 0 .. 4) {
            dp[i][j][0] = long.min;
            dp[i][j][1] = long.max;
        }
    }

    void f(ref long x, long y) {
        if (x < y) x = y;
    }

    void g(ref long x, long y) {
        if (x > y) x = y;
    }

    foreach (i, x; a) {
        foreach (j; 0 .. 4) {
            foreach (k; 0 .. 2) {
                f(dp[i+1][0][0], dp[i][j][k]+x);
                f(dp[i+1][1][0], dp[i][j][k]-x);
                f(dp[i+1][2][0], dp[i][j][k]*x);
                if (x != 0) f(dp[i+1][3][0], dp[i][j][k]/x);

                g(dp[i+1][0][1], dp[i][j][k]+x);
                g(dp[i+1][1][1], dp[i][j][k]-x);
                g(dp[i+1][2][1], dp[i][j][k]*x);
                if (x != 0) g(dp[i+1][3][1], dp[i][j][k]/x);
            }
        }
    }

    auto res = dp[N-1].map!(x => x.maxElement).maxElement;
    res.writeln;
}
0