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 sc(ar, n)                                                              \\
  for (int pen = 0; pen < n; pen++) {                                          \\
    cin >> ar[pen];                                                            \\
  }
#define pr(ar, n)                                                              \\
  for (int pen = 0; pen < n; pen++) {                                          \\
    cout << ar[pen] << " ";                                                    \\
  }                                                                            \\
  cout << endl;
#define mem(ar, x) memset(ar, x, sizeof(ar))
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mod 1000000007
#define rt return 0;
#define ct continue;
#define pb push_back
#define ll long long int
#define ff first
#define ss second
#define mod 1000000007
#define maxn 300005
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

ll ch[maxn], cw[maxn];
int main() {

  fastio;
  ll h, w, m, x, y;
  cin >> h >> w >> m;

  set<pair<ll, ll>> s;

  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    s.insert({x, y});
    ch[x]++;
    cw[y]++;
  }
  ll m1 = *max_element(ch, ch + h + 1);
  ll m2 = *max_element(cw, cw + w + 1);

  ll ans = m1 + m2 - 1;

  vector<ll> v1, v2;
  for (int i = 0; i < maxn; i++) {
    if (ch[i] == m1)
      v1.pb(i);
  }
  for (int i = 0; i < maxn; i++) {
    if (cw[i] == m2)
      v2.pb(i);
  }

  for (auto &i : v1) {
    for (auto &j : v2) {
      if (!s.count({i, j})) {
        cout << ans + 1;
        rt;
      }
    }
  }
  cout << ans;
}


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 sc(ar, n)                                                              \\
  for (int pen = 0; pen < n; pen++) {                                          \\
    cin >> ar[pen];                                                            \\
  }
#define pr(ar, n)                                                              \\
  for (int pen = 0; pen < n; pen++) {                                          \\
    cout << ar[pen] << " ";                                                    \\
  }                                                                            \\
  cout << endl;
#define mem(ar, x) memset(ar, x, sizeof(ar))
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mod 1000000007
#define rt return 0;
#define ct continue;
#define pb push_back
#define ll long long int
#define ff first
#define ss second
#define mod 1000000007
#define maxn 1000003
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif

int main() {

  fastio;
  ll h, w, m, x, y;
  cin >> h >> w >> m;
  unordered_map<ll, ll> xmap, ymap;
  multiset<pair<ll, ll>> s;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    xmap[x]++;
    ymap[y]++;
    s.insert({x, y});
  }
  ll ans = INT_MIN;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      ll curr = 0;
      if (ymap.count(j))
        curr += ymap[j];
      if (xmap.count(i))
        curr += xmap[i];
      if (s.find({i, j}) != s.end())
        curr--;
      ans = max(ans, curr);
    }
  }

  cout << ans;
}