Canada Cup 2016 D. Contest Balloons详解(贪心+优先队列)

54 篇文章 0 订阅
42 篇文章 0 订阅



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
Note

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

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

在这里膜拜一发潘学长,这个方法我自己敲得的过程中不断感叹,实在是太妙了。。。潘学长许多题目的方法都让我叹为观止~希望潘学长接下来的区域赛可以取得很好的成绩~

题意:给你n 个队伍,你的队伍是第一个队伍,每个队伍有气球数量,和最大数量。如果气球数量多于最大数量,你就会飞走,无法进行排名!你现在可以给你别的队伍气球,使得他们飞走,从而使你的排名尽量靠前!问你的最好名次

思路:这道题的一般会想到先对他们的气球数排名,然后找前面需要最少气球的就可以飞了的(必须给前面的队伍气球,这样自己才能往前前进),这样就有个问题,让前面的飞了,自己往前前进一名,但是自己的气球也减少了啊,后面的人可能会超过自己啊!我想到这里,就不知道怎么做了。。。先对总的气球数排名,然后又优先队列存前面需要多少气球才能飞(因为优先队列自动更新的),既然给前面一次气球,排名可能往前或者往后变化,那就每给前面一次气球,就看看现在的气球数能能排第几,直到当前的气球不能让前面任何一个队伍飞了。。(大体思路就是这样,写的程序才精髓跟奇妙~)

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 5;
struct node
{
    ll weight, t;
    node() {}
    node(ll tt, ll dd) : t(tt), weight(dd){}
    bool operator < (const node &a) const
    {
        return weight > a.weight;  //优先队列,队尾先走
    }
}a[maxn];
bool cmp(node a, node b)
{
    return a.t > b.t;  //对现在的气球排名
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%I64d%I64d", &a[i].t, &a[i].weight); //一开始存的重量
        a[i].weight = a[i].weight - a[i].t + 1;  //这里再改成需要多少个气球才能飞,直接相减是让他们相等再+1就让他们飞了~
    }
    sort(a+1, a+n, cmp);
    int ok = 1, index = 1, s = 1, ans = maxn; //index是记录现在的排名,s是记录扫过第几个队伍,ans记录过程当中最靠前的排名
    priority_queue<node> pq;
    while(ok)  //这个while够精髓~
    {
        ok = 0;  //一开始让ok等于0,如果下面没有更新,就说明当前的气球不能让前面任何一个队伍飞了
        while(s < n && a[s].t > a[0].t)  // 这一步把排名在自己队伍前面的队伍放进优先队列
            pq.push(a[s++]), index++;  //放一个,自己的排名往后一个
        ans = min(ans, index);  //记录最往前的成绩,注意要放在这里,因为现实往前方气球,然后看看后面有没有超过自己的,这时候排名才稳定
        if(!pq.empty())  //这里是if,一开始写成while,一直不对,因为放一次,后面的队伍可能就超过自己了,或许后面的队伍飞起来需要的气球更少,所以就要更新一次
        {
            a[0].t -= pq.top().weight; //把气球给前面
            pq.pop(); //飞起来
            if(a[0].t < 0) break;
            ok = 1; //如果这次可以放给前面就要更新排名。
            index--; //前面队伍飞一个就要排名往前一个,然后退出if,看看后面有没有超过自己的。。
        }
    }
    printf("%d\n", ans);
    return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值