이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
#define int long long
#define fi first
#define se second
#define pb push_back
using namespace std;
const int BIG = 1e18;
const int is_query = -BIG;
struct line {
int m, b;
mutable function<const line*()> succ;
bool operator<(const line& rhs) const {
if (rhs.b != is_query) return m < rhs.m;
const line* s = succ();
if (!s) return 0;
int x = rhs.m;
return b - s->b < (s->m - m) * x;
}
};
struct dynamic_hull : public multiset<line> {
const int inf = BIG;
bool bad(iterator y) {
auto z = next(y);
if (y == begin()) {
if (z == end()) return 0;
return y->m == z->m && y->b <= z->b;
}
auto x = prev(y);
if (z == end()) return y->m == x->m && y->b <= x->b;
int v1 = (x->b - y->b);
if (y->m == x->m) v1 = x->b > y->b ? inf : -inf;
else v1 /= (y->m - x->m);
int v2 = (y->b - z->b);
if (z->m == y->m) v2 = y->b > z->b ? inf : -inf;
else v2 /= (z->m - y->m);
return v1 >= v2;
}
void insert_line(int m, int b) {
auto y = insert({m,b});
y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
if (bad(y)) { erase(y); return; }
while (next(y) != end() && bad(next(y))) erase(next(y));
while (y != begin() && bad(prev(y))) erase(prev(y));
}
int eval(int x) {
auto l = *lower_bound((line) { x, is_query });
return l.m * x + l.b;
}
};
int N,M,K;
int R[50000], C[50000];
int dp[101][50000];
double m(int i,int k,int l){
double numer = dp[i-1][l]+2*C[l]*R[l+1]-C[l]*C[l]-2*C[l]-dp[i-1][k]-2*C[k]*R[k+1]+C[k]*C[k]+2*C[k];
double denom = 2*R[l+1] - 2*R[k+1];
return numer / denom;
}
int take_photos(signed N, signed M,signed K,vector<signed>r, vector<signed>c){
map<pair<int,int>,int>iexist;
int MAX[M];
for(int i=0;i<M;i++) MAX[i]=-1;
for(int i=0;i<N;i++){
if(r[i]>c[i]){
swap(r[i],c[i]);
}
iexist[{r[i],c[i]}]=1;
MAX[r[i]]=max(MAX[r[i]],(int)c[i]);
}
int prefMAX[M];
prefMAX[0]=MAX[0];
for(int i=1;i<M;i++){
prefMAX[i]=max(prefMAX[i-1], MAX[i]);
}
map<pair<int,int>,int>idontexist;
for(pair<pair<int,int>,int> x:iexist){
if(x.fi.fi>0 && prefMAX[x.fi.fi-1]>=x.fi.se) continue;
if(MAX[x.fi.fi]>x.fi.se) continue;
idontexist[x.fi]=1;
}
iexist.clear();
for(pair<pair<int,int>,int> x:idontexist){
iexist[x.fi]=1;
}
N=iexist.size();
int pointer=0;
for(pair<pair<int,int>,int> x:iexist){
r[pointer]=x.fi.fi;
c[pointer]=x.fi.se;
pointer++;
}
for(int i=0;i<N;i++){
R[i]=r[i];
C[i]=c[i];
}
K=min(K,N);
for(int i=0;i<=K;i++){
for(int j=0;j<N;j++){
dp[i][j]=1e14;
}
}
for(int i=0;i<N;i++){
dp[1][i]=(c[i]-r[0]+1)*(c[i]-r[0]+1);
}
for(int i=2;i<=K;i++){
deque<int> d;
d.push_back(i-2);
for(int j=i-1;j<N;j++){
while(d.size()>1 && m(i,d[0],d[1])<c[j]){
d.pop_front();
}
dp[i][j]=dp[i-1][d[0]]+(c[j]-r[d[0]+1]+1)*(c[j]-r[d[0]+1]+1)-(c[d[0]]-r[d[0]+1]+1)*(c[d[0]]-r[d[0]+1]+1);
while(d.size()>1 && m(i,d[d.size()-2], d[d.size()-1])>=m(i, d[d.size()-1], j)){
d.pop_back();
}
d.push_back(j);
}
}
int ans=1e18;
for(int i=1;i<=K;i++) ans=min(ans, dp[i][N-1]);
return ans;
}
# | 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... |