题目链接:Problem - 1574C - Codeforces

Recently, Petya learned about a new game "Slay the Dragon". As the name suggests, the player will have to fight with dragons. To defeat a dragon, you have to kill it and defend your castle. To do this, the player has a squad of nn heroes, the strength of the ii-th hero is equal to aiai.

According to the rules of the game, exactly one hero should go kill the dragon, all the others will defend the castle. If the dragon's defense is equal to xx, then you have to send a hero with a strength of at least xx to kill it. If the dragon's attack power is yy, then the total strength of the heroes defending the castle should be at least yy.

The player can increase the strength of any hero by 1 for one gold coin. This operation can be done any number of times.

There are mm dragons in the game, the i-th of them has defense equal to xixi and attack power equal to yiyi. Petya was wondering what is the minimum number of coins he needs to spend to defeat the ii-th dragon.

Note that the task is solved independently for each dragon (improvements are not saved).

Input

The first line contains a single integer n (2≤n≤2⋅10^5) — number of heroes.

The second line contains nn integers a1,a2,…,an(1≤ai≤10^12), where aiai is the strength of the i-th hero.

The third line contains a single integer mm (1≤m≤2⋅10^5) — the number of dragons.

The next mm lines contain two integers each, xixi and yiyi (1≤xi≤10^12;1≤yi≤10^18) — defense and attack power of the ii-th dragon.

Output

Print mm lines, i-th of which contains a single integer — the minimum number of coins that should be spent to defeat the i-th dragon.

Example

input

Copy

4
3 6 2 3
5
3 12
7 9
4 14
1 10
8 7

output

Copy

1
2
4
0
2

Note

To defeat the first dragon, you can increase the strength of the third hero by 1, then the strength of the heroes will be equal to [3,6,3,3]. To kill the dragon, you can choose the first hero.

To defeat the second dragon, you can increase the forces of the second and third heroes by 1, then the strength of the heroes will be equal to [3,7,3,3] To kill the dragon, you can choose a second hero.

To defeat the third dragon, you can increase the strength of all the heroes by 1, then the strength of the heroes will be equal to [4,7,3,4]. To kill the dragon, you can choose a fourth hero.

To defeat the fourth dragon, you don't need to improve the heroes and choose a third hero to kill the dragon.

To defeat the fifth dragon, you can increase the strength of the second hero by 2, then the strength of the heroes will be equal to [3,8,2,3]. To kill the dragon, you can choose a second hero.

题意:有n个勇者,然后给出他的攻击力(也是防御力)

然后有m条巨龙,给出防御力和攻击力

只能派出一个勇者去攻击,其他防御,我们可以花费1点金币去提升勇者的属性,问最少要多少花费

思路:二分查找,找到第一个大于等于巨龙防御的勇者

如果不是第一个就考虑他,或者他前面那个去攻击, 取最小值,如果是第一个就是他去攻击

先附上lower_bound和upper_bound的用法:

数组必须是一个有序的是数组

lower_bound

lower_bound(a+1,a+n+1,x);(a + 1)是数组的头部, a+n+1是数组的尾部,x是查找的数字,lower_bound的作用是在数组a中从a[1]开始到a[n]按照cmp函数来比较进行二分查找第一个大于等于x的数的位置,如果有第一个大于等于x的数则返回该数的地址,否则返回a[n+1]的地址。

lower_bound的返回值是查找到的数据的地址,如果要转换为int,需减去数组a。

upper_bound:

upper_bound(a+1,a+n+1,x);(a + 1)是数组的头部, a+n+1是数组的尾部,x是查找的数字,

upper_bound的作用与lower_bound有点类似,它是在数组a中从a[1]开始到a[n]按照cmp函数来比较进行二分查找第一个大于(没有等于)x的数的位置,如果有第一个大于x的数则返回该数的地址,否则返回a[n+1]的地址。

upper_bound的返回值是查找到的数据的地址,如果要转换为int,需减去数组a。

#include<bits/stdc++.h>
using namespace std;long long arr[200005];int main(){ios::sync_with_stdio(false);int n, m;cin >> n;long long a, b;long long sum = 0;for(int i = 0; i < n; i++){cin >> arr[i];sum += arr[i];}long long ans = 0;sort(arr, arr + n);cin >> m;long long cnt = 0;for(int i = 0; i < m; i++){cnt = 0;long long cnt1 = 0;cin >> a >> b;ans = lower_bound(arr, arr + n, a) - arr;if(ans == n){ans--;}if(ans == 0){long long a1 = arr[ans];long long sum1 = sum - a1;if(a1 < a){cnt += (a - arr[ans]);}if(sum1 < b){cnt += (b - sum1);}cout << cnt << "\n";}else{long long a1 = arr[ans];long long a2 = arr[ans - 1];long long sum1 = sum - a1;long long sum2 = sum - a2;if(a1 < a){cnt += (a - arr[ans]);}if(sum1 < b){cnt += (b - sum1);}if(a2 < a){cnt1 += (a - a2);}if(sum2 < b){cnt1 += (b - sum2);}cout << min(cnt, cnt1) << "\n";}}return 0;
}

Educational Codeforces Round 114 (Rated for Div. 2)C. Slay the Dragon相关推荐

  1. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  2. Educational Codeforces Round 114 (Rated for Div. 2) D. The Strongest Build 暴力 + bfs

    传送门 文章目录 题意: 思路: 题意: 你有nnn个装备槽,每个槽里面有cic_ici​个力量加成,对于每个槽只能选一个力量加成,现在给你mmm个力量组合[b1,b2,...,bn][b_1,b_2 ...

  3. Educational Codeforces Round 114 (Rated for Div. 2) 个人题解

    中秋节快乐! A. Regular Bracket Sequences 题意 输出nnn个不同的长度为2n2n2n的合法括号序列. 分析 先输出一个"()()()-"序列. 然后依 ...

  4. 1574D The Strongest Build (Educational Codeforces Round 114 (Rated for Div. 2))

    题意 给定n个从小到大的数组,从每个数组中选出一个下标构成一个序列,但是有m种序列被ban了,要求选出的序列对应的数字和最大且没有被ban. 思路 因为有m个序列被ban了,那么最坏的情况可以假设为最 ...

  5. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  6. Educational Codeforces Round 37 (Rated for Div. 2) 1

    Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...

  7. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

  8. Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)

    Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...

  9. Educational Codeforces Round 72 (Rated for Div. 2) D. Coloring Edges dfs树/拓扑找环

    传送门 文章目录 题意: 思路: 题意: 给你一张图,你需要给这个图的边染色,保证如果有环那么这个环内边的颜色不全相同,输出染色方案和用的颜色个数. n,m≤5e3n,m\le5e3n,m≤5e3 思 ...

最新文章

  1. 【Codeforces】158B-Taxi(贪心,怎么贪咧)
  2. Bzoj 3680 吊打xxx【[模拟退火】
  3. 存在就不插入_动画:面试官问我插入排序和冒泡排序哪个更牛逼?
  4. 「 ThoughtWorks面试 —— 一次愉快的技术交流 | 掘金技术征文」
  5. hive_0.11中文用户手册
  6. SQLserver2000 实例管理工具
  7. php 整行插入mysql_MySQL的多行插入
  8. php sql 长字符串 查找被包含的短字符串_PHP字符串
  9. conda clean -i
  10. GD32F103VET6替代STM32F103VET6遇到的问题
  11. Python 音频调整音量(附代码) | Python工具
  12. 实验室计算机维修申请条件,计算机实验室管理制度
  13. 我国数学如何面对西方数学?
  14. php 母版 登陆,幻灯片母版的作用有哪些
  15. 开源路由器-OpenWRT/梅林
  16. SpringBoot项目怎么重命名
  17. 基于Gin+Vue+ElementUI实现的OA办公系统
  18. 【2021-05-06】JS逆向之微店登入ua
  19. 最全软件测试工具大全
  20. python期末试题汇总

热门文章

  1. 绿之韵背后不得不说的故事
  2. 某人一年Android工作经验,一举拿下百度、网易、美团、小米、快手等Offer面经
  3. 宋维刚老师词霸天下38000词汇思维导图使用指南
  4. 网狐荣耀6701,6801(二) 开发环境搭建与编辑
  5. openGL-梁友栋-Barsky算法
  6. 沃尔玛logo_最受欢迎的云项目,沃尔玛对开源的投资以及更多
  7. ubuntu下PDF转图片--python
  8. 【立创开源】【国民技术】 N32G340C8LT最小系统开发板
  9. 交叉导轨具体应用在哪些领域?
  10. vue 动态设置div的高度_Vue 动态设置元素高度