In the year 2117, Professor Jaemin Yu developed a linear-time algorithm for TSP(Traveling Salesperson Problem). Not long after that happened, all computer systems were destroyed, and nuclear weapons demolished all the lands. You, a great computer expert, also lost your job. With a great despair, you lost your meaning of life long ago. All those things that made your heart beat – where had they gone? After questioning yourself again and again, your conclusion is …
“If I go to KAIST where I started my first ICPC, can I find a meaning of my life?”
All transportations were destroyed, but you were an avid ICPC participant, and you collected a lot of century-old balloons in Korean Regionals. If you could float a house with some of those balloons…
Currently you have N balloons, and you are trying to float the house into the sky by attaching balloons on the rooftop. Every balloon have altitude limit Li and capacity Di, which indicates you can blow balloons in altitude at most Li, and the balloon busts after increasing the altitude by Di.
Your journey starts at altitude 0. If you have more than 1 balloons enlarged, then the house will ascend too fast. Thus, you will blow one balloon and attach it at the rooftop, increase the altitude until the balloons bust, blow the other balloon and attach it to increase the altitude… to make your house float. For convenience, you may assume that balloons can only increase the altitude.
You don’t care about your final altitude, but a balloon can move a fixed amount of distance. Thus, you want to bust as many balloons as possible. You want to calculate a maximum number of balloons you can bust, and check if you can make a journey to KAIST. Let’s see whether your 100-year-old ICPC experience can help on this problem!
Input
The first line contains N, the number of balloons. (1 ≤ N ≤ 250, 000)
In next N lines, the altitude limit of i-th balloon Li, and capacity of i-th balloon Di are given as two space-separated integers. (0 ≤ Li ≤ 1015, 1 ≤ Di ≤ 109)
Output
Print the maximum number of balloons you can bust.
Examples
Input
3
30 10
30 20
30 30
Output
3
Input
4
0 10
2 4
5 3
8 2
Output
3
题意:
给你n个气球,所有气球捆在一起,每个气球有一个上限x和一个升高的高度y。给一个气球充气就可以升高y[i],但是只能在高度小于等于x[i]的时候充气,而且一次只能充一个,问你最多能给多少气球充气。
题解:
比赛的时候想到了类似的方法,但不完全对,正解是把一个气球升高的高度和上限加起来,这就是每一个气球充气之后的上限,只要加上它的高度小于等于上限,那么这个气球就可以被使用,如果大于这个上限了,那么就把升高高度最高的给减掉,就是维护一个升高的最小值
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=250005;
struct node
{
ll lim,up,sum;
bool operator< (const node& a)const
{
return sum<a.sum;
}
}p[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld%lld",&p[i].lim,&p[i].up),p[i].sum=p[i].lim+p[i].up;
sort(p+1,p+1+n);
priority_queue<ll>Q;
ll high=0;
for(int i=1;i<=n;i++)
{
high+=p[i].up;
Q.push(p[i].up);
if(high>p[i].sum)
high-=Q.top(),Q.pop();
}
printf("%d\n",Q.size());
return 0;
}