结构体的优先队列 【模板】

xiaoxiao2021-02-28  104

一批幸福的列车即将从杭州驶向幸福的终点站——温州,身为总列车长的linle有一些奇怪的癖好。

他会记录下全部乘客的名字(name)和他们的人品值(RP),根据这些将他们排序,并不时地从某辆列车里踢出人品最不好(RP值最低)的一个人,当两个人人品一样不好时,他就会踢出名字难听的人(linle认为按字典顺序,排在越在后面的人名字越难听)。

当然出于列车行驶需要,他还会不时的发布一些命令,比如让某个乘客上车,合并某两辆列车等。

linle的上一任秘书*因为不能高效地执行他的这些命令而被炒鱿鱼,他现在正在寻觅新的秘书人选,你能不能胜任呢?(谢绝男士,待遇丰厚~~~) Input 本题包含多组测试,请处理到文件结束。 对于每一组测试,第一行包含两个整数 N ,M ,表示一共有N( N<=10000 ) 辆列车,执行M( M<=10000 )次操作。 接下来有 N (从1开始记数)辆列车的信息,每辆列车先有一个数字 Xi(1 <= Xi <= 100 ),表示该列车有Xi个乘客,接下来Xi行乘客信息,每个乘客包含名字(20个字符以内,不包含空白符)和人品(0<= RP <=30000)。 再接下来有 M 行操作信息,一共有3种操作,分别为

GETON Xi name RP 表示有一个叫name的人品为RP的人登上第Xi列车

JOIN Xi Xj 表示有将第Xj辆列车合并到Xi辆列车

GETOUT Xi 表示从第Xi辆列车踢出一个人品最差的人

测试数据保证每个操作均合法,即不会将已经被合并到其他列车的列车再进行合并,也不会从一辆空列车里踢出乘客 Output 对于每个 GETOUT 命令,输出被踢出的那个人的名字 Sample Input 3 5 2 xhd 0 zl 1 2 8600 1 ll 2 1 Ignatius 3 GETOUT 1 JOIN 1 2 GETOUT 1 GETON 3 hoho 2 GETOUT 3 Sample Output xhd zl hoho

Hint Huge input, scanf is recommended.

就是一个 模拟的过程,不过要用到结构体的优先队列

模板 代码

#include <iostream> #include <cstdio> #include <vector> #include <queue> using namespace std; struct Node{ int x , y; Node(int a = 0 , int b = 0){ x = a , y = b; } }; bool operator<(Node a , Node b){ if(a.x == b.x) return a.y>b.y; return a.x>b.x; } int main(){ priority_queue<Node> q; q.push(Node(0 , 1)); q.push(Node(1 , 1)); q.push(Node(1 , 2)); while(!q.empty()){ Node t = q.top(); q.pop(); cout << t.x<<" " << t.y << endl; } return 0; }

AC 代码

#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int MAXN = 1e4+100; struct Man{ char name[25]; int rp; }; // 自定义 结构体的比较,要注意这个比较的方式,和我们预期的方式相反 bool operator<(Man a , Man b){ if(a.rp != b.rp ) return a.rp>b.rp; return strcmp(a.name,b.name)<0; } int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ priority_queue<Man>que[MAXN]; int num; for(int i=1;i<=n;i++){ scanf("%d",&num); while(num--){ Man temp ; scanf("%s %d",temp.name,&temp.rp); que[i].push(temp); } } while(m--){ char op[10]; scanf("%s",op); if(strcmp(op,"GETON")==0){ int ge;Man one; scanf("%d %s %d ",&ge,one.name,&one.rp); que[ge].push(one); }else if(strcmp(op,"JOIN")==0){ int a,b; scanf("%d%d",&a,&b); while(!que[b].empty()){ Man two=que[b].top();que[b].pop(); que[a].push(two); } }else { int a;scanf("%d",&a); Man tree;tree=que[a].top(); printf("%s\n",tree.name); que[a].pop(); } } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-19682.html

最新回复(0)