初学算法, 这是一道十分有意义的优先队列的题。思路基本没有问题,但是WA了很多遍,最终经过大佬指点才知道是用字符串数组存字符串的问题,因为这个字符串(每个人的名字)可能会很大,(表示真的很无语,谁的名字会那么长啊啊啊啊啊),最后改成存每个人的id就过了_.
下面附上问题链接和代码~~~
http://acm.hdu.edu.cn/showproblem.php?pid=5437
小白同学的代码片 ~~~
#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <queue>
#include <vector>
#include <algorithm>
#define N 150010
using namespace std;
struct F //创建结构体,把每个人的先后次序看成他们的id
{
int id, value;
char name[210];
friend bool operator < (F ff, F f)// 定义小于符号,才可以比较大小,才可以使用优先队列
{
if(ff.value == f.value)
return ff.id > f.id;
else
return ff.value < f.value;
}
}friends[N];
struct OPENRULE{
int t, p;
}open[N]; //把每个开门的时候和放进的人数看成是一个结构体的两个成员
bool cmp(OPENRULE a, OPENRULE b)
{
return a.t < b.t;
}
int T, k, m, q, t, p, n;
int order[N];// 最开始用的string order[N] 然后就像成了segmentation fault............
int main()
{
cin >> T;
while(T--)
{
priority_queue<F> que;
memset(order, 0, sizeof order); //initialize the order array
scanf("%d%d%d", &k, &m, &q);
for (int i = 1; i <= k; ++i)
{
scanf("%s%d", &friends[i].name, &friends[i].value);
friends[i].id = i;
}
//read in the open rules
for (int i = 0; i < m; i++)
{
scanf("%d%d", &open[i].t, &open[i].p);
}
sort(open, open+m, cmp); //注意题目没有说输入的开门规则是排好序了的
//we are going to create the order list:
int r = 1;
int pushcnt = 1; // 记录push的id的最大值
for (int i = 0; i < m; ++i)
{
while(pushcnt <= open[i].t) //一直push人到等待区直到达到要求的id
//push the required number of people into the que
{
que.push(friends[pushcnt]);
pushcnt++;
}
//based on the number limit, we can make a partial list order
//现在从等待区挑选人进门
while(open[i].p && !que.empty())//j is a counter
{
//if the que is not empty, keep on creating list
order[r++] = que.top().id;
que.pop();
open[i].p--;
}
}
// then push all friends outside into the queue
while(pushcnt <= k)
{
que.push(friends[pushcnt]);
pushcnt++;
}
//pop all friends from the queue into the order list
while (!que.empty())
{
order[r++] = que.top().id; // 记录id而不是name!!!
que.pop();
}
//now everything is ready, read in querise
while (q--)
{
scanf("%d", &n);
printf("%s", friends[order[n]].name);
if(q)
printf(" ");
}
puts("");
}
return 0;
}