Codeforces 1373F. Network Coverage(二分)

传送门

题意:

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";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值