結果
| 問題 |
No.808 Kaiten Sushi?
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-03-22 23:14:19 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 189 ms / 2,000 ms |
| コード長 | 2,803 bytes |
| コンパイル時間 | 2,516 ms |
| コンパイル使用メモリ | 221,408 KB |
| 実行使用メモリ | 29,652 KB |
| 最終ジャッジ日時 | 2024-06-13 04:25:45 |
| 合計ジャッジ時間 | 7,716 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 56 |
ソースコード
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;
}