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 ...
随机推荐
- ASP.NET MVC之单元测试分分钟的事
一.为什么要进行单元测试? 大部分开发者都有个习惯(包括本人在内),常常不喜欢去做单元测试.因为我们对自己写的程序总是盲目自信,或者存在侥幸心理每次运行通过后就直接扔给测试组的妹子们了.结果妹子一测, ...
- jQuery (样式篇)
1.$(document).ready 的作用是等页面的文档(document)中的节点都加载完毕后,再执行后续的代码,因为我们在执行代码的时候,可能会依赖页面的某一个元素,我们要确保这个元素真正的的 ...
- 每日一个机器学习算法——LR(逻辑回归)
本系列文章用于汇集知识点,查漏补缺,面试找工作之用.数学公式较多,解释较少. 1.假设 2.sigmoid函数: 3.假设的含义: 4.性质: 5.找一个凸损失函数 6.可由最大似然估计推导出 单个样 ...
- Python中函数参数传递问题【转】
1. Python passes everything the same way, but calling it "by value" or "by reference& ...
- python challenge - map.py
Hint: K->M O->Q E->G everybody thinks twice before solving this. g fmnc wms bgblr rpy ...
- spring学习笔记(五)
1.后置通知 需求:调用相应业务方法后,完成资源的关闭. a. 在beans.xml中配置 .... <beans> <!--配置被代理对象--> <bean id=&q ...
- rabbitmqctl 报错
RabbitMQ 安装成功后,cmd dos命令进入RabbitMQ的安装路径,如 E:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.10\sb ...
- 【Scala】使用Option、Some、None,避免使用null
避免null使用 大多数语言都有一个特殊的关键字或者对象来表示一个对象引用的是"无",在Java,它是null.在Java 里,null 是一个关键字,不是一个对象,所以对它调用不 ...
- 谈谈 OC 中的内联函数
内联函数 即在编译的时候将函数体替换函数调用.从而不须要将parameter,returnaddress进行push/pop stack的操作, 从而加速app的执行.然而,会添加二进制文件的大小. ...
- OSX: bash的更新
本文尽量详述眼下来说的bash补丁的进展,从以下4个方面解释: 最全最新的更新安装包,:Oct 5为止的 測试已知的bash漏洞的脚本:更新后能够用来检測已知bash漏洞的情况 脚本编译更新版本号:也 ...