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 r first
#define c second
typedef long long ll;
typedef pair<int,int> ii;
const int N = 1e5;
const ll INF = 1e18;
ll dp[N+2];
vector<ii> p;
struct line {
ll m, c;
line(ll x, ll y): m(x), c(y) {}
ll operator () (ll x) {
return m*x+c;
}
};
struct convexHull {
vector<line> v;
bool greater(ll a, ll b, ll c, ll d) {
if (a/b != c/d) return a/b > c/d;
return (a%b)*d >= (c%d)*b;
}
bool bad(line a, line b, line c) {
return greater(b.c-a.c,a.m-b.m,c.c-b.c,b.m-c.m);
}
void insert(line x) {
int sz = v.size();
while (sz >= 2 && bad(v[sz-2],v[sz-1],x)) {
v.pop_back(); sz--;
}
v.push_back(x);
}
ll query(ll x) {
ll ret = INF;
for (int l = 0, r = v.size()-1; l <= r; ) {
int mid = (l+r)/2;
ret = min(ret,v[mid](x));
if (mid+1 <= r && v[mid+1](x) < v[mid](x)) l = mid+1;
else r = mid-1;
}
return ret;
}
};
ll sqr(ll a) {
return a*a;
}
ll take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
vector<ii> tmp;
for (int i = 0; i < n; i++) {
if (r[i] > c[i]) swap(r[i],c[i]);
tmp.push_back(ii(r[i],c[i]));
}
sort(tmp.begin(),tmp.end());
p.push_back(ii(-1,-1));
for (auto i : tmp) {
while (!p.empty() && p.back().r == i.r) p.pop_back();
p.push_back(i);
}
n = p.size()-1;
convexHull ch;
for (int i = 1; i <= n; i++) {
ch.insert(line(-2*(p[i].r-1),dp[i-1]+sqr(p[i].r-1)));
dp[i] = ch.query(p[i].c)+sqr(p[i].c);
// cout << dp[i] << ' ';
}
// cout << '\n';
return dp[n];
}
/*
5 7 2
0 3
4 4
4 6
4 5
4 6
2 6 2
1 4
4 1
*/
# | 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... |