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;
}
思路:直接用优先队列模拟
题目链接: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;
}
相关文章推荐
- 优先队列(priority_queue)的C语言实现代码
- PHP 数据结构队列(SplQueue)和优先队列(SplPriorityQueue)简单使用实例
- 堆排序/优先级队列
- 反向拓扑排序 HDU 4857 逃生
- 优先队列<堆>
- 二叉堆
- vijosP1016北京2008的挂钟 BFS + 优先队列
- 优先队列的学习笔记
- 优先队列的学习笔记
- hdu 1596 find the safest road(spfa算法)
- Black Box
- 基于算法导论6.5用最大堆实现的优先队列(C++)
- Rescue
- Rescue
- 拯救公主
- 优先队列
- [C++ STL] 优先队列容器 priority_queue
- hdu2066 最短路 dijkstra
- hdu3790 dijkstra算法
- Fence Repair(简单贪心)