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");elseprintf("%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]);elseprintf("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]);elseprintf("%lld\n",atk+def-sum);}}return 0;
}

//感谢大佬ljl帮我找bug

C. Slay the Dragon相关推荐

  1. slay the dragon

    参考了一下tourist的代码 C. Slay the Dragon time limit per test2 seconds memory limit per test256 megabytes i ...

  2. codeforces 1574 C. Slay the Dragon

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

  3. 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 ...

  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. Python自定义主从分布式架构
  2. 从输入 URL 到页面加载完的过程中都发生了什么事情 —— 网络优化篇
  3. jzoj3896-战争游戏【tarjan,割点,点双联通分量】
  4. 跨平台开发框架 Lynx 初探
  5. 计算机音乐 phd,美国大学音乐(Music)专业PhD排名
  6. 一个DEMO让你彻底理解线程池
  7. VS2010工程转VS2005工程的方法
  8. MyEclipse格式化代码设置
  9. 有效软件开发的25条军规
  10. 锐起无盘服务器缓存多少,锐起无盘缓存分析
  11. 抢红包算法 c语言,微信红包的随机算法是怎样实现的?
  12. Keil C51 的printf
  13. linux 7查看网络流量,CentOS7 监控网络流量
  14. 人月神话札记:编程的苦恼和乐趣
  15. 游戏设计自学记录(20)
  16. 请求转发(request对象)和重定向(response)的区别及何时使用请求转发和重定向
  17. iframe标签控制视频大小及自动播放
  18. 中华大地第二次大变革 看印度反思自我之六 印度模式更有吸引力
  19. 点击劫持漏洞修复(前端、后端)
  20. 【苹果CMS技术教程】苹果CMSV10基础安装过程,如何拥有自己的视频网站

热门文章

  1. 硬核总结!快递分拣中心设备应用解读
  2. 深度Deep系统的使用问题记录
  3. 自媒体原创视频怎么做?有哪些类型可以做?
  4. Vuforia网站及Vuforia码下载使用方法
  5. 从玄学到网红,互联网企业取名有何学问?
  6. [转帖]贪官给儿子的一封家信
  7. AHT10温湿度传感器设计
  8. ISLR读书笔记八:交叉验证法(Cross-Validation)
  9. HTML、XHTML和HTML5区别和概念
  10. 阿里云建站教程文档汇总(详细指南)