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


// Problem: E - Bomber
// Contest: AtCoder - AtCoder Beginner Contest 176
// URL: https://atcoder.jp/contests/abc176/tasks/abc176_e
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define ll long long
const int N = 2e5 + 5;
const int mod = 1e9;
#define ld long double
#define PI 3.14159265358979323846
const int INF = 10000000;

void flash() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(9);
}

// 3. Structure or classes
// 3. Ends here

// 1. Data structures declaration here
// 1. Ends here

// 2. Extra Functions here
// 2. Ends here

void solve();
int32_t main() {
  flash();
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
    // cout<<"\\n";
  }
  return 0;
}

void solve() {
  int n, m, k;
  cin >> n >> m >> k;

  map<int, map<int, int>> mp;
  map<int, int> r, c;

  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    mp[x][y] = 1;
    r[x]++;
    c[y]++;
  }

  int ans = 0;
  int maxr = 0;
  vector<int> vr;
  for (auto rr : r) {
    if (rr.second > maxr) {
      maxr = rr.second;
      vr.clear();
      vr.push_back(rr.first);
    } else if (rr.second == maxr) {
      vr.push_back(rr.first);
    }
  }

  int maxc = 0;
  vector<int> vc;
  for (auto cc : c) {
    if (cc.second > maxc) {
      maxc = cc.second;
      vc.clear();
      vc.push_back(cc.first);
    } else if (cc.second == maxc) {
      vc.push_back(cc.first);
    }
  }

  ans = 0;

  for (auto i : vr) {
    for (auto j : vc) {
      ans = max(ans, r[i] + c[j] - mp[i][j]);
      if (ans == maxr + maxc)
        break;
    }
    if (ans == maxr + maxc)
      break;
  }

  cout << ans << "\\n";

  return;
}
// how r u bro

Error: TLE

Fixed submission:


// Problem: E - Bomber
// Contest: AtCoder - AtCoder Beginner Contest 176
// URL: https://atcoder.jp/contests/abc176/tasks/abc176_e
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

using namespace std;
#define int long long
#define ll long long
const int N = 2e5 + 5;
const int mod = 1e9;
#define ld long double
#define PI 3.14159265358979323846
const int INF = 10000000;

void flash() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(9);
}

// 3. Structure or classes
// 3. Ends here

// 1. Data structures declaration here
// 1. Ends here

// 2. Extra Functions here
// 2. Ends here

void solve();
int32_t main() {
  flash();
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
    // cout<<"\\n";
  }
  return 0;
}

void solve() {
  int n, m, k;
  cin >> n >> m >> k;

  map<int, map<int, int>> mp;
  map<int, int> r, c;

  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    mp[x][y] = 1;
    r[x]++;
    c[y]++;
  }

  int ans = 0;
  for (auto rr : r) {
    for (auto cc : c) {
      int ans1 = (rr.second + cc.second) - (mp[rr.first][cc.first]);
      ans = max(ans, ans1);
    }
  }

  cout << ans << "\\n";

  return;
}
// how r u bro