이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "aliens.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
struct interval
{
ll left, right;
interval(ll _left = 0, ll _right = 0)
{
left = _left;
right = _right;
}
};
bool cmp(const interval &it1, const interval &it2)
{
if (it1.left == it2.left)
return it1.right > it2.right;
return it1.left < it2.left;
}
const ll inf = 1e18;
interval h[maxn];
ll dp[4010][4010];
ll area(ll left, ll right)
{
if (left > right)
return 0;
return (right - left + 1) * (right - left + 1);
}
struct line
{
ll k, m;
line(ll _k, ll _m)
{
k = _k;
m = _m;
}
double do_math(ll x)
{
return k * x + m;
}
};
double intersect(const line &l1, const line &l2) /// when does the second line become better
{
if (l1.k == l2.k)
{
if (l1.m < l2.m)
return -inf;
else
return inf;
}
/**
l1.k * x + l1.m = l2.k * x + l2.m
(l1.k - l2.k) * x = l2.m - l1.m
*/
return (double)(l2.m - l1.m) / (double)(l1.k - l2.k);
}
struct convex_hull_trick
{
vector < line > cht;
void add_line(ll k, ll m)
{
line l(k, m);
while(cht.size() > 1 && intersect(cht[cht.size() - 2], cht.back()) > intersect(cht.back(), l))
cht.pop_back();
cht.push_back(l);
}
ll query(ll x)
{
int lf = 0, rf = (int)(cht.size()) - 2;
while(lf <= rf)
{
int mf = (lf + rf) / 2;
if (intersect(cht[mf], cht[mf + 1]) < (double)(x))
lf = mf + 1;
else
rf = mf - 1;
}
return cht[lf].do_math(x);
}
};
ll take_photos(int n, int m, int k, vector<int> r, vector<int> c)
{
vector < interval > vec;
for (int i = 0; i < n; i ++)
{
if (r[i] > c[i])
swap(r[i], c[i]);
interval cur(r[i], c[i]);
vec.push_back(cur);
}
sort(vec.begin(), vec.end(), cmp);
n = 0;
int to = -1;
for (int i = 0; i < vec.size(); i ++)
{
if (vec[i].right > to)
{
to = vec[i].right;
h[++ n] = vec[i];
}
}
///for (int i = 1; i <= n; i ++)
/// cout << h[i].left << " : " << h[i].right << endl;
for (int i = 0; i <= n; i ++)
for (int j = 0; j <= k; j ++)
dp[i][j] = inf;
k = min(k, n);
dp[0][0] = 0;
h[0] = interval(-1, -1);
for (int j = 1; j <= k; j ++)
{
convex_hull_trick cht;
cht.add_line(- 2 * h[1].left, h[1].left * h[1].left + dp[0][j - 1] - area(h[1].left, h[0].right));
for (int i = 1; i <= n; i ++)
{
dp[i][j] = cht.query(h[i].right + 1) + (h[i].right + 1) * (h[i].right + 1);
cht.add_line(- 2 * h[i + 1].left, h[i + 1].left * h[i + 1].left + dp[i][j - 1] - area(h[i + 1].left, h[i].right));
}
/**for (int p = 1; p <= i; p ++)
{
dp[i][j] = min(dp[i][j], dp[p - 1][j - 1] + area(h[p].left, h[i].right) - area(h[p].left, h[p - 1].right));
}*/
}
/**for (int i = 1; i <= n; i ++, cout << endl)
for (int j = 1; j <= k; j ++)
cout << dp[i][j] << " ";*/
return dp[n][k];
}
컴파일 시 표준 에러 (stderr) 메시지
aliens.cpp: In function 'll take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:114:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<interval>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for (int i = 0; i < vec.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... |