이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct line
{
int64_t a, b;
int64_t operator() (int64_t x) const { return a*x + b; }
};
vector<pair<int, int>> filter_dominated(vector<pair<int, int>> I)
{
vector<pair<int, int>> J = {{INT_MIN, INT_MIN}};
sort(I.begin(), I.end(), [](pair<int, int> lhs, pair<int, int> rhs) {
return lhs.first != rhs.first ? lhs.first < rhs.first : lhs.second > rhs.second;
});
for(auto [l, r] : I)
if(not (J.back().first <= l and r <= J.back().second))
J.emplace_back(l, r);
J.erase(J.begin());
return J;
}
constexpr int64_t square(int64_t x) { return x*x; }
int64_t take_photos(int _n, int _m, int _k, vector<int> r, vector<int> c)
{
size_t n = _n, m = _m, k = _k; (void)m;
vector<pair<int, int>> I;
for(size_t i = 0; i < n; i++)
I.emplace_back(min(r[i], c[i]), max(r[i], c[i]));
I = filter_dominated(I);
n = I.size();
vector<vector<int64_t>> cost(k+1, vector<int64_t>(n+1, INT64_MAX / 3));
vector<line> hull(k+1);
cost[0][0] = 0;
for(size_t l = 1; l <= k; l++)
{
cost[l][0] = 0;
for(size_t i = 1; i <= n; i++)
{
for(size_t j = 0; j < i; j++)
{
line fj {
-2 * I[j].first,
cost[l-1][j] + square(I[j].first) -
square(max(0, (j ? I[j-1].second : -1) - I[j].first + 1))
};
auto x = I[i-1].second + 1;
cost[l][i] = min(cost[l][i], square(x) + fj(x));
}
}
}
return cost[k][n];
}
#ifdef XHOVA
int main()
{
cout << take_photos(5, 7, 2, {0, 4, 4, 4, 4}, {3, 4, 6, 5, 6}) << endl;
cout << take_photos(2, 6, 2, {1, 4}, {4, 1}) << endl;
}
#endif
# | 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... |