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

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORR(i, a, b) for (int i = a; i < b; i++)
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define printmp(b)                                                             \\
  for (auto y : b) {                                                           \\
    printP(y);                                                                 \\
  }

#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << ' ';                                                          \\
  cout << endl;
#define printM(b)                                                              \\
  for (auto y : b) {                                                           \\
    print(y);                                                                  \\
  }
#define printP(p) cout << p.first << ' ' << p.second << endl;
typedef long long int ll;
typedef unsigned long long int ull;

/*

*/
ll apowb(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b % 2 == 0) {
    return apowb(a * a, b / 2);

  } else {
    return apowb(a * a, b / 2) * a;
  }
}

void solve()

{

  int r, c, n, x, y;
  cin >> r >> c >> n;

  // vector<vector<int>> arr(r,vector<int>(c));
  int rw[r], cl[c];
  FOR(i, r)
  rw[i] = 0;

  FOR(i, c)
  cl[i] = 0;

  // unordered_map<int,int>  row,col;
  map<vector<int>, int> mat;

  FOR(i, n) {
    cin >> x >> y;
    mat[{x - 1, y - 1}] = 1;
    //  arr[x-1][y-1]=1;

    cl[y - 1]++;
    rw[x - 1]++;
  }

  int mx = 0;

  int mxr = 0, mxc = 0;

  for (int x : rw) {
    mxr = max(x, mxr);
  }

  for (int x : cl) {
    mxc = max(x, mxc);
  }

  vector<int> nr, nc;

  FOR(i, r) {
    if (rw[i] == mxr) {
      nr.pb(i);
    }
  }

  FOR(i, c) {
    if (cl[i] == mxc) {
      nc.pb(i);
    }
  }

  mx = 0;
  for (int i : nr) {
    for (int j : nc) {
      if (mat[{i, j}] == 0) {
        cout << mxr + mxc;
        return;
      }
    }
  }

  cout << mxr + mxc - 1;

  //  FOR(i,r)
  //  {
  //    if(row[i]>0)
  //    FOR(j,c)
  //    {

  //      mx=max(mx,row[i]-mat[{i,j}]+col[j]);
  //      if(mx==n)
  //      {
  //        cout<<mx;
  //        return;
  //      }

  //    }

  //  }

  // cerr << "\\ntime taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" <<
  // endl;
}

int main() {
  /*
6
4 0
4 41
0 1000000
44 0
1 0
10000 1000000


  */
  fast;
  int t = 1;
  // cin>>t;
  while (t--) {

    solve();
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl '\\n'
#define all(x) (x).begin(), (x).end()
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FORR(i, a, b) for (int i = a; i < b; i++)
#define fast                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define printmp(b)                                                             \\
  for (auto y : b) {                                                           \\
    printP(y);                                                                 \\
  }

#define print(a)                                                               \\
  for (auto x : a)                                                             \\
    cout << x << ' ';                                                          \\
  cout << endl;
#define printM(b)                                                              \\
  for (auto y : b) {                                                           \\
    print(y);                                                                  \\
  }
#define printP(p) cout << p.first << ' ' << p.second << endl;
typedef long long int ll;
typedef unsigned long long int ull;

/*

*/
ll apowb(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b % 2 == 0) {
    return apowb(a * a, b / 2);

  } else {
    return apowb(a * a, b / 2) * a;
  }
}

void solve()

{

  int r, c, n, x, y;
  cin >> r >> c >> n;

  // vector<vector<int>> arr(r,vector<int>(c));
  int rw[r], cl[c];
  FOR(i, r)
  rw[i] = 0;

  FOR(i, c)
  cl[i] = 0;

  // unordered_map<int,int>  row,col;
  map<vector<int>, int> mat;

  FOR(i, n) {
    cin >> x >> y;
    mat[{x - 1, y - 1}] = 1;
    //  arr[x-1][y-1]=1;

    cl[y - 1]++;
    rw[x - 1]++;
  }

  int mx = 0;

  int mxr = 0, mxc = 0;

  for (int x : rw) {
    mxr = max(x, mxr);
  }

  for (int x : cl) {
    mxc = max(x, mxc);
  }

  vector<int> nr, nc;

  FOR(i, r) {
    if (rw[i] == mxr) {
      nr.pb(i);
    }
  }

  FOR(i, c) {
    if (cl[i] == mxc) {
      nc.pb(i);
    }
  }

  mx = 0;
  for (int i : nr) {
    for (int j : nc) {
      mx = max(mx, mxr + mxc - mat[{i, j}]);
    }
  }
  cout << mx;

  //  FOR(i,r)
  //  {
  //    if(row[i]>0)
  //    FOR(j,c)
  //    {

  //      mx=max(mx,row[i]-mat[{i,j}]+col[j]);
  //      if(mx==n)
  //      {
  //        cout<<mx;
  //        return;
  //      }

  //    }

  //  }

  // cerr << "\\ntime taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" <<
  // endl;
}

int main() {
  /*
6
4 0
4 41
0 1000000
44 0
1 0
10000 1000000


  */
  fast;
  int t = 1;
  // cin>>t;
  while (t--) {

    solve();
    cout << endl;
  }

  return 0;
}