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>
#define f first
#define s second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int N = (int)1e5 + 123, inf = 1e9;
const ll INF = 1e18;
using namespace std;
struct line{
ll k, b;
double intersect(line a){
return -1.0 * (a.b - b) / (a.k - k);
}
};
struct cvh{
vector< line > v;
void add(line x){
while(v.size() > 1){
line a = v[v.size() - 1];
line b = v[v.size() - 2];
if(x.intersect(a) < x.intersect(b))
v.pop_back();
else
break;
}
v.pb(x);
}
ll get(ll x){
if(v.empty())
return INF;
ll res = INF;
for(auto it : v)
res = min(res, it.k * x + it.b);
return res;
}
};
ll sqr(ll x){
return x * x;
}
ll take_photos(int n, int m, int k, vector<int> row, vector<int> col) {
vector< pii > arr, tmp;
for(int i = 0; i < n; i++){
int l, r;
l = row[i];
r = col[i];
if(l > r)
swap(l, r);
tmp.pb({l, -r});
}
sort(tmp.begin(), tmp.end());
for(int i = 0, mx = -1; i < n; i++){
int l = tmp[i].f;
int r = -tmp[i].s;
if(r <= mx)
continue;
mx = r;
arr.pb({l, r});
}
n = arr.size();
vector< ll > dp(n, INF);
vector< line > ln(n, {0, INF});
ll ans = INF;
for(int q = 1; q <= k ; q++){
cvh btr;
for(int i = 0; i < n; i++){
dp[i] = min(sqr(arr[i].s - arr[0].f + 1), sqr(arr[i].s) + btr.get(arr[i].s));
btr.add(ln[i]);
if(i != n - 1){
ll b = dp[i] + sqr(arr[i + 1].f - 1) - sqr(max(0, arr[i].s - arr[i + 1].f + 1));
ll k = - 2 * (arr[i + 1].f - 1);
ln[i] = {k, b};
}
}
if(ans > dp[n - 1])
ans = dp[n - 1];
}
return ans;
}
# | 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... |