求hack
查看原帖
求hack
593299
Qerucy楼主2023/10/30 22:29

rt,写的单调栈,luogu 民间数据 90,CCF 的数据 100。

主要是不知道哪炸了。

代码:

#include<cstdio>
#include<stack>

using namespace std;

#define int long long

const int N=1e5+10;

int a[N],v[N];
int n,d;
int sum;
int pre[N];
int qz[N];
int ans,now;

stack<int>q;

signed main(){
	//freopen("road.in","r",stdin);
	//freopen("road.out","w",stdout);
	scanf("%lld%lld",&n,&d);
	for(int i=1;i<=n-1;i++){
		scanf("%lld",&v[i]);
		sum+=v[i];
		qz[i]=qz[i-1]+v[i];
	}
	
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	
	for(int i=n-1;i>=1;i--){
		while(!q.empty()&&a[q.top()]>=a[i]) q.pop();
		if(q.empty()) pre[i]=0;
		else pre[i]=q.top();
		q.push(i);
	}
	
	for(int i=1;i<=n-1;){
		
		if(pre[i]==0){
			int need=qz[n-1]-qz[i-1]-now;
			int qwq;
			
			if(need%d==0) qwq=need/d;
			else qwq=need/d+1;
			
			now=max(1ll*0,qwq*d-need);
			ans+=max(qwq,1ll*0)*a[i];
			
			break;
			
		}
		
		else{
			int need=qz[pre[i]-1]-qz[i-1]-now;
			int qwq;
			if(need%d==0) qwq=need/d;
			else qwq=need/d+1;
			
			now=max(1ll*0,qwq*d-need);
			ans+=max(qwq,1ll*0)*a[i];
			i=pre[i];
			
		}
	}
	printf("%lld",ans);
	return 0;
}
/*
5 4
10 10 10 10
9 8 9 6 5

5 4
10 20 30 40
9 8 9 6 5
*/
2023/10/30 22:29
加载中...