F - Network Coverage
题目大意
有一个环,1~n 代表n个城市,n个城市围成一个环(1与n相连)。每个城市有ai家用户要网。现在有n个供网的在每两个城市之间,bi代表这个供网的最多可以给几家用户供网,每个供网的只能给他旁边两个城市的用户供网,问最后能不能让所有的人有网。
日常不会做
题解
不会做,于是看别人的题解。
为什么我想不到? 因为我菜?
给的是一个环,如果是一个链怎么做?肯定是从左边往右边走,每次把左边的都连完,看能不能连右边的,对吧。只能是那一种情况,
现在有个环,也就是左边还有一个可以供,这时,先不管那个,先看成一条链,尽量把第一个的往右边身上连,能连多少连多少,但是前提是后边的都可以成立。然后 就可以二分一个第一个连到他右边的数量,然后判断后面依次的情况。
最后看一下这个答案符不符合就可以了。
代码:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <cstring>
#include <string>
#include <bitset>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
#define st first
#define sd second
#define mkp make_pair
#define pb push_back
void wenjian(){freopen("concatenation.in","r",stdin);freopen("concatenation.out","w",stdout);}
void tempwj(){freopen("hash.in","r",stdin);freopen("hash.out","w",stdout);}
ll gcd(ll a,ll b){return b == 0 ? a : gcd(b,a % b);}
ll qpow(ll a,ll b,ll mod){a %= mod;ll ans = 1;while(b){if(b & 1)ans = ans * a % mod;a = a * a % mod;b >>= 1;}return ans;}
struct cmp{bool operator()(const pii & a, const pii & b){return a.second > b.second;}};
int lb(int x){return x & -x;}
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 9901;
const int M = 10000007;
const int maxn = 1e6+5;
int a[maxn];
int b[maxn];
int c[maxn];
int d[maxn];
int n;
bool pd(int x)
{
for (int i = 1; i <= n; i ++ )
{
c[i] = a[i];
d[i] = b[i];
}
c[1] = x;
for (int i = 1; i <= n; i ++ )
{
d[i] -= c[i];
if(d[i] < 0)
return false;
c[i + 1] = max(c[i + 1] - d[i],0);
}
return true;
}
bool pdd(int x)
{
for (int i = 1; i <= n; i ++ )
{
c[i] = a[i];
d[i] = b[i];
}
c[1] = x;
for (int i = 1; i <= n; i ++ )
{
d[i] -= c[i];
if(d[i] < 0)
return false;
c[i + 1] = max(c[i + 1] - d[i],0);
}
int y = a[1] - x;
if(y <= d[n])
return true;
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T -- )
{
scanf("%d",&n);
for (int i = 1; i <= n; i ++ )
scanf("%d",&a[i]);
for (int i = 1; i <= n; i ++ )
scanf("%d",&b[i]);
int l = 0, r = a[1];
while(l < r)
{
int mid = l + r + 1 >> 1;
if(pd(mid))
l = mid;
else
r = mid - 1;
}
// printf("%d\n", l);
if(pdd(l))
{
printf("YES\n");
}
else
printf("NO\n");
}
}