福州专业网站建设价格,深圳做商城网站建设,智慧团建电脑版登录入口,小程序商店模板CF1574C Slay the Dragon 题解题目链接字面描述题面翻译题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1提示代码实现题目
链接
https://www.luogu.com.cn/problem/CF1574C
字面描述
题面翻译
给定长度为 nnn 的序列 aaa#xff0c;mmm 次询问#xff0c;每次询…
CF1574C Slay the Dragon 题解题目链接字面描述题面翻译题目描述输入格式输出格式样例 #1样例输入 #1样例输出 #1提示代码实现题目
链接
https://www.luogu.com.cn/problem/CF1574C
字面描述
题面翻译
给定长度为 nnn 的序列 aaammm 次询问每次询问包含两个参数 x,yx,yx,y你可以给序列任意位置 111最后你需要找出一个位置 ppp 满足
ap≥xa_p\ge xap≥x∑i1nai[i̸p]≥y\displaystyle\sum_{i1}^n a_i[i\not p] \ge yi1∑nai[ip]≥y
最小化 111 次数输出其次数。
限制2≤n≤2×105,1≤m≤2×105,1≤ai,x≤1012,1≤y≤10182\le n\le2\times 10^5,1\le m\le 2\times10^5,1\le a_i,x\le 10^{12},1\le y\le 10^{18}2≤n≤2×105,1≤m≤2×105,1≤ai,x≤1012,1≤y≤1018
Translated by 飞丞
题目描述
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 $ a_i $ .
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 $ x_i $ and attack power equal to $ y_i $ . 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).
输入格式
The first line contains a single integer $ n $ ( $ 2 \le n \le 2 \cdot 10^5 $ ) — number of heroes.
The second line contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \le a_i \le 10^{12} $ ), where $ a_i $ is the strength of the $ i $ -th hero.
The third line contains a single integer $ m $ ( $ 1 \le m \le 2 \cdot 10^5 $ ) — the number of dragons.
The next $ m $ lines contain two integers each, $ x_i $ and $ y_i $ ( $ 1 \le x_i \le 10^{12}; 1 \le y_i \le 10^{18} $ ) — defense and attack power of the $ i $ -th dragon.
输出格式
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.
样例 #1
样例输入 #1
4
3 6 2 3
5
3 12
7 9
4 14
1 10
8 7样例输出 #1
1
2
4
0
2提示
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.
代码实现
#includebits/stdc.h
#define ll long long
using namespace std;const int maxn2e510;
int n,m;
ll tot;
ll a[maxn];
int main(){scanf(%d,n);for(int i1;in;i){scanf(%lld,a[i]);tota[i];}sort(a1,an1);scanf(%d,m);while(m--){ll x,y;scanf(%lld%lld,x,y);if(totxy){if(a[1]x){printf(%lld\n,y-(tot-a[1]));continue;}int l1,rn;while(lr){int midlr1;if(a[mid]x)rmid-1;else if(a[mid]x)lmid1;else break;}int oplr1;if(ytot-a[op])printf(%lld\n,x-a[op]);else printf(%lld\n,x-a[op]y-(tot-a[op]));continue;}else{if(a[1]x){if(ytot-a[1])printf(%lld\n,y-(tot-a[1]));else printf(0\n);continue;}int l1,rn;while(lr){int midlr1;if(a[mid]x)rmid-1;else if(a[mid]x)lmid1;else break;}int oplr1;int op1op1;if(totxy){if(op1n)printf(%lld\n,min(x-a[op],a[op1]-x));else printf(%lld\n,x-a[op]);}else{if(ytot-a[op1]){if(op1n)printf(0\n);else printf(%lld\n,x-a[op]);}else {if(op1n)printf(%lld\n,min(x-a[op],y-(tot-a[op1])));else printf(%lld\n,x-a[op]);}}}}return 0;
}
文章转载自: http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.ykmg.cn.gov.cn.ykmg.cn http://www.morning.lthgy.cn.gov.cn.lthgy.cn http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn http://www.morning.kqhlm.cn.gov.cn.kqhlm.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.srgwr.cn.gov.cn.srgwr.cn http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.wgqtt.cn.gov.cn.wgqtt.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.spkw.cn.gov.cn.spkw.cn http://www.morning.bplqh.cn.gov.cn.bplqh.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.dlwzm.cn.gov.cn.dlwzm.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.pqkyx.cn.gov.cn.pqkyx.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.jtybl.cn.gov.cn.jtybl.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.mywnk.cn.gov.cn.mywnk.cn http://www.morning.pngfx.cn.gov.cn.pngfx.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.kjgrg.cn.gov.cn.kjgrg.cn http://www.morning.rkdzm.cn.gov.cn.rkdzm.cn http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn http://www.morning.qtwd.cn.gov.cn.qtwd.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.pyncx.cn.gov.cn.pyncx.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.frsbf.cn.gov.cn.frsbf.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.geledi.com.gov.cn.geledi.com http://www.morning.ymdhq.cn.gov.cn.ymdhq.cn http://www.morning.junyaod.com.gov.cn.junyaod.com http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.mrbzq.cn.gov.cn.mrbzq.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.wktbz.cn.gov.cn.wktbz.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.gxtbn.cn.gov.cn.gxtbn.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.gpfuxiu.cn.gov.cn.gpfuxiu.cn