이 제출은 이전 버전의 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, fr;
Line *hull;
int *cnt;
ConvexHull(int n): sz(0), B(0), fr(0){
hull = new Line[++n];
cnt = new int[n];
}
bool is_bad(int curr, int prev, int next){
Line c = hull[curr], p = hull[prev], n = hull[next];
return make_pair((c.c - n.c) * (c.m - p.m), cnt[next]) < make_pair((p.c - c.c) * (n.m - c.m), cnt[curr]);
}
void add_line(LL m, LL c, int newcnt){
hull[sz++] = Line(m, c);
cnt[sz - 1] = newcnt;
while (sz - fr > 2 && is_bad(sz-2, sz-3, sz-1)){
hull[sz-2] = hull[sz-1]; cnt[sz-2] = cnt[sz-1]; sz--;
}
}
pair<LL, int> query(LL x){
while (fr < sz - 1 && make_pair(hull[fr].get(x), cnt[fr]) > make_pair(hull[fr+1].get(x), cnt[fr + 1])) fr++;
return make_pair(hull[fr].get(x), cnt[fr]);
}
};
const LL LINF = 1e18;
const int MX = 1e5;
int n, m, k;
vector<pii> ori;
LL L[MX + 5], R[MX + 5], nx[MX + 5];
LL dp[MX + 5];
LL dp2[2][MX + 5];
int cnt[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 )
(R[n] - L[j] + 1) ^ 2 = (R[n] + 1) ^ 2 - 2L[j](R[n] + 1) + L[j] ^ 2
*/
int check(LL C){
ch.sz = 0; ch.fr = 0;
for (int j = 1; j <= n; j++){
ch.add_line(-2LL * L[j], dp[j-1] + L[j] * L[j] - nx[j] * nx[j], cnt[j - 1]);
pair<LL, int> res = ch.query(R[j] + 1);
dp[j] = res.first + (R[j] + 1) * (R[j] + 1) + C;
cnt[j] = res.second + 1;
// cout << "J/" << j << " : " << dp[j] << ' ' << cnt[j] << '\n';
}
cout << "Checking C : " << C << " with result " << dp[n] << ' ' << cnt[n] << '\n';
return cnt[n];
}
long long take_photos(int N, int M, int K, std::vector<int> r, std::vector<int> c) {
n = N, m = M, k = K;
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] = -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 j = 1; j <= n; j++) dp2[0][j] = LINF;
for (int i = 1; i <= k; i++){
ch.sz = ch.fr = 0;
for (int j = 1; j <= n; j++){
ch.add_line(-2LL * L[j], dp2[i&1^1][j-1] + L[j] * L[j] - nx[j] * nx[j], cnt[j - 1]);
pair<LL, int> res = ch.query(R[j] + 1);
dp2[i&1][j] = res.first + (R[j] + 1) * (R[j] + 1);
cnt[j] = res.second + 1;
// cout << "J/" << j << " : " << dp[j] << ' ' << cnt[j] << '\n';
}
}
return dp2[k&1][n];
LL lo = 0, hi = 1e18, md, ans = 0;
//increase cost then k decreases
while (lo <= hi){
md = (lo + hi) >> 1;
if (check(md) <= k){
ans = md; hi = md - 1;
} else lo = md + 1;
}
int kk = check(ans);
// cout << ans << ' ' << kk << '\n';
return dp[n] - ans * kk;
}
컴파일 시 표준 에러 (stderr) 메시지
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:97:34: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
ch.add_line(-2LL * L[j], dp2[i&1^1][j-1] + L[j] * L[j] - nx[j] * nx[j], cnt[j - 1]);
~^~
# | 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... |