Educational Codeforces Round 114 (Rated for Div. 2)C. Slay the Dragon

题目链接: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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值