이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pb emplace_back
template<class T> void out(T a){cout<<a<<endl;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
const ll inf=1001001001001001001;
struct CHT{
deque<PP> deq;
void insert(ll id,ll a,ll b){
ll tmp;
while(deq.size()>1){
long double a1,b1,a2,b2;tie(a1,b1,tmp)=deq[deq.size()-2];tie(a2,b2,tmp)=deq.back();
long double x=(b1-b)/(a-a1);
if(a2*x+b2<a*x+b)break;
deq.pop_back();
}
deq.pb(a,b,id);
}
P sol(ll x){
while(deq.size()>1){
if(get<0>(deq[0])*x+get<1>(deq[0])<get<0>(deq[1])*x+get<1>(deq[1]))break;
deq.pop_front();
}
return P(get<0>(deq[0])*x+get<1>(deq[0]),get<2>(deq[0]));
}
};
long long take_photos(int n, int m, int K, std::vector<int> r, std::vector<int> c) {
vp srt(n);
rep(i,n)srt[i]=P(max(r[i],c[i]),min(r[i],c[i]));
sort(all(srt));
vp v;v.pb(-1,-1);
for(auto x:srt){
while(v.size()&&v.back().fi>=x.se)v.pop_back();
if(v.size()==0||v.back().se<x.fi)v.pb(x.se,x.fi);
}
n=v.size();chmin(K,n-1);
auto sol=[&](ll lambda){
vp dp(n);
CHT cht;
dp[0]=P(0,0);
rep(i,n){
if(i){
auto res=cht.sol(v[i].se);
dp[i].fi=res.fi+v[i].se*v[i].se+lambda;
dp[i].se=dp[res.se].se+1;
}
if(i<n-1)cht.insert(i,2*(-v[i+1].fi+1),dp[i].fi+(-v[i+1].fi+1)*(-v[i+1].fi+1)-max(0ll,v[i].se-v[i+1].fi+1)*max(0ll,v[i].se-v[i+1].fi+1));
}
return dp[n-1];
};
if(K==n-1)return sol(0).fi;
ll ok=(ll)(m)*(ll)(m)+5,ng=-1;
while(ok-ng>1){
ll md=(ok+ng)/2;
auto res=sol(md);
if(res.se<=K)ok=md;
else ng=md;
}
P r1=sol(ok),r2=sol(ng);
r1.fi-=r1.se*ok;r2.fi-=r2.se*ng;
if(r1.se+r1.fi-(ng*(r2.se-r1.se)+r2.fi)>=K)return r1.fi-ok*(K-r1.se);
return r2.fi+ng*(r2.se-K);
}
# | 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... |