Alisha's Birthday!!! 字符信息的存储 + 优先队列 + 结构体

初学算法, 这是一道十分有意义的优先队列的题。思路基本没有问题,但是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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Victayria

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值