QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#488304 | #9127. Optimal Train Operation | XY_Eleven | WA | 2ms | 7332kb | C++23 | 7.0kb | 2024-07-23 20:38:16 | 2024-07-23 20:38:16 |
Judging History
answer
#include <bits/stdc++.h>
// #include <bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
//#pragma GCC optimize(3)
#define DB double
#define LL long long
#define ULL unsigned long long
#define in128 __int128
#define cint const int
#define cLL const LL
#define For(z,e1,e2) for(int z=(e1);z<=(e2);z++)
#define Rof(z,e1,e2) for(int z=(e2);z>=(e1);z--)
#define For_(z,e1,e2) for(int z=(e1);z<(e2);z++)
#define Rof_(z,e1,e2) for(int z=(e2);z>(e1);z--)
#define inint(e) scanf("%d",&e)
#define inll(e) scanf("%lld",&e)
#define inpr(e1,e2) scanf("%d%d",&e1,&e2)
#define in3(e1,e2,e3) scanf("%d%d%d",&e1,&e2,&e3)
#define outint(e) printf("%d\n",e)
#define outint_(e) printf("%d%c",e," \n"[i==n])
#define outint2_(e,e1,e2) printf("%d%c",e," \n"[(e1)==(e2)])
#define outll(e) printf("%lld\n",e)
#define outll_(e) printf("%lld%c",e," \n"[i==n])
#define outll2_(e,e1,e2) printf("%lld%c",e," \n"[(e1)==(e2)])
#define exc(e) if(e) continue
#define stop(e) if(e) break
#define ret(e) if(e) return
#define ll(e) (1ll*(e))
#define pb push_back
#define ft first
#define sc second
#define pii pair<int,int>
#define pli pair<LL,int>
#define vct vector
#define clean(e) while(!e.empty()) e.pop()
#define all(ev) ev.begin(),ev.end()
#define sz(ev) ((int)ev.size())
#define debug(x) printf("%s=%d\n",#x,x)
#define x0 __xx00__
#define y1 __yy11__
#define ffo fflush(stdout)
cLL mod=998244353,G=404;
// cLL mod[2]={1686688681ll,1888686661ll},base[2]={166686661ll,188868881ll};
template <typename Type> void get_min(Type &w1,const Type w2) { if(w2<w1) w1=w2; } template <typename Type> void get_max(Type &w1,const Type w2) { if(w2>w1) w1=w2; }
template <typename Type> Type up_div(Type w1,Type w2) { return (w1/w2+(w1%w2?1:0)); }
template <typename Type> Type gcd(Type X_,Type Y_) { Type R_=X_%Y_; while(R_) { X_=Y_; Y_=R_; R_=X_%Y_; } return Y_; } template <typename Type> Type lcm(Type X_,Type Y_) { return (X_/gcd(X_,Y_)*Y_); }
template <typename Type> Type md(Type w1,const Type w2=mod) { w1%=w2; if(w1<0) w1+=w2; return w1; } template <typename Type> Type md_(Type w1,const Type w2=mod) { w1%=w2; if(w1<=0) w1+=w2; return w1; }
void ex_gcd(LL &X_,LL &Y_,LL A_,LL B_) { if(!B_) { X_=1ll; Y_=0ll; return ; } ex_gcd(Y_,X_,B_,A_%B_); X_=md(X_,B_); Y_=(1ll-X_*A_)/B_; } LL inv(LL A_,LL B_=mod) { LL X_=0ll,Y_=0ll; ex_gcd(X_,Y_,A_,B_); return X_; }
template <typename Type> void add(Type &w1,const Type w2,const Type M_=mod) { w1=md(w1+w2,M_); } void mul(LL &w1,cLL w2,cLL M_=mod) { w1=md(w1*md(w2,M_),M_); } template <typename Type> Type pw(Type X_,Type Y_,Type M_=mod) { Type S_=1; while(Y_) { if(Y_&1) mul(S_,X_,M_); Y_>>=1; mul(X_,X_,M_); } return S_; }
template <typename Type> Type bk(vector <Type> &V_) { auto T_=V_.back(); V_.pop_back(); return T_; } template <typename Type> Type tp(stack <Type> &V_) { auto T_=V_.top(); V_.pop(); return T_; } template <typename Type> Type frt(queue <Type> &V_) { auto T_=V_.front(); V_.pop(); return T_; }
template <typename Type> Type bg(set <Type> &V_) { auto T_=*V_.begin(); V_.erase(V_.begin()); return T_; } template <typename Type> Type bk(set <Type> &V_) { auto T_=*prev(V_.end()); V_.erase(*prev(V_.end())); return T_; }
mt19937 gen(time(NULL)); int rd() { return abs((int)gen()); }
void main_init()
{
}
cint N=5100;
int n;
cLL inf=1e18;
LL c[N],a[N],dp[N];
array <int,2> h[N];
int get_tree()
{
stack <int> st; clean(st);
For(i,1,n)
{
int p=0;
while(!st.empty()&&a[st.top()]<a[i])
p=tp(st);
if(!st.empty())
h[st.top()][1]=i;
h[i][0]=p;
st.push(i);
}
// For(i,1,n) printf("%d: %d,%d\n",i,h[i][0],h[i][1]);
while(sz(st)>1) st.pop();
return st.top();
}
int id;
deque <int> v[N];
LL get(int k,LL w)
{
return (dp[k]-w*k);
}
struct Node
{
int l,r,mid;
LL w1,w2;
};
struct SegTree
{
Node t[N<<2];
void bld(int p,int l,int r)
{
int mid=l+r>>1;
t[p]={l,r,mid,0ll,inf};
ret(l>=r);
bld(p<<1,l,mid),
bld(p<<1|1,mid+1,r);
}
int L,R;
LL W1,W2;
LL fun(int x,LL w1,LL w2)
{
return (w1*x+w2);
}
void add2(int p,LL w1,LL w2)
{
// printf("%d:%lld,%lld\n",p,w1,w2);
if(fun(t[p].mid,w1,w2)<fun(t[p].mid,t[p].w1,t[p].w2))
swap(t[p].w1,w1),swap(t[p].w2,w2);
ret(t[p].l>=t[p].r);
if(fun(t[p].l,w1,w2)<fun(t[p].l,t[p].w1,t[p].w2)) add2(p<<1,w1,w2);
if(fun(t[p].r,w1,w2)<fun(t[p].r,t[p].w1,t[p].w2)) add2(p<<1|1,w1,w2);
}
void add(int p)
{
// printf("> [%d,%d]\n",p,t[p].l,t[p].r);
if(L<=t[p].l&&t[p].r<=R)
return add2(p,W1,W2);
if(L<=t[p].mid) add(p<<1);
if(R>t[p].mid) add(p<<1|1);
}
void add(int l,int r,LL w1,LL w2)
{
// printf("%d,%d %lld,%lld\n",l,r,w1,w2);
L=l,R=r,W1=w1,W2=w2;
add(1);
}
int K;
LL W;
void qry_(int p)
{
// printf("> %lld,%lld:%lld\n",t[p].w1,t[p].w2,K);
get_min(W,fun(K,t[p].w1,t[p].w2));
if(t[p].l<t[p].r) qry_(p<<1|(K>t[p].mid));
}
LL qry(int k)
{
// printf("qry %d\n",k);
K=k;
W=inf;
qry_(1);
return W;
}
}tr;
bool check(int i,int j,int k)
{
return ((dp[k]-dp[j])*(j-i)-(dp[j]-dp[i])*(k-j)<0ll);
}
int solve(int l,int r,int mid)
{
int p=++id;
if(!mid)
{
v[p].push_back(l);
get_min(dp[l],tr.qry(l)+c[l]);
return p;
}
// printf("[%d,%d]:%d\n",l,r,mid);
v[p].swap(v[solve(l,mid-1,h[mid][0])]);
LL w=a[mid];
while(sz(v[p])>1&&get(v[p][0],w)>=get(v[p][1],w))
v[p].pop_front();
// printf("??? %d,%d,%lld,%lld\n",mid,r,w,get(v[p][0],w));
tr.add(mid,r,w,get(v[p][0],w));
// LL t=get(v[p][0],w);
// For(i,mid,r) get_min(dp[i],i*w+t+c[i]);
int p2=solve(mid,r,h[mid][1]);
if(sz(v[p2])<sz(v[p]))
{
int len=sz(v[p])-1;
for(auto i:v[p2])
{
while(len>=1&&!check(v[p][len-1],v[p][len],i))
v[p].pop_back(),len--;
v[p].push_back(i); len++;
}
v[p2].clear();
}
else
{
v[p].swap(v[p2]);
int len=sz(v[p])-1;
for(auto i:v[p2])
{
while(len>=1&&!check(i,v[p][0],v[p][1]))
v[p].pop_front(),len--;
v[p].push_front(i); len++;
}
v[p2].clear();
}
return p;
}
void main_solve()
{
inint(n);
For(i,1,n) inll(a[i]);
For_(i,1,n) inll(c[i]);
int rt=get_tree();
id=0;
tr.bld(1,0,n);
For(i,1,n) dp[i]=inf;
solve(0,n,rt);
// For(i,0,n) outll_(dp[i]);
outll(dp[n]);
}
int main()
{
// ios::sync_with_stdio(0); cin.tie(0);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
// srand(time(NULL));
main_init();
// int _; inint(_); For(__,1,_) // T>1 ?
// printf("\n------------\n\n"),
main_solve();
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7328kb
input:
4 3 1 4 1 5 9 2
output:
15
result:
ok "15"
Test #2:
score: 0
Accepted
time: 2ms
memory: 7332kb
input:
9 28 35 19 27 84 98 78 79 60 40 35 54 63 72 71 27 94
output:
682
result:
ok "682"
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 7328kb
input:
7 476190629 262703497 211047202 971407775 628894325 731963982 822804784 877914575 602436426 24979445 861648772 623690081 433933447
output:
6799854425
result:
wrong answer 1st words differ - expected: '5339182432', found: '6799854425'