This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
*/
#include <bits/stdc++.h>
#ifndef GEDUTIS
#include "aliens.h"
#endif
using namespace std;
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
}
using namespace my_template;
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
int N, M, K;
vpl sk;
ll eval(pl line, ll at) {
return at * line.x + line.y;
}
bool bad(pl x, pl y, pl z) {
return (x.y - y.y) * (z.x - x.x) >= (x.y - z.y) * (y.x - x.x);
}
void daryk(vl &dp, vi &cnt, ll lambda) {
dp.resize(N + 1, 0);
cnt.resize(N + 1, 0);
deque<pair<pl, int>> hull;
hull.push_back({{ -2 * sk[1].y, (sk[1].y - 1) * (sk[1].y - 1) - 1}, 0});
for (int i = 1; i <= N; ++i)
{
auto curr = hull.front(); hull.pop_front();
while (hull.size() and eval(curr.x, sk[i].x) >= eval(hull.front().x, sk[i].x)) {
curr = hull.front();
hull.pop_front();
}
hull.push_front(curr);
dp[i] = eval(curr.x, sk[i].x) + (sk[i].x + 1) * (sk[i].x + 1) + lambda;
cnt[i] = cnt[curr.y] + 1;
// printf("curr = %d %d, lambda = %lld, i = %d, dp = %lld, cnt = %d\n", curr.x.x, curr.x.y, lambda, i, dp[i], cnt[i]);
if (i == N) break;
ll h = max(sk[i].x - sk[i + 1].y + 1, 0ll);
pl newLine = { -2 * sk[i + 1].y, dp[i] - h*h + (sk[i + 1].y - 1)*(sk[i + 1].y - 1) - 1};
auto last = hull.back();
hull.pop_back();
while (hull.size() >= 1 and bad(hull.back().x, last.x, newLine)) {
last = hull.back();
hull.pop_back();
}
hull.push_back(last);
hull.push_back({newLine, i});
}
dp[N] -= cnt[N] * lambda;
}
// y < x
void sanitize(vi &r, vi &c) {
vector<pi> temp;
for (int i = 0; i < N; ++i)
{
if (r[i] > c[i]) swap(r[i], c[i]);
temp.emplace_back(c[i], r[i]);
}
sort(temp.begin(), temp.end(), [](const pi & a, const pi & b) {
return a.x < b.x or (a.x == b.x and a.y > b.y);
});
for (int i = 0; i < N; ++i)
{
while (sk.size() and sk.back().y >= temp[i].y) sk.pop_back();
sk.push_back(temp[i]);
}
N = (int)sk.size();
sk.insert(sk.begin(), { -1, -1});
// for (auto u : sk) printf("%d %d\n", u.x, u.y);
}
ll take_photos(int n, int m, int k, vi r, vi c) {
N = n; M = m; K = k;
sanitize(r, c);
ll l = 0;
ll h = 1e14;
vl dp;
vi cnt;
while (l < h) {
ll mi = (l + h + 1) / 2;
daryk(dp, cnt, mi);
if (cnt[N] >= K)
l = mi;
else
h = mi - 1;
}
daryk(dp, cnt, l);
pl V1 = {dp[N], cnt[N]};
l++;
daryk(dp, cnt, l);
pl V2 = {dp[N], cnt[N]};
if (V2.y == K or V1.y == V2.y) return V2.x;
return V1.x + (K - V1.y) * (V2.x - V1.x) / (V2.y - V1.y);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |