Educational Codeforces Round 90 (Rated for Div. 2) F - Network Coverage 二分
#include<map> #include<queue> #include<time.h> #include<limits.h> #include<cmath> #include<ostream> #include<iterator> #include<set> #include<stack> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep_1(i,m,n) for(int i=m;i<=n;i++) #define mem(st) memset(st,0,sizeof st) int read() { int res=0,ch,flag=0; if((ch=getchar())=='-') //判断正负 flag=1; else if(ch>='0'&&ch<='9') //得到完整的数 res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; return flag?-res:res; } typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; typedef pair<double,double> pdd; const int inf = 0x3f3f3f3f; const int N=1000000 + 10; int a[N]; int b[N]; int a1[N]; int b1[N]; int n; int sum; bool check(int mid) { for(int i=1; i<=n; i++) a1[i]=a[i],b1[i]=b[i]; a1[1]=mid; for(int i=1; i<=n; i++) { b1[i]-=a1[i]; if(b1[i]<0) return false; a1[i+1]-=b1[i]; a1[i+1]=max(a1[i+1],0); if(i==n) sum=mid+b1[i]; } return true; } void solve() { n=read(); for(int i=1; i<=n; i++) a[i]=read(); for(int i=1; i<=n; i++) b[i]=read(); int l=0,r=b[1]+1; while(l<r) { int mid=(l+r+1)>>1; if(check(mid)) l=mid; else r=mid-1; } if(!check(l)) { cout<<"NO"<<endl; return ; } if(sum<a[1]) { cout<<"NO"<<endl; return ; } cout<<"YES"<<endl; } int main() { int t; cin>>t; while(t--) solve(); return 0; }