POJ2051总结

Argus

题目来源

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

我的代码

很羞耻的是我在算完复杂度之后发现可以暴力就直接暴力了。

#include<stdio.h>
#include<algorithm>
using namespace std;

struct regi
{
	int Q_num;
	int period;
}ins[1001];
bool cmp(regi a,regi b)
{
	return a.Q_num<b.Q_num;
}
int main()
{
	char temp[10];
	int t=0,k,now=0;
	int ans[100000];
	scanf(" %s",temp);
	while (temp[0]=='R')
	{
		scanf("%d%d",&ins[t].Q_num,&ins[t].period);
		t++;
		scanf(" %s",temp);
	}
	scanf("%d",&k);
	sort(ins,ins+t,cmp);
	for (int i=1;i<3000001;i++)
	{
		if (now==k)
		{
			break;
		}
		for (int j=0;j<t;j++)
		{
			if(i%ins[j].period==0)
			{
				ans[now++]=ins[j].Q_num;
			}
		}
	}
	for (int i=0;i<k;i++)
	{
		printf("%d\n",ans[i]);
	}
	return 0;
}

实际上用优先队列做才合适一些。

#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;

struct instruction
{
	int Q_num;
	int period;
	int time;
	bool operator<(const instruction &a)const
	{
		//优先队列是降序 
		return time>a.time||(time==a.time&&Q_num>a.Q_num);
	}
	instruction(int Q_num,int period,int time):Q_num(Q_num),period(period),time(time){}
};
 
int main()
{
	priority_queue<instruction> q;
	char temp[20];
	int k,num,period;
	while(scanf(" %s",temp)&&temp[0]!='#')
	{
		scanf("%d%d",&num,&period);
		q.push(instruction(num,period,period));
	}
	scanf("%d",&k);
    while (k--)
	{
		instruction a=q.top();
		q.pop();
		printf("%d\n",a.Q_num);
		q.push(instruction(a.Q_num,a.period,a.period+a.time));
	} 
}

反思

优先队列默认是降序排序,要在结构体内重载运算符。

发表评论