結果
| 問題 |
No.2553 Holidays
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-11-20 23:30:15 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,440 bytes |
| コンパイル時間 | 3,978 ms |
| コンパイル使用メモリ | 185,984 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-26 10:19:57 |
| 合計ジャッジ時間 | 5,570 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 WA * 18 |
ソースコード
import std;
void main () {
int N, M; readln.read(N, M);
string S = readln.chomp;
solve(N, M, S);
}
void solve (int N, int M, string S) {
/*
'x'や'o'のあるところで区切って良い。本質的に、区切られた文字は3種類のみ
1. x --- x
2. x --- o
3. o --- o
このうち、最も優先すべきはタイプ3で、かつ-の長さが奇数のもの。これは最高効率で埋めることができる可能性がある。
次に優先度が高いのが長さ2以上の2と、3かつ-の長さが偶数のもの。これらは確実に1個で2つの祝日を生み出せる。
次に優先度が高いのが長さ3以上の1
一番優先度が低いのが長さ2以下の1と長さ1の2
これらを守って処理すれば必ず最高効率になるはず
*/
/* 区間分割 */
auto T = new BinaryHeap!(Array!int, "a<b")[](3);
int l = 0;
int r = 0;
while (true) {
if (S.length <= l) break;
if (S[l] == '-') {
int type = 0;
r = l;
if (0 < r && S[r-1] == 'o') type++;
while (true) {
if (r == S.length || S[r] == 'x') {
break;
}
if (S[r] == 'o') {
type++;
break;
}
r++;
}
T[type].insert(r-l);
l = r;
}
l++;
}
int ans = 0;
foreach (c; S) if (c == 'o') ans++;
/* 処理 */
while (0 < M || !T[2].empty) {
// dbg
/*
foreach (t; T) writeln(t.dup);
writeln("M : ", M);
writeln("");
*/
if (!T[2].empty) {
auto head = T[2].front; T[2].removeFront;
if (head == 1) { ans++; continue; }
ans += min(head, 2*M);
M -= min(head/2, M);
continue;
}
if (!T[1].empty && 2 <= T[1].front) {
auto head = T[1].front; T[1].removeFront;
if (head % 2 == 0) {
ans += min(head, 2*M);
M -= min(head/2, M);
}
else {
ans += min(head-1, 2*M);
M -= min(head/2, M);
head -= min(head-1, 2*M);
T[1].insert(head);
}
continue;
}
if (!T[0].empty && 3 <= T[0].front) {
auto head = T[0].front; T[0].removeFront;
ans++;
M--;
T[1].insert(head-1);
continue;
}
/* 優先度最悪 */
if (!T[0].empty && T[0].front <= 2) {
if (T[0].front <= M) {
ans += T[0].front;
M -= T[0].front;
T[0].removeFront;
}
else {
ans += M;
auto head = T[0].front - M;
T[0].removeFront;
T[1].insert(head);
M = 0;
}
continue;
}
if (!T[1].empty && T[1].front == 1) {
T[1].empty;
ans++;
M--;
continue;
}
/* 選択できるやつがもうない */
break;
}
writeln(ans);
}
void read(T...)(string S, ref T args) {
auto buf = S.split;
foreach (i, ref arg; args) {
arg = buf[i].to!(typeof(arg));
}
}