ABC370

Sep 9, 2024

Problem A

There are three cases total, if both L and R are 0 or 1, output “Invalid”, if L is 1 and R is 0 output “Yes” other cases(L is 0 and R is 1) output “No”.

#include <bits/stdc++.h>
using namespace std;
int main() {
  int l, r;
  cin >> l >> r;
  if (r - l == 0) {
    cout << "Invalid" << endl;
  } else if (l - r == 1) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}

Problem B

At first I didn’t understand the meaning of the question, stranger problems. The “element” transform with (means j) the previous results are incrementing from 1 each time.

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector <vector<int> > a(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      cin >> a[i][j];
    }
  }
  int ans = 1;
  for (int i = 1; i <= n; ++i) {
    if (ans >= i) {
      ans = a[ans - 1][i - 1];
    } else {
      ans = a[i - 1][ans - 1];
    }
  }
  cout << ans << endl;
  return 0;
}

Problem C

Modify the string S one letter at a time until it matches the string T. Save each modified string in an array X, maintaining lexicographically smallest of X. I used greedy algorithm to solve.

#include <bits/stdc++.h>
using namespace std;
string s, t;
vector<string> x;
int main() {
  cin >> s >> t;
  vector<int> a, b;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == t[i]) {
      continue;
    } else if (s[i] > t[i]) {
      a.push_back(i);
    } else {
      b.push_back(i);
    }
  }
  reverse(b.begin(), b.end());
  for (int i : b) {
    a.push_back(i);
  }
  cout << a.size() << endl;
  for (int i : a) {
    s[i] = t[i];
    cout << s << endl;
  }

  return 0;
}

Problem D

There is a grid where all the cells initially have walls. Each time a cell position is queried, if there is a wall, it destroys the wall at that position; if there is no wall, it destroys the walls at the positions above, below, left, and right. I didn’t realize using set to solve, so I lost…

#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, q;
  cin >> h >> w >> q;
  int ans = h * w;
  vector<set<int>> col(w + 1), row(h + 1);
  vector<vector<bool>> kb(h + 1, vector<bool>(w + 1, true));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      col[j].insert(i);
      row[i].insert(j);
    }
  }
  for (int i = 0; i < q; i++) {
    int r, c;
    cin >> r >> c;
    if (kb[r][c]) {
      ans--;
      kb[r][c] = false;
      col[c].erase(r);
      row[r].erase(c);
      continue;
    }
    auto up = col[c].upper_bound(r), down = up;
    auto left = row[r].upper_bound(c), right = left;
    if (up != col[c].begin()) {
      up--;
      ans--;
      kb[*up][c] = false;
      col[c].erase(up);
      row[*up].erase(c);
    }
    if (down != col[c].end()) {
      ans--;
      kb[*down][c] = false;
      col[c].erase(down);
      row[*down].erase(c);
    }
    if (left != row[r].begin()) {
      left--;
      ans--;
      kb[r][*left] = false;
      col[*left].erase(r);
      row[r].erase(left);
    }
    if (right != row[r].end()) {
      ans--;
      kb[r][*right] = false;
      col[*right].erase(r);
      row[r].erase(right);
    }
  }
  cout << ans << endl;
  return 0;
}