Gym - 101806T: Touch The Sky(贪心)
Figure: The house floats up in the sky by balloons. This picture is also used in 2018 KAIST RUN Spring Contest poster.
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)
OutputPrint the maximum number of balloons you can bust.
Examples3
30 10
30 20
30 30
3
4
0 10
2 4
5 3
8 2
3
题意:现在有N个气球,第i个气球可以使用的条件是高度不大于Li,使用后高度会增加Di,问最多用多少个气球。
思路:这类题肯定是要先排序,然后贪心或者DP。 反正就是按Li和Di排序后都不好操作。 我们按照Li+Di排序,然后用一个大根堆,里面保存的是增加的高度。
如果当前高度大于Li,但是Di比较小,我们用它的Di去换大根堆里最大的一个。 这样是完全无后效性的。
#include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; priority_queue<int>q; struct in{ ll x,y; bool friend operator<(in w,in v){ return w.x<v.x; } }s[300000]; int main() { int N; ll Now=0; scanf("%d",&N); rep(i,1,N) { scanf("%lld%lld",&s[i].x,&s[i].y); s[i].x+=s[i].y; } sort(s+1,s+N+1); rep(i,1,N){ Now+=s[i].y; q.push(s[i].y); if(Now>s[i].x){ Now-=q.top(); q.pop(); } } printf("%d\n",q.size()); return 0; }