이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "aliens.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
struct Line{
LL m, c;
Line(LL m = 0, LL c = 0): m(m), c(c){}
LL get(LL x){return m * x + c;}
};
struct ConvexHull{
int sz, B;
Line *hull;
ConvexHull(int n): sz(0), B(0){
hull = new Line[++n];
}
bool is_bad(int curr, int prev, int next){
Line c = hull[curr], p = hull[prev], n = hull[next];
return (c.c - n.c) * (c.m - p.m) <= (p.c - c.c) * (n.m - c.m);
}
void add_line(LL m, LL c){
hull[sz++] = Line(m, c);
while (sz > 2 && is_bad(sz-2, sz-3, sz-1)){
hull[sz-2] = hull[sz-1]; sz--;
}
B = min(B, sz - 1);
}
LL query(LL x){
// int l = -1, r = sz-1;
// while (r - l > 1){
// int m = (l + r) >> 1;
// if (hull[m].get(x) >= hull[m+1].get(x)) l = m;
// else r = m;
// }
// return hull[r].get(x);
while (B < sz - 1 && hull[B].get(x) >= hull[B+1].get(x)) B++;
return hull[B].get(x);
}
};
const LL LINF = 1e18;
const int MX = 5e4;
int n, m, k;
vector<pii> ori;
LL L[MX + 5], R[MX + 5], nx[MX + 5];
LL dp[105][MX + 5];
ConvexHull ch(MX + 5);
bool cmp(pii a, pii b){
return a.fi != b.fi ? a.fi < b.fi : a.se > b.se;
}
/*
dp[k][n] = min(dp[k-1][n], min{j<=n} (dp[k-1][j-1] + (L[j]^2-2L[j]) + max(0,R[j-1]-L[j]+1)^2 - 2L[j]•R[n]) + R[n]^2 + 2R[n] + 1;
( c1 ) ( c2 ) ( m ) ( x ) ( extra )
*/
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
for (int i = 0; i < n; i++){
ori.emplace_back(r[i], c[i]);
if (ori.back().fi > ori.back().se) swap(ori.back().fi, ori.back().se);
}
sort(ori.begin(), ori.end());
int idx = 0;
for (int i = 0; i < n; i++){
if (idx && L[idx] <= ori[i].fi && R[idx] >= ori[i].se) continue;
while (idx && ori[i].fi <= L[idx] && ori[i].se >= R[idx]) idx--;
idx++; tie(L[idx], R[idx]) = ori[i];
}
n = idx;
// for (int i = 1; i <= n; i++) printf("(%d, %d)%c", L[i], R[i], " \n"[i==n]);
L[0] = R[0] = -1;
for (int i = 1; i <= n; i++) dp[0][i] = LINF;
for (int i = 1; i <= n; i++)
nx[i] = max(0LL, R[i-1] - L[i] + 1);
// for (int i = 1; i <= n; i++) printf("%lld%c", nx[i], " \n"[i==n]);
for (int i = 1; i <= k; i++){
for (int j = 1; j <= n; j++){
dp[i][j] = dp[i-1][j];
ch.add_line(-2LL * L[j], dp[i-1][j-1] + L[j] * (L[j] - 2) - nx[i] * nx[i]);
dp[i][j] = min(dp[i][j], ch.query(R[j]) + R[j] * R[j] + 2LL * R[j] + 1);
// printf("%lld%c", dp[i][j], " \n"[j==n]);
}
}
return dp[k][n];
}
# | 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... |