結果

問題 No.953 席
ユーザー nebukuro09nebukuro09
提出日時 2019-12-17 04:44:39
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 2,863 bytes
コンパイル時間 2,583 ms
コンパイル使用メモリ 180,196 KB
実行使用メモリ 26,908 KB
最終ジャッジ日時 2023-09-04 04:08:24
合計ジャッジ時間 10,585 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 229 ms
22,788 KB
testcase_03 AC 226 ms
23,740 KB
testcase_04 AC 224 ms
22,744 KB
testcase_05 AC 214 ms
22,872 KB
testcase_06 AC 229 ms
22,884 KB
testcase_07 AC 250 ms
20,360 KB
testcase_08 AC 271 ms
25,912 KB
testcase_09 AC 218 ms
19,420 KB
testcase_10 AC 74 ms
13,368 KB
testcase_11 AC 200 ms
15,484 KB
testcase_12 AC 252 ms
25,064 KB
testcase_13 AC 275 ms
25,652 KB
testcase_14 AC 248 ms
23,964 KB
testcase_15 AC 247 ms
24,884 KB
testcase_16 AC 245 ms
25,912 KB
testcase_17 AC 267 ms
26,840 KB
testcase_18 AC 253 ms
25,256 KB
testcase_19 AC 285 ms
26,796 KB
testcase_20 AC 247 ms
26,348 KB
testcase_21 AC 231 ms
22,976 KB
testcase_22 AC 262 ms
26,512 KB
testcase_23 AC 235 ms
22,752 KB
testcase_24 AC 240 ms
24,036 KB
testcase_25 AC 285 ms
26,908 KB
testcase_26 AC 204 ms
22,428 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;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K1 = s[1] - 1;
    auto K2 = s[2] - 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-K1)==abs(b-K1)?abs(a-K2)<abs(b-K2):abs(a-K1)<abs(b-K1));
    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;
    }

    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