[CF] C. You Are Given a WASD-string... - Educational Codeforces Round 70 (Rated for Div. 2)

https://codeforces.com/contest/1202/problem/C

题目大意

给出一个 $WSAD$ 操作序列 $S \ (|S| \le 2 \times 10^5)$,其中

  1. $W$ 为 $x = x - 1$
  2. $A$ 为 $y = y - 1$
  3. $S$ 为 $x = x + 1$
  4. $D$ 为 $y = y + 1$

可以在任意位置加入最多一个字符,使得机器人运行这个序列而不移出边界所需的矩形框面积最小。

简要题解

这题分高大概就是写起来麻烦。

考虑对于某序列最小的矩形框为这个序列最左最右最上最下为边界的框。考虑一个序列正着走和反着走得到的框大小其实是一样的,如果想要得到完全一样的框范围,则反着走,并把 $WS$ 对换,$AD$ 对换即可。

于是我们可以从左到右和从右到左各自维护一套边界值,再枚举插入的位置合并即可。

复杂度

$T$:$O(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
*/

map<char, int> dx, dy;

void init() {
  dx['W'] = -1; dy['W'] = 0;
  dx['A'] = 0; dy['A'] = -1;
  dx['S'] = 1; dy['S'] = 0;
  dx['D'] = 0; dy['D'] = 1;
}

void solve() {
  string s; cin >> s;
  int n = s.length();
  vector<int> lmnx(n + 1), lmxx(n + 1), lmny(n + 1), lmxy(n + 1);
  vector<int> rmnx(n + 2), rmxx(n + 2), rmny(n + 2), rmxy(n + 2);

  for (int i = 0; i < n; i++) {
    lmnx[i + 1] = lmnx[i] - dx[s[i]];
    if (lmnx[i + 1] > 0) lmnx[i + 1] = 0;
    lmxx[i + 1] = lmxx[i] - dx[s[i]];
    if (lmxx[i + 1] < 0) lmxx[i + 1] = 0;
    lmny[i + 1] = lmny[i] - dy[s[i]];
    if (lmny[i + 1] > 0) lmny[i + 1] = 0;
    lmxy[i + 1] = lmxy[i] - dy[s[i]];
    if (lmxy[i + 1] < 0) lmxy[i + 1] = 0;
  }

  for (int i = n; i >= 1; i--) {
    rmnx[i] = rmnx[i + 1] + dx[s[i - 1]];
    if (rmnx[i] > 0) rmnx[i] = 0;
    rmxx[i] = rmxx[i + 1] + dx[s[i - 1]];
    if (rmxx[i] < 0) rmxx[i] = 0;
    rmny[i] = rmny[i + 1] + dy[s[i - 1]];
    if (rmny[i] > 0) rmny[i] = 0;
    rmxy[i] = rmxy[i + 1] + dy[s[i - 1]];
    if (rmxy[i] < 0) rmxy[i] = 0;
  }

  LL ans = 1LL * (lmxx[n] - lmnx[n] + 1) * (lmxy[n] - lmny[n] + 1);

  for (int i = 1; i < n; i++) { // insert between i and i + 1
    for (char c : {'W', 'A', 'S', 'D'}) {
      int mnx = rmnx[i + 1] + dx[c];
      if (mnx > 0) mnx = 0;
      int mxx = rmxx[i + 1] + dx[c];
      if (mxx < 0) mxx = 0;
      int mny = rmny[i + 1] + dy[c];
      if (mny > 0) mny = 0;
      int mxy = rmxy[i + 1] + dy[c];
      if (mxy < 0) mxy = 0;

      cmin(mnx, lmnx[i]);
      cmax(mxx, lmxx[i]);
      cmin(mny, lmny[i]);
      cmax(mxy, lmxy[i]);

      cmin(ans, 1LL * (mxx - mnx + 1) * (mxy - mny + 1));
    }
  }
  cout << ans << '\n';
}

int main() {
  init();
  int t = 1; 
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] C. Game with Multiset - Educational Codeforces Round 160 (Rated for Div. 2)
Next: [CF] D. Print a 1337-string... - Educational Codeforces Round 70 (Rated for Div. 2)