Canada Cup 2016-D. Contest Balloons(优先队列)

本文介绍了一个竞赛场景下的气球分配策略问题,通过合理分配气球队伍可以取得更好的排名。使用优先队列和数组来实现算法,确保代码效率。

原题链接

D. Contest Balloons
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

Input

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.

Output

Print one integer denoting the best place Limak can get.

Examples
input
8
20 1000
32 37
40 1000
45 50
16 16
16 16
14 1000
2 1000
output
3
input
7
4 4
4 4
4 4
4 4
4 4
4 4
5 5
output
2
input
7
14000000003 1000000000000000000
81000000000 88000000000
5000000000 7000000000
15000000000 39000000000
46000000000 51000000000
0 1000000000
0 0
output
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值