結果

問題 No.953 席
ユーザー nebukuro09nebukuro09
提出日時 2019-12-17 04:22:11
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,901 bytes
コンパイル時間 2,168 ms
コンパイル使用メモリ 185,984 KB
実行使用メモリ 28,124 KB
最終ジャッジ日時 2023-09-04 04:07:51
合計ジャッジ時間 10,559 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 -
権限があれば一括ダウンロードができます

ソースコード

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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K1 = s[1] - 1;
    auto K2 = s[2] - 1;
    float offset = K1 < K2 ? K1 + 0.1 : K2 - 0.1;
    auto Q = readln.chomp.to!int;
    auto A = iota(Q).map!(_ => readln.split.map!(to!long).array).array;

    int[] events;
    alias PQ = BinaryHeap!(Array!(Tuple!(int, long)), "a[1] > b[1]");
    auto pq1 = new PQ;
    auto pq2 = new PQ;
    foreach (i; 0..Q) pq1.insert(tuple(i, A[i][0]));
    long time = 0;

    while (!pq1.empty) {
        while (!pq2.empty && pq2.front[1] <= pq1.front[1]) {
            auto idx = pq2.front[0];
            events ~= -(idx + 1);
            pq2.removeFront;
        }
        if (pq2.length < N) {
            auto idx = pq1.front[0];
            auto t = pq1.front[1];
            time = max(time, t);
            events ~= idx + 1;
            pq1.removeFront;
            pq2.insert(tuple(idx, time + A[idx][1]));
        } else {
            auto t = pq2.front[1];
            while (!pq2.empty && pq2.front[1] == t) {
                auto idx = pq2.front[0];
                time = max(time, t);
                events ~= -(idx + 1);
                pq2.removeFront;
            }
        }
    }

    auto seats = new bool[](N);
    alias SET = RedBlackTree!(int, (a,b)=>abs(a-offset)<abs(b-offset));
    auto set1 = new SET;
    auto set2 = new SET;
    foreach (i; 0..N) set1.insert(i);
    foreach (i; 0..N) set2.insert(i);

    auto ans = new int[](Q);

    bool both_empty(int a) {
        bool left_empty = a == 0 || !seats[a-1];
        bool right_empty = a == N - 1 || !seats[a+1];
        return left_empty && right_empty;
    }

    events.writeln;

    foreach (e; events) {
        int idx = abs(e) - 1;
        if (e > 0) {
            int a;
            if (!set1.empty) {
                a = set1.front;
                ans[idx] = a;
                seats[a] = true;
                set1.removeKey(a);
                set2.removeKey(a);
            } else {
                a = set2.front;
                ans[idx] = a;
                seats[a] = true;
                set2.removeKey(a);
            }
            if (a > 0 && !set1.equalRange(a-1).empty) {
                set1.removeKey(a-1);
            }
            if (a < N - 1 && !set1.equalRange(a+1).empty) {
                set1.removeKey(a+1);
            }
        } else {
            auto a = ans[idx];
            seats[a] = false;
            set2.insert(a);
            if (both_empty(a)) set1.insert(a);
            if (a > 0 && !seats[a-1] && both_empty(a-1)) set1.insert(a-1);
            if (a < N - 1 && !seats[a+1] && both_empty(a+1)) set1.insert(a+1);
        }
    }

    ans.map!(i => i + 1).each!writeln;
}


0