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
把比Limak队的气球数多的队伍都放入优先队列中,优先队列按照队伍还有几个球才能飞起,从小到大排序.剩下的对放入数组中,按照气球数从小到大排序.取出队列第一个元素,判断Limak的气球数是否能够让其飞起,若是,则队伍淘汰,更新Limak的气球数,把数组中比Limak对多的气球放入优先队列中,循环操作
#include <bits/stdc++.h>
#define maxn 105
using namespace std;
typedef long long ll;
struct Node{
Node(){
}
Node(ll a, ll b){
t = a;
w = b;
}
friend bool operator < (const Node&a, const Node&b){
return (a.w-a.t+1) > (b.w-b.t+1);
}
ll t, w;
};
priority_queue<Node> q;
vector<Node> v;
bool cmp(const Node &a, const Node&b){
return a.t > b.t;
}
int main(){
//freopen("in.txt", "r", stdin);
int n, ans;
ll a, b, tt, ww;
scanf("%d", &n);
scanf("%I64d%I64d", &tt, &ww);
for(int i = 1; i < n; i++){
scanf("%I64d%I64d", &a, &b);
if(a > tt){
q.push(Node(a, b));
}
else{
v.push_back(Node(a, b));
}
}
int h = 0;
sort(v.begin(), v.end(), cmp);
ans = q.size() + 1;
while(!q.empty()){
Node d = q.top();
if(d.w - d.t + 1 <= tt){
q.pop();
tt -= (d.w - d.t + 1);
}
else
break;
for(; h < v.size(); h++){
if(v[h].t > tt){
q.push(v[h]);
}
else
break;
}
ans = min(ans, (int)q.size() + 1);
}
printf("%d\n", ans);
return 0;
}
本文介绍了一个竞赛场景下的气球分配策略问题,通过合理分配气球队伍可以取得更好的排名。使用优先队列和数组来实现算法,确保代码效率。

847

被折叠的 条评论
为什么被折叠?



