Problem p02604 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
// #include <queue>
// #include <stack>
#include <cassert>
#include <cstring>

#include <cstdlib>

using namespace std;

#ifdef LOCAL_DEBUG
#include <local_debug.h>
#define DEBUG(...) DBG2::print(#__VA_ARGS__, __LINE__, __VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define SZ(a) int((a).size())
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;

int N;
int X[15], Y[15], P[15];

const llong INF = 1e18;

inline int nearest_dist(int k, int V[], int pos) {
  int dist = 2000000001;
  /*
  int j = lower_bound(V, V+k, pos) - V;
  if (j < k)
     dist = min(dist, V[j]-pos);
  if (j > 0)
     dist = min(dist, pos-V[j-1]);
  */

  for (int i = 0; i < k; ++i) {
    int d = abs(pos - V[i]);
    if (dist > d)
      dist = d;
  }

  return dist;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> N;
  REP(i, N)
  cin >> X[i] >> Y[i] >> P[i];

  /*
  srand(123);
  for (int i = N-1; i > 0; --i) {
     int j = rand() % (i+1);
     if (i != j) {
        swap(X[i], X[j]);
        swap(Y[i], Y[j]);
        swap(P[i], P[j]);
     }
  }
  */

  int P3 = 1;
  for (int n = 1; n <= N; ++n)
    P3 *= 3;

  llong ans[16];
  FOR(k, 0, N)
  ans[k] = INF;

  int Vx[16], Vy[16];

  REP(mask, P3) {
    int kx = 1, ky = 1;
    Vx[0] = 0, Vy[0] = 0;
    for (int m = mask, i = 0; i < N; ++i) {
      switch (m % 3) {
      case 1:
        Vx[kx++] = X[i];
        break;
      case 2:
        Vy[ky++] = Y[i];
        break;
      }
      m /= 3;
    }

    sort(Vx, Vx + kx);
    sort(Vy, Vy + ky);

    int k = (kx - 1) + (ky - 1);
    llong cost = 0;
    REP(i, N) {
      int distx = nearest_dist(kx, Vx, X[i]);
      int disty = nearest_dist(ky, Vy, Y[i]);
      cost += min(distx, disty) * 1LL * P[i];
      if (ans[k] <= cost)
        break;
    }
    if (ans[k] > cost) {
      ans[k] = cost;
      FOR(kk, k, N) {
        if (ans[kk] <= ans[k])
          break;
        ans[kk] = ans[k];
      }
    }
  }

  FOR(k, 0, N)
  cout << ans[k] << '\n';

  return 0;
}


Fixed submission:
// #include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
// #include <queue>
// #include <stack>
#include <cassert>
#include <cstring>

#include <cstdlib>

using namespace std;

#ifdef LOCAL_DEBUG
#include <local_debug.h>
#define DEBUG(...) DBG2::print(#__VA_ARGS__, __LINE__, __VA_ARGS__)
#else
#define DEBUG(...)
#endif

#define SZ(a) int((a).size())
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)

typedef long long llong;
typedef vector<int> VI;
typedef vector<VI> VVI;

int N;
int X[15], Y[15], P[15];

const llong INF = 1e18;

inline int nearest_dist(int k, int V[], int pos) {
  int dist = 2000000001;
  /*
  int j = lower_bound(V, V+k, pos) - V;
  if (j < k)
     dist = min(dist, V[j]-pos);
  if (j > 0)
     dist = min(dist, pos-V[j-1]);
  */

  for (int i = 0; i < k; ++i) {
    int d = abs(pos - V[i]);
    if (dist > d)
      dist = d;
  }

  return dist;
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> N;
  REP(i, N)
  cin >> X[i] >> Y[i] >> P[i];

  /*
  srand(123);
  for (int i = N-1; i > 0; --i) {
     int j = rand() % (i+1);
     if (i != j) {
        swap(X[i], X[j]);
        swap(Y[i], Y[j]);
        swap(P[i], P[j]);
     }
  }
  */

  int P3 = 1;
  for (int n = 1; n <= N; ++n)
    P3 *= 3;

  llong ans[16];
  FOR(k, 0, N)
  ans[k] = INF;

  int Vx[16], Vy[16];

  REP(mask, P3) {
    int kx = 1, ky = 1;
    Vx[0] = 0, Vy[0] = 0;
    for (int m = mask, i = 0; i < N; ++i) {
      switch (m % 3) {
      case 1:
        Vx[kx++] = X[i];
        break;
      case 2:
        Vy[ky++] = Y[i];
        break;
      }
      m /= 3;
    }

    // sort(Vx, Vx+kx);
    // sort(Vy, Vy+ky);

    int k = (kx - 1) + (ky - 1);
    llong cost = 0;
    REP(i, N) {
      int distx = nearest_dist(kx, Vx, X[i]);
      int disty = nearest_dist(ky, Vy, Y[i]);
      cost += min(distx, disty) * 1LL * P[i];
      if (ans[k] <= cost)
        break;
    }
    if (ans[k] > cost) {
      ans[k] = cost;
      FOR(kk, k, N) {
        if (ans[kk] <= ans[k])
          break;
        ans[kk] = ans[k];
      }
    }
  }

  FOR(k, 0, N)
  cout << ans[k] << '\n';

  return 0;
}