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];
long long D[100009];
inline long long p(long long x) { return x*x; }
pli DP(vector<pii>& A, ll lambda) {
int N = A.size();
for(int i=1; i<N; i++) D[i] = 1LL * 1e18, c[i] = 0;
for(int i=1; i<N; i++) {
for(int j=0; j<i; j++) {
ll nw = D[j] + p(A[i].se - A[j+1].fi + 1) - p(max(0, A[j].se - A[j+1].fi + 1)) + lambda;
if(D[i] > nw) D[i] = nw, c[i] = c[j] + 1;
else if(D[i] == nw) c[i] = min(c[i], c[j] + 1);
}
}
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]);
}
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:45:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
long long m = L+R >> 1, v; int cnt;
~^~
aliens.cpp:50: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... |