Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define INF 1000000000000
#define MOD 10000007
#define PI 3.14159265359
#define dij                                                                    \\
  priority_queue<pair<int, int>, vector<pair<int, int>>,                       \\
                 greater<pair<int, int>>>
typedef long long ll;
//

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> v(m);
  vector<bool> check(min(h, w), false);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    v[i] = make_pair(a, b);
    if (a == b)
      check[a] = true;
  }
  vector<int> che1, che2;
  map<int, int> mp1, mp2;
  int ma1 = 0, ma2 = 0;
  for (auto e : v) {
    mp1[e.F]++;
    mp2[e.S]++;
    ma1 = max(ma1, mp1[e.F]);
    ma2 = max(ma2, mp2[e.S]);
  }
  int ans = 0;
  for (auto e : v) {
    if (mp1[e.F] == ma1)
      che1.push_back(e.F);
    if (mp2[e.S] == ma2)
      che2.push_back(e.S);
  }
  sort(all(che1));
  sort(all(che2));
  che1.erase(unique(all(che1)), che1.end());
  che2.erase(unique(all(che2)), che2.end());
  // for(auto e:che1)cout<<e<<endl;for(auto e:che2)cout<<e<<endl;
  ans = mp1[che1[0]] + mp2[che2[0]];
  // vector<bool> flag;
  bool flag = true;
  sort(all(v));

  for (auto e : che1) {
    for (auto f : che2) {
      auto itr1 = lower_bound(all(v), make_pair(e, f));
      if (v[itr1 - v.begin()] != make_pair(e, f)) {
        flag = false;
        break;
      }
      // else flag.push_back(false);
    }
    if (!flag)
      break;
  }
  // for(auto e:flag)cout<<e<<endl;
  if (flag)
    ans--;
  cout << ans << endl;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define INF 1000000000000
#define MOD 10000007
#define PI 3.14159265359
#define dij                                                                    \\
  priority_queue<pair<int, int>, vector<pair<int, int>>,                       \\
                 greater<pair<int, int>>>
typedef long long ll;
//

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> v(m);
  vector<bool> check(min(h, w), false);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    v[i] = make_pair(a, b);
    if (a == b)
      check[a] = true;
  }
  vector<int> che1, che2;
  map<int, int> mp1, mp2;
  int ma1 = 0, ma2 = 0;
  for (auto e : v) {
    mp1[e.F]++;
    mp2[e.S]++;
    ma1 = max(ma1, mp1[e.F]);
    ma2 = max(ma2, mp2[e.S]);
  }
  int ans = 0;
  for (auto e : v) {
    if (mp1[e.F] == ma1)
      che1.push_back(e.F);
    if (mp2[e.S] == ma2)
      che2.push_back(e.S);
  }
  sort(all(che1));
  sort(all(che2));
  che1.erase(unique(all(che1)), che1.end());
  che2.erase(unique(all(che2)), che2.end());
  // for(auto e:che1)cout<<e<<endl;for(auto e:che2)cout<<e<<endl;
  ans = mp1[che1[0]] + mp2[che2[0]];
  vector<bool> flag;
  sort(all(v));

  for (auto e : che1) {
    for (auto f : che2) {
      auto itr1 = lower_bound(all(v), make_pair(e, f));
      if (v[itr1 - v.begin()] == make_pair(e, f))
        flag.push_back(true);
      else
        flag.push_back(false);
    }
  }
  // for(auto e:flag)cout<<e<<endl;
  if (find(all(flag), false) == flag.end())
    ans--;
  cout << ans << endl;
}