Meteor Flow(想法题)

原题:Gym-100247I

题意:

你有能量盾,每秒加1点能量,初始为0,有n次攻击,时间为t,伤害为d,你可以闪避一部分伤害,不闪避的情况下,你的盾的能量减去d,能量不能小于0,求最小的闪避次数

解析:

看上去有点复杂,但是我们可以换一种看法,看成时间的累加和伤害的累加的大小比较即可

我们用优先队列存伤害,如果有一次伤害过来的时间伤害的累加大于时间的累加的话,就说明必须要闪避了,那么不管闪避哪次伤害都算一次,那么我们肯定会去闪避d大的那次伤害

代码:


D read(){ D ans=0; char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans; return ans;
}


int main(){
    int sum=0,ans=0;
    int n=read();priority_queue<int>q;
    while(n--){
        int t=read(),d=read();
        sum+=d;q.push(d);

        if(sum>t){
            sum-=q.top();q.pop();ans++;
        }
    }
    printf("%d\n",ans);
}

猜你喜欢

转载自blog.csdn.net/jk_chen_acmer/article/details/81407985