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

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
#define fs first
#define sd second

typedef long long ll;

typedef pair<int, int> pi;
typedef pair<pi, int> ppi;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}

void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; }

const long long INF = 1E18;

int main() {

  fast();
  int n;
  cin >> n;
  vector<int> x(n), y(n), p(n);
  for (int i = 0; i < n; i++) {
    // x[i] = 2*i, y[i] = 2*i + 1, p[i] = 1;
    cin >> x[i] >> y[i] >> p[i];
  }

  vector<long long> res(n + 1, INF);
  vector<int> hor, ver;
  vector<vector<int>> closest_hor((1 << n), vector<int>(n, 1000100)),
      closest_ver((1 << n), vector<int>(n, 1000100));

  for (int mask = 0; mask < (1 << n); mask++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (mask & (1 << j)) {
          closest_ver[mask][i] = min(closest_ver[mask][i], abs(x[i] - x[j]));
          closest_hor[mask][i] = min(closest_hor[mask][i], abs(y[i] - y[j]));
        }
      }
      closest_ver[mask][i] = min(closest_ver[mask][i], abs(x[i] - 0));
      closest_hor[mask][i] = min(closest_hor[mask][i], abs(y[i] - 0));
    }
  }

  // cerr << "TIME: " << get_time() << '\\n';

  for (int mask = 0; mask < (1 << n); mask++) {
    for (int s_mask = mask;; s_mask = ((s_mask - 1) & mask)) {

      int hor_mask = s_mask, ver_mask = (mask ^ s_mask);
      long long cost = 0;
      for (int i = 0; i < n; i++)
        cost +=
            (long long)min(closest_ver[ver_mask][i], closest_hor[hor_mask][i]) *
            p[i];
      int num_lines = __builtin_popcount(mask);
      res[num_lines] = min(res[num_lines], cost);
      // vector<int> hor, ver;
      // for(int bit = 0; bit < n; bit++) {
      // 	if(mask & (1 << bit)) {
      // 		if(s_mask & (1 << bit)) {
      // 			hor.push_back(y[bit]);
      // 		}
      // 		else {
      // 			ver.push_back(x[bit]);
      // 		}
      // 	}
      // }
      // vector<int> val(n, 1000100);
      // for(int i = 0; i < n; i++) {
      // 	if(mask & (1 << i)) {
      // 		bool b = (s_mask & (1 << i));
      // 		if(b) {
      // 			for(int j = 0; j < n; j++)
      // 				val[j] = min(val[j], abs(x[j] - x[i]));
      // 		}
      // 		else {
      // 			for(int j = 0; j < n; j++)
      // 				val[j] = min(val[j], abs(y[j] - y[i]));
      // 		}
      // 	}
      // }

      // long long cost = 0;
      // for(int i = 0; i < n; i++)
      // 	cost += (long long) val[i] * p[i];

      // int num_lines = __builtin_popcount(mask);
      // res[num_lines] = min(res[num_lines], cost);

      // int line_cnt = __builtin_popcount(mask);
      // hor.push_back(0);
      // ver.push_back(0);
      // sort(hor.begin(), hor.end());
      // sort(ver.begin(), ver.end());

      // long long cost = 0;

      // for(int i = 0; i < n; i++) {
      // 	int idx = lower_bound(hor.begin(), hor.end(), y[i]) -
      // hor.begin(); 	int val = 1000010; 	if(idx != (int) hor.size()) { 		val =
      // min(val, abs(y[i] - hor[idx]));
      // 	}
      // 	if(idx - 1 >= 0) {
      // 		val = min(val, abs(y[i] - hor[idx - 1]));
      // 	}

      // 	idx = lower_bound(ver.begin(), ver.end(), x[i]) - ver.begin();
      // 	if(idx != (int) ver.size())
      // 		val = min(val, abs(x[i] - ver[idx]));
      // 	if(idx - 1 >= 0)
      // 		val = min(val, abs(x[i] - ver[idx - 1]));

      // 	cost += (long long) val * p[i];
      // }
      // res[line_cnt] = min(res[line_cnt], cost);
      if (s_mask == 0)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    cout << res[i] << '\\n';

  // debug(get_time());
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
#define fs first
#define sd second

typedef long long ll;

typedef pair<int, int> pi;
typedef pair<pi, int> ppi;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}

void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; }

const long long INF = 1E18;

int main() {
  fast();
  int n;
  cin >> n;
  vector<int> x(n), y(n), p(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];

  vector<long long> res(n + 1, INF);
  for (int mask = 0; mask < (1 << n); mask++) {
    for (int s_mask = mask;; s_mask = ((s_mask - 1) & mask)) {
      vector<int> hor, ver;
      for (int bit = 0; bit < n; bit++) {
        if (mask & (1 << bit)) {
          if (s_mask & (1 << bit)) {
            hor.push_back(y[bit]);
          } else {
            ver.push_back(x[bit]);
          }
        }
      }
      int line_cnt = __builtin_popcount(mask);
      hor.push_back(0);
      ver.push_back(0);
      sort(hor.begin(), hor.end());
      sort(ver.begin(), ver.end());

      long long cost = 0;

      for (int i = 0; i < n; i++) {
        int idx = lower_bound(hor.begin(), hor.end(), y[i]) - hor.begin();
        int val = 1000010;
        if (idx != (int)hor.size()) {
          val = min(val, abs(y[i] - hor[idx]));
        }
        if (idx - 1 >= 0) {
          val = min(val, abs(y[i] - hor[idx - 1]));
        }

        idx = lower_bound(ver.begin(), ver.end(), x[i]) - ver.begin();
        if (idx != (int)ver.size())
          val = min(val, abs(x[i] - ver[idx]));
        if (idx - 1 >= 0)
          val = min(val, abs(x[i] - ver[idx - 1]));

        cost += (long long)val * p[i];
      }
      res[line_cnt] = min(res[line_cnt], cost);
      if (s_mask == 0)
        break;
    }
    // s_mask = 0 case
  }
  for (int i = 0; i <= n; i++)
    cout << res[i] << '\\n';
}