POJ 2051 优先队列/小顶堆O(klog(n))轻松实现

话不多说,先上题:

Argus
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9449 Accepted: 4391

Description

A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logs and telephone call records. Likewise, queries over streams run continuously over a period of time and incrementally return new results as new data arrives. For example, a temperature detection system of a factory warehouse may run queries like the following. 
Query-1: "Every five minutes, retrieve the maximum temperature over the past five minutes." 
Query-2: "Return the average temperature measured on each floor over the past 10 minutes."

We have developed a Data Stream Management System called Argus, which processes the queries over the data streams. Users can register queries to the Argus. Argus will keep the queries running over the changing data and return the results to the corresponding user with the desired frequency. 

For the Argus, we use the following instruction to register a query: 
Register Q_num Period

Q_num (0 < Q_num <= 3000) is query ID-number, and Period (0 < Period <= 3000) is the interval between two consecutive returns of the result. After Period seconds of register, the result will be returned for the first time, and after that, the result will be returned every Period seconds. 

Here we have several different queries registered in Argus at once. It is confirmed that all the queries have different Q_num. Your task is to tell the first K queries to return the results. If two or more queries are to return the results at the same time, they will return the results one by one in the ascending order of Q_num. 

Input

The first part of the input are the register instructions to Argus, one instruction per line. You can assume the number of the instructions will not exceed 1000, and all these instructions are executed at the same time. This part is ended with a line of "#". 

The second part is your task. This part contains only one line, which is one positive integer K (<= 10000). 

Output

You should output the Q_num of the first K queries to return the results, one number per line.

Sample Input

Register 2004 200
Register 2005 300
#
5

Sample Output

2004
2005
2004
2004
2005

Source


题目大意如下:
有n个ID,n <= 3000,0 < ID <= 3000,每个ID要被轮流打印出来(不止一次),打印的顺序则根据period,也就是每经过一个period,这个ID就会被加入打印队列。如果有同时打印的情况,则ID小的先被打出来,再打印ID较大的。
为了理解题意,以样例为准,首先,我们要设定一个零时刻,可见2004,2005分别要在200,300秒后被打印出来,那么先打出200。然后更新时间: 2004,2005分别要在400,300秒后被打印出来,打印出2005。再更新:2004,2005分别要在400,600秒后被打印出来,打印出2004……以此类推,打印出了2004,2005,2004,2004,2005.
不难想到,可以把所有账号的ID、period、time存到一个节点里,n个账号则有n个节点,我们以time为第一要素、ID为第二要素构建小顶堆heap[n],每次都取顶,输出它的ID。再更新time,让它再加上period,再调整堆,于是又得到了新的顶。这样就能逐步打印出需要的ID。
有人说筛选time条件用快排也很方便。但是快排一次需要O(nlogn)的时间,输出K次一共要O(Knlogn)的时间。相比之下,小顶堆每次向下调整用O(logn),一共(Klogn),显然要快出不少。

#include <stdio.h>
typedef struct node
{
	int ID;
	int period;
	long long time;
}Node;//构造节点
Node heap[1005];
static int hlength;
int down(int p)
{
	int q = 2 * p;
	Node a = heap[p];          <span style="font-family: Arial, Helvetica, sans-serif;">//小顶堆的调整条件,要么time小,要么一样的time但是ID小</span>
	while(q <= hlength)
	{
		if(q < hlength)
			if(heap[q].time > heap[q+1].time || (heap[q].time == heap[q+1].time && heap[q].ID > heap[q+1].ID))
				q++;
		if(a.time < heap[q].time || (a.time == heap[q].time && a.ID <= heap[q].ID))
			break;
		else
		{
			heap[p] = heap[q];
			p = q;
			q = 2 * p;
		}
	}
	heap[p] = a;
	return 0;
}
int build()
{
	for(int i = hlength/2;i > 0;i--)
		down(i);
	return 0;
}
int main()
{
	int i = 1, k;
	Node data;
	char command[10];
	while(scanf("%s", command) && command[0] != '#')
	{
		scanf("%d%d", &heap[i].ID, &heap[i].period);  //i从1开始,heap[0]就不用啦
		heap[i].time = heap[i].period;
		i++;
	}
	scanf("%d",&k);
	hlength = --i;
	build();
	while(k--)
	{
		printf("%d\n", heap[1].ID);
		heap[1].time += heap[1].period;
		down(1);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值