51Nod 1191 消灭兔子 (贪心+优先队列)

版权声明:菜鸡博客大佬随便转 https://blog.csdn.net/qq_40642465/article/details/83244791

  对兔子血量排个降序(即从血多的开始杀),对弓箭按伤害值降序排。对每一只兔子,都要把能杀死他的弓箭的价值入队,入队完毕以后,如果队列为空说明这个兔子杀不了,不为空说明这个兔子能杀,那就从能杀这个兔子的弓箭里选个最便宜的,按照这样对n只兔子操作

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=5e4+10;
struct node
{
    int d,p;
    node(int dd=0,int pp=0)
    {
        d=dd;
        p=pp;
    }
}a[maxn];
struct node1
{
    int p;
    node1(int pp=0)
    {
        p=pp;
    }
    friend bool operator<(const node1&ii,const node1&oo)
    {
        return ii.p>oo.p;
    }
};
bool cmp1(const int&aa,const int&bb)
{
    return aa>bb;
}
bool cmp2(const node&j1,const node&j2)
{
    return j1.d>j2.d;
}
int b[maxn];
int main()
{
    int n,m,flag=1;
    ll ans=0;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&(b[i]));
    for(int i=1;i<=m;i++) scanf("%d %d",&(a[i].d),&(a[i].p));
    sort(b+1,b+n+1,cmp1);
    sort(a+1,a+m+1,cmp2);
    priority_queue<node1>q;
    while(!q.empty())
        q.pop();
    for(int i=1,j=1;i<=n;i++)
    {
        while(j<=m&&a[j].d>=b[i])
                q.push(node1(a[j].p)),j++;
        if(q.empty())
        {
            printf("No Solution\n");
            return 0;
        }
        ans+=q.top().p;
        q.pop();
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40642465/article/details/83244791
今日推荐