参考了一下tourist的代码
C. Slay the Dragon
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;int main() {ios::sync_with_stdio(false);cin.tie(0);int n;cin >> n;vector<long long> a(n);long long sum=0;for (int i = 0; i < n; i++) {cin >> a[i];sum += a[i];}sort(a.begin(), a.end());int m;cin >> m;while (m--) {long long x, y;cin >> x >> y;`在这里插入代码片`long long ans = 9e18;auto it = lower_bound(a.begin(), a.end(),x);if (it != a.end()) {long long remain = sum - *(it);long long add = max(0ll, y-remain);ans = min(ans, add);}if (it != a.begin()) {long long remain = sum - *prev(it);long long add = max(0ll, x - *prev(it));add += max(0ll, y - remain);ans = min(ans, add);}cout << ans << endl;}return 0;
}

只要二分找到第一个小于x的值和第一个大于x的值,分别算一算花费,哪个小就去哪个。
说实话大神的代码真的简洁,不像我的又臭又长。

slay the dragon相关推荐

  1. codeforces 1574 C. Slay the Dragon

    本场比赛其他题目题解 A. Regular Bracket Sequences B. Combinatorics Homework C. Slay the Dragon D. The Stronges ...

  2. 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&quo ...

  3. C. Slay the Dragon

    Recently, Petya learned about a new game "Slay the Dragon". As the name suggests, the play ...

  4. 【Dragon of Loowater】【UVA - 11292】(思维)

    题目: Once upon a time, in the Kingdom of Loowater, a mi- nor nuisance turned into a major problem. Th ...

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

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

  6. 《spring实战第四版》的读书笔记

    <spring实战第四版>的读书笔记 1 概述 <Spring实战第四版>描述了Spring4架构的设计,看完了以后,最大感觉是Spring的IOC与aop理念实在是太强大了, ...

  7. Spring实战1:Spring初探

    现在的Java程序员赶上了好时候.在将近20年的历史中,Java的发展历经沉浮.尽管有很多为人诟病的产品,例如applets.EJB.Java Data Object(JDO)和数不清的日志框架,Ja ...

  8. spring(1)Spring之旅

    [0]README 0.1)本文部分文字描述转自:"Spring In Action(中/英文版)",旨在reviewSpring(1)Spring之旅 的相关知识: [1]简化j ...

  9. SQL Server中的查询优化技术:基础

    描述 (Description) Fixing and preventing performance problems is critical to the success of any applic ...

最新文章

  1. 人生苦短,我要换Go!
  2. mysql 开发基础系列18 存储过程和函数(下)
  3. 当复选框中打勾时后面自动显示y或者n_基于轮廓系数确定K-Means聚类中的K
  4. leetcode 3Sum C++
  5. 苹果系统使用之输入法的呈现与设置问题
  6. 《重大技术需求征集系统》项目目标文档
  7. 【交通标志识别】基于matlab GUI SIFT交通标志识别【含Matlab源码 864期】
  8. MySQL启动报错: Failed to start LSB start and stop MySQL
  9. 利用GAN生成动漫头像
  10. 2022年详细诠释保税区一日游 一般贸易企业与加贸企业如何完成保税区一日游
  11. MNE学习笔记(六):Epoched data的可视化
  12. 【深度学习训练流程】浅析深度学习训练流程
  13. 瞻博网络持续深耕 以“本土化”撬动“中国心”
  14. ubuntu18.04程序开机自启动
  15. 大厂与小厂招人的区别,看完多少有点不敢相信
  16. 一、Java面试基础之面向对象的特征:继承、封装和多态(原创实例)
  17. Spring WebFlux框架概述
  18. 匿名内部类的定义格式
  19. 机械工程毕业论文题目
  20. CNN经典网络模型(四):GoogLeNet简介及代码实现(PyTorch超详细注释版)

热门文章

  1. 如何把游戏BGM文件夹中的.bank文件转换为.wav或者.mp3
  2. kd树是怎么画出来的_你绝对猜不出这些画是怎么画出来的……
  3. 对码农工作的感悟或总结
  4. 信息科学学术论文常用词汇
  5. 对不起,你的密码被破解了!
  6. STM32连接HT1621段码屏驱动
  7. 回顾历史“312”,展望未来牛市
  8. 大炮打蚂蚁94级截图
  9. Ubuntu上使用CLion开发STM32,并使用JLink下载、调试
  10. android7.1.2彩蛋,在Android 7.0牛轧糖中解锁秘密猫收集复活节彩蛋 | MOS86