이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
// #define COMPILE 1
// #define BRUTE
bool DEBUG=0;
typedef long long ll;
typedef pair<ll,ll> ii;
#define db(x) cout<<#x<<" = "<<x<<"\n";
#define fore(i,a,b) for(ll i=a,ggdem=b;i<ggdem;i++)
#define FIN ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define SZ(a) ((ll)(a).size())
#define ALL(a) a.begin(),a.end()
#define mset(a,b) memset(a,b,sizeof(a));
#define pb push_back
#define fst first
#define snd second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// x,y -> -y,x
ll sq(ll x){return x*x;}
#ifdef BRUTE
const ll N=1<<10,INF=1e18;
#else
const ll N=1<<20,INF=1e18;
#endif
ll n,m,k,h,to[N],vis[N],qid;
ii dp[N];
vector<ii>v;
#ifdef BRUTE
ll mat[N][N];
ii dp_brute[N];
#endif
struct Line{ll m,h,id;};
struct CHT { // for minimum (for maximum view comments)
vector<Line> c;
int pos=0;
ll in(Line a, Line b){
ll x=b.h-a.h,y=a.m-b.m;
return x/y+(x%y?!((x>0)^(y>0)):0); // ==ceil(x/y)
}
void add(ll m, ll h,ll id){ // m's should be non increasing
if(DEBUG) {
cout<<"add "<<m<<"*x + "<<h<<","<<id<<endl;
}
Line l=(Line){m,h,id};
if(SZ(c)&&m==c.back().m){
l.h=min(h,c.back().h);c.pop_back();if(pos)pos--; // max for min
}
// nonInc->nonDec : <= for >=
while(SZ(c)>1&&in(c.back(),l)<=in(c[SZ(c)-2],c.back())){ // <= for >
c.pop_back();if(pos)pos--;
}
c.pb(l);
}
inline bool fbin(ll x, int m){return in(c[m],c[m+1])>x;} // <= for >
ii eval(ll x){
// nonInc->nonDec : swap all fbin for !fbin
// O(log n) query:
int s=0,e=SZ(c);
while(e-s>1){
int m=(s+e)/2;
if(fbin(x,m-1))e=m; // nonInc->nonDec : fbin for !fbin
else s=m;
}
ii res={c[s].m*x+c[s].h,c[s].id};
if(0<s)res=min(res,{c[s-1].m*x+c[s-1].h,c[s-1].id});
if(s+1<SZ(c))res=min(res,{c[s+1].m*x+c[s+1].h,c[s+1].id});
return res;
// O(1) query (for non-decreasing ordered x's):
// while(pos>0&&fbin(x,pos-1))pos--;
// while(pos<SZ(c)-1&&!fbin(x,pos))pos++;
// return {c[pos].m*x+c[pos].h,c[pos].id};
}
};
#ifdef BRUTE
ii solve_brute(ll i) {
if(i==n)return {0ll,0ll};
if(vis[i]==qid)return dp_brute[i];
vis[i]=qid;
ii res={INF,INF};
fore(k,i,n) {
ii a=solve_brute(k+1);
ll bc=mat[i][k];
res=min(res,{a.fst+bc+h,a.snd+1});
// cout<<i<<" to "<<k<<" with "<<cost<<" "<<a.fst<<"+"<<b<<"-"<<c<<"+"<<h<<endl;
}
if(DEBUG)cout<<"brute "<<i<<" : "<<res.fst<<","<<res.snd<<endl;
return dp_brute[i]=res;
}
#endif
void add(CHT &cht, int i,int h) {
int A=dp[i+1].fst;
int B=to[i]+1;
int C=sq(max(0ll,to[i]-v[i+1].fst+1));
int D=A-C+h+B*B;
cht.add(2*B,D,dp[i+1].snd);
}
ii solve(int h) {
CHT cht;
dp[n]={0,0};
// dp[n-1]={sq(to[n-1]-v[n-1].fst+1)+h,1};
// add(cht,n-1);
if(DEBUG)cout<<"solve "<<n-1<<" : "<<dp[n-1].fst<<" "<<dp[n-1].snd<<endl;
for(int i=n-1;i>=0;i--) {
add(cht,i,h);
auto it=cht.eval(-v[i].fst);
dp[i]={it.fst+sq(v[i].fst),it.snd+1};
if(DEBUG)cout<<"solve "<<i<<" : "<<dp[i].fst<<" "<<dp[i].snd<<endl;
}
return dp[0];
}
struct STree{
int n;vector<int> t;
STree(int n):n(n),t(2*n+5,-1){}
void upd(int p, int value){
for(t[p += n]=value;p>1;p>>=1) t[p>>1]=max(t[p],t[p^1]);
}
int query(int l, int r){ // [l, r)
int res=-1;
for(l+=n,r+=n;l<r;l>>=1,r>>=1){
if(l&1) res=max(res,t[l++]);
if(r&1) res=max(res,t[--r]);
}
return res;
}
};
vector<ii> gen(vector<int> &r,vector<int> &c) {
vector<ii>v;
fore(i,0,SZ(r)){
if(r[i]>c[i])swap(r[i],c[i]);
v.pb({r[i],-c[i]});
}
sort(ALL(v));
STree st(m+1);
vector<ii>res;
for(auto it:v) {
if(st.query(0,it.fst+1)>=-it.snd){
// cout<<"skip "<<it.fst<<","<<-it.snd<<endl;
continue;
}
// cout<<"add "<<it.fst<<","<<-it.snd<<endl;
res.pb({it.fst,-it.snd});
st.upd(it.fst,-it.snd);
}
return res;
}
ll take_photos(int _n,int _m,int _k,vector<int>r,vector<int> c) {
n=_n,m=_m,k=_k;
map<ll,ll>mp;
v=gen(r,c);
n=SZ(v);
fore(i,0,n) {
ll pr=mp[v[i].fst];
mp[v[i].fst]=max(pr,1ll*v[i].snd);
}
v.pb({INF,INF});
if(DEBUG) {
cout<<"v: ";for(auto it:v)cout<<it.fst<<","<<it.snd<<" ";cout<<endl;
}
ll y=0;
fore(i,0,n) {
y=max(y,mp[v[i].fst]);
to[i]=y;
}
if(DEBUG) {
cout<<"v: "<<endl;for(auto it:v)cout<<it.fst<<" "<<it.snd<<endl;cout<<"n: "<<n<<endl;
cout<<"to: ";fore(i,0,n) cout<<to[i]<<" ";cout<<endl;
}
#ifdef BRUTE
fore(i,0,n)fore(j,i,n)mat[i][j]=sq(to[j]-v[i].fst+1)-sq(max(0ll,to[j]-v[j+1].fst+1));
#endif
// fore(i,0,20) {
// h=i,qid++;
// cout<<i<<" : "<<solve_brute(0).fst<<" "<<solve_brute(0).snd<<endl;
// }
ll ml=0,mr=sq(m+1)+10ll;
while(ml<=mr) {
h=(ml+mr)/2,qid++;
if(DEBUG)cout<<"start: "<<h<<endl;
ii a=solve(h);
#ifdef BRUTE
ii b=solve_brute(0);
if(a!=b) {
cout<<"for "<<h<<" expected "<<b.fst<<","<<b.snd<<" got "<<a.fst<<","<<a.snd<<endl,exit(0);
}
#endif
if(a.snd<=k)mr=h-1;
else ml=h+1;
}
#ifdef DEBUG
cout<<"done: "<<ml<<" "<<mr<<endl;
#endif
h=ml,qid++;
ii res=solve(ml);
// cout<<ml<<" : "<<res.fst<<" "<<res.snd<<endl;
return res.fst-ml*k;
}
#ifdef COMPILE
int main() {FIN;
cin>>n>>m>>k;
vector<int>r(n),c(n);
fore(i,0,n)cin>>r[i]>>c[i];
cout<<take_photos(n,m,k,r,c)<<"\n";
}
#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... |