Codeforces1574C

本文介绍了一个涉及英雄能力和龙的能力值的算法问题。通过选择合适的英雄击杀一条龙并防御另一条龙来最小化代价。文章提供了具体的实现代码,采用C++编写,展示了如何使用排序和二分查找等数据结构和算法技巧解决问题。

问题描述

  • 给你nnn个英雄,每个英雄的能力值为aia_iai,然后有两条龙,你必须派一个英雄去杀其中一条龙,然后派其他的英雄去防御另外一条龙。
  • 注意,你选择的英雄的能力值必须要比龙的能力值大,并且,你可以用1coin1coin1coin去提升一位英雄的能力值(当然只提升111点)。要你求能杀死一条龙并且防御另外一条龙的最小代价。

思路分析

  • 我们直接找到刚好比第一条龙能力值大于等于或者小于等于的英雄去杀那条龙,比较一下两种情况的代价大小即可。

代码如下

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2e6 + 10;
ll a[maxn];
ll sum;
signed main()
{
        ll n;
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cin >> n;
        for (ll i = 1; i <= n; i++)
        {
                cin >> a[i];
                sum += a[i];
        }
        sort(a + 1, a + 1 + n);
        ll t;
        cin >> t;
        while (t--)
        {
                ll sum1 = sum;
                ll ans = 0;
                ll x, y;
                cin >> x >> y;
                ll pos = lower_bound(a + 1, a + 1 + n, x) - a;
                //第一个不小于
                if (pos > n)
                {
                        cout << x - a[n] + max(0ll, y - sum1 + a[n]) << endl;
                }
                else if (a[pos] == x)
                {
                        cout << max(0ll, y - sum1 + x) << endl;
                }
                else
                {
                        //大于的
                        sum1 -= a[pos];
                        ans = max(0ll, y - sum1);
                        sum1 += a[pos];
                        if (pos != 1)
                        {
                                //小于的
                                sum1 -= a[pos - 1];
                                ans = min(ans, x - a[pos - 1] + max(0ll, y - sum1));
                        }
                        cout << ans << endl;
                }
        }
        return 0;
}
### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值