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

C. Slay the Dragon

宰父保臣
2023-12-01

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 n heroes, the strength of the i-th hero is equal to ai.

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 x, then you have to send a hero with a strength of at least x to kill it. If the dragon’s attack power is y, then the total strength of the heroes defending the castle should be at least y.
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 m dragons in the game, the i-th of them has defense equal to xi and attack power equal to yi. Petya was wondering what is the minimum number of coins he needs to spend to defeat the i-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⋅105) — number of heroes.
The second line contains n integers a1,a2,…,an (1≤ai≤1012), where ai is the strength of the i-th hero.
The third line contains a single integer m (1≤m≤2⋅105) — the number of dragons.
The next m lines contain two integers each, xi and yi (1≤xi≤1012;1≤yi≤1018) — defense and attack power of the i-th dragon.
Output
Print m lines, i-th of which contains a single integer — the minimum number of coins that should be spent to defeat the i-th dragon.

input

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

output

1
2
4
0
2

先二分查找判断能不能找到,若能找到则使用这个值。
找不到就判断比所有制都大还是比所有值都小,还是在这个值的区间范围内。
若是比所有值都大则使用最大值
比所有值都小使用最小值。
若在这两个值范围之内则分别使用查找出的i和i-1求出结果取min

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n;
ll a[2000006];
int main()
{
    cin>>n;
    ll sum=0;
    for(ll i=1; i<=n; i++)
    {
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    sort(a+1,a+n+1);
    ll t;
    cin>>t;
    ll atk,def;
    while(t--)
    {
        scanf("%lld%lld",&atk,&def);
        ll i=lower_bound(a+1,a+n+1,atk)-a;//找到第一个大于等于他的数
        ll ans=0;
        if(a[i]==atk)//找到一个值等于龙的攻击值
        {
            if(sum-a[i]>=def)
                printf("0\n");
            else
                printf("%lld\n",def-sum+a[i]);
        }
        else if(i<n)
        {
            ll ans1=0;
            ll ans2=0;
            if(i==1)
            {
                if(sum-a[1]<def)
                    printf("%lld\n",def-sum+a[1]);
                else
                    printf("0\n");
                continue;
            }
            ans1+=abs(a[i-1]-atk);
            if(sum-a[i-1]<def)
                ans1=ans1+def-(sum-a[i-1]);

            if(a[i]>=atk)
            {
                if(sum-a[i]<def)
                    ans2=def-sum+a[i];
            }
            else
            {
                if(sum-a[i]<def)
                    ans2=def-sum+atk;
            }
            printf("%lld\n",min(ans1,ans2));
        }
        else//所有数都比龙的攻击值要小
        {
            if(sum-a[n]>=def)
                printf("%lld\n",atk-a[n]);
            else
                printf("%lld\n",atk+def-sum);
        }
    }
    return 0;
}

//感谢大佬ljl帮我找bug

 类似资料:

相关阅读

相关文章

相关问答