优先队列——HDU1873
2015-06-19 11:46
295 查看
优先队列
(1) priority_queue<int>q;
//按照元素从大到小的顺序出队
(2) 自定义优先级
定义方法:
priority_queue<int,vector<int>,cmp>q;
//第二个参数为容器类型,第三个为比较函数
struct cmp
{
bool operator()(stu a,sut b)
{
return a.yxj<b.yxj; //通过定义operator< 操作符来比较元素中的优先级
}
};
例题:HDU1873
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
char s[10];
int t,a,b;
struct stu
{
int num,yxj;
};
struct cmp
{
bool operator()(stu a,stu b)
{
if(a.yxj!=b.yxj)
return a.yxj<b.yxj; //yxj大的先出队列
return a.num>b.num;
}
};
priority_queue<stu,vector<stu>,cmp>q[4];
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=0;i<4;i++)
{
while(!q[i].empty())
q[i].pop();
}
stu cur;
int k=1;
while(t--)
{
scanf("%s",s);
if(strcmp(s,"IN")==0)
{
scanf("%d%d",&a,&b);
cur.yxj=b;
cur.num=k;
q[a].push(cur);
k++;
}
else if(strcmp(s,"OUT")==0)
{
scanf("%d",&a);
if(q[a].empty())
printf("EMPTY\n");
else
{
cur=q[a].top();
q[a].pop();
printf("%d\n",cur.num);
}
}
}
}
return 0;
}
$(".MathJax").remove();
(1) priority_queue<int>q;
//按照元素从大到小的顺序出队
(2) 自定义优先级
定义方法:
priority_queue<int,vector<int>,cmp>q;
//第二个参数为容器类型,第三个为比较函数
struct cmp
{
bool operator()(stu a,sut b)
{
return a.yxj<b.yxj; //通过定义operator< 操作符来比较元素中的优先级
}
};
例题:HDU1873
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
char s[10];
int t,a,b;
struct stu
{
int num,yxj;
};
struct cmp
{
bool operator()(stu a,stu b)
{
if(a.yxj!=b.yxj)
return a.yxj<b.yxj; //yxj大的先出队列
return a.num>b.num;
}
};
priority_queue<stu,vector<stu>,cmp>q[4];
int main()
{
while(scanf("%d",&t)!=EOF)
{
for(int i=0;i<4;i++)
{
while(!q[i].empty())
q[i].pop();
}
stu cur;
int k=1;
while(t--)
{
scanf("%s",s);
if(strcmp(s,"IN")==0)
{
scanf("%d%d",&a,&b);
cur.yxj=b;
cur.num=k;
q[a].push(cur);
k++;
}
else if(strcmp(s,"OUT")==0)
{
scanf("%d",&a);
if(q[a].empty())
printf("EMPTY\n");
else
{
cur=q[a].top();
q[a].pop();
printf("%d\n",cur.num);
}
}
}
}
return 0;
}
$(".MathJax").remove();
相关文章推荐
- tcpcopy-1.0.0实战之拷贝生产服务器流量做压力测试
- mac上安装window双系统
- 日语学习之沪江N3基础 20150619 -1
- iOS开发——加载、滑动翻阅大量图片解决方案详解
- Make AngularJS $http service behave like jQuery.ajax()(转)
- 分布式缓存管理与查询系统
- spi lcd知识简要
- 把assert目录中的文件copy到sd卡中
- Linux 基础之查看CPU信息
- webdriver+python 对三大浏览器的支持
- (转)Linux系统调用--getrlimit/setrlimit函数
- POJ 1769
- MySQL优化之——查看字段长度
- SQL(2)
- 在Visual Studio和MyEclipse中建立UML图
- Android 程序框架设计
- linux下core文件调试方法(转载)
- 水题 Codeforces Round #308 (Div. 2) A. Vanya and Table
- struts2的拦截器AbstractInterceptor
- mybatis语法总结