結果

問題 No.2553 Holidays
ユーザー Nzt3
提出日時 2024-01-11 23:24:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,596 bytes
コンパイル時間 2,877 ms
コンパイル使用メモリ 203,948 KB
最終ジャッジ日時 2025-02-18 17:20:39
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, M;
  cin >> N >> M;
  string S;
  cin >> S;
  S += 'x';
  vector oo(0, 0), ox(0, 0), xx(0, 0);
  int cnt1 = 0;
  ll ans = 0;
  for (int i = 0, o = 0, cnt = 0; i <= N; i++) {
    if (S[i] != '-') {
      if (S[i] == 'o') ans += 1;
      if (cnt == 0) {
        o = (S[i] == 'o' ? 1 : 0);
        continue;
      }
      if (S[i] == 'o') o += 1;
      switch (o) {
        case 0:
          xx.push_back(cnt);
          break;
        case 1:
          if (cnt == 0)
            cnt1 += 1;
          else
            ox.push_back(cnt);
          break;
        case 2:
          oo.push_back(cnt);
          break;
      }
      cnt = 0;
      o = (S[i] == 'o' ? 1 : 0);
    } else {
      cnt += 1;
    }
  }
  sort(oo.begin(), oo.end(), [](int a, int b) {
    if (a % 2 != b % 2) {
      return (a % 2 == 1);
    }
    return a < b;
  });
  sort(ox.begin(), ox.end());
  sort(xx.begin(), xx.end(), greater<int>());
  for (int i : oo) {
    if (i / 2 <= M) {
      ans += i;
      M -= i / 2;
    } else {
      ans += M * 2;
      M = 0;
    }
  }
  for (int i : ox) {
    if (i / 2 <= M) {
      ans += i - i % 2;
      M -= i / 2;
      cnt1 += i % 2;
    } else {
      ans += M * 2;
      M = 0;
    }
  }
  for (int i : xx) {
    if ((i + 1) / 2 <= M) {
      cnt1 += 1 - i % 2;
      ans += i - 1 + i % 2;
      M -= (i + 1) / 2;
    } else if (M > 0) {
      ans += M * 2 - 1;
      M = 0;
    }
  }
  ans += min(M, cnt1);
  cout << ans << '\n';
}
0