VOJ - Victor's Research (贪心+堆)

VOJ - Meteor Flow (贪心+堆)

题目链接: I - Meteor Flow Gym - 100247I

题意

有n个导弹要进攻你,你的能源每个单位会涨一,现在你需要拦截第i个导弹需要ai的能源,可以使用拦截导弹消灭一个导弹,问你最少拦截几次。

思路

把前面的导弹的攻击力存储下来,如果抵挡不住接下来的攻击,就取出一个攻击力最高的导弹消灭掉。


代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue<int> sp;
int main(){
    ll n,i,j,pre=0,now=0,ans=0;
    scanf("%lld",&n);
    for(i = 1;i <= n;i ++){
        ll t,d;
        scanf("%lld%lld",&t,&d);
        now += t-pre-d;
        pre = t;
        sp.push(d);
        if(now < 0){
            now += sp.top();
            sp.pop();
            ans ++;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40513946/article/details/81174189