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<bits/stdc++.h>
#include "aliens.h"
using namespace std;
#define int long long
#define dbl long double
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
const int inf = 1e18+10;
int n;
vector<pair<int,int>> pts;
deque<pair<int,int>> cht;
dbl intx(int a1, int b1, int a2, int b2) {
// x*a1 + b1 = x*a2 + b2
// x*(a1-a2) = b2-b1
// x = (b2-b1)/(a1-a2)
return ((dbl) b2-b1)/((dbl) a1-a2);
}
int func(int a, int b, int x) {
return a*x+b;
}
void chtclear() {
cht.clear();
}
void chtadd(int a, int b) {
while(cht.size() >= 2 && intx(a,b,cht[1].fr,cht[1].sc) >= intx(cht[0].fr,cht[0].sc,cht[1].fr,cht[1].sc)) {
cht.pop_front();
}
cht.push_front(mp(a,b));
}
int chtqry(int x) {
while(cht.size() >= 2 && func(cht.back().fr,cht.back().sc,x) >= func(cht[cht.size()-2].fr,cht[cht.size()-2].sc,x)) {
cht.pop_back();
}
return func(cht.back().fr,cht.back().sc,x);
}
int sol(int K) {
int dp[n][K+1];
for(int i = 0; i < n; i++) dp[i][0] = inf;
for(int k = 1; k <= K; k++) {
chtclear();
for(int i = 0; i < n; i++) {
dp[i][k] = inf;
int xi = pts[i].fr;
int yi = pts[i].sc;
if(i == 0) {
int xj = pts[i].fr;
int yj = pts[i].sc;
chtadd(yj,yj*yj - 2*yj);
}
else {
int j = i;
int xj = pts[i].fr;
int yj = pts[i].sc;
int xj1 = pts[i-1].fr;
int yj1 = pts[i-1].sc;
chtadd(yj,(dp[j-1][k-1] + yj*yj - 2*yj - max((int)0,xj1-yj+1)*max((int) 0,xj1-yj+1)));
}
dp[i][k] = chtqry(-2*xi)+(xi*xi + 2*xi + 1);
// cout << i << " " << k << " " << dp[i][k] << endl;
// for(int j = 0; j <= i; j++) {
// int xj = pts[j].fr;
// int yj = pts[j].sc;
// if(j == 0) {
// dp[i][k] = min(dp[i][k], (xi*xi + 2*xi + 1) + (yj*yj - 2*yj) + (- 2*xi*yj));
// }
// else {
// int xj1 = pts[j-1].fr;
// int yj1 = pts[j-1].sc;
// dp[i][k] = min(dp[i][k] ,(xi*xi + 2*xi + 1) + (dp[j-1][k-1] + yj*yj - 2*yj - max((int)0,xj1-yj+1)*max((int) 0,xj1-yj+1)) + (- 2*xi*yj));
// }
// }
}
}
return dp[n-1][K];
}
// int sol(int K) {
// int dp[n][K+1];
// for(int i = 0; i < n; i++) dp[i][0] = inf;
// for(int k = 1; k <= K; k++) {
// for(int i = 0; i < n; i++) {
// dp[i][k] = inf;
// int xi = pts[i].fr;
// int yi = pts[i].sc;
// for(int j = 0; j <= i; j++) {
// int xj = pts[j].fr;
// int yj = pts[j].sc;
// if(j == 0) {
// dp[i][k] = min(dp[i][k], (xi*xi + 2*xi + 1) + (yj*yj - 2*yj) + (- 2*xi*yj));
// }
// else {
// int xj1 = pts[j-1].fr;
// int yj1 = pts[j-1].sc;
// dp[i][k] = min(dp[i][k] ,(xi*xi + 2*xi + 1) + (dp[j-1][k-1] + yj*yj - 2*yj - max((int)0,xj1-yj+1)*max((int) 0,xj1-yj+1)) + (- 2*xi*yj));
// }
// }
// }
// }
// return dp[n-1][K];
// }
int take_photos(int32_t N, int32_t m, int32_t k, vector<int32_t> r, vector<int32_t> c) {
for(int i = 0; i < N; i++) {
int x = c[i];
int y = r[i];
if(x < y) swap(x,y);
pts.pb(mp(x,-y));
}
sort(all(pts));
stack<pair<int,int>> prt;
for(auto X : pts) {
int x = X.fr;
int y = -X.sc;
while(prt.size() && prt.top().sc >= y) prt.pop();
prt.push(mp(x,y));
}
pts.clear();
while(prt.size()) {
pts.pb(prt.top());
prt.pop();
}
sort(all(pts));
n = pts.size();
return sol(k);
}
Compilation message (stderr)
aliens.cpp: In function 'long long int sol(long long int)':
aliens.cpp:58:21: warning: unused variable 'xj' [-Wunused-variable]
58 | int xj = pts[i].fr;
| ^~
aliens.cpp:64:21: warning: unused variable 'xj' [-Wunused-variable]
64 | int xj = pts[i].fr;
| ^~
aliens.cpp:67:21: warning: unused variable 'yj1' [-Wunused-variable]
67 | int yj1 = pts[i-1].sc;
| ^~~
aliens.cpp:55:17: warning: unused variable 'yi' [-Wunused-variable]
55 | int yi = pts[i].sc;
| ^~
# | 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... |