CodeForces 994B Knights of a Polygonal Table(STL、贪心)

CodeForces 994B Knights of a Polygonal Table(STL、贪心)

http://codeforces.com/problemset/problem/994/B

 

 

 

 

题意:

给出n和m,有n个骑士,每个骑士的战力为ai,这个骑士有bi的钱,如果一个骑士的战力比另一个骑士的战力高,那么,他就可以夺取这个骑士的钱,但是每个骑士最多夺取m个人,问,这些骑士最多可以获得

多少钱。

 

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 //const double PI=acos(-1);
17 #define Bug cout<<"---------------------"<<endl
18 const int maxn=1e5+10;
19 using namespace std;
20 
21 struct node
22 {
23     int val;
24     int num;
25     LL ans;
26     int pos;
27 }PE[maxn];
28 
29 bool cmp1(node a,node b)
30 {
31     return a.val<b.val;
32 }
33 
34 bool cmp2(node a,node b)
35 {
36     return a.pos<b.pos;
37 }
38 
39 int main()
40 {
41     int n,m;
42     scanf("%d %d",&n,&m);
43     for(int i=1;i<=n;i++)
44     {
45         scanf("%d",&PE[i].val);
46         PE[i].pos=i;
47     }
48     for(int i=1;i<=n;i++)
49     {
50         scanf("%d",&PE[i].num);
51         PE[i].ans=0;
52     }
53     sort(PE+1,PE+1+n,cmp1);//按战力排序 
54     priority_queue<int,vector<int> ,greater<int> > qe;//存放m个大的硬币数 
55     LL sum=0;
56     for(int i=1;i<=n;i++)
57     {
58         if(qe.size()<m)
59         {
60             qe.push(PE[i].num);
61             sum+=PE[i].num;
62         }
63         else if(!qe.empty())
64         {
65             int t=qe.top();
66             if(t<PE[i].num)
67             {
68                 qe.pop();
69                 sum=sum-t+PE[i].num;
70                 qe.push(PE[i].num);
71             }
72         }
73         PE[i+1].ans+=sum;//注意是i+1 
74     }
75     sort(PE+1,PE+1+n,cmp2);//按位置排序 
76     for(int i=1;i<=n;i++)
77     {
78         printf("%lld ",PE[i].ans+PE[i].num);//注意是LL 
79     }
80     return 0;
81 }

 

posted @ 2019-10-31 15:31  jiamian22  阅读(208)  评论(0编辑  收藏  举报