이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "aliens.h"
#include <algorithm>
#include <queue>
#define x first
#define y second
long long sq(int a) { return a * a; }
bool cmp(std::pair<int, int> a, std::pair<int, int> b) {
if (a.x == b.x) return (a.y > b.y);
return a.x < b.y;
}
long long dp[100001], cnt[100001]{1};
float slope(int i, int j, std::vector<std::pair<int, int>> points) {
return (float)(sq(points[j + 1].y) - sq(points[i + 1].y) + dp[j] - dp[i] +
sq(std::max(points[i].x - points[i + 1].y + 1, 0)) -
sq(std::max(points[j].x - points[j + 1].y + 1, 0))) /
(points[j + 1].y - points[i + 1].y);
}
void compute(int lambda, std::vector<std::pair<int, int>> points, int k) {
std::deque<int> dq;
dq.push_back(0);
dp[0] = lambda + sq(points[0].y - points[0].x + 1);
for (int i = 1; i < points.size(); i++) {
while (dq.size() > 1 &&
slope(dq[0], dq[1], points) >= 2 * (points[i].x + 1))
dq.pop_front();
int opt = dq.front();
dp[i] = sq(points[i].y - points[opt + 1].x + 1) + dp[opt] -
sq(std::max(0, points[opt].x - points[opt + 1].y + 1)) + lambda;
cnt[i] = cnt[opt] + 1;
while (dq.size() > 1 && slope(dq[dq.size() - 2], dq.back(), points) <=
slope(dq.back(), i, points))
dq.pop_back();
dq.push_back(i);
}
}
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
std::vector<std::pair<int, int>> points, useful;
for (int i = 0; i < n; i++) {
int x = r[i], y = c[i];
if (x > y) std::swap(x, y);
points.push_back({x, y});
}
std::sort(points.begin(), points.end(), cmp);
for (int i = 0; i < n; i++)
if (useful.empty() || useful.back().y < points[i].y)
useful.push_back(points[i]);
long long l = 0, u = 1e9;
while (l != u) {
long long mid = (l + u + 1) / 2;
compute(mid, useful, k);
if (cnt[useful.size() - 1] <= k) l = mid;
else u = mid;
}
compute(l, useful, k);
return dp[useful.size() - 1] - l * cnt[useful.size() - 1];
}
컴파일 시 표준 에러 (stderr) 메시지
aliens.cpp: In function 'void compute(int, std::vector<std::pair<int, int> >, int)':
aliens.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < points.size(); i++) {
~~^~~~~~~~~~~~~~~| # | 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... |