您的位置:首页 > 其它

Codeforces Round #346 (Div. 2) B - Qualifying Contest 优先队列

2016-04-29 17:15 183 查看
题意:n个人,m个地区,每个地区要选出2个最高分的人,如果第三高分和第二高分一样分数,那么输出“?”

思路:直接用优先队列模拟

题目链接:http://codeforces.com/contest/659/problem/B

#include <bits/stdc++.h>

using namespace std;

struct node
{
string name;
int num;
bool operator < (const node &rhs) const
{
return rhs.num > num;//按num排序
}
};

priority_queue<node> q[10005];//开一个优先队列数组
int n, m, k, x;
string name;
node e, f, g;

int main()
{
cin>>n>>m;
for(int i=1; i<=m; i++)
while(!q[i].empty()) q[i].pop();
for(int i=0; i<n; i++)
{
cin>>name>>k>>x;
f.name = name, f.num = x;
q[k].push(f);
}
int cnt = 0;
for(int i=1; i<=m; i++)
{
cnt = 0;
while(!q[i].empty())
{
if(cnt == 0)
{
cnt++;
e = q[i].top();
q[i].pop();
}
else if(cnt == 1)
{
cnt++;
f = q[i].top();
q[i].pop();
}
else if(cnt == 2)
{
cnt++;
g = q[i].top();
q[i].pop();
}
else if(cnt == 3)
break;
}
if(cnt == 3)
{
if(f.num == g.num)
puts("?");
else
printf("%s %s\n", e.name.c_str(), f.name.c_str());//string的printf输出
}
else
{
printf("%s %s\n",e.name.c_str(), f.name.c_str());
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息