time limit per test3 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn’t matter and teams are sorted only by the number of balloons they have. It means that one’s place is equal to the number of teams with more balloons, increased by 1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed.

You should know that it’s important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly forbidden by the rules. The team is then disqualified and isn’t considered in the standings.

A contest has just finished. There are n teams, numbered 1 through n. The i-th team has ti balloons and weight wi. It’s guaranteed that ti doesn’t exceed wi so nobody floats initially.

Limak is a member of the first team. He doesn’t like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously, he can’t give away more balloons than his team initially has.

What is the best place Limak can get?

Input

The first line of the standard input contains one integer n (2 ≤ n ≤ 300 000) — the number of teams.

The i-th of n following lines contains two integers ti and wi (0 ≤ ti ≤ wi ≤ 1018) — respectively the number of balloons and the weight of the i-th team. Limak is a member of the first team.

Output

Print one integer denoting the best place Limak can get.

Examples

input

8

20 1000

32 37

40 1000

45 50

16 16

16 16

14 1000

2 1000

output

3

input

7

4 4

4 4

4 4

4 4

4 4

4 4

5 5

output

2

input

7

14000000003 1000000000000000000

81000000000 88000000000

5000000000 7000000000

15000000000 39000000000

46000000000 51000000000

0 1000000000

0 0

output

2

Note

In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is:

Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has only 14 balloons now and he would get the fifth place.

Limak gives 6 balloons away to a team with 45 balloons. Now they have 51 balloons and weight 50 so they fly and get disqualified.

Limak gives 1 balloon to each of two teams with 16 balloons initially.

Limak has 20 - 6 - 6 - 1 - 1 = 6 balloons.

There are three other teams left and their numbers of balloons are 40, 14 and 2.

Limak gets the third place because there are two teams with more balloons.

In the second sample, Limak has the second place and he can’t improve it.

In the third sample, Limak has just enough balloons to get rid of teams 2, 3 and 5 (the teams with 81 000 000 000, 5 000 000 000 and 46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7).

【题解】



先思考个问题;

如果把气球给比自己的气球少的人有意义吗?

肯定没有意义;

那如果给了前面一个人气球前面一个人飞了然后自己的气球比后一个人少了怎么办?

没关系,先给再说。

因为如果你半途发现比后一个人少了就不给了;反而失去了这次给的意义;要给就给到底。

(因为你答案已经更新过了,所以只有把前面一个人挤掉才能获取更优的答案);

用两个单调队列维护一下就好;

注意开long long- -哎,细节要做到位啊。

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define LL long long using namespace std; const int MAXN = 4e5; struct abcd
{
LL t,w;
}; int n,ans;
abcd a[MAXN];
priority_queue <LL> qb;
priority_queue < pair<LL,LL> > qa; void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_int(n);
LL myt,myw;
input_LL(myt);input_LL(myw);
for (int i = 2;i <= n;i++)
{
input_LL(a[i].t),input_LL(a[i].w);
if (a[i].t>myt)
qb.push(a[i].t-a[i].w);
else
qa.push(make_pair(a[i].t,a[i].w));
}
ans = qb.size();
while (!qb.empty() && myt > -qb.top())
{
myt-=(-qb.top()+1);
qb.pop();
while (!qa.empty() && qa.top().first > myt)
{
qb.push(qa.top().first-qa.top().second);
qa.pop();
}
int len = qb.size();
ans = min(ans,len);
}
printf("%d\n",ans+1);
return 0;
}

【37.74%】【codeforces 725D】Contest Balloons的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【74.89%】【codeforces 551A】GukiZ and Contest

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【21.37%】【codeforces 579D】"Or" Game

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【74.00%】【codeforces 747A】Display Size

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 757D】Felicity's Big Secret Revealed

    [题目链接]:http://codeforces.com/problemset/problem/757/D [题意] 给你一个01串; 让你分割这个01串; 要求2切..n+1切; 对于每一种切法 所 ...

  8. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  9. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

随机推荐

  1. Effective C++: 07模板与泛型编程

    C++ template机制自身是一部完整的图灵机(Turing-complete):它可以被用来计算任何可计算的值.于是导出了模板元编程(TMP, template metaprogramming) ...

  2. CoreData遇见iCloud的那些坑

    尽管苹果把iCloud与CoreData之间的完美配合吹的天花乱坠,但在iOS7之前,想用iCloud同步CoreData数据简直就是噩梦,苹果自己也承认了之前的诸多bug和不稳定性,这让苹果不得不重 ...

  3. 当async/await碰见forEach-------------爆炸

    let p = ['http://img3.imgtn.bdimg.com/it/u=3278834702,2663618759&fm=26&gp=0.jpg', 'http://im ...

  4. poj 3107 Godfather 求树的重心【树形dp】

    poj 3107 Godfather 和poj 1655差不多,那道会了这个也就差不多了. 题意:从小到大输出树的重心. 题会卡stl,要用邻接表存树..... #include<iostrea ...

  5. python if 选择结构

  6. C++讲课总结 标签: c++总结 2015-02-28 14:48 671人阅读 评论(25) 收藏

    昨天老师算是给串了一本C++ 的课本,根据自己的理解,赶紧记录一下,也好作为自己学习时候的根据. C++编程简介:每本讲语言的书,第一章总是简介,内容无非是发展历史,语言特色等东西,专业的东西不多,都 ...

  7. 在SAE上使用Flask插件

    因为我之前学习的时候使用的是虚拟环境,下载的所有需要用到的插件都在flask这个文件夹里面,SAE上Flask的版本和我本地用的版本对不上,导致有时候import都不对,于是我就把本地的环境直接放到S ...

  8. Python 官方文档:入门教程

    https://pythoncaff.com/docs/tutorial/3.7.0 官方入门教程,从这里开始你的 Python 之旅,将长久维护 基础信息 翻译说明 关于本教程 已完成 正文 1. ...

  9. re模块下的常用方法

    一  :  re模块的查找 findall  优先级查找  返回列表 找所有的匹配项(从大段的内容中找匹配到的项目) import re str = "qwer asdf zxcv qwer ...

  10. 即插即用,基于阿里云Ganos快速构建云上开源GIS方案

    对于轻量级GIS应用,选择具备时空能力的云上数据库再搭配开源GIS软件,能够快速构建稳定.廉价.实用的GIS解决方案.Ganos是阿里云自研时空基础设施(PaaS层)的核心引擎,该引擎整合了云上异构计 ...