This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "aliens.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define sqr(x) ((x)*(x))
#define ld long double
using pii = pair<int, int>;
const int inf = 3e18;
struct line {
int k, b;
line() {
k = 0, b = inf;
}
line(int k, int b) : k(k), b(b) {}
int get(int x) {return k * x + b;}
};
ld inter(line a, line b) {
return 1.0 * (a.b - b.b) / (b.k - a.k);
}
vector<line> cht;
vector<ld> xs;
vector<int> ks;
void push(line t, int k) {
while (cht.size() && inter(cht.back(), t) < xs.back()) {
cht.pop_back();
xs.pop_back();
ks.pop_back();
}
if (cht.size())
xs.push_back(inter(cht.back(), t));
else
xs.push_back(-inf);
cht.push_back(t);
ks.push_back(k);
}
pii get(int x) {
int ind = upper_bound(xs.begin(), xs.end(), (ld)x) - xs.begin() - 1;
return {cht[ind].get(x), ks[ind]};
}
int calc(int n, vector<int>& x, vector<int>& y, vector<int>& t, vector<int>& d, int lambda) {
cht.clear(), xs.clear(), ks.clear();
d[0] = 0;
vector<int> c(n + 1, 0);
for (int i = 0; i < n; i++) {
int k = -2 * y[i];
int b = sqr(y[i]) - t[i] + d[i];
push(line(k, b), i);
auto p = get(x[i] + 1);
int f = p.second;
c[i + 1] = c[f] + 1;
d[i + 1] = p.first + sqr(x[i] + 1) + lambda;
}
return c.back();
}
long long take_photos(signed n, signed m, signed k, std::vector<signed> r, std::vector<signed> c) {
vector<pii> a(n);
for (int i = 0; i < n; i++) a[i] = {max(r[i], c[i]), min(r[i], c[i])};
sort(a.begin(), a.end(), [](pii a, pii b) {return a.first < b.first || a.first == b.first && a.second > b.second;});
vector<pii> st;
for (pii p : a) {
while (st.size() && st.back().second >= p.second)
st.pop_back();
st.push_back(p);
}
n = st.size();
k = min(k, n);
vector<int> x(n), y(n);
for (int i = 0; i < n; i++)
x[i] = st[i].first, y[i] = st[i].second;
vector<int> t(n, 0);
for (int i = 1; i < n; i++)
t[i] = sqr(max(0ll, x[i - 1] - y[i] + 1));
vector<int> d(n + 1);
int lo = -1, hi = inf+1;
while (hi - lo > 1) {
int c = (hi + lo) / 2;
if (calc(n, x, y, t, d, c) > k)
lo = c;
else
hi = c;
}
int x1 = calc(n, x, y, t, d, lo);
int y1 = d.back() - lo * x1;
int x2 = calc(n, x, y, t, d, hi);
int y2 = d.back() - hi * x2;
if (y1 == y2)
return y1;
int cf = (y2 - y1) / (x2 - x1);
int ans = y1 + (k - x1) * cf;
return ans;
}
Compilation message (stderr)
aliens.cpp: In lambda function:
aliens.cpp:68:95: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
68 | sort(a.begin(), a.end(), [](pii a, pii b) {return a.first < b.first || a.first == b.first && a.second > b.second;});
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |