이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "aliens.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
#define sq(a) ((ll)(a) * (a))
#define cost(j, i) (sq(r[i] - l[j + 1] + 1) - sq(max(0, r[j] - l[j + 1] + 1)))
struct cht {
vector<ll> m, b; int ptr = 0;
bool bad(int l1, int l2, int l3) {
return 1.0 * (b[l3] - b[l1]) * (m[l1] - m[l2]) <= 1.0 * (b[l2] - b[l1]) * (m[l1] - m[l3]);
}
void add(ll _m, ll _b) {
m.push_back(_m); b.push_back(_b);
int s = m.size();
while(s >= 3 && bad(s - 3, s - 2, s - 1)) {
m.erase(m.end() - 2);
b.erase(b.end() - 2);
s--;
}
}
ll f(int i, ll x) { return m[i] * x + b[i]; }
ll query(ll x) {
if(ptr >= m.size()) ptr = m.size() - 1;
while(ptr < m.size() - 1 &&
f(ptr, x) > f(ptr + 1, x)) ptr++;
return f(ptr, x);
}
void clear() { m.clear(), b.clear(), ptr = 0; }
} ds;
ll take_photos(int n, int m, int K, vector<int> R, vector<int> C) {
vector<ii> tmp;
for(int i = 0; i < n; i++) {
if(R[i] > C[i]) swap(R[i], C[i]);
tmp.emplace_back(C[i], R[i]);
}
sort(tmp.begin(), tmp.end());
tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
vector<int> l = {-1}, r = {-1};
for(ii seg : tmp) {
if(seg.first == r.back()) continue;
while(l.size() && seg.second <= l.back() && r.back() <= seg.first)
r.pop_back(), l.pop_back();
l.push_back(seg.second);
r.push_back(seg.first);
}
n = l.size() - 1;
ll dp[K + 1][n + 1];
memset(dp, 63, sizeof dp);
for(int i = 1; i <= n; i++)
dp[1][i] = sq(r[i] - l[1] + 1);
for(int k = 2; k <= K; k++) {
ds.clear();
ds.add(-2ll * l[1 + 1], dp[k - 1][1] + sq(l[1 + 1]) - sq(max(0, r[1] - l[1 + 1] + 1)));
for(int i = 2; i <= n; i++) {
ll x = r[i] + 1;
dp[k][i] = ds.query(x) + sq(x);
if(i < n) ds.add(-2ll * l[i + 1], dp[k - 1][i] + sq(l[i + 1]) - sq(max(0, r[i] - l[i + 1] + 1)));
}
}
ll ret = 1e18;
for(int i = 1; i <= K; i++) ret = min(ret, dp[i][n]);
return ret;
}
#ifdef LOCAL_TESTING
int main() {
freopen("in", "r", stdin);
int n, m, k;
assert(3 == scanf("%d %d %d", &n, &m, &k));
std::vector<int> r(n), c(n);
for (int i = 0; i < n; i++) {
assert(2 == scanf("%d %d", &r[i], &c[i]));
}
ll ans = take_photos(n, m, k, r, c);
printf("%lld\n", ans);
return 0;
}
#endif
컴파일 시 표준 에러 (stderr) 메시지
aliens.cpp: In member function 'll cht::query(ll)':
aliens.cpp:27:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ptr >= m.size()) ptr = m.size() - 1;
~~~~^~~~~~~~~~~
aliens.cpp:28:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr < m.size() - 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... |