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 thatti 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 and46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7).
分析:优先队列或multiset模拟即可,答案就在过程中取个最小值即可;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] #define sys system("pause") const int maxn=3e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,k,t,ans,pos; multiset<ll>pq; ll bal; struct node { int id; ll x,y; node(){} node(int _id,int _x,int _y):id(_id),x(_x),y(_y){} bool operator<(const node&p)const { return x==p.x?id<p.id:x>p.x; } }a[maxn]; int main() { int i,j; pos=-1; scanf("%d",&n); rep(i,1,n) { scanf("%lld%lld",&a[i].x,&a[i].y); a[i].id=i; if(a[i].x>a[1].x)pq.insert(a[i].y-a[i].x+1); } sort(a+1,a+n+1); rep(i,1,n)if(a[i].id==1){pos=i+1,bal=a[i].x;break;} ans=pq.size()+1; while(true) { if(pq.empty()||bal<*pq.begin())break; bal-=*pq.begin(); pq.erase(pq.begin()); for(int cnt=1;pos<=n&&a[pos].x>bal;pos++,cnt++) { pq.insert(a[pos].y-a[pos].x+1); } if(pq.size()+1<ans)ans=pq.size()+1; } printf("%d ",ans); //system("Pause"); return 0; }