题意:
有 n n n个地点,第 i i i个地点需要接入 a i a_i ai 个网络,第 i i i个地点存在一个基站,能为 i i i和 ( i + 1 ) % n (i+1)\%n (i+1)%n 提供 b i b_i bi的网络,求是否有一种分配方法,使得每个地点都能够满足。
题解:
当确定 b 1 b_1 b1 分配多少给 a 1 a_1 a1后,那么就可以在线性时间判断是否满足。
所以考虑二分 b 1 b_1 b1 为 a 1 a_1 a1 分配多少。
1. 1. 1. 当出现某个地点无法满足时,说明 b 1 b_1 b1需要减少分配给 a 1 a_1 a1 的量
2. 2. 2. 当其他地点都能满足,且 b n b_n bn 剩余出来的与二分值相加能满足 a 1 a_1 a1 ,那么就是 Y E S YES YES
3. 3. 3. 当其他地点都能满足,且 b n b_n bn 剩余出来的与二分值相加不能满足 a 1 a_1 a1 ,那么说明 b 1 b_1 b1 需要加大给 a 1 a_1 a1分配的量。
代码:
#pragma GCC diagnostic error "-std=c++11"
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int MAXN=1e6+5;
const int inf=0x3f3f3f3f;
int a[MAXN], b[MAXN];
int check(int x,int n)
{
int nex = b[1] - x;
for (int i = 2; i <= n;i++){
int c = a[i];
c = max(c - nex, 0);
if(c>b[i])
return -1;
nex = b[i] - c;
}
return nex;
}
int main()
{
iss;
cin.tie(0);
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for (int i = 1; i <= n;i++){
cin >> a[i];
}
for (int i = 1; i <= n;i++){
cin >> b[i];
}
int l = 0, r = b[1];
int flag = 0;
while(l<=r){
int mid = (l + r) >> 1;
int c = check(mid, n);
if(c==-1){
r = mid - 1;
}
else{
if(c+mid>=a[1]){
flag = 1;
break;
}
else{
l = mid + 1;
}
}
}
if(flag)
cout << "YES" << "\n";
else
cout << "NO" << "\n";
}
}