alexjercan/bugnet huggingface dataset:
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <sys/time.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using i_i = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)
const ll mod = 1000000007LL;

int main() {
  int h, w, m;

  cin >> h >> w >> m;

  set<i_i> ps;

  map<int, int> xs;
  map<int, int> ys;

  rep(i, m) {
    int y, x;

    cin >> y >> x;
    ps.insert(mkp(y, x));

    ys[y]++;
    xs[x]++;
  }
  int y_max = 0;
  int x_max = 0;
  for (auto cur : ys) {
    y_max = max(y_max, cur.second);
  }
  for (auto cur : xs) {
    x_max = max(x_max, cur.second);
  }

  vector<int> xps;
  vector<int> yps;

  for (auto cur : ys) {
    if (cur.second == y_max) {
      yps.push_back(cur.first);
    }
  }
  for (auto cur : xs) {
    if (cur.second == x_max) {
      xps.push_back(cur.first);
    }
  }

  for (int y : yps) {
    for (int x : xps) {
      if (ps.find(mkp(y, x)) == ps.end()) {
        cout << x_max + y_max << endl;
        return 0;
      }
    }
  }

  cout << x_max + y_max - 1 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <sys/time.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<int, int>;
using VP = pair<int, pair<int, int>>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)
const ll mod = 1000000007LL;

int main() {
  int h, w, m;

  cin >> h >> w >> m;

  vector<set<int>> ys(w);
  vector<set<int>> xs(h);
  rep(i, m) {
    int ty, tx;

    cin >> ty >> tx;
    ty--;
    tx--;
    ys[tx].insert(ty);
    xs[ty].insert(tx);
  }
  int max_ys = 0;
  int max_xs = 0;
  rep(i, w) { max_ys = max((int)ys[i].size(), max_ys); }
  rep(i, h) { max_xs = max((int)xs[i].size(), max_xs); }

  int ans = 0;
  rep(x, w) {

    if (ys[x].size() != max_ys) {
      continue;
    }

    rep(y, h) {
      if (xs[y].size() != max_xs) {
        continue;
      }
      int cur_ans = ys[x].size() + xs[y].size();

      if (xs[y].find(x) != xs[y].end()) {
        cur_ans--;
      }
      ans = max(ans, cur_ans);
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ll long long
#define F first
#define S second
#define ii pair<int, int>
#define ever (;;)

const int N = 300100;

int n, m, k, ans;
map<int, int> row, col;
vector<int> v[N];
multiset<int> s;

int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    row[x]++;
    col[y]++;
    v[x].push_back(y);
  }

  for (auto x : col)
    s.insert(x.S);

  for (auto X : row) {
    int mx = 0;

    for (auto x : v[X.F]) {
      s.erase(s.find(col[x]));
      s.insert(col[x] - 1);
    }

    ans = max(ans, X.S + (*s.rbegin()));

    for (auto x : v[X.F]) {
      s.erase(s.find(col[x] - 1));
      s.insert(col[x]);
    }
  }

  printf("%d\\n", ans);
}


Error: TLE

Fixed submission:

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

#define ll long long
#define F first
#define S second
#define ii pair<int, int>
#define ever (;;)

const int N = 300100;

int n, m, k, ans;
map<int, int> row, col;
set<int> s;
vector<int> v[N];

int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= k; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    row[x]++;
    col[y]++;
    s.insert({x, y});
    v[x].push_back(y);
  }

  for (auto X : row) {
    int mx = 0;

    for (auto x : v[X.F])
      col[x]--;

    for (auto x : col)
      mx = max(mx, x.S);

    ans = max(ans, mx + X.S);

    for (auto x : v[X.F])
      col[x]++;
  }

  printf("%d\\n", ans);
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (ll i = 1; i < n; ++i)
#define mrep(i, n) for (ll i = n; i >= 0; --i)
#define all(a) (a).begin(), (a).end()
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define pl pair<ll, ll>
#define inf 1001001001001001000
#define mod 1000000007
// #define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<pl> ngo;
  vl tate(h, 0);
  vl yoko(w, 0);
  ll tatemax = 0;
  ll yokomax = 0;
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    ngo.push_back({a, b});
    tate[a]++;
    yoko[b]++;
    tatemax = max(tatemax, tate[a]);
    yokomax = max(yokomax, yoko[b]);
  }
  vl tatems, yokoms;
  rep(i, h) if (tate[i] == tatemax) tatems.push_back(i);
  rep(i, w) if (yoko[i] == yokomax) yokoms.push_back(i);
  ll kosu = (ll)(tatems.size()) * (ll)(yokoms.size());
  rep(i, m) {
    if (binary_search(all(tatems), ngo[i].first) &&
        binary_search(all(yokoms), ngo[i].second)) {
      kosu--;
    }
  }
  if (kosu == 0) {
    cout << (tatemax + yokomax - 1) << endl;
  } else {
    cout << (tatemax + yokomax) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep1(i, n) for (ll i = 1; i < n; ++i)
#define mrep(i, n) for (ll i = n; i >= 0; --i)
#define all(a) (a).begin(), (a).end()
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define pl pair<ll, ll>
#define inf 1001001001001001000
#define mod 1000000007
// #define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vvb board(h, vb(w, false));
  vl tate(h, 0);
  vl yoko(w, 0);
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    board[a][b] = true;
    tate[a]++;
    yoko[b]++;
  }
  ll ans = 0;
  rep(i, h) rep(j, w) { ans = max(ans, tate[i] + yoko[j] - board[i][j]); }
  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 23;

int r[maxn], c[maxn], w[maxn];
int n, m, k;
pii a[maxn];
set<int> s[maxn];
#define x first
#define y second

int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &a[i].first, &a[i].second);
    r[a[i].x]++;
    c[a[i].y]++;
    s[a[i].x].insert(a[i].y);
  }
  vector<int> q, w;
  for (int i = 1, mx = *max_element(r + 1, r + n + 1); i <= n; i++) {
    if (r[i] == mx)
      q.push_back(i);
  }
  for (int i = 1, mx = *max_element(c + 1, c + m + 1); i <= m; i++) {
    if (c[i] == mx)
      w.push_back(i);
  }
  int ans = 0;
  for (int i : q) {
    for (int j : w) {
      if (!s[i].count(j)) {
        ans = r[i] + c[j];
        cout << ans;
        return 0;
      } else {
        ans = r[i] + c[j] - 1;
      }
    }
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 23;

int r[maxn], c[maxn], w[maxn];
int n, m, k;
pii a[maxn];
#define x first
#define y second

int main() {
  cin >> n >> m >> k;
  vector<vector<int>> vis(n + 1, vector<int>(m + 1));
  for (int i = 0; i < k; i++) {
    scanf("%d%d", &a[i].first, &a[i].second);
    r[a[i].x]++;
    c[a[i].y]++;
    vis[a[i].x][a[i].y] = 1;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ans = max(ans, r[i] + c[j] - vis[i][j]);
    }
  }
  cout << ans;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
#define N 300300
pair<int, int> v[N];
pair<int, int> l[N], c[N];
int i, x, y, ml, mc, h, w, n, ii, jj, j;
long long cntc, cntl, ans;
map<pair<int, int>, bool> sel;
int main() {
  cin >> h >> w >> n;
  for (i = 0; i < n; ++i) {
    cin >> v[i].first >> v[i].second;
    sel[v[i]] = 1;
  }
  for (i = 0; i < h; ++i)
    l[i + 1].second = i + 1;
  for (i = 0; i < w; ++i)
    c[i + 1].second = i + 1;

  for (i = 0; i < n; ++i) {
    ++l[v[i].first].first;
    ++c[v[i].second].first;
  }
  sort(l + 1, l + h + 1);
  sort(c + 1, c + w + 1);
  i = h;
  j = w;
  ans = l[i].first + c[j].first;
  cntl = 0;
  cntc = 0;
  while (i > 0 && l[i].first == l[h].first) {
    --i;
    ++cntl;
  }
  while (j > 0 && c[j].first == c[w].first) {
    --j;
    ++cntc;
  }
  if (cntc * cntl > n) {
    cout << ans;
    return 0;
  } else {
    for (ii = 0; ii < cntl; ++ii)
      for (jj = 0; jj < cntc; ++jj) {
        if (!sel[make_pair(l[h - ii].second, c[w - jj].second)]) {
          cout << ans;
          return 0;
        }
      }
  }
  cout << ans - 1;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define N 200200
pair<int, int> v[N];
pair<int, int> l[N], c[N];
int i, x, y, ml, mc, h, w, n, ii, jj, j;
long long cntc, cntl, ans;
map<pair<int, int>, bool> sel;
int main() {
  cin >> h >> w >> n;
  for (i = 0; i < n; ++i) {
    cin >> v[i].first >> v[i].second;
    sel[v[i]] = 1;
  }
  for (i = 0; i < h; ++i)
    l[i + 1].second = i + 1;
  for (i = 0; i < w; ++i)
    c[i + 1].second = i + 1;

  for (i = 0; i < n; ++i) {
    ++l[v[i].first].first;
    ++c[v[i].second].first;
  }
  sort(l + 1, l + h + 1);
  sort(c + 1, c + w + 1);
  i = h;
  j = w;
  ans = l[i].first + c[j].first;
  cntl = 0;
  cntc = 0;
  while (i > 0 && l[i].first == l[h].first) {
    --i;
    ++cntl;
  }
  while (j > 0 && c[j].first == c[w].first) {
    --j;
    ++cntc;
  }
  if (cntc * cntl > n) {
    cout << ans;
    return 0;
  } else {
    for (ii = 0; ii < cntl; ++ii)
      for (jj = 0; jj < cntc; ++jj) {
        if (!sel[make_pair(l[h - ii].second, c[w - jj].second)]) {
          cout << ans;
          return 0;
        }
      }
  }
  cout << ans - 1;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int h, w, m, b, bb;
  cin >> h >> w >> m;
  map<pair<int, int>, bool> a;
  map<int, int> tate;
  map<int, int> yoko;
  for (int i = 0; i < m; i++) {
    cin >> b >> bb;
    a[make_pair(b - 1, bb - 1)] = true;
    tate[b - 1]++;
    yoko[bb - 1]++;
  }
  b = 0, bb = 0;
  set<int> mh;
  set<int> mw;
  for (auto x : tate)
    b = max(b, x.second);
  for (auto x : tate)
    if (x.second == b)
      mh.insert(x.first);
  for (auto x : yoko)
    bb = max(bb, x.second);
  for (auto x : yoko)
    if (x.second == bb)
      mw.insert(x.first);
  bool ok = false;
  for (auto x : mh) {
    for (auto y : mw) {
      if (!a[make_pair(x, y)]) {
        ok = true;
        break;
      }
    }
    if (ok)
      break;
  }
  cout << b + bb - (ok ? 0 : 1) << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int h, w, m, b, bb;
  cin >> h >> w >> m;
  vector<vector<bool>> a(h, vector<bool>(w, false));
  map<int, int> tate;
  map<int, int> yoko;
  for (int i = 0; i < m; i++) {
    cin >> b >> bb;
    a.at(b - 1).at(bb - 1) = true;
    tate[b - 1]++;
    yoko[bb - 1]++;
  }
  b = 0, bb = 0;
  set<int> mh;
  set<int> mw;
  for (auto x : tate)
    b = max(b, x.second);
  for (auto x : tate)
    if (x.second == b)
      mh.insert(x.first);
  for (auto x : yoko)
    bb = max(bb, x.second);
  for (auto x : yoko)
    if (x.second == bb)
      mw.insert(x.first);
  bool ok = false;
  for (auto x : mh) {
    for (auto y : mw) {
      if (!a.at(x).at(y)) {
        ok = true;
        break;
      }
    }
    if (ok)
      break;
  }
  cout << b + bb - (ok ? 0 : 1) << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  const int MAX = 300000;
  int H, W, M;
  cin >> H >> W >> M;
  int h[MAX], w[MAX] = {0};
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }

  // Create unordered map to reference the existence of a bomb later
  // The key cannot be pair as long as I don't implement a has function for pair
  unordered_map<string, int> map;

  int rows[MAX], cols[MAX] = {0};

  int row_best_count = 0;
  int col_best_count = 0;

  for (int i = 0; i < M; i++) {
    string key = to_string(h[i]) + ',' + to_string(w[i]);
    map[key] = 1;

    rows[h[i]]++;
    cols[w[i]]++;

    row_best_count = max(row_best_count, rows[h[i]]);
    col_best_count = max(col_best_count, cols[w[i]]);
  }

  vector<int> best_rows;
  vector<int> best_cols;

  for (int i = 0; i < H; i++) {
    if (rows[i] == row_best_count) {
      best_rows.push_back(i);
    }
  }

  for (int i = 0; i < W; i++) {
    if (cols[i] == col_best_count) {
      best_cols.push_back(i);
    }
  }

  int ans = row_best_count + col_best_count - 1;
  for (int r : best_rows) {
    bool break_signal = false;
    for (int c : best_cols) {
      string key = to_string(r) + ',' + to_string(c);
      if (!map[key]) {
        ans = row_best_count + col_best_count;
        break_signal = true;
        break;
      }
    }

    if (break_signal)
      break;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  const int MAX = 30000;
  int H, W, M;
  cin >> H >> W >> M;
  int h[MAX], w[MAX] = {0};
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }

  // Create unordered map to reference the existence of a bomb later
  // The key cannot be pair as long as I don't implement a has function for pair
  unordered_map<string, int> map;

  int rows[MAX], cols[MAX] = {0};

  int row_best_count = 0;
  int col_best_count = 0;

  for (int i = 0; i < M; i++) {
    string key = to_string(h[i]) + ',' + to_string(w[i]);
    map[key] = 1;

    rows[h[i]]++;
    cols[w[i]]++;

    row_best_count = max(row_best_count, rows[h[i]]);
    col_best_count = max(col_best_count, cols[w[i]]);
  }

  vector<int> best_rows;
  vector<int> best_cols;

  for (int i = 0; i < H; i++) {
    if (rows[i] == row_best_count) {
      best_rows.push_back(i);
    }
  }

  for (int i = 0; i < W; i++) {
    if (cols[i] == col_best_count) {
      best_cols.push_back(i);
    }
  }

  int ans = row_best_count + col_best_count - 1;
  for (int r : best_rows) {
    bool break_signal = false;
    for (int c : best_cols) {
      string key = to_string(r) + ',' + to_string(c);
      if (!map[key]) {
        ans = row_best_count + col_best_count;
        break_signal = true;
        break;
      }
    }

    if (break_signal)
      break;
  }

  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *    author:  FromDihPout
 *    created: 2020-08-25
 **/

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

#define f first
#define s second

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> rows(h), cols(w);
  set<pair<int, int>> targets;
  for (int i = 0; i < m; i++) {
    int r, c;
    cin >> r >> c;
    r--;
    c--;
    targets.insert({r, c});
    rows[r].f++;
    rows[r].s = r;
    cols[c].f++;
    cols[c].s = c;
  }
  sort(rows.begin(), rows.end(), greater<pair<int, int>>());
  sort(cols.begin(), cols.end(), greater<pair<int, int>>());

  int ans = 0;
  int p1 = 0, p2 = 0;
  while (p1 < h) {
    if (rows[p1].f + cols[p2].f > ans) {
      bool extra = targets.count({rows[p1].s, cols[p2].s});
      ans = max(ans, rows[p1].f + cols[p2].f - extra);
      p2++;
      if (p2 >= w) {
        p1++;
        p2 = 0;
      }
    } else if (p1 + 1 >= h || rows[p1 + 1].f + cols[0].f <= ans) {
      break;
    } else {
      p1++;
      p2 = 0;
    }
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

/**
 *    author:  FromDihPout
 *    created: 2020-08-25
 **/

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

#define f first
#define s second

const int INF = 1e2;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> rows(h), cols(w);
  set<pair<int, int>> targets;
  for (int i = 0; i < m; i++) {
    int r, c;
    cin >> r >> c;
    r--;
    c--;
    targets.insert({r, c});
    rows[r].f++;
    rows[r].s = r;
    cols[c].f++;
    cols[c].s = c;
  }
  sort(rows.begin(), rows.end(), greater<pair<int, int>>());
  sort(cols.begin(), cols.end(), greater<pair<int, int>>());

  int ans = 0;
  int p1 = 0, p2 = 0;
  while (p1 < h) {
    if (rows[p1].f + cols[p2].f >= ans) {
      bool extra = targets.count({rows[p1].s, cols[p2].s});
      ans = max(ans, rows[p1].f + cols[p2].f - extra);
      p2++;
      if (p2 >= w) {
        p1++;
        p2 = 0;
      }
    } else if (p1 + 1 >= h || rows[p1 + 1].f + cols[0].f < ans) {
      break;
    } else {
      p1++;
      p2 = 0;
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

/**
 *  author: rits1272
 *  created: Aug 23, 2020
 **/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define print(x) cout << "[" << #x << ":" << x << "] ";
#define println(x) cout << "[" << #x << ":" << x << "]\\n";

const int mxN = 3e5 + 1;
int h, w, m;
int X[mxN], Y[mxN];
int row[mxN], col[mxN];

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> m;
  int xmax = 0, ymax = 0;
  for (int i = 0; i < m; i++) {
    cin >> X[i] >> Y[i];
    xmax = max(xmax, ++row[X[i]]);
    ymax = max(ymax, ++col[Y[i]]);
  }

  int cnt_row = 0, cnt_col = 0;
  for (int i = 1; i <= h; i++) {
    if (row[i] == xmax)
      cnt_row++;
  }

  for (int i = 1; i <= w; i++) {
    if (col[i] == ymax)
      cnt_col++;
  }

  int dup = 0;
  for (int i = 0; i < m; i++) {
    if (row[X[i]] == xmax && col[Y[i]] == ymax) {
      dup++;
    }
  }

  int ans = xmax + ymax - (dup == cnt_row * cnt_col ? 1 : 0);
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

/**
 *  author: rits1272
 *  created: Aug 23, 2020
 **/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define print(x) cout << "[" << #x << ":" << x << "] ";
#define println(x) cout << "[" << #x << ":" << x << "]\\n";

int h, w, m;

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> m;

  vector<int> row(h, 0), col(w, 0);
  map<pair<int, int>, int> mp;

  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    mp[{x, y}]++;
    row[x]++;
    col[y]++;
  }

  int ans = 0;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      int bombs = row[i] + col[j] - (mp[{i, j}] > 0 ? 1 : 0);
      ans = max(ans, bombs);
    }
  }

  cout << ans << "\\n";
  return 0;
}
Problem p02581 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 2005;

int a[N * 3], f[N][N], g[N], gt[N], now, ans;
void update(int x, int y, int t) {
  f[x][y] = max(f[x][y], t);
  now = max(now, t);
  gt[x] = max(gt[x], t);
  gt[y] = max(gt[y], t);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      f[i][j] = -INF;
  for (int i = 1; i <= n; i++)
    gt[i] = -INF;
  update(a[1], a[2], 0);
  for (int j = 1; j <= n; j++)
    g[j] = gt[j];
  ans = now;
  int cnt = 0;
  for (int i = 1; i < n; i++) {
    int x = a[i * 3], y = a[i * 3 + 1], z = a[i * 3 + 2];
    if (x == y && x == z) {
      cnt++;
      continue;
    }
    if (x == y || x == z || y == z) {
      if (x == z)
        swap(y, z);
      if (y == z)
        swap(x, z);
      int t = f[z][z];
      int sb = f[x][z]; // 可能会被改掉
      for (int j = 1; j <= n; j++) {
        update(j, z, max(f[j][x], j == z ? sb : f[x][j]) + 1);
      }
      update(x, y, t + 1);
    } else {
      update(x, y, f[z][z] + 1);
      update(x, z, f[y][y] + 1);
      update(y, z, f[x][x] + 1);
    }
    for (int j = 1; j <= n; j++) {
      update(j, x, g[j]);
      update(j, y, g[j]);
      update(j, z, g[j]);
    }
    update(x, y, ans);
    update(x, z, ans);
    update(y, z, ans);
    for (int j = 1; j <= n; j++)
      g[j] = gt[j];
    ans = now;
  }
  ans = max(ans, f[a[n * 3]][a[n * 3]] + 1);
  printf("%d\\n", ans + cnt);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 2005;

map<pair<int, int>, int> f[N];
int a[N * 3];
int b[5], d1[5], d2[5];
int k, g;
void dfs(int u, int c1, int c2) {
  if (u == 5) {
    int v1 = d2[0], v2 = d2[1];
    if (v1 > v2)
      swap(v1, v2);
    f[k][{v1, v2}] =
        max(f[k][{v1, v2}], g + (d1[0] == d1[1] && d1[0] == d1[2]));
    return;
  }
  if (c1 < 3) {
    d1[c1] = b[u];
    dfs(u + 1, c1 + 1, c2);
  }
  if (c2 < 2) {
    d2[c2] = b[u];
    dfs(u + 1, c1, c2 + 1);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%d", &a[i]);
  f[0][{a[1], a[2]}] = 0;
  for (int i = 1; i < n; i++) {
    b[0] = a[i * 3], b[1] = a[i * 3 + 1], b[2] = a[i * 3 + 2];
    k = i;
    for (auto t : f[i - 1]) {
      b[3] = t.first.first;
      b[4] = t.first.second;
      g = t.second;
      dfs(0, 0, 0);
    }
  }
  int ans = 0;
  for (auto t : f[n - 1]) {
    int v1 = t.first.first, v2 = t.first.second;
    ans = max(ans, t.second + (v1 == v2 && v1 == a[3 * n]));
  }
  cout << ans << endl;
  return 0;
}