結果

問題 No.258 回転寿司(2)
ユーザー izuru_matsuura
提出日時 2016-10-18 17:37:50
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,036 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 150,416 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 04:34:00
合計ジャッジ時間 5,188 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 67
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.conv;
import std.algorithm;
import std.array;
import std.string;
import std.typecons;

void main() {
    int N = stdin.readln.chomp.to!int;
    auto V = stdin.readln.chomp.split(' ').map!(to!int).array;

    alias R = Tuple!(int, int);
    alias K = Tuple!(int, bool);
    R[K] memo;
    auto next = new int[N];
    next[] = -1;
    R dfs(int n, bool prev_used) {
        if (n == N) return R(0, -1);
        auto key = K(n, prev_used);
        if (key in memo) return memo[key];
        if (prev_used) return dfs(n + 1, false);
        auto a = dfs(n + 1, true);
        a[0] += V[n];
        auto b = dfs(n + 1, false);
        if (a[0] > b[0]) {
            next[n] = a[1];
            a[1] = n;
            memo[key] = a;
        } else {
            memo[key] = b;
        }
        return memo[key];
    }

    auto ans = dfs(0, false);
    writeln(ans[0]);

    int n = ans[1];
    int[] path;
    while (n >= 0) {
        path ~= n + 1;
        n = next[n];
    }

    writefln("%(%s %)", path);

}
0