【贪心+堆】Codeforces725D[Contest Balloons]题解

题目概述

n 个队伍,第 i 个队伍有 ti 个气球,重量为 wi ,其中第 1 个队伍是你所在的队伍。你可以给别的队伍任意数量的气球(不超过你的气球数),如果一个队伍的气球数 > 重量,这个队伍就会飞起来(滑稽)并失去比赛资格。求你的队伍能达到的最小名次是多少(按气球数排名)。

解题报告

这是一道挺简单的贪心题。

我们肯定要选气球数多于我们的队伍弄飞,且在这些队伍中选需要气球数少的队伍显然更优秀,那么我们用堆就可以快速找到最小值。

需要注意到,每次弄飞其他队伍我们的气球数也会减少,此时就会有新的气球数多于我们的队伍出现,所以弄飞一个队我们就需要重新统计一下答案,从所有答案中刷出最小的就是最小名次。

示例程序

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long LL;
const int maxn=300000;

int n,ans;LL T,W;
pair<LL,LL> a[maxn+5];
priority_queue< LL,vector<LL>,greater<LL> > Heap;

inline bool Eoln(char ch) {return ch==10||ch==13||ch==EOF;}
inline char readc()
{
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF; else return *l++;
}
inline int readl(LL &x)
{
    LL tot=0,f=1;char ch=readc(),lst='+';
    while ('9'<ch||ch<'0') {if (ch==EOF) return EOF;lst=ch;ch=readc();}
    if (lst=='-') f=-f;
    while ('0'<=ch&&ch<='9') tot=tot*10+ch-48,ch=readc();
    return x=tot*f,Eoln(ch);
}
int main()
{
    freopen("program.in","r",stdin);
    freopen("program.out","w",stdout);
    scanf("%d",&n);n--;readl(T);readl(W);
    for (int i=1;i<=n;i++) readl(a[i].first),readl(a[i].second);
    sort(a+1,a+1+n);
    int i=n;while (i&&a[i].first>T) Heap.push(a[i].second-a[i].first+1),i--;
    ans=n-i+1;
    while (!Heap.empty())
    {
        LL MIN=Heap.top();Heap.pop();
        if (MIN>T) break;T-=MIN;n--;
        while (i&&a[i].first>T) Heap.push(a[i].second-a[i].first+1),i--;
        ans=min(ans,n-i+1);
    }
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值