【题解】codeforces725D Contest Balloons

本文探讨了一个ACM-ICPC竞赛中的特殊场景,即通过策略性地分配气球来提升团队排名的方法。文章介绍了一种利用贪心算法结合堆数据结构的解决方案,以确保团队能在竞赛中取得最佳排名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is:

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.
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.
Limak gives 1 balloon to each of two teams with 16 balloons initially.
Limak has 20 - 6 - 6 - 1 - 1 = 6 balloons.
There are three other teams left and their numbers of balloons are 40, 14 and 2.
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 with 81 000 000 000, 5 000 000 000 and 46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7).

【题解】
经过该题的标签”greedy”提示,我想到了这道题可以用贪心+堆来做
好久没打堆了,今天练了练手

贪心策略:每次让气球数比自己多的队伍中最容易上天的队伍上天
那么我们会注意到,气球数比自己多的队伍是会增加或减少的,因为当自己让人家上天后,自己气球数也减少了,排名也可能会靠后

所以思路就来了,每次用一个小根堆维护气球数比自己多的队伍,关键字为让他上天需要的气球数(比如t=5,w=10,那么需要6个气球才能上天),然后当前自己的名次为堆大小+1,更新给ans,更新完后送一个队伍上天。

循环结束条件:堆为空,或自己已没有能力再送人家上天

Code:

uses math;
var
    heap,t,w:array[0..1000000] of int64;
    ans,len,n:int64;
    i:longint;

procedure swap(var x,y:int64);
var
    tmp:int64;

begin
    tmp:=x; x:=y; y:=tmp;
end;

procedure sort(l,r:longint);
var
    i,j,mid,mid1:int64;

begin
    i:=l; j:=r; mid:=t[(l+r)>>1]; mid1:=w[(l+r)>>1];
    repeat
        while (t[i]>mid) or (t[i]=mid) and (w[i]<mid1) do inc(i);
        while (t[j]<mid) or (t[j]=mid) and (w[j]>mid1) do dec(j);
        if i<=j then
        begin
            swap(t[i],t[j]); swap(w[i],w[j]); inc(i); dec(j);
        end;
    until i>j;
    if i<r then sort(i,r);
    if l<j then sort(l,j);
end;

procedure push(k:int64);//入堆
var
    s:int64;

begin
    inc(len);
    heap[len]:=k;
    s:=len;
    while (s>1) and (heap[s]<heap[s>>1]) do
    begin
        swap(heap[s],heap[s>>1]); s:=s>>1;
    end;
end;

function pop:int64;//弹出
var
    s,x:int64;

begin
    pop:=heap[1];
    heap[1]:=heap[len];
    dec(len);
    s:=1;
    while (s<<1)<=len do
    begin
        if ((s<<1+1)>len) or (heap[s<<1]<heap[s<<1+1]) then x:=s<<1 else x:=s<<1+1;
        if heap[s]>heap[x] then
        begin
            swap(heap[s],heap[x]); s:=x;
        end else break;
    end;
end;

begin
    readln(n);
    for i:=1 to n do readln(t[i],w[i]);
    sort(2,n);//自己不排
    ans:=maxlongint; i:=2;
    while true do
    begin
        while (i<=n) and (t[i]>t[1]) do
        begin
            push(w[i]-t[i]+1);
            inc(i);
        end;
        ans:=min(ans,len+1);
        if (len=0) or (t[1]<heap[1]) then break;//结束条件
        dec(t[1],pop);
    end;
    writeln(ans);
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值