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

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

struct city_t {
  ll x, y, p;
};

struct solve {
  int n;
  vvll dx, dy;
  vll x, y, p;
  vll ans;

  void ready_calc(vll &dx, vll &x, vll &axis) {
    dx.resize(n, 30000);
    for (int i = 0; i < n; i++) {
      auto it = upper_bound(axis.begin(), axis.end(), x[i]);
      int idx = it - axis.begin();
      if (it != axis.end())
        min_u(dx[i], axis[idx] - x[i]);
      if (idx > 0)
        min_u(dx[i], x[i] - axis[idx - 1]);
      dx[i] *= p[i];
    }
  }
  void ready(vvll &dx, vll &x) {
    dx.resize(1 << n);
    for (int i = 0; i < (1 << n); i++) {
      vll axis = {0};
      for (int j = 0; j < n; j++) {
        if ((i >> j) & 1)
          axis.push_back(x[j]);
      }
      sort(axis.begin(), axis.end());
      ready_calc(dx[i], x, axis);
    }
  }
  solve(vll &x, vll &y, vll &p) : x(x), y(y), p(p) {
    n = x.size();
    ready(dx, x);
    ready(dy, y);
    ans.resize(n + 1, 1LL << 60);
    ll m = 1;
    for (int i = 0; i < n; i++)
      m *= 3;
    for (ll i = 0; i < m; i++) {
      ll x = i;
      int x_idx = 0;
      int y_idx = 0;
      int k = 0;
      for (int j = 0; j < n; j++) {
        x_idx <<= 1;
        y_idx <<= 1;
        switch (x % 3) {
        case 1:
          x_idx |= 1;
          k++;
          break;
        case 2:
          y_idx |= 1;
          k++;
          break;
        }
        x /= 3;
      }
      ll t = 0;
      for (int j = 0; j < n; j++) {
        t += min(dx[x_idx][j], dy[y_idx][j]);
      }
      if (min_u(ans[k], t)) {
        //				cout << "(x_idx,y_idx,k)=" << x_idx << "," <<
        //y_idx << "," << k << "  " << t << "\\n";
      }
    }
  }
  ll operator()(int k) { return ans[k]; }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n;
    cin >> n;
    if (cin.fail())
      break;
    vll x(n), y(n), p(n);
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i] >> p[i];
    }

    solve solver(x, y, p);
    for (int k = 0; k <= n; k++) {
      cout << solver(k) << "\\n";
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

struct city_t {
  ll x, y, p;
};

struct solve {
  int n;
  solve() {}
  int search(vi &axis, int v) {
    auto it = lower_bound(axis.begin(), axis.end(), v);
    int idx = it - axis.begin();
    if (idx == 0)
      return axis[idx] - v;
    if (idx >= axis.size())
      return v - axis[idx - 1];
    return min(axis[idx] - v, v - axis[idx - 1]);
  }
  ll calc(vector<city_t> &city, vi &x_axis, vi &y_axis) {
    ll ret = 0;
    for (auto &m : city) {
      ll d = min(search(x_axis, m.x), search(y_axis, m.y));
      ret += d * m.p;
    }
    return ret;
  }
  ll check_k(vector<city_t> &city, vi &x_axis, vi &y_axis, int i, int k) {
    if (k <= 0) {
      return calc(city, x_axis, y_axis);
    }
    ll ret = 9876543210987654;
    for (; i + k <= n; i++) {
      vi n_x_axis = x_axis;
      vi n_y_axis = y_axis;
      auto it = lower_bound(n_x_axis.begin(), n_x_axis.end(), city[i].x);
      if (*it != city[i].x)
        n_x_axis.insert(it, city[i].x);
      min_u(ret, check_k(city, n_x_axis, y_axis, i + 1, k - 1));

      it = lower_bound(n_y_axis.begin(), n_y_axis.end(), city[i].y);
      if (*it != city[i].y)
        n_y_axis.insert(it, city[i].y);
      min_u(ret, check_k(city, x_axis, n_y_axis, i + 1, k - 1));
    }
    return ret;
  }
  ll operator()(vector<city_t> &city, int k) {
    vi x_axis = {0};
    vi y_axis = {0};
    n = city.size();

    return check_k(city, x_axis, y_axis, 0, k);
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n;
    cin >> n;
    if (cin.fail())
      break;
    vector<city_t> city(n);
    for (auto &m : city)
      cin >> m.x >> m.y >> m.p;

    for (int k = 0; k <= n; k++) {
      cout << solve()(city, k) << "\\n";
    }
  }
  return 0;
}