51 Nod 1191消灭兔子

1191 消灭兔子

  1. 1 秒
  2.  
  3. 131,072 KB
  4.  
  5. 40 分
  6.  
  7. 4 级题

有N只兔子,每只有一个血量B[i],需要用箭杀死免子。有M种不同类型的箭可以选择,每种箭对兔子的伤害值分别为D[i],价格为P[i](1 <= i <= M)。假设每种箭只能使用一次,每只免子也只能被射一次,计算要消灭地图上的所有兔子最少需要多少Q币。如不能杀死所有兔子,请输出No Solution。

特别说明:1、当箭的伤害值大于等于兔子的血量时,能将兔子杀死;2、血量B[i],箭的伤害值D[i],箭的价格P[i],均小于等于100000。

 收起

输入

第1行:两个整数N,M,中间用空格分隔(1 <= N, M <= 50000),分别表示兔子的个数和箭的种类。
第2 - N + 1行:每行1个正整数(共N行),表示兔子的血量B[i](1 <= B[i] <= 100000)。
第N + 2 - N + M + 1行:每行2个正整数(共M行),中间用空格分隔,表示箭所能造成的伤害值D[i],和需要花费的Q币P[i](1 <= D[i], P[i] <= 100000)。

输出

输出最少需要多少Q币才能消灭所有的兔子。如果不能杀死所有兔子,请输出"No Solution"。

输入样例

3 3
1
2
3
2 1
3 2
4 3

输出样例

6

#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
int n,m;
struct node
{
    int d;
    int p;
} nod[50005];
int B[50005];
bool cmp(node &n1,node &n2)
{
    return n1.d>n2.d;
}
bool operator<(const node &n1,const node &n2)
{
    return n1.p<n2.p;
}
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&B[i]);
    }
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&nod[i].d,&nod[i].p);
    }
    sort(nod,nod+m,cmp);
    sort(B,B+n,greater<int>());

    ll ans=0;bool ok=1;
    int j=-1;
    for(int i=0;i<n;i++)
    {
        while(nod[j+1].d>=B[i]&&j+1<m)
        {
            q.push(nod[j+1].p);
            j++;
        }
        if(!q.empty())
        {
            ans+=q.top();
            q.pop();
        }
        else {ok=0;break;}
    }
    if(!ok)printf("No Solution
");
    else printf("%lld
",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/linruier/p/9919161.html