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 ...
随机推荐
- oracle常用函数使用大全 Oracle除法(转)
http://blog.csdn.net/chenmeng2192089/article/details/9155625 一.运算符算术运算符:+ - * / 可以在select 语句中使用连接运算符 ...
- 虚拟机实现https网络设置
现在很多网站使用的都是https协议,想在自己的电脑上实现下, 由于自己的电脑是win10,我总是觉得在windows上布置环境不如在linux上稳定,所以在电脑上安装了虚拟机,cento系统 . 上 ...
- CefSharp.WinForms
CefSharp.WinForms 一. 前言 银医通项目,现在另外一家医院需要上系统,所以项目需要重新搭建,由于这家医院的His系统和另外一家医院的His系统不同,界面风格也不一致,所以重新搭建, ...
- /u200B 8203 Zero-width space 问题
[TestMethod] public void TestBom() { string str = "123";//这个字符串是错误的有问题 长度4 ).Select(x =&g ...
- stm32 IDR寄存器软件仿真的BUG
/* * 函数名:Key_GPIO_Config * 描述 :配置按键用到的I/O口 * 输入 :无 * 输出 :无 */ void Key_GPIO_Config(void) { GPIO_Init ...
- Linux下lvm在线扩容步骤
转 :https://jingyan.baidu.com/article/25648fc18f22b29191fd0011.html 图片看不清可以看原文 LVM是逻辑盘卷管理(Logical Vol ...
- Java线程安全同步容器
线程安全同步容器(使用 synchronized关键字) 1.ArrayList->Vector,Stack 2.HashMap->HashTable(key.value不能为null) ...
- TCP/IP的三次握手与四次挥手详解
TCP((Transmission Control Protocol)传输控制协议,是一个面向连接的协议.在运用此协议进行数据传输前都会进行连接的建立工作(三次握手):当数据传输完毕,连接的双方都会通 ...
- DatabaseFactory.CreateDatabase 方法操作数据库
using Microsoft.Practices.EnterpriseLibrary.Data;using Microsoft.Practices.EnterpriseLibrary.Data.Sq ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...