codeforces上一道贪心算法题

简介:

贪心算法:

STL优先队列实现最大堆,最小堆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<queue>
using  namespace  std;
const  int  MAX=1001;
int  a[MAX];
int  calPrize( int  n,  int  x)
{
     int  start=n-x+1;
     return  (start+n)*x/2;
}
int  fun( int  n,  int  m,  int  pos[])
{
     priority_queue< int ,vector< int >,greater< int > >heap; //最小堆
     priority_queue< int ,vector< int >,less< int > >heap2; //最大堆
     for ( int  i=0;i<m;i++)
     {
             heap.push(pos[i]);
             heap2.push(pos[i]);
     }
     int  ret1=0,ret2=0;
     for ( int  i=0;i<n;i++)
     {
             int  tmp=heap.top();
             ret1+=tmp;
             heap.pop();
             if (tmp>1)
             heap.push(tmp-1);
             tmp=heap2.top();
             ret2+=tmp;
             heap2.pop();
             if (tmp>1)
             heap2.push(tmp-1);
     }
     cout<<ret2<< " " <<ret1<<endl;
     
}
int  main()
{
     int  n, m;
     cin>>n>>m;
     for ( int  i=0;i<m;i++)
             cin>>a[i];
     fun(n,m,a);
     //cin>>m;
}

本文转自博客园知识天地的博客,原文链接:codeforces上一道贪心算法题,如需转载请自行联系原博主。

相关文章
|
8月前
codeforces 272C. Dima and Staircase(线段树)
题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。
19 0
|
人工智能
codeforces455——A. Boredom(线性DP)
codeforces455——A. Boredom(线性DP)
103 0
codeforces455——A. Boredom(线性DP)
HDU7018.Banzhuan(计算几何+贪心)
HDU7018.Banzhuan(计算几何+贪心)
86 0
HDU7018.Banzhuan(计算几何+贪心)
|
人工智能 vr&ar Perl
codeforces1509 C. The Sports Festival (区间DP)
codeforces1509 C. The Sports Festival (区间DP)
91 0
|
数据安全/隐私保护
Codeforces 417D.Cunning Gena (状压DP)
Codeforces 417D.Cunning Gena (状压DP)
69 0
|
人工智能
Codeforces-1260-E. Tournament贪心
题意: 有n个人,第i个人有力量值i,这n个人中,每次对局两两之间进行solo,如果说一个人的力量之大于他的对手,这个人是赢的,赢得比赛的选手会进入下一轮比赛中。 然而里面有一个人为你的朋友,他或许并不是里面最强的人,要想让朋友成为冠军,就需要对必要的人进行贿赂,求出最少需要花费多少才能使得朋友成为冠军。在每次对局中,都可以进行任意的对局安排,对局安排只取决于自己
76 0
|
人工智能
codeforces1143B Nirvana(贪心)
codeforces1143B题解(贪心)
952 0
|
iOS开发
codeforces1141D Colored Boots(暴力+贪心)
codeforces1141D题解(暴力+贪心)
907 0

热门文章

最新文章