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

/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - M&#39;s Solution
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 15;
const int NBITS = 1 << MAX_N;
const int INF = 1 << 30;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

/* global variables */

int xs[MAX_N], ys[MAX_N], ps[MAX_N];
int dxs[NBITS][MAX_N], dys[NBITS][MAX_N];
int hls[MAX_N + 1], vls[MAX_N + 1], hlns[NBITS], vlns[NBITS], vs[MAX_N];
ll minss[MAX_N + 1];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", xs + i, ys + i, ps + i);
  int nbits = 1 << n;

  for (int bits = 0; bits < nbits; bits++) {
    hls[0] = vls[0] = 0;
    int hln = 1, vln = 1;
    for (int i = 0, bi = 1; i < n; i++, bi <<= 1)
      if (bits & bi) {
        hls[hln++] = ys[i];
        vls[vln++] = xs[i];
      }

    sort(hls, hls + hln);
    hlns[bits] = hln = unique(hls, hls + hln) - hls;
    sort(vls, vls + vln);
    vlns[bits] = vln = unique(vls, vls + vln) - vls;

    for (int i = 0; i < n; i++) {
      int hi = upper_bound(hls, hls + hln, ys[i]) - hls;
      int dy0 = (hi > 0) ? (ys[i] - hls[hi - 1]) : INF;
      int dy1 = (hi < hln) ? (hls[hi] - ys[i]) : INF;
      dys[bits][i] = min(dy0, dy1);

      int vi = upper_bound(vls, vls + vln, xs[i]) - vls;
      int dx0 = (vi > 0) ? (xs[i] - vls[vi - 1]) : INF;
      int dx1 = (vi < vln) ? (vls[vi] - xs[i]) : INF;
      dxs[bits][i] = min(dx0, dx1);
    }
  }

  fill(minss, minss + n + 1, LINF);

  for (int bits = 0; bits < nbits; bits++) {
    int m = 0;
    for (int i = 0, bi = 1; i < n; i++, bi <<= 1)
      if (bits & bi)
        vs[m++] = i;
    int mbits = 1 << m;

    for (int eits = 0; eits < mbits; eits++) {
      int hbits = 0, vbits = 0;
      for (int i = 0, bi = 1; i < m; i++, bi <<= 1) {
        if (eits & bi)
          hbits |= (1 << vs[i]);
        else
          vbits |= (1 << vs[i]);
      }

      ll sum = 0;
      for (int i = 0; i < n; i++)
        sum += (ll)min(dys[hbits][i], dxs[vbits][i]) * ps[i];

      int k = hlns[hbits] + vlns[vbits] - 2;
      if (minss[k] > sum)
        minss[k] = sum;
    }
  }

  for (int i = 0; i <= n; i++) {
    if (i > 0 && minss[i] > minss[i - 1])
      minss[i] = minss[i - 1];
    printf("%lld\\n", minss[i]);
  }
  return 0;
}


Error: TLE

Fixed submission:

/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - M&#39;s Solution
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 15;
const int NBITS = 1 << MAX_N;
const int INF = 1 << 30;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;

/* global variables */

int xs[MAX_N], ys[MAX_N], ps[MAX_N];
int vs[MAX_N], hls[MAX_N + 1], vls[MAX_N + 1];
ll minss[MAX_N + 1];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d%d", xs + i, ys + i, ps + i);
  int nbits = 1 << n;

  fill(minss, minss + n + 1, LINF);

  for (int bits = 0; bits < nbits; bits++) {
    int m = 0;
    for (int i = 0, bi = 1; i < n; i++, bi <<= 1)
      if (bits & bi)
        vs[m++] = i;
    int mbits = 1 << m;

    for (int eits = 0; eits < mbits; eits++) {
      hls[0] = vls[0] = 0;
      int hln = 1, vln = 1;
      for (int i = 0, bi = 1; i < m; i++, bi <<= 1) {
        if (eits & bi)
          hls[hln++] = ys[vs[i]];
        else
          vls[vln++] = xs[vs[i]];
      }

      sort(hls, hls + hln);
      hln = unique(hls, hls + hln) - hls;
      sort(vls, vls + vln);
      vln = unique(vls, vls + vln) - vls;

      ll sum = 0;
      for (int i = 0; i < n; i++) {
        int hi = upper_bound(hls, hls + hln, ys[i]) - hls;
        int dy0 = (hi > 0) ? (ys[i] - hls[hi - 1]) : INF;
        int dy1 = (hi < hln) ? (hls[hi] - ys[i]) : INF;
        int vi = upper_bound(vls, vls + vln, xs[i]) - vls;
        int dx0 = (vi > 0) ? (xs[i] - vls[vi - 1]) : INF;
        int dx1 = (vi < vln) ? (vls[vi] - xs[i]) : INF;

        sum += (ll)min(min(dy0, dy1), min(dx0, dx1)) * ps[i];
      }

      int k = hln + vln - 2;
      if (minss[k] > sum)
        minss[k] = sum;
    }
  }

  for (int i = 0; i <= n; i++) {
    if (i > 0 && minss[i] > minss[i - 1])
      minss[i] = minss[i - 1];
    printf("%lld\\n", minss[i]);
  }
  return 0;
}