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;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
#define fi first
#define se second
int c[100009];
ll D[100009];
inline ll p(ll x) { return x*x; }
struct line {
ll a, b;
int idx;
};
line stk[100009]; int t;
// is Q smaller than P?
bool chk(line P, line Q, ll x) {
if(x*P.a + P.b == x*Q.a + Q.b) return c[P.idx] > c[Q.idx];
return x*P.a + P.b > x*Q.a + Q.b;
}
inline bool del(line P, line Q, line R) {
return (R.a - P.a) * (Q.b - R.b) < (R.a - Q.a) * (P.b - R.b);
}
void add(line P) {
while(1<t && del(stk[t-1], stk[t], P)) --t;
stk[++t] = P;
}
pli DP(vector<pii>& A, ll lambda) {
int N = (int)A.size() - 1;
for(int i=1; i<N; i++) D[i] = 1LL * 1e18, c[i] = 0;
t = -1; add((line){-2LL * A[1].fi, p(A[1].fi) - 2*A[1].fi, 0});
for(int i=1, j=0; i<N; i++) {
j = min(j, t);
while(j<t && A[i].se * stk[j].a + stk[j].b > A[i].se * stk[j+1].a + stk[j+1].b) ++j;
ll nw = A[i].se * stk[j].a + stk[j].b + p(A[i].se) + 2*A[i].se + 1 + lambda;
if(D[i] > nw) D[i] = nw, c[i] = c[stk[j].idx] + 1;
add({-2LL * A[i+1].fi, D[i] + p(A[i+1].fi) - 2*A[i+1].fi - p(max(0, A[i].se - A[i+1].fi + 1)), i});
}
return {D[N-1], c[N-1]};
}
long long take_photos(int n, int m, int k, vector<int> r, vector<int> c) {
vector<pii> A, B;
for(int i=0; i<n; i++) {
if(r[i] > c[i]) swap(r[i], c[i]);
A.push_back({r[i], c[i]});
}
sort(A.begin(), A.end(), [&](pii P, pii Q) {
if(P.se == Q.se) return P.fi > Q.fi;
return P.se < Q.se;
});
B.push_back({-1, -1});
for(int i=0; i<n; i++) {
while(B.size() && B.back().fi >= A[i].fi) B.pop_back();
B.push_back(A[i]);
} B.push_back({0, 0});
long long L = 0, R = 1LL*1e12, f;
while(L <= R) {
long long m = L+R >> 1, v; int cnt;
tie(v, cnt) = DP(B, m);
if(cnt > k) L = m+1;
else R = m-1, f = v - m * k;
}
return f;
}
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:68:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
long long m = L+R >> 1, v; int cnt;
~^~
aliens.cpp:73:12: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
return f;
^
# | 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... |