任意门

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 longconst 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);}
}

Check Problems---二分相关推荐

  1. Codeforces Round #409 (Div. 2) C Voltage Keepsake(二分)

    题意:有n(n<=100000)个机器...第i个机器最开始有bi(1<=bi <= 100000)个单位的电量,机器可以储存的电量没有上限,启动后每秒消耗ai(1<=ai&l ...

  2. Musical Theme pku1743 (后缀数组)

    Musical Theme(后缀数组) 题意: n个数,选取一段子序列,满足以下条件: 1.长度至少为5 2.在数列中其他位置出现过(允许转置) 3.与其他位置出现的不重叠 转置:将恒定的正或负值添加 ...

  3. ZJOI2019Round#1

    考的这么差二试基本不用去了 不想说什么了.就把这几天听课乱记的东西丢上来吧 这里是二试乱听课笔记ZJOI2019Round#2 ZJOI Round#1 Day1 M.<具体数学>选讲 罗 ...

  4. 2022济南大学acm新生赛题解

    通过答题情况的难度系数: 签到:ABL 简单:DGKQ 中等:CMN 困难:EFHIJOPRST A-和 算出n个数的和判断正负性即可!!! 发现很多同学的代码错误:要么sum未赋初值,要么数组大小定 ...

  5. 2019-2020 ICPC, NERC, Southern and Volga Russian Regional Contest

    A.Berstagram 题意:起始数列是1,2,3,--,n,给你m个操作x,表示将数字x和前一个位置的数交换,如果已经在第一个则不做操作,求每个数能到达的位置的最大和最小值: 分析:扫一遍模拟,更 ...

  6. qlu_新生赛_2018_代码汇总

    https://blog.csdn.net/weixin_43537190/article/details/84570246 记得看看梁延杰的blog 可能会有小惊喜 还有就是别的学校的新生题 本文章 ...

  7. 二分查找板子(check()函数)

    之前找了很多博客,想找一个万能的二分板子,始终没有找到合适的,后来找同学大佬要了一个(鸣谢大佬!QAQ),现在将其上传,供日后自己学习使用. int erfen() {int l = 1, r = n ...

  8. bzoj 1863 二分+dp check

    思路:二分之后用dp去check就好啦. #include<bits/stdc++.h> #define LL long long #define fi first #define se ...

  9. 【11.5校内测试】【倒计时5天】【DP】【二分+贪心check】【推式子化简+线段树】...

    Solution 非常巧妙的建立DP方程. 据dalao们说题目明显暗示根号复杂度??(反正我是没看出来 因为每次分的块大小一定不超过$\sqrt n$,要不然直接每个位置开一个块答案都才为$n$. ...

  10. Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing 二分 + check

    传送门 文章目录 题意: 思路: 题意: 思路: 直接算不好算,考虑二分这个中位数midmidmid. 考虑如何checkcheckcheck,这个分情况来就好了: (1)mid>a[i].r( ...

最新文章

  1. 3月机器学习在线班第六课笔记--信息熵与最大熵模型
  2. 2014北京三险一金缴存比例
  3. 去重是distinct还是group by?
  4. [Head First Java] - 给线程命名
  5. MySql 数据库基本设计规范
  6. 软件协会发布开源软件与商业软件知识产权报告
  7. oracle升级补丁报错,oracle rac升级补丁及中间的错误提示
  8. “云”话数字经济:2020 腾讯全球数字生态大会定档 9 月!​
  9. string.split方法 保留分隔符_Python pandas库159个常用方法使用说明
  10. UNIX环境高级编程(中文第三版)
  11. jar包转换为exe可执行文件
  12. Rational Rose如何删除线段
  13. win7 蓝牙4.0 ble驱动_低功耗蓝牙时代,纽扣电池寿命可达十年
  14. MySQL数据导出:ERROR 1 (HY000) 错误解决
  15. percentile_approx函数用法
  16. ASBR上配置外部OSPF路由汇总
  17. Android Studio显示“Hardcoded String XXX,should use @string resource”的解决方法2-1
  18. SQL SERVER 使用stuff函数进行分组且合并
  19. 如何用深度学习进行语音识别
  20. (JAVA) 相反数

热门文章

  1. 基于msp430G2553的低频方波频率、占空比、峰峰值测量函数
  2. pptv首页导航效果
  3. Win10 C盘 系统和保留 占用空间 非常大
  4. 仙剑奇侠传4狐仙打法
  5. 吐槽世界名校:只有我上不了的名校,没有不敢吐槽的名校
  6. 茄子科技(海外SHAREit Group),助力跨境电商有效实现出海稳定发展
  7. Redis6在Liunx系统下的安装、启动和关闭
  8. Aegisub的视频窗口详解
  9. 普通用户如何获取超级管理员权限的步骤
  10. 如何两个电脑共享文件实现多人编辑_怎么才能几台电脑同时编辑共享的同一word文档...