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


// Problem : E - M's Solution
// Contest : AtCoder - M-SOLUTIONS Programming Contest 2020
// URL : https://atcoder.jp/contests/m-solutions2020/tasks/m_solutions2020_e
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define inf 1e18
#define PI acos(-1.0)
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 100005

// Solution

int x[15], y[15], p[15], D[15], d[15];
ll ans[16];

void solve() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  memset(ans, 0x3f, sizeof(ans));
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    D[i] = min(abs(x[i]), abs(y[i]));
    ans[0] += (ll)D[i] * p[i];
  }
  for (int mask1 = 0; mask1 < (1 << n); mask1++) {
    for (int mask2 = mask1; ~mask2; mask2 = (mask2 - 1) & mask1) {
      memcpy(d, D, sizeof(d));
      for (int i = 0; i < n; i++) {
        if ((mask1 >> i) & 1) {
          if ((mask2 >> i) & 1) {
            for (int j = 0; j < n; j++)
              d[j] = min(d[j], abs(x[i] - x[j]));
          } else {
            for (int j = 0; j < n; j++)
              d[j] = min(d[j], abs(y[i] - y[j]));
          }
        }
      }
      ll cur = 0;
      for (int i = 0; i < n; i++)
        cur += (ll)d[i] * p[i];
      int oc = __builtin_popcountll(mask1);
      ans[oc] = min(ans[oc], cur);
      if (!mask2)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    cout << ans[i] << '\\n';
}

signed main() {
  io;
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  int tests;
  // cin>>tests;
  tests = 1;
  while (tests--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:


// Problem : E - M's Solution
// Contest : AtCoder - M-SOLUTIONS Programming Contest 2020
// URL : https://atcoder.jp/contests/m-solutions2020/tasks/m_solutions2020_e
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define inf 1e18
#define PI acos(-1.0)
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 100005

// Solution

void solve() {
  int n;
  cin >> n;
  vi x(n), y(n), p(n), D(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i] >> p[i];
  vector<ll> ans(n + 1, inf);
  ans[0] = 0;
  for (int i = 0; i < n; i++) {
    D[i] = min(abs(x[i]), abs(y[i]));
    ans[0] += (ll)D[i] * p[i];
  }
  for (int mask1 = 0; mask1 < (1 << n); mask1++) {
    for (int mask2 = mask1; ~mask2; mask2 = (mask2 - 1) & mask1) {
      vi d = D;
      for (int i = 0; i < n; i++) {
        if ((mask1 >> i) & 1) {
          if ((mask2 >> i) & 1) {
            for (int j = 0; j < n; j++)
              d[j] = min(d[j], abs(x[i] - x[j]));
          } else {
            for (int j = 0; j < n; j++)
              d[j] = min(d[j], abs(y[i] - y[j]));
          }
        }
      }
      ll cur = 0;
      for (int i = 0; i < n; i++)
        cur += (ll)d[i] * p[i];
      int oc = __builtin_popcountll(mask1);
      ans[oc] = min(ans[oc], cur);
      if (!mask2)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    cout << ans[i] << '\\n';
}

signed main() {
  io;
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  int tests;
  // cin>>tests;
  tests = 1;
  while (tests--) {
    solve();
  }
  return 0;
}