hdoj 4544 B

 湫湫减肥 
  越减越肥! 
   
  最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏。 
  游戏规则很简单,用箭杀死免子即可。 
  箭是一种消耗品,已知有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

 1 /*将兔子的血量从大到小排序,将剑的杀伤力从大到小排序,
 2 对于每一个兔子血量,将比他大的杀伤力大的剑压入优先队列,
 3 优先队列自己重写,让它每次抛出的数为价钱最小的
 4 定义优先队列
 5 priority_queueq;
 6 struct node{
 7 int d;
 8 int p;
 9 }
10 friend bool operator<(node c,node d){
11 //此行中的<是固定的,需要自己写的只有括号里边的
12 return c.p>d.p;//实际优先队列抛出的q.top().p是优先队列里边最小的
13 }
14 }b[N];
15 */
16 #include<stdio.h>
17 #include<iostream>
18 #include<algorithm>
19 using namespace std;
20 #define N 100005
21 #include<queue>
22 struct node{
23     int d;
24     int p;
25     friend bool operator<(node c,node d){
26         return c.p>d.p;
27     }
28 }b[N];
29 int a[N];
30 int cmp(int a,int b){
31     return a>b;
32 }
33 int cmp1(struct node a,struct node b){
34     return a.d>b.d;
35 }
36 int main(){
37     int n,m;
38     while(scanf("%d%d",&n,&m)!=EOF){
39         priority_queue<node> q;//定义注意类型是node
40         for(int i=1;i<=n;i++)
41         scanf("%d",&a[i]);
42         for(int i=1;i<=m;i++)
43         scanf("%d",&b[i].d);
44         for(int i=1;i<=m;i++)
45         scanf("%d",&b[i].p);
46         sort(a+1,a+n+1,cmp);
47         sort(b+1,b+m+1,cmp1);
48         int res=1;
49         long long ans=0;
50         int flag=0;
51         for(int i=1;i<=n;i++){
52             while(res<=m&&b[res].d>=a[i]){
53                 q.push(b[res]);
54                 //printf("&&&%d
",q.top().p);   加上返回NO
55                 res++;
56             }
57             if(q.empty()){
58                 flag=1;
59                 break;
60             }
61             else{
62                 ans+=q.top().p;
63                 q.pop();
64             }
65         }
66         if(flag==1)
67         printf("No
");
68         else
69         cout<<ans<<endl;
70         //printf("%lld
",ans);此题不知道为什么只能用cout输出,这样写wrong了一遍
71     }
72     return 0;
73 }
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 #define N 100005
 6 #include <queue>
 7 struct node{
 8 int d,p;
 9 friend bool operator<(node c,node d){
10     return c.p>d.p;
11     }
12 }b[N] ;
13 int a[N];
14 int cmp(int a,int b){
15     return a>b;
16 }
17 int cmp2(struct node a,struct node b){
18     return a.d>b.d;
19 }
20 int main(){
21     int n,m;
22     while(~scanf("%d%d",&n,&m)){
23         priority_queue<node> q;
24         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
25         for(int i=1;i<=m;i++) scanf("%d",&b[i].d);
26         for(int i=1;i<=m;i++) scanf("%d",&b[i].p);
27         sort(a+1,a+n+1;cmp);
28         sort(b+1,b+m+1;cmp2);
29         int res=1;
30         long long ans=0;
31         int flag=0;
32         for(int i=1;i<=n;i++){
33             while(res<=m&&b[res].d>=a[i]){
34                 q.push(b[res])
35                 res++
36             }
37             if(q.empty()){
38                 flag=1;
39                 break;
40             }
41             else {
42                 ans+=q.top.p;
43                 q.pop();
44             }
45         }
46         if(flag==1) printf("NO
");
47         else  cout<<ans<<endl;
48     }
49     return 0;
50 }
 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 struct arrow
 5 {
 6     __int64 d,p;
 7 };
 8 bool cmp(__int64 a,__int64 b)
 9 {
10     return a>b;
11 }
12 bool cmp1(arrow a,arrow b)   //  按照 Q币的升序 排序
13 {
14     return a.p<b.p;
15 }
16 int main()
17 {
18     __int64 n,i,j,m,jishu,b[10011],count,num;
19     arrow a[100011];
20     while(scanf("%I64d%I64d",&n,&m)!=EOF)
21     {
22         num=count=0;
23         for(i=0;i<n;i++)
24             scanf("%I64d",&b[i]);
25         for(i=0;i<m;i++)
26             scanf("%I64d",&a[i].d);
27         for(i=0;i<m;i++)
28             scanf("%I64d",&a[i].p);
29         sort(b,b+n,cmp);          //   兔子的 血量 降序
30         sort(a,a+m,cmp1);   //箭的 加个 升序
31         for(i=0,num=n;i<n;i++)  //从最厉害的  兔子开始杀
32         {
33             for(j=0;j<m;j++) //  从最便宜的 箭  开始 一个一个试
34             {
35                 if(a[j].d==-1)
36                     continue;
37                 if(a[j].d>=b[i])  //如果 箭的伤害 大于兔子的血量的话.
38                 {
39                     a[j].d=-1;                //该  箭   失效           //这一句和下一句如果加上 就超时 如果不加上 就300ms.........
40                     num--;
41                     count=count+a[j].p;            //计算  花费
42                     break;
43                 }
44             }
45         }
46         if(num==0)
47             printf("%I64d
",count);
48         else
49             printf("No
");
50     }
51 }


原文地址:https://www.cnblogs.com/z-712/p/7324444.html