题意:PUT为存入,GET为取队头查询,不停地存入与取队头元素,输出相应的信息即可。
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1724
——>>之前见题目的通过率只有26.65%,又赶时间,早早地跳过了这题,刚刚看了下,用优先队列一试,原来是那么水的……无语
#include <iostream>
#include <queue>
using namespace std;
struct node //定义结点数据类型
{
string name;
int val;
int weight;
};
bool operator < (node e1, node e2) //定义优先队列的排序方式,权值小的在前面
{
return e1.weight > e2.weight;
}
int main()
{
string type; //输入的第一个单词,要么是"GET",要么是"PUT"
priority_queue<node> qu; //程序的核心,优先队列
while(cin>>type)
{
if(type == "GET") //当为读取数据的时候
{
if(qu.empty()) cout<<"EMPTY QUEUE!"<<endl; //队空时
else //队不空时
{
cout<<qu.top().name<<" "<<qu.top().val<<endl;
qu.pop();
}
}
else //当为存取数据的时候
{
node newnode;
cin>>newnode.name>>newnode.val>>newnode.weight; //入列
qu.push(newnode);
}
}
return 0;
}
本文介绍了一道ACM竞赛题目,通过使用优先队列(Priority Queue)进行高效的数据处理。该题目要求实现PUT操作(存入)和GET操作(取队头),并输出相应信息。文章提供了完整的C++代码示例,展示了如何定义优先队列的排序方式及核心逻辑。
498

被折叠的 条评论
为什么被折叠?



