QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#476700#9127. Optimal Train Operationucup-team1447#WA 1ms13936kbC++174.4kb2024-07-13 20:47:482024-07-13 20:47:50

Judging History

你现在查看的是最新测评结果

  • [2024-07-13 20:47:50]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:13936kb
  • [2024-07-13 20:47:48]
  • 提交

answer

// what is matter? never mind. 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
typedef int iint;
#define int long long
#define ull unsigned long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;

//#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//char buf[1<<21],*p1=buf,*p2=buf;
inline int read()
{
	char c=getchar();int x=0;bool f=0;
	for(;!isdigit(c);c=getchar())f^=!(c^45);
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;return x;
}

#define mod 998244353
struct modint{
	int x;
	modint(int o=0){x=o;}
	modint &operator = (int o){return x=o,*this;}
	modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
	modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
	modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
	modint &operator ^=(int b){
		modint a=*this,c=1;
		for(;b;b>>=1,a*=a)if(b&1)c*=a;
		return x=c.x,*this;
	}
	modint &operator /=(modint o){return *this *=o^=mod-2;}
	friend modint operator +(modint a,modint b){return a+=b;}
	friend modint operator -(modint a,modint b){return a-=b;}
	friend modint operator *(modint a,modint b){return a*=b;}
	friend modint operator /(modint a,modint b){return a/=b;}
	friend modint operator ^(modint a,int b){return a^=b;}
	friend bool operator ==(modint a,modint b){return a.x==b.x;}
	friend bool operator !=(modint a,modint b){return a.x!=b.x;}
	bool operator ! () {return !x;}
	modint operator - () {return x?mod-x:0;}
	bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}

vector<modint> fac,ifac,iv;
inline void initC(int n)
{
	if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
	int m=iv.size(); ++n;
	if(m>=n)return;
	iv.resize(n),fac.resize(n),ifac.resize(n);
	For(i,m,n-1){
		iv[i]=iv[mod%i]*(mod-mod/i);
		fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
	}
}
inline modint C(int n,int m){
	if(m<0||n<m)return 0;
	return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;

#define maxn 500055
#define inf 0x3f3f3f3f3f3f3f3f

int n,c[maxn],a[maxn],f[maxn];

int L[maxn],R[maxn];

int tp;
pii t[maxn];

bool chk(pii a,pii b,pii c){
	if(b.fi==c.fi && b.se>=c.se) return 1;
	if(a.fi>c.fi) swap(a,c);
	// bcjiaodian <= ac jiaodian x 
	return (__int128)(b.se-c.se)*(c.fi-a.fi) >= (__int128)(a.se-c.se)*(c.fi-b.fi);
}
void add(int k,int b){
	// k inc
	if(t[tp].fi==k && t[tp].se<=b) return;
	t[++tp]=mkp(k,b);
	while(tp>=3 && chk(t[tp-2],t[tp-1],t[tp])) t[tp-1]=t[tp],--tp;
}
int F(pii t,int x){
	return t.fi*x+t.se;
}
int ask(int x){
	if(!tp) return inf;
	//For(i,1,tp) cout<<F(t[i],x)<<" "; cout<<"\n";
	//int res=inf; For(i,1,tp) res=min(res,F(t[i],x)); return res;
	while(tp>=2 && F(t[tp-1],x)<F(t[tp],x)) --tp;
	return F(t[tp],x);
}

void solve(int l,int r)
{
	if(l==r)return /*cout<<"F: "<<l<<" "<<f[l]<<"\n",*/void();
	int mid=l+r>>1;
	solve(l,mid);
	
	//cout<<"solve "<<l<<" "<<r<<"\n";
	
	int mx=-inf;
	Rep(i,mid,l) mx=max(mx,c[i]),L[i]=mx;
	mx=-inf;
	For(i,mid+1,r) mx=max(mx,c[i-1]),R[i]=mx;
	
	int p1=mid,p2=mid+1; tp=0;
	while(p1>=l || p2<=r){
		int x=(p1>=l?L[p1]:inf);
		int y=(p2<=r?R[p2]:inf);
	//	cout<<"xy "<<x<<" "<<y<<"\n";
		if(x<=y){
//			cout<<"add "<<-p1<<" "<<f[p1]<<"\n";
			// k inc
			add(-p1,f[p1]);
			--p1;
		}else{
			// ask inc
	//		cout<<"ask "<<p2<<"\n";
			f[p2]=min(f[p2],a[p2]+ask(y)+p2*y);
			++p2;
		}
	}
	
	p1=l,p2=r; tp=0;
	while(p1<=mid || p2>=mid+1){
		int x=(p1<=mid?L[p1]:-inf);
		int y=(p2>=mid+1?R[p2]:-inf);
	//	cout<<"XY "<<x<<" "<<y<<"\n";
		if(x>y){
		//	cout<<"Add "<<x<<" "<<-p1*x+f[p1]<<"\n";
			// k dec
			add(x,-p1*x+f[p1]);
			++p1;
		}else{
			// ask dec
	//		cout<<"Ask "<<p2<<"\n";
			f[p2]=min(f[p2],a[p2]+ask(p2));
			--p2;
		}
	}
	
	solve(mid+1,r);
}

signed main()
{
	n=read();
	For(i,0,n-1)c[i]=read();
	For(i,1,n-1)a[i]=read();
	For(i,1,n+2)f[i]=inf;
	solve(0,n);
	cout<<f[n];
	return 0;
}
/*

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13936kb

input:

4
3 1 4 1
5 9 2

output:

15

result:

ok "15"

Test #2:

score: 0
Accepted
time: 0ms
memory: 13768kb

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: 0
Accepted
time: 1ms
memory: 13868kb

input:

7
476190629 262703497 211047202 971407775 628894325 731963982 822804784
877914575 602436426 24979445 861648772 623690081 433933447

output:

5339182432

result:

ok "5339182432"

Test #4:

score: 0
Accepted
time: 1ms
memory: 13800kb

input:

8
910286918 802329211 404539679 303238506 317063340 492686568 773361868 125660016
430302156 982631932 161735902 880895728 923078537 707723857 189330739

output:

6166732840

result:

ok "6166732840"

Test #5:

score: 0
Accepted
time: 1ms
memory: 13848kb

input:

5
191890310 576823355 782177068 404011431 818008580
839296263 462224593 492601449 384836991

output:

4069897043

result:

ok "4069897043"

Test #6:

score: -100
Wrong Answer
time: 1ms
memory: 13872kb

input:

6
138996221 501899080 353195922 545531545 910748511 350034067
160449218 155374934 840594328 164163676 797829355

output:

4167786878

result:

wrong answer 1st words differ - expected: '3921700772', found: '4167786878'