Submission #789367

#TimeUsernameProblemLanguageResultExecution timeMemory
789367Sohsoh84Aliens (IOI16_aliens)C++17
100 / 100
306 ms19624 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 = 1e6 + 10;
const ll INF = 1e18;

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

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

namespace CHT {
	vector<pair<pll, int>> lines;	
	vector<pll> vec;

	inline ll isect(pll a, pll b) {
		if (a.X > b.X) swap(a, b);
		return (a.Y - b.Y + (a.Y <= b.Y ? 0 : 1) * (b.X - a.X - 1)) / (b.X - a.X);
	} // TODO check

	inline void clear() {
		vec.clear();
		lines.clear();
	}

	inline void add(pair<pll, int> line) {
		lines.push_back(line);
		while (!vec.empty()) {
			ll ind = isect(lines[vec.back().Y].X, line.X);
			ll a = lines[vec.back().Y].X.X * ind + lines[vec.back().Y].X.Y;
			ll b = line.X.X * ind + line.X.Y;
			if (ind < vec.back().X) {
				vec.pop_back();
				continue;
			}

			if (a == b && lines[vec.back().Y].Y < line.Y) ind++; 	
			vec.push_back({ind, int(lines.size()) - 1});
			break;
		}

		if (vec.empty())
			vec.push_back({-INF, int(lines.size()) - 1});
	}

	inline int get(ll x) {
		auto it = lower_bound(all(vec), pll(x + 1, -INF));
		return prev(it) -> Y;
	}
}

inline pll calc(ll lambda) {
	CHT::clear();

	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);
		}
	}

	int ind = 0;
	for (int e : opts) {
		int len = f[e] - e + 1;
		dp[ind] = {pw(len + e - opts.front()) + lambda, 1};
		if (!(e == opts.front())) {
			ll x = (len + e);
			int ii = CHT::get(x); 
 
			int tlen = len + (e - opts[ii + 1]);
			int mn_cov = opts[ii + 1];
			pll tmp = {dp[ii].X + pw(tlen) - cov[ii] + lambda, dp[ii].Y + 1};
			dp[ind] = min(dp[ind], tmp);
		}

		if (ind < int(opts.size()) - 1) {
			cov[ind] = (f[e] >= opts[ind + 1] ? pw(f[e] - opts[ind + 1] + 1) : 0);
			CHT::add({{-2 * opts[ind + 1], 1ll * opts[ind + 1] * opts[ind + 1] + dp[ind].X - cov[ind]}, int(dp[ind].Y)});	
		}
		ind++;
	}

	return dp[int(opts.size()) - 1];
}

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));
	}

	/*
	CHT::add({{1, 1}, 0});
	CHT::add({{0, 2}, 0});
	debug(CHT::get(-1));
*/
	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;
}

Compilation message (stderr)

aliens.cpp: In function 'pll calc(ll)':
aliens.cpp:88:8: warning: unused variable 'mn_cov' [-Wunused-variable]
   88 |    int mn_cov = opts[ii + 1];
      |        ^~~~~~
#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...