結果

問題 No.808 Kaiten Sushi?
ユーザー nebukuro09nebukuro09
提出日時 2019-03-22 23:14:19
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 2,803 bytes
コンパイル時間 1,786 ms
コンパイル使用メモリ 215,568 KB
実行使用メモリ 30,632 KB
最終ジャッジ日時 2023-09-03 23:53:35
合計ジャッジ時間 7,408 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 4 ms
4,380 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 115 ms
21,244 KB
testcase_24 AC 91 ms
15,200 KB
testcase_25 AC 94 ms
21,152 KB
testcase_26 AC 91 ms
19,804 KB
testcase_27 AC 170 ms
24,044 KB
testcase_28 AC 51 ms
12,200 KB
testcase_29 AC 162 ms
23,488 KB
testcase_30 AC 104 ms
19,376 KB
testcase_31 AC 180 ms
26,816 KB
testcase_32 AC 205 ms
30,632 KB
testcase_33 AC 102 ms
19,348 KB
testcase_34 AC 117 ms
20,364 KB
testcase_35 AC 147 ms
24,076 KB
testcase_36 AC 101 ms
20,496 KB
testcase_37 AC 1 ms
4,384 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 114 ms
22,984 KB
testcase_40 AC 27 ms
6,540 KB
testcase_41 AC 39 ms
11,448 KB
testcase_42 AC 102 ms
21,840 KB
testcase_43 AC 52 ms
13,068 KB
testcase_44 AC 76 ms
15,652 KB
testcase_45 AC 53 ms
13,008 KB
testcase_46 AC 31 ms
10,056 KB
testcase_47 AC 133 ms
22,036 KB
testcase_48 AC 132 ms
23,304 KB
testcase_49 AC 133 ms
22,048 KB
testcase_50 AC 133 ms
23,116 KB
testcase_51 AC 132 ms
23,032 KB
testcase_52 AC 67 ms
12,168 KB
testcase_53 AC 92 ms
12,680 KB
testcase_54 AC 1 ms
4,376 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 29 ms
7,844 KB
testcase_58 AC 48 ms
8,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long MOD = 10^^9 + 7;

void main() {
    auto s = readln.split;
    auto N = s[0].to!int;
    auto L = s[1].to!long;
    auto A = readln.split.map!(to!long).array;
    auto B = readln.split.map!(to!long).array;

    Tuple!(int, int, int, long)[] C;
    for (int i = 0, j = 0; i < N || j < N; ) {
        if (j >= N || (i < N && A[i] < B[j])) {
            if (!C.empty && C.back[2] == 0) {
                C.back[1] = i;
                C.back[3] = A[i];
            } else {
                C ~= tuple(i, i, 0, A[i]);
            }
            ++i;
        } else {
            if (!C.empty && C.back[2] == 1) {
                C.back[1] = j;
                C.back[3] = B[j];
            } else {
                C ~= tuple(j, j, 1, B[j]);
            }
            ++j;
        }
    }

    if (C.back[2] == C.front[2]) {
        auto t = C.back;
        C.popBack;
        C.front[0] = t[0];
    }

    auto rbt = new RedBlackTree!(Tuple!(int[], int, long), "a[2] < b[2]");
    foreach (c; C) {
        if (c[1] >= c[0]) {
            rbt.insert(tuple(iota(c[0], c[1]+1).array, c[2], c[3]));
        } else {
            rbt.insert(tuple(iota(c[0], N).array ~ iota(0, c[1]+1).array, c[2], c[3]));
        }
    }

    long ans = 0;
    long pos = 0;
    int turn = 0;
    auto hoge = new int[](0);

    foreach (_; 0..2*N) {
        auto ub = rbt.upperBound(tuple(hoge, 0, pos));

        if (ub.empty) {
            ub = rbt.upperBound(tuple(hoge, 0, 0L));
        }

        if (turn != ub.front[1]) {
            ub.popFront;
            if (ub.empty) ub = rbt.upperBound(tuple(hoge, 0, 0L));
        }

        auto tar = ub.front;

        long next = tar[2];
        if (pos < next) ans += next - pos;
        else ans += L - pos + next;

        debug {
            writeln([pos, next, ans]);
        }

        pos = next;
        if (_ == 2 * N - 1) break;


        rbt.removeKey(tar);
        if (tar[0].length == 1) {
            auto x = rbt.upperBound(tar);
            auto y = rbt.lowerBound(tar);
            auto n = x.empty ? rbt.front : x.front;
            auto p = y.empty ? rbt.back : y.back;
            if (n[2] != p[2]) {
                rbt.removeKey(n);
                rbt.removeKey(p);
                rbt.insert(tuple(p[0] ~ n[0], p[1], n[2]));
            }
        } else {
            tar[0].popBack;
            if (tar[1] == 0) tar[2] = A[tar[0].back];
            else tar[2] = B[tar[0].back];
            rbt.insert(tar);
        }

        debug {
            _.writeln;
            rbt.writeln;
        }

        turn ^= 1;
    }

    ans.writeln;
}
0