HDU-4544 湫湫系列故事——消灭兔子

 湫湫减肥
  越减越肥!

  最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏。
  游戏规则很简单,用箭杀死免子即可。
  箭是一种消耗品,已知有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
解题思路:贪心+优先队列裸题。。。后来发现优先队列全忘了,果然还是敲少了代码。果断复习了一下模板。
兔子的血量降序排,将能一剑杀死兔子的剑存入优先队列,再弹出qq币最少的剑就ok。
但是本人在写的时候,想到的是用multiset容器来解决这个问题,开始tle,后面用了二分的一个函数就wa,到现在也不知道怎么解决这个问题。
还是等以后再看吧,如果有大佬愿意指出错误,不胜感激!
 
ac代码
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<queue>
 4 #include<algorithm>
 5 using namespace std;
 6 struct Node{
 7     int d;
 8     int cost;
 9 }a[100050];
10 bool cmp1(Node a,Node b)
11 {
12     return a.d<b.d;
13 }
14 struct cmp
15 {
16     bool operator()(int x,int y)
17     {
18             return x>y;
19     }
20 };
21 int num[100050];
22 int main()
23 {
24     int n,m;
25     while(cin>>n>>m)
26     {
27         priority_queue<int,vector<int>,cmp> q;
28         for(int i=0;i<n;i++)
29             scanf("%d",&num[i]);
30         for(int i=0;i<m;i++)
31             scanf("%d",&a[i].d);
32         for(int i=0;i<m;i++)
33             scanf("%d",&a[i].cost);
34         if(n>m)
35         {
36             cout<<"No"<<endl;
37             continue;
38         }
39         sort(num,num+n);
40         sort(a,a+m,cmp1);
41         int t=m-1;
42         bool flag=true;
43         long long ans=0;
44         for(int i=n-1;i>=0;i--)
45         {
46             while(t>=0&&a[t].d>=num[i])
47             {
48                 q.push(a[t].cost);
49                 t--;
50             }
51             if(q.empty())
52             {
53                 flag=false;
54                 break;
55             }
56             ans+=q.top();
57             q.pop();
58         }
59         if(flag)
60             cout<<ans<<endl;
61         else
62             cout<<"No"<<endl;
63     }
64 }

用multiset提交的wa代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<set>
#include<string.h>
using namespace std;
struct Node{
    int d;
    int cost;
}a[100050];
multiset<int> num;
bool cmp(Node a,Node b)
{
    if(a.cost==b.cost)
        return a.d>b.d;
    else
        return a.cost<b.cost;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        num.clear();
        int y;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&y);
            num.insert(y);
        }
        for(int i=0;i<m;i++)
            scanf("%d",&a[i].d);
        for(int i=0;i<m;i++)
            scanf("%d",&a[i].cost);
        sort(a,a+m,cmp);
        if(n>m)
            cout<<"No"<<endl;
        else
        {
            multiset<int>::iterator it,st,last;
            long long sum=0;
            for(int i=0;i<m;i++)
            {
                if(num.empty())
                    break;
                int key=a[i].d;
                if(key<*num.begin())
                    continue;
                if(key>=*num.end())
                {
                    it=num.end();
                    num.erase(--it);
                    sum+=a[i].cost;
                    continue;
                }
                it=num.find(key);
                if(it!=num.end())
                {
                    num.erase(it);
                    sum+=a[i].cost;
                    continue;
                }
                else
                {
                    st=num.begin();
                    last=num.end();
                    it=lower_bound(st,last,key);//二分查找的函数,百度好久才知道。。
                    num.erase(it);
                    sum+=a[i].cost;
                }
            }
            if(num.empty())
                cout<<sum<<endl;
            else
                cout<<"No"<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ISGuXing/p/7242605.html