F. Network Coverage
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The government of Berland decided to improve network coverage in his country. Berland has a unique structure: the capital in the center and nn cities in a circle around the capital. The capital already has a good network coverage (so the government ignores it), but the ii-th city contains aiai households that require a connection.
The government designed a plan to build nn network stations between all pairs of neighboring cities which will maintain connections only for these cities. In other words, the ii-th network station will provide service only for the ii-th and the (i+1)(i+1)-th city (the nn-th station is connected to the nn-th and the 11-st city).
All network stations have capacities: the ii-th station can provide the connection to at most bibi households.
Now the government asks you to check can the designed stations meet the needs of all cities or not — that is, is it possible to assign each household a network station so that each network station ii provides the connection to at most bibi households.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains the single integer nn (2≤n≤1062≤n≤106) — the number of cities and stations.
The second line of each test case contains nn integers (1≤ai≤1091≤ai≤109) — the number of households in the ii-th city.
The third line of each test case contains nn integers (1≤bi≤1091≤bi≤109) — the capacities of the designed stations.
It's guaranteed that the sum of nn over test cases doesn't exceed 106106.
Output
For each test case, print YES, if the designed stations can meet the needs of all cities, or NO otherwise (case insensitive).
Example
input
Copy
5 3 2 3 4 3 3 3 3 3 3 3 2 3 4 4 2 3 4 5 3 7 2 2 4 4 5 2 3 2 3 2 7 2 1 1 10 10
output
Copy
YES YES NO YES YES
Note
In the first test case:
In the second test case:
In the third test case, the fourth city needs 55 connections, but the third and the fourth station has 44 connections in total.
题意:
n(<=1e6)个城市构成一个环,每个城市有人口a[i](<=1e9),容量b[i] (<=1e9),第i个城市的人可以留在第i-1或者第i个城市,问能否使所有城市的人口不超过其容量,能则输出YES,否则NO。
思路:有两种做法,第一种做法比较直观,就是 二分 第1个城市 留给 第1个城市的人 的容量。然后剩下的容量留给第i+1个城市的人,以此类推,最后判断第n个城市的剩余容量和第一个城市留下的容量是否足够容纳a[1],如果可以,则直接为YES。否则如果是中间某个城市人口不够了,我们就减少 第1个城市 留给 第1个城市的人 的容量,如果是最后a[1]容不下了,就提高留下的容量。时间复杂度O(nlog(1e9))
第二种做法就是开一个数组pre[i]记录i城市的人留在i-1城市的人数,b[i]表示当前城市的剩余容量。
初始先将b[1]全部留给a[2],中间若有容量不足直接NO,否则从第n-1个城市开始:b[i-1]+=min(b[i],pre[i]) (好好思考这个式子)。最后判断b[1]+b[n]是否大于等于a[1]即可。时间复杂度O(n)
代码(二分):
#include
#define ll long long
#define inf 0x3f3f3f3f
#define mst(head,x,n) memset(head+1,x,n*sizeof(head[0]))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dep(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int maxn=2e6+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,k;
int a[maxn],c[maxn],b[maxn];
int ans,tmp,cnt;
int flag;
int d[maxn];
bool ok[maxn];
template
inline void read(T &X){
X=0;int w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
if(w) X=-X;
}
int jud(int x){
int res=b[0]-x;
rep(i,1,n-1){
if(a[i]>res+b[i]) return 0;
if(res>=a[i]) res=b[i];
else res=b[i]-(a[i]-res);
}
if(res+x>=a[0]) return 1;
return 2;
}
void solve(){
rep(i,0,n-1) read(a[i]);
rep(i,0,n-1) read(b[i]);
int l=0,r=b[0];
while(l<=r){
int mid=(l+r)>>1;
int ans=jud(mid);
if(ans==1) {
puts("YES");
return ;
}
if(!ans) r=mid-1;
else l=mid+1;
}
puts("NO");
}
int main(){
/*
#ifdef ONLINE_JUDGE
#else
freopen("D:/Temp/in.txt", "r", stdin);
#endif
*/
int T,cas=1;
read(T);
while(T--)
{
read(n);
solve();
}
return 0;
}
代码(pre记录):
#include
#define ll long long
#define inf 0x3f3f3f3f
#define mst(head,x,n) memset(head+1,x,n*sizeof(head[0]))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dep(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int maxn=2e6+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,k;
ll a[maxn],c[maxn],b[maxn];
int ans,tmp,cnt;
int flag;
ll pre[maxn];
bool ok[maxn];
template
inline void read(T &X){
X=0;int w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
if(w) X=-X;
}
int solve(){
read(n);
rep(i,0,n-1) read(a[i]);
rep(i,0,n-1) read(b[i]);
rep(i,0,n-1){
if(b[(i-1+n)%n]+b[i]=a[i]){
pre[i]=a[i];
b[i-1]-=a[i];
}
else {
pre[i]=b[i-1];
b[i-1]=0;
b[i]-=(a[i]-pre[i]);
if(b[i]<0) return 0;
}
}
dep(i,n-2,1){
b[i-1]+=min(pre[i],b[i]);
if(b[i-1]<0) return 0;
}
if(b[0]+b[n-1]>=a[0]) return 1;
return 0;
}
int main(){
/*
#ifdef ONLINE_JUDGE
#else
freopen("D:/Temp/in.txt", "r", stdin);
#endif
*/
int T,cas=1;
read(T);
while(T--)
{
if(solve()) puts("YES");
else puts("NO");
}
return 0;
}