이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "aliens.h"
#include <bits/stdc++.h>
//#include "grader.cpp"
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
const ll LINF = 1e18;
class convex_hull {
private:
vector<long long> a, b;
vector<int> id;
int ptr = 0;
int bad(int l1, int l2, long long _a, long long _b) {
if ((a[l1] == _a) && (b[l1] == _b)) return 1;
return ((_b - b[l1]) * (a[l1] - a[l2]) > (b[l2] - b[l1]) * (a[l1] - _a));
}
public:
void add(long long _a, long long _b, int _id) {
while ((int) a.size() >= 2 && bad((int) a.size() - 1, (int) a.size() - 2, _a, _b)) {
a.pop_back();
b.pop_back();
id.pop_back();
}
a.push_back(_a);
b.push_back(_b);
id.push_back(_id);
}
pair<int, long long> query(long long x) {
if (ptr >= (int) a.size()) {
ptr = (int) a.size() - 1;
}
while (ptr < (int) a.size() - 1 && a[ptr + 1] * x + b[ptr + 1] >= a[ptr] * x + b[ptr]) {
ptr++;
}
return {id[ptr], a[ptr] * x + b[ptr]};
}
};
long long take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
for (int i = 0; i < n; i++) if (r[i] > c[i]) swap(r[i], c[i]);
vector<pii> segments;
for (int i = 0; i < n; i++) segments.push_back({r[i], c[i]});
sort(segments.begin(), segments.end(), [&](const pii &a, const pii &b) {
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
});
vi nr = {segments[0].first}, nc = {segments[0].second};
for (int i = 1; i < n; i++) {
if (!(nr.back() <= segments[i].first && segments[i].second <= nc.back())) {
nr.push_back(segments[i].first);
nc.push_back(segments[i].second);
}
}
swap(r, nr); swap(c, nc); n = (int) r.size();
vector<convex_hull> lct(k + 1);
for (int j = 0; j < k; j++) lct[j].add(-(-2 * r[0]), -((ll) r[0] * r[0] - 2 * r[0]), -1);
vector<ll> dp(k + 1, 0);
dp[0] = LINF;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) dp[j] = -lct[j - 1].query(c[i - 1]).second + (ll) (c[i - 1] + 1) * (ll) (c[i - 1] + 1);
if (i < n) for (int j = 0; j <= k; j++) {
lct[j].add(-(-2 * r[i]), -(dp[j] + (ll) r[i] * r[i] - 2 * r[i] - max(0ll, (ll) c[i - 1] - r[i] + 1) * max(0ll, (ll) c[i - 1] - r[i] + 1)), -1);
}
}
return dp[k];
}
# | 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... |