이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "aliens.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll nx=5e4+5, inf=1e17;
ll dp[505][nx], area[nx];
stack<ll> s;
struct point
{
ll x, y;
bool operator< (const point &o) const {
if (x==o.x) return y>o.y;
return x<o.x;
}
} v[nx], p[nx];
struct line
{
ll m, c, p;
line(ll m, ll c, ll p): m(m), c(c), p(p){}
};
struct convexhull
{
deque<line> dq;
ll div(ll a, ll b)
{
if ((a^b)>=0||(a%b)==0) return a/b;
return a/b-1;
}
void add(ll m, ll c)
{
line nw=line(m, c, inf);
while (dq.size()>=2&&div(nw.c-dq[dq.size()-2].c, dq[dq.size()-2].m-nw.m)<dq[dq.size()-2].p) dq.pop_back();
if (!dq.empty()) dq.back().p=div(nw.c-dq.back().c,dq.back().m-nw.m);
dq.push_back(nw);
}
ll query(ll x)
{
while (x>=dq.front().p) dq.pop_front();
return dq.front().m*x+dq.front().c;
}
} cvh;
long long take_photos(int n, int m, int k, std::vector<int> r, std::vector<int> c) {
for (int i=0; i<n; i++) if (r[i]>c[i]) swap(r[i], c[i]);
for (int i=1; i<=n; i++) v[i].x=c[i-1], v[i].y=r[i-1];
sort(v+1, v+n+1);
for (int i=1; i<=n; i++)
{
while (!s.empty()&&v[s.top()].y>=v[i].y) s.pop();
s.push(i);
}
n=s.size();
while (!s.empty()) p[s.size()]=v[s.top()], s.pop();
for (int i=2; i<=n; i++) if (p[i-1].x>=p[i].y) area[i]=(p[i-1].x-p[i].y+1)*(p[i-1].x-p[i].y+1);
for (int i=1; i<=n; i++) dp[1][i]=(p[i].x-p[1].y+1)*(p[i].x-p[1].y+1);
k=min(k, n);
for (int i=2; i<=k; i++)
{
while (cvh.dq.size()>0) cvh.dq.pop_back();
for (int j=i; j<=n; j++)
{
cvh.add(-2*p[j].y, p[j].y*p[j].y-2*p[j].y+1-area[j]+dp[i-1][j-1]);
dp[i][j]=cvh.query(p[j].x)+p[j].x*p[j].x+2*p[j].x;
}
}
return dp[k][n];
}
# | 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... |