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

来源:互联网 发布:知乎2017校园招聘 编辑:程序博客网 时间:2024/06/05 21:01

大体题意:

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

思路:

借鉴了学长的博客:

贪心的方式:

你给气球肯定要给气球比你多的人的队伍,让他们飞走后,可能你的名次会下降,但为了更好的名次,你必须在给比你靠前的人的气球,这样给一次 更新一次名次最大值即可!

可以用优先队列的方式来模拟这一个过程!

要注意的是,给一次气球更新最大名次,否则可能得不到最优解,因为这一次你能给,但是不应该给,给了就再也无法翻身了= =!  所以这也同样完成给与不给的选择!!!

详细见代码:

#include <bits/stdc++.h>using namespace std;typedef long long ll;struct Node{    ll t,w;    Node(ll t = 0ll,ll w =0ll):t(t),w(w){}    bool operator < (const Node & rhs) const {        return t < rhs.t || (t == rhs.t && w > rhs.w);    }};priority_queue<ll,vector<ll>,greater<ll> >q1;priority_queue<Node>q2;int main(){    ll t1,w1;    int n;    scanf("%d",&n);    scanf("%lld %lld",&t1, &w1);    for (int i = 1; i < n; ++i){        ll t,w;        scanf("%lld %lld",&t, &w);        if (t > t1) q1.push(w-t);        else q2.push(Node(t,w));    }    ll ans = q1.size()+1ll;    while(!q1.empty() && q1.top() + 1 <= t1){        t1 -= q1.top() + 1;        q1.pop();        while(!q2.empty() && q2.top().t > t1){            Node u = q2.top();q2.pop();            ll tt = u.w - u.t;//            if (tt > t1){                q1.push(tt);//                continue;//            }C//            t1 -= (tt + 1);        }        ans = min(ans,q1.size()+1ll);    }//    ans = q1.size() + 1ll;//    if (ans == (ll)1e18)printf("1\n");    printf("%lld\n",ans);    return 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, numbered1 through n. Thei-th team has ti balloons and weightwi. It's guaranteed thatti doesn't exceedwi 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 integersti andwi (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
820 100032 3740 100045 5016 1616 1614 10002 1000
Output
3
Input
74 44 44 44 44 44 45 5
Output
2
Input
714000000003 100000000000000000081000000000 880000000005000000000 700000000015000000000 3900000000046000000000 510000000000 10000000000 0
Output
2
Note

In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32,40 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 only14 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 weight50 so they fly and get disqualified.
  3. Limak gives 1 balloon to each of two teams with16 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 40, 14 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 2, 3 and 5 (the teams with81 000 000 000, 5 000 000 000 and46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team6 and team 7).



0 0