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?
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.
Print one integer denoting the best place Limak can get.
8 20 1000 32 37 40 1000 45 50 16 16 16 16 14 1000 2 1000
3
7 4 4 4 4 4 4 4 4 4 4 4 4 5 5
2
7 14000000003 1000000000000000000 81000000000 88000000000 5000000000 7000000000 15000000000 39000000000 46000000000 51000000000 0 1000000000 0 0
2
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).
题目大意:
给你N个队伍,每个队伍有两个属性:
1、拥有的气球数。
2、权值。
当一个队伍的气球数大于权值的时候,这个队就漂上了天上。
现在你是一号队伍,你希望你的排名尽可能的高,所以你可以把你的气球给其他队伍,让他们飞出场外。
问最高可以排名到哪里。
思路:
这种题很套路,明显有两种需要排序的属性:
①气球数。
②每个队需要气球的个数能够飞出场外。
对①排序,我们的目的是看看有哪些队伍的气球数是大于我们队伍的球球数的。
对②排序,我们的目的是贪心的看看哪些队伍既气球比我们多,而且需要的气球数飞上天空最少。
那么我们不妨首先将所有队伍按照①排序,然后维护一个优先队列。
优先级队列肯定优先的是最小值(飞上天空需要的气球数);
那么我们维护这个队列中的所有队伍都必须气球数大于我们队伍。
在过程中不断维护排名的最小值,并且过程不断维护新队伍入优先队列。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
struct node
{
ll num,w,ned;
friend bool operator < (node a,node b)
{
return a.ned > b.ned;
}
}a[308000];
int cmp(node a,node b)
{
if(a.num==b.num)return a.ned<b.ned;
else return a.num>b.num;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
priority_queue<node >s;
for(int i=1;i<=n;i++)
{
scanf("%I64d%I64d",&a[i].num,&a[i].w);
a[i].ned=a[i].w-a[i].num+1;
}
int output=0x3f3f3f3f;
sort(a+2,a+1+n,cmp);
int j=2;
while(1)
{
int flag=0;
while(j<=n&&a[j].num>a[1].num)s.push(a[j]),flag=1,j++;
int tmp=s.size()+1;output=min(output,tmp);
if(s.size()>0)
{
if(s.top().ned<=a[1].num)a[1].num-=s.top().ned,s.pop(),flag=1;
}
if(flag==0)break;
}
int tmp=s.size()+1;output=min(output,tmp);
printf("%d\n",output);
}
}