Check Problems---二分

任意门

Now, n people are checking the problems. There are 101010 problems in total and numbered from 1 to 101010. The i-th person starts the check from the ai problem. It takes one minute to check a problem. More formally, the problem with the number ai+j−1 will be checked by the i-th person at the j-th minute.

Now, your task is to answer q queries. Each query will give you an integer t, please calculate the number of problems that have been tested at least once after t minutes.

Input
The first line contains a single integer n(1≤n≤5⋅105) indicating the number of people.

The second line contains n integers a1,a2,⋯,an (1≤a1≤a2≤⋯≤an≤1018) indicating the problem number of the first check for the i-th person.

The next line contains a single integer q (1≤q≤5⋅105) indicating the number of queries.

Each of the next q lines contains an integer t (0≤t≤1018).

it is guaranteed that ai+1−ai≤ai+2−ai+1 for every 1≤i≤n−2.

Output
For each query, output the total number of problems that have been tested at least once after t minutes in one line.

这道题目里保证了两个数之间的差值是越来越大的,那这样子题目就会简单很多,我们就可以很自然的想到二分查找。

upper_bound(a+m,a+n,b):二分查找a当中从m到n中大于b的第一项

lower_bound(a+m,a+n,b):二分查找a当中从m到n中大于等于b的第一项

upper_bound和lower_bound在使用前务必!务必要进行排序!

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
 
#define int long long
 
const int maxn = 500100;
int n;
int a[maxn];
int sum[maxn];
int q;
 
signed main(){
    scanf("%lld", &n);
    for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
    sum[1] = 0;
    for(int i = 2; i <= n; ++i) sum[i] = a[i] - a[i - 1]; 
    scanf("%lld", &q);
    int x;
    int ans = 0;
    for(int i = 1; i <= q; ++i){
        scanf("%lld", &x);
        int now = upper_bound(sum + 1, sum + n + 1, x) - sum - 1;
        ans = a[now] - a[1] + x;//前面的
        ans += (n - now) * x;//后面并没有填满的
        printf("%lld\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值