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

/* [Template].cpp
Example code to use when starting new code. This particular problem was problem
977A_Wrong_Subtraction from CodeForces

Compile: g++  -o E.exe E.cpp
Execute: ./E
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <unordered_set>
using namespace std;

// Driver function to sort the vector elements by
// first element of pair in descending order
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}

int main() {
  int h, w, m, h_dummy, w_dummy;
  vector<int> h_vals(300000, 0), w_vals(300000, 0);
  vector<pair<int, int>> best_rows, best_cols;
  set<pair<int, int>> bombs;

  cin >> h >> w >> m;

  for (int i = 0; i < m; i++) {
    cin >> h_dummy;
    cin >> w_dummy;
    h_vals[h_dummy - 1]++;
    w_vals[w_dummy - 1]++;
    bombs.insert(make_pair(h_dummy - 1, w_dummy - 1));
  }

  for (int i = 0; i < h; i++) {
    if (h_vals[i] > 0)
      best_rows.push_back(make_pair(h_vals[i], i));
  }

  for (int i = 0; i < w; i++) {
    if (w_vals[i] > 0)
      best_cols.push_back(make_pair(w_vals[i], i));
  }

  sort(best_rows.begin(), best_rows.end(), sortinrev);
  sort(best_cols.begin(), best_cols.end(), sortinrev);

  int bomb_dummy;
  if (bombs.find(make_pair(best_rows[0].second, best_cols[0].second)) !=
      bombs.end())
    bomb_dummy = 1;
  else
    bomb_dummy = 0;
  int max_value = best_rows[0].first + best_cols[0].first - bomb_dummy,
      biggest_possible = best_rows[0].first + best_cols[0].first;

  for (int i = 0; best_rows[i].first + best_cols[0].first >= max_value &&
                  i < best_rows.size();
       i++) {
    for (int j = 0; j < best_cols.size() &&
                    best_rows[i].first + best_cols[j].first >= max_value;
         j++) {

      if (best_rows[i].first + best_cols[j].first < max_value)
        break;

      if (bombs.find(make_pair(best_rows[i].second, best_cols[j].second)) !=
          bombs.end())
        bomb_dummy = 1;
      else
        bomb_dummy = 0;

      if (best_rows[i].first + best_cols[j].first - bomb_dummy ==
          biggest_possible) {
        max_value = biggest_possible;
        break;
      }

      if (max_value == biggest_possible)
        break;

      // cout << "Check: " << i << " " << j << "\\t" << best_rows[i].first << " "
      // << best_cols[j].first << " " << best_rows[i].first + best_cols[j].first
      // - bomb_dummy << endl;
    }
    if (max_value == biggest_possible)
      break;
  }

  cout << max_value << endl;

  return 0;
}


Error: TLE

Fixed submission:

/* [Template].cpp
Example code to use when starting new code. This particular problem was problem
977A_Wrong_Subtraction from CodeForces

Compile: g++  -o E.exe E.cpp
Execute: ./E
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <unordered_set>
using namespace std;

// Driver function to sort the vector elements by
// first element of pair in descending order
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}

int main() {
  int h, w, m, h_dummy, w_dummy;
  vector<int> h_vals(300000, 0), w_vals(300000, 0);
  vector<pair<int, int>> best_rows, best_cols;
  unordered_set<int> bombs;

  cin >> h >> w >> m;

  for (int i = 0; i < m; i++) {
    cin >> h_dummy;
    cin >> w_dummy;
    h_vals[h_dummy - 1]++;
    w_vals[w_dummy - 1]++;
    // bombs.insert(make_pair(h_dummy-1, w_dummy-1));
    bombs.insert(3000 * (h_dummy - 1) + w_dummy - 1);
  }

  for (int i = 0; i < h; i++) {
    if (h_vals[i] > 0)
      best_rows.push_back(make_pair(h_vals[i], i));
  }

  for (int i = 0; i < w; i++) {
    if (w_vals[i] > 0)
      best_cols.push_back(make_pair(w_vals[i], i));
  }

  sort(best_rows.begin(), best_rows.end(), sortinrev);
  sort(best_cols.begin(), best_cols.end(), sortinrev);

  int max_value = 0, bomb_dummy = 0;

  for (int i = 0; i < best_rows.size() &&
                  best_rows[i].first + best_cols[0].first >= max_value;
       i++) {
    for (int j = 0; j < best_cols.size() &&
                    best_rows[i].first + best_cols[j].first >= max_value;
         j++) {
      if (best_rows[i].first + best_cols[j].first < max_value)
        break;

      if (bombs.find(3000 * best_rows[i].second + best_cols[j].second) !=
          bombs.end())
        bomb_dummy = 1;
      else
        bomb_dummy = 0;

      if (best_rows[i].first + best_cols[j].first - bomb_dummy > max_value) {
        max_value = best_rows[i].first + best_cols[j].first - bomb_dummy;
      }
    }
  }

  cout << max_value << endl;

  return 0;
}