当前位置: 首页 > 工具软件 > Dragon Player > 使用案例 >

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

孔阳炎
2023-12-01

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

 类似资料: