B. No Time for Dragons(贪心)
2.0 s
256 MB
standard input
standard output
One fairy king hated dragons to death. Not only that these monsters burn whole villages to ashes, kidnap princesses and guard treasures that they don't need at all, but they are also mentioned in statements of programming problems very often. To end their tyranny, he decided to recruit an army and destroy these damned creatures once and forever.
The king found out that there are n dragons in total, and to defeat the i-th of them he needs an army of ai soldiers, bi of which will be killed during the battle. Now he wants to know the minimal number of soldiers he needs to recruit in order to kill all the dragons. The king doesn't care about the order of battles: the only thing that matters is that none of the dragons will be left alive.
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of dragons.
Each of the next n lines contains two space-separated integers: ai and bi (1 ≤ bi ≤ ai ≤ 109) — the number of soldiers needed to defeat the i-th dragon, and the number of soldiers that will be killed in the battle against him.
Output a single integer — the minimal number of soldiers that is sufficient to kill all the dragons.
2
7 4
5 1
8
3
4 1
6 4
5 3
10 题意:有n只龙需要杀掉,杀龙 i 需要 ai 的军队,会死掉 bi 的军队,问最小需要派出的军队数是? //题解:想到的都说很简单,但我很久都没想通,我的理解是,逆序思考,假如杀完所有龙后,剩下 x 个人,使 x 尽量小就是使ans尽量小。
对于每只龙,可以这么考虑,设 ci = ai - bi; 就变为了:
需要 x >= ci 人 ,x 才能 +bi ,所以,为了让 x 尽量小,对 ci 进行排序,就让 x 在尽量小的情况下加更多 bi,就能使 ans 最小了
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
#define MX 200005
struct Dr
{
LL a,b;
bool operator < (const Dr &x)const
{
return a-b<x.a-x.b;
}
}dr[MX]; int main()
{
int n;
cin>>n;
for (int i=;i<n;i++)
scanf("%lld%lld",&dr[i].a,&dr[i].b);
sort(dr,dr+n);
LL ans =;
for (int i=;i<n;i++)
{
if (ans<dr[i].a-dr[i].b)
ans=dr[i].a;
else
ans += dr[i].b;
}
cout<<ans<<endl;
return ;
}
B. No Time for Dragons(贪心)的更多相关文章
- sgu548 Dragons and Princesses 贪心+优先队列
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=548 题目意思: 有一个骑士,要经过n个房间,开始在第一个房间,每个房间里面有龙或者 ...
- Dragons
http://codeforces.com/problemset/problem/230/A Dragons time limit per test 2 seconds memory limit pe ...
- C、Guard the empire(贪心)
链接:https://ac.nowcoder.com/acm/contest/3570/C 来源:牛客网 题目描述 Hbb is a general and respected by the enti ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
随机推荐
- 倍福TwinCAT(贝福Beckhoff)应用教程13.2 TwinCAT控制松下伺服 NC自定义直线插补
对于MOVEJ的关节运动来说,我们只关心每个电机的角度(只需要考虑多个电机协同开始运动和结束运动,关键是对每个电机加速度均一化,从而一起跑一起停,这部分内容可以参考机器人学导论以获取更加详细的说明), ...
- Django——如何使用Template以及如何向template传递变量
如何使用模板系统 在Python代码中使用Django模板的最基本方式如下: 可以用原始的模板代码字符串创建一个 Template 对象, Django同样支持用指定模板文件路径的方式来创建 Temp ...
- mui 上拉加载更多
看起来很简单的东西,实践过程中还是出现了很多麻烦,比如上拉时,状态条跑到了顶部,因为内容没有添加到容器中,再比如下拉的回调函数使用问题,this的传递. html实现部分: <div class ...
- lodash random
_.random([min=0], [max=1], [floating]) 产生一个包括 min 与 max 之间的数. 如果只提供一个参数返回一个0到提供数之间的数. 如果 floating 设为 ...
- rpm安装找不到.so库文件(linux动态库连接的相关知识)(转)
1.找不到库文件的原因 库文件不存在 这种情况一般是因为所需要的包没装,只要安装相应的包就可以解决 存在而系统不知道 这种情况一般出现在自己编译软件时候 确保库文件所在的路径已加入系统,在/etc/l ...
- MQTT压力测试之Tsung的使用
简介 Tsung 是一个压力测试工具,可以测试包括HTTP, WebDAV, PostgreSQL, MySQL, LDAP, and XMPP/Jabber等服务器.针对 HTTP 测试,Tsung ...
- Service#onStartCommand返回值解析
Service#onStartCommand返回值解析 Service类有个生命周期方法叫onStartCommand,每次启动服务(startService)都会回调此方法.此方法的原型例如以下: ...
- linux后台运行命令
Ctrl+z/bg/nohup/setsid/& screen 区别待续
- Linux Tools
WinSCP http://winscp.net/eng/download.php Xshell 5
- 李洪强经典面试题53-Swift
李洪强经典面试题53-Swift Swift 网上有很多Swift的语法题,但是Swift现在语法还未稳定,所以在这里暂时不贴出语法题,可以自行搜索. Swift和Objective-C的联系 Swi ...