[CF] B. Students in Railway Carriage - Educational Codeforces Round 42 (Rated for Div. 2)

https://codeforces.com/contest/962/problem/B

题目大意

给出 $n$ 个座位,以及一个 $n$ 长的序列给出每个座位是否可用。有 $a$ 个程序员,$b$ 个运动员。程序员不能相邻,运动员不能相邻,问最多可以让多少个人坐下。其中 $n, a, b \le 2 \times 10^5, a + b > 0$。

简要题解

首先对于不连续的段,如何安排是互不影响的,对于 $1$ 的段我们可以任意安排人,于是想到先安排长的段后安排短的段。对于偶数长度,先安排谁没关系,奇数长度则先安排当前多的即可(这样多的可以被多消耗一个)。

复杂度

$T$:$O(n \log n)$

$S$:$O(n)$

代码实现

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

int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();

using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;

template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }

// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());

/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

void solve() {
  int n, a, b; cin >> n >> a >> b;
  string s; cin >> s;

  vector<int> vec;
  for (int i = 0; i < n; ) {
    int j = i;
    while (j < n && s[i] == s[j]) j++;
    if (s[i] == '.') vec.push_back(j - i);
    i = j;
  }

  int ans = 0;
  sort(vec.rbegin(), vec.rend());

  for (int l : vec) {
    if (a < b) swap(a, b);
    int da = min(a, (l + 1) / 2), db = min(b, l / 2);
    a -= da;
    b -= db;
    ans += da + db;
  }

  cout << ans << '\n';
}

int main() {
  int t = 1; 
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] D. Pythagorean Triples - Educational Codeforces Round 104 (Rated for Div. 2)
Next: [CF] C. Make a Square - Educational Codeforces Round 42 (Rated for Div. 2)