51nod-1191 消灭兔子(贪心)

记录一个菜逼的成长。。

题目链接

这题是上题的进阶。
对于每支箭,我们要找到第一个血量大于伤害值的兔子的位置pos,那么我们把这只箭的伤害值设为pos-1的兔子的血量值
跟上题类似,从后往前贪心
将伤害值为当前兔子血量值的价格放入小顶堆,并且清空数组,因为血量可以相等,防止重复放入。
每次选取堆顶元素就可以了。
如果在消灭某一只兔子时堆为空时,那么说明没有弓箭可以消灭这只兔子,所以输出No Solution

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define cl(a,b) memset(a,b,sizeof(a))
typedef long long LL;
const int maxn = 100000 + 10;
int b[maxn];
vector<int>a[maxn];
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int mx = 0;
    for( int i = 1; i <= n; i++ )
        scanf("%d",b+i);
    sort(b+1,b+1+n);
    for( int i = 1; i <= m; i++ ){
        int x,y;
        scanf("%d%d",&x,&y);
        int pos = upper_bound(b+1,b+1+n,x) - b;
        x = b[pos-1];
        a[x].pb(y);
    }
    LL ans = 0;
    for( int i = n; i >= 1; i-- ){
        for( int j = 0; j < a[b[i]].size(); j++ ){
            q.push(a[b[i]][j]);
        }
        a[b[i]].clear();
        if(!q.empty())ans += (LL)q.top(),q.pop();
        else {puts("No Solution");return 0;}
    }
    printf("%lld\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值