B. No Time for Dragons
time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

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.

Input

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

Output a single integer — the minimal number of soldiers that is sufficient to kill all the dragons.

Examples
input
2
7 4
5 1
output
8
input
3
4 1
6 4
5 3
output
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(贪心)的更多相关文章

  1. sgu548 Dragons and Princesses   贪心+优先队列

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=548 题目意思: 有一个骑士,要经过n个房间,开始在第一个房间,每个房间里面有龙或者 ...

  2. Dragons

    http://codeforces.com/problemset/problem/230/A Dragons time limit per test 2 seconds memory limit pe ...

  3. C、Guard the empire(贪心)

    链接:https://ac.nowcoder.com/acm/contest/3570/C 来源:牛客网 题目描述 Hbb is a general and respected by the enti ...

  4. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  5. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  6. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

随机推荐

  1. [Android] android:visibility属性应用

    在Android开发中,有这样一种场景: 当edittext中有内容时候,旁边的提交或确定按钮显示: 当edittext中内容为空的时候,提交或确定按钮隐藏: 要实现起来其实并不困难.很多控件都有vi ...

  2. 自己定义modal动画

    在非常多场景中.我们都须要实现各种动画.这回我们来尝试搞一下控制器间跳转的modal动画. - (void)touchesBegan:(NSSet<UITouch *> *)touches ...

  3. app接口开发(php)

    1.JSON方式封装通信接口: 封装: response.php <?php // JSON方式封装通信接口 // 定义 response类 class Response { // 定义一个静态 ...

  4. mod_tile编译出错 -std=c++11 or -std=gnu++11

    make[1]: 正在进入文件夹 /home/wml/src/mod_tile-master' depbase=echo src/gen_tile.o | sed 's|[^/]*$|.deps/&a ...

  5. 获取request中的查询参数

    //获取request中的查询参数 public static Map<String, Object> getRequestParamsByMap(HttpServletRequest r ...

  6. Python手势识别与控制

    代码地址如下:http://www.demodashi.com/demo/12968.html Python手势识别与控制 概述 本文中的手势识别与控制功能主要采用 OpenCV 库实现, OpenC ...

  7. shll 基础讲解

    http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html Shell编程基础 $# 命令行得到的参数个数 $@ 命令行得到的所有参数作 ...

  8. c#中从string数组转换到int数组及比较两个字符串相等

    string[] input = { "1", "2", "3", "4", "5", " ...

  9. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  10. Python内置函数之filter()

    filter(function,iterable)用来过滤可迭代对象 如果提供过滤条件的函数为None,则可迭代对象中为False的元素将被过滤掉. 例如: >>> a = [,,F ...