Submission #789286

#TimeUsernameProblemLanguageResultExecution timeMemory
789286Sohsoh84Aliens (IOI16_aliens)C++17
4 / 100
2050 ms340 KiB
#include "aliens.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll; 

#define all(x)			(x).begin(), (x).end()
#define X			first
#define Y			second
#define sep			' '
#define debug(x)		cerr << #x << ": " << x << endl;

const ll MAXN = 1000 + 10;
const ll INF = 1e18;

int n, m, k;
pll dp[MAXN];
ll f[MAXN];

inline ll pw(ll a) {
	return a * a;
}

inline pll calc(ll lambda) {
	vector<int> opts;
	ll mx_f = -1;
	for (int i = 0; i < m; i++) {
		if (f[i] > mx_f) {
			mx_f = f[i];
			opts.push_back(i);
		}
	}

	for (int e : opts) {
		int len = f[e] - e + 1;
		dp[e] = {pw(len + e - opts.front()) + lambda, 1};
		for (int ii = 0; ii < int(opts.size()); ii++) {
			int e2 = opts[ii];
			if (e2 >= e) break;
		
			int tlen = len + (e - opts[ii + 1]);
			int mn_cov = opts[ii + 1];
			for (int j = 1; j <= k; j++) {
				pll tmp = {dp[e2].X + pw(tlen) - (mn_cov > f[e2] ? 0 : pw(f[e2] - mn_cov + 1)) + lambda, dp[e2].Y + 1};
				dp[e] = min(dp[e], tmp);
			}
		}
	}

	return dp[opts.back()];
}

ll take_photos(int n_, int m_, int k_, vector<int> r_, vector<int> c_) {
	fill(f, f + MAXN, -1);

	n = n_, m = m_, k = k_;
	for (int i = 0; i < n; i++) {
		int x = r_[i], y = c_[i];
		if (y < x) swap(x, y); // TODO: check
		f[x] = max(f[x], ll(y));
	}
	
	ll l = 0, r = INF / n;
	while (l < r) {
		ll mid = (l + r) / 2;
		if (calc(mid).Y > k) l = mid + 1;
		else r = mid;
	}

	return calc(l).X - k * l;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...