priority_queue顾名思意是优先队列的意思,在STL内部的实现形式是堆。使用priority_queue的关键就是重载比较函数,去实现不同要求的堆。另外,值得注意的是,二叉堆实现的优先队列是不稳定的,这点要特别注意
在重载优先队列比较函数的时候注意是重载一个类,在类中重写一个()操作符。
class problem
{
public:
int a,b;
};//定义prblem类型
struct cmp
{
bool operator()(const problem x,const problem y)
{
return x.b<y.b;//极大堆
}
};//重载比较函数
priority_queue<problem,vector<problem>,cmp >q;//声明优先队列
如下:
zoj_2212
#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
using namespace std;
class reg
{
public:
int num;
int per;
int n;
reg(int num,int per,int n)
{
this->num=num;
this->per=per;
this->n=n;
}
};
struct cmp
{
bool operator()(const reg a,const reg b)
{
//return a>b;//最小堆
//return a<b;//最大堆
if(a.n==b.n)
return a.num>b.num;
return a.n>b.n;
}
};
int k;
string s;
int main()
{
freopen("e:\\zoj\\zoj_2212.txt","r",stdin);
priority_queue< reg, vector < reg > , cmp >q;
while(cin>>s&&s!="#")
{
int num,per;
cin>>num>>per;
//for(int i=1;i<=5;i++)
//q.push(reg(num,per,per*i));
q.push(reg(num,per,per));
}
cin>>k;
for(int i=0;i<k;i++)
{
reg g=q.top();
q.pop();
reg a(g.num,g.per,g.n+g.per);
q.push(a);
cout<<g.num<<endl;
}
return 0;
}
zoj_2724
#include<iostream>
#include<queue>
#include<cstdio>
#include<string>
#include<string.h>
using namespace std;
class message
{
public:
char name[20];
int parameter;
int priority;
};
struct cmp
{
bool operator()(const message a,const message b)
{
//return a>b;//最小堆
//return a<b;//最大堆
return a.priority>b.priority;
}
};
int main()
{
//string s;
char ch[3];
priority_queue< message, vector < message > , cmp >q;
freopen("e:\\zoj\\zoj_2724.txt","r",stdin);
while(scanf("%s",ch)!=EOF)
{
if(!strcmp(ch,"PUT"))
{
//string s1;
char name[20];
int para,p;
//cin>>s1>>para>>p;
message m;
scanf("%s %d %d",&m.name,&m.parameter,&m.priority);
q.push(m);
}
if(!strcmp(ch,"GET"))
{
if(!q.empty())
{
message m=q.top();
q.pop();
//cout<<m.name<<" "<<m.parameter<<endl;
printf("%s %d\n",m.name,m.parameter);
}
else
//cout<<"EMPTY QUEUE!"<<endl;
printf("EMPTY QUEUE!\n");
}
}
}
zoj_3230
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
class problem
{
public:
int a,b;
};
struct cmp
{
bool operator()(const problem x,const problem y)
{
return x.b<y.b;//升序
}
};
bool cmp2(const problem x,const problem y)
{
return x.a<y.a;//极大堆
}
int m,n,p,a,b;
problem plist[100005];
int main()
{
freopen("e:\\zoj\\zoj_3230.txt","r",stdin);
priority_queue<problem,vector<problem>,cmp >q;
while(cin>>n>>m>>p)
{
while(!q.empty())
q.pop();
for(int i=0;i<n;++i)
{
cin>>a>>b;
problem pr;
pr.a=a;
pr.b=b;
plist[i]=pr;
}
sort(plist,plist+n,cmp2);
for(int i=0;i<n;i++)
{
if(m==0)
break;
if(plist[i].a<=p)
q.push(plist[i]);
else
{
if(!q.empty())
{
problem pr=q.top();
p+=pr.b;
--m;
--i;
q.pop();
}
else
break;
}
}
while(m--)
{
if(!q.empty())
{
problem pr=q.top();
p+=pr.b;
q.pop();
}
else
break;
}
cout<<p<<endl;
}
}
分享到:
相关推荐
STL、线段树代码库 关于队列和栈的有关STL的操作vc++实现
python库。 资源全名:numpy_stl-2.4.1-cp27-cp27m-macosx_10_11_x86_64.whl
priority_queue用法,希望对大家会有所帮助
看STL文件的小软件,可以自由的实现旋转,等功能,现在只是一小部分,以后会发后面的
NX二次开发-UFUN导出STL函数UF_STD_put_solid_in_stl_file博客文章源代码
gdb 打印功能扩展 ...# std::priority_queue<T> -- via ppqueue command # std::bitset<n> -- via pbitset command # std::string -- via pstring command # std::widestring -- via pwstring command
numpy_stl-2.16.0-cp37-cp37m-win_amd64
numpy_stl-2.10.1-cp27-cp27m-win_amd64
numpy_stl-2.13.0-cp36-cp36m-win_amd64
用STL写的WEB服务器,服务端与客户端,测试例子
1. Access out-of-range element 2. Use std::out_of_range Exception for vector 3. Catch out_of_range exception
numpy_stl-2.10.1-cp35-cp35m-win_amd64
文档包含70个stl例子,适合初学者练习使用
vc显示stl,读取顶点坐标,并显示在独立窗口中
STL的源码详细介绍其内部的实现原理和机制,对一些想彻底了解STL的有一些帮助。。。
基本的三维模型的stl文件格式,可以用于三维模型的显示及操作
介绍标准模板库(STL)的重要参考资料。
boost-STL学习资料合集.chm,可以学习stl,相当好
Generic programming and STL