HDU 4544 湫湫系列故事——消灭兔子 (优先队列)

湫湫系列故事——消灭兔子

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 138    Accepted Submission(s): 49


Problem Description
  湫湫减肥
  越减越肥!
  
  最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏。
  游戏规则很简单,用箭杀死免子即可。
  箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di(1 <= i <= M),每种箭需要一定的QQ币购买。
  假设每种箭只能使用一次,每只免子也只能被射一次,请计算要消灭地图上的所有兔子最少需要的QQ币。
 

 

Input
输入数据有多组,每组数据有四行;
第一行有两个整数N,M(1 <= N, M <= 100000),分别表示兔子的个数和箭的种类;
第二行有N个正整数,分别表示兔子的血量Bi(1 <= i <= N);
第三行有M个正整数,表示每把箭所能造成的伤害值Di(1 <= i <= M);
第四行有M个正整数,表示每把箭需要花费的QQ币Pi(1 <= i <= M)。

特别说明:
1、当箭的伤害值大于等于兔子的血量时,就能将兔子杀死;
2、血量Bi,箭的伤害值Di,箭的价格Pi,均小于等于100000。
 

 

Output
如果不能杀死所有兔子,请输出”No”,否则,请输出最少的QQ币数,每组输出一行。
 

 

Sample Input
3 3 1 2 3 2 3 4 1 2 3 3 4 1 2 3 1 2 3 4 1 2 3 1
 

 

Sample Output
6 4
 

 

Source
 

 

Recommend
liuyiding
 

 

 

 

优先队列。

很容易。

 

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <math.h>
using namespace std;
const int MAXN=100010;
int B[MAXN];
struct Node
{
    int D,P;
}node[MAXN];
bool cmp1(Node a,Node b)
{
    return a.D<b.D;
}

struct cmp
{
    bool operator ()(int x, int y)
    {
        return x > y;// x小的优先级高
      //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
    }
};
priority_queue<int, vector<int>, cmp>q;//定义方法


int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        while(!q.empty())q.pop();
        for(int i=0;i<n;i++)scanf("%d",&B[i]);
        for(int i=0;i<m;i++)scanf("%d",&node[i].D);
        for(int i=0;i<m;i++)scanf("%d",&node[i].P);
        if(n>m)
        {
            printf("No\n");
            continue;
        }
        sort(B,B+n);
        sort(node,node+m,cmp1);
        int t=m-1;
        bool flag=true;
        long long ans=0;
        for(int i=n-1;i>=0;i--)
        {
            while(t>=0 && node[t].D>=B[i])
            {
                q.push(node[t].P);
                t--;
            }
            if(q.empty())
            {
                flag=false;
                break;
            }
            ans+=q.top();
            q.pop();
        }
        if(flag)cout<<ans<<endl;        //printf("%d\n",ans);
        else printf("No\n");
    }
    return 0;
}

 

posted on 2013-03-31 22:21  kuangbin  阅读(1308)  评论(1编辑  收藏  举报

导航

JAVASCRIPT: