跳到正文

个人记录

该变量不应为空。

fan_xiaoyi约 4 分钟
原文
文章信息与版本

文章信息

收录
2026年4月15日
更新
2026年4月15日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1
C++
#include<bits/stdc++.h>
#define fir first
#define sec second
#define int long long
#define pii pair<int,int>
#define fep(i,s,e) for(int i=s;i<e;i++)
#define pef(i,s,e) for(int i=s;i>e;i--)
#define rep(i,s,e) for(int i=s;i<=e;i++)
#define per(i,s,e) for(int i=s;i>=e;i--)
namespace FastIO{
	template<typename T>inline void read(T &x){
		x=0;int f=1;char c=getchar();
		for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
		for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);x*=f;
	}
	template<typename T,typename...Args>
    inline void read(T &x,Args&...args){
        read(x);
        read(args...);
    }
	template<typename T>void print(T x){
		if(x<0)x=-x,putchar('-');
		if(x>9)print(x/10);
		putchar((x%10)^48);
	}
}
using namespace std;
using namespace FastIO;
const int N=2e6+47;
const int inf=1e13;
int T,n,a[N],b[N],nxt[N];
bool check(int t){
	int cnt=0,rmb=0;
	rep(i,1,n){
		rmb+=b[i];
		int mx=t*(nxt[i]-1)-cnt;//最多买到足够到达下一次更便宜的天数的
		mx=max(mx,(int)0);//就是这一步,不加会WA
		if(mx%a[i]!=0){
			mx=mx/a[i]*a[i];
			mx+=a[i];
		}
		int mi=t*i-cnt;//至少能买够今天的
		if(rmb*a[i]<mi)return false;//剩下的钱连今天的都买不了
		int cur=min(mx,rmb*a[i]);//最多把当前所剩的钱花完
		cur=max(cur,(int)0);//至少买0元的,不存在负数
		int _rmb=cur/a[i];
//		cout<<t<<" "<<i<<" "<<mi<<" "<<mx<<" "<<cnt<<" "<<rmb<<"\n";
		rmb-=_rmb;cnt+=cur;
	}
	return true;
}
signed main(){
//	freopen("fusu.in","r",stdin);
//	freopen("fusu.out","w",stdout);
	read(T);
	while(T--){
		read(n);
		rep(i,1,n)read(a[i]);
		rep(i,1,n)read(b[i]);
		int l=0,r=inf,ans=0;
		stack<pii>s;
		s.push({inf,0});
		rep(i,1,n){
			while(a[i]>=s.top().fir){
				nxt[s.top().sec]=i;
				s.pop();
			}
			s.push({a[i],i});
		}
		while(s.size()){
			nxt[s.top().sec]=n+1;
			s.pop();
		}
		while(l<=r){
			int mid=(l+r)/2;
			if(check(mid)){
				l=mid+1;
				ans=mid;
			}
			else r=mid-1;
		}
		print(ans);
		puts("");
	}
}
/*
3
5
1 2 3 4 5
5 4 3 2 1
5
1 1 1 1 1
2 3 4 5 6
9
9 9 8 2 4 4 3 5 3
10 10 10 10 10 10 10 10 10
*/
原文