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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
int N;
ll x[15], y[15], p[15];
vector<int> vx[1 << 15], vy[1 << 15];
ll min_diff_x[1 << 15][15];
ll min_diff_y[1 << 15][15];

// s: people, t: rail
ll calc_x(int s, int t) {
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (!((1 << i) & s))
      continue;
    ans += min_diff_x[t][i];
  }
  return ans;
}

// s: people, t: rail
ll calc_y(int s, int t) {
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (!((1 << i) & s))
      continue;
    ans += min_diff_y[t][i];
  }
  return ans;
}

ll min_x[1 << 15][16];
ll min_y[1 << 15][16];

int full;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  full = (1 << N) - 1;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  for (int i = 1; i < (1 << N); i++) {
    for (int j = 0; j <= N; j++) {
      min_x[i][j] = 1e18;
      min_y[i][j] = 1e18;
    }
  }
  for (int i = 0; i < (1 << N); i++) {
    vx[i].push_back(0);
    vy[i].push_back(0);
    vx[i].push_back(-100000);
    vy[i].push_back(-100000);
    vx[i].push_back(100000);
    vy[i].push_back(100000);
    for (int j = 0; j < N; j++) {
      if (i & (1 << j)) {
        vx[i].push_back(x[j]);
        vy[i].push_back(y[j]);
      }
    }
    sort(vx[i].begin(), vx[i].end());
    sort(vy[i].begin(), vy[i].end());

    for (int j = 0; j < N; j++) {
      auto ptr = lower_bound(vx[i].begin(), vx[i].end(), x[j]);
      ll m = 1e15;
      if (ptr != vx[i].end())
        m = *ptr - x[j];
      ptr--;
      m = min(m, x[j] - (*ptr));
      min_diff_x[i][j] = p[j] * m;
    }

    for (int j = 0; j < N; j++) {
      auto ptr = lower_bound(vy[i].begin(), vy[i].end(), y[j]);
      ll m = 1e15;
      if (ptr != vy[i].end())
        m = *ptr - y[j];
      ptr--;
      m = min(m, y[j] - (*ptr));
      min_diff_y[i][j] = p[j] * m;
    }
  }
  for (int i = 0; i < (1 << N); i++) {
    for (int j = i; j >= 0; j = (j - 1) & i) {
      int cnt = 0;
      for (int k = 0; k < N; k++) {
        if (j & (1 << k))
          cnt++;
      }
      min_x[i][cnt] = min(min_x[i][cnt], calc_x(i, j));
      min_y[i][cnt] = min(min_y[i][cnt], calc_y(i, j));
      if (j == 0)
        break;
    }
  }
  for (int i = 0; i <= N; i++) {
    ll ans = 1e18;
    for (int j = 0; j < (1 << N); j++) {
      for (int k = 0; k <= i; k++) {
        ll tmp = min_x[j][k] + min_y[full - j][i - k];
        ans = min(ans, tmp);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
int N;
ll x[15], y[15], p[15];
vector<int> vx[1 << 15], vy[1 << 15];

// s: people, t: rail
ll calc_x(int s, int t) {
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (!((1 << i) & s))
      continue;
    auto ptr = lower_bound(vx[t].begin(), vx[t].end(), x[i]);
    ll m = 1e15;
    if (ptr != vx[t].end())
      m = *ptr - x[i];
    if (ptr != vx[t].begin()) {
      ptr--;
      m = min(m, x[i] - (*ptr));
    }
    ans += m * p[i];
  }
  return ans;
}

// s: people, t: rail
ll calc_y(int s, int t) {
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if (!((1 << i) & s))
      continue;
    auto ptr = lower_bound(vy[t].begin(), vy[t].end(), y[i]);
    ll m = 1e15;
    if (ptr != vy[t].end())
      m = *ptr - y[i];
    if (ptr != vy[t].begin()) {
      ptr--;
      m = min(m, y[i] - (*ptr));
    }
    ans += m * p[i];
  }
  return ans;
}

ll min_x[1 << 15][16];
ll min_y[1 << 15][16];

int full;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  full = (1 << N) - 1;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i] >> p[i];
  }
  for (int i = 1; i < (1 << N); i++) {
    for (int j = 0; j <= N; j++) {
      min_x[i][j] = 1e18;
      min_y[i][j] = 1e18;
    }
  }
  for (int i = 0; i < (1 << N); i++) {
    vx[i].push_back(0);
    vy[i].push_back(0);
    for (int j = 0; j < N; j++) {
      if (i & (1 << j)) {
        vx[i].push_back(x[j]);
        vy[i].push_back(y[j]);
      }
    }
    sort(vx[i].begin(), vx[i].end());
    sort(vy[i].begin(), vy[i].end());
  }
  for (int i = 0; i < (1 << N); i++) {
    for (int j = i; j >= 0; j = (j - 1) & i) {
      int cnt = 0;
      for (int k = 0; k < N; k++) {
        if (j & (1 << k))
          cnt++;
      }
      min_x[i][cnt] = min(min_x[i][cnt], calc_x(i, j));
      min_y[i][cnt] = min(min_y[i][cnt], calc_y(i, j));
      if (j == 0)
        break;
    }
  }
  for (int i = 0; i <= N; i++) {
    ll ans = 1e18;
    for (int j = 0; j < (1 << N); j++) {
      for (int k = 0; k <= i; k++) {
        ll tmp = min_x[j][k] + min_y[full - j][i - k];
        ans = min(ans, tmp);
      }
    }
    cout << ans << endl;
  }
}