Canada Cup 2016 D. Contest Balloons

Contest Balloons

time limit per test: 3 second
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 1 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 1 1 through n n n. The i i i-th team has t i t_i ti balloons and weight w i w_i wi. It’s guaranteed that t i t_i ti doesn’t exceed w i w_i 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 n n ( 2   ≤   n   ≤   300   000 2 \le n \le 300\ 000 2 n 300 000) — the number of teams.

The i i i-th of n n n following lines contains two integers t i t_i ti and w i w_i wi ( 0   ≤   t i   ≤   w i   ≤   1 0 18 0 \le t_i \le w_i \le 10^{18} 0 tiwi 1018) — respectively the number of balloons and the weight of the i i i-th team. Limak is a member of the first team.

Output

Print one integer denoting the best place Limak can get.

Example

i n p u t \tt input input
8
20 1000
32 37
40 1000
45 50
16 16
16 16
14 1000
2 1000
o u t p u t \tt output output
3
i n p u t \tt input input
7
4 4
4 4
4 4
4 4
4 4
4 4
5 5
o u t p u t \tt output output
2
i n p u t \tt input input
7
14000000003 1000000000000000000
81000000000 88000000000
5000000000 7000000000
15000000000 39000000000
46000000000 51000000000
0 1000000000
0 0
o u t p u t \tt output output
2

Note

In the first sample, Limak has 20 20 20 balloons initially. There are three teams with more balloons ( 32 32 32, 40 40 40 and 45 45 45 balloons), so Limak has the fourth place initially. One optimal strategy is:

  1. Limak gives 6 6 6 balloons away to a team with 32 32 32 balloons and weight 37 37 37, which is just enough to make them fly. Unfortunately, Limak has only 14 14 14 balloons now and he would get the fifth place.
  2. Limak gives 6 6 6 balloons away to a team with 45 45 45 balloons. Now they have 51 51 51 balloons and weight 50 50 50 so they fly and get disqualified.
  3. Limak gives 1 1 1 balloon to each of two teams with 16 16 16 balloons initially.
  4. Limak has 20   −   6   −   6   −   1   −   1   =   6 20 - 6 - 6 - 1 - 1 = 6 20  6  6  1  1 = 6 balloons.
  5. There are three other teams left and their numbers of balloons are 40 40 40, 14 14 14 and 2 2 2.
  6. 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 2 2, 3 3 3 and 5 5 5 (the teams with 81   000   000   000 81\ 000\ 000\ 000 81 000 000 000, 5   000   000   000 5\ 000\ 000\ 000 5 000 000 000 and 46   000   000   000 46\ 000\ 000\ 000 46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 6 6 and team 7 7 7).

Tutorial

本题难点在于 Limak 的队伍在送出气球后自己的气球也会减少,需要动态维护当前比自己气球多的队伍,这时我们可以用堆来维护,可以用到优先队列来解决这个问题,将气球比 Limak 队伍多的队伍全部放进优先队列,其中放入的值为 如果 Limak 想放飞这支队伍,需要给他的气球数量,每次争取用最少的气球放飞其他气球比自己多的队伍

此解法时间复杂度为 O ( n log ⁡ n ) \mathcal O(n\log n) O(nlogn)

Solution

import sys
input = lambda: sys.stdin.readline().strip()
from queue import PriorityQueue

n = int(input())
t, w = [], []
for _ in range(n):
    tt, ww = map(int, input().split())
    t.append(tt)
    w.append(ww)
idx = sorted(range(1, n), key = lambda x: t[x], reverse = True)
ans, j = n, 0
q = PriorityQueue()
for i in range(n):
    while j < n - 1 and t[idx[j]] > t[0]:
        q.put(w[idx[j]] - t[idx[j]] + 1)
        j += 1
    ans = min(ans, j - i + 1)
    tp = 10 ** 18 + 1
    if q.qsize():
        tp = q.get()
    if tp > t[0]:
        break
    t[0] -= tp
print(ans)
  • 24
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值