Argus
Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u
Submit
Status
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
Beijing 2004
题意大概是说设置多个每隔固定时间就执行的指令,让你按时间和指令大小排序输出前K条指令
通过最小堆 读入的时候就进行insert 全部读完之后每delete 一个最小点 再将其时间加上周期insert进去
AC代码如下
#include<stdio.h>
struct argus
{
int id;
int time;
int period;
}heap[2001];
int total;
void swap(int a,int b)
{
struct argus x;
x = heap[a];
heap[a] = heap[b];
heap[b] = x;
}
void heapup(int pos)
{
int q;
q = pos/2;
while(q>=1)
{
if(heap[pos].time<heap[q].time)
swap(pos,q);
else if(heap[pos].time==heap[q].time)
if(heap[pos].id<heap[q].id)
swap(pos,q);
else break;
pos = q;
q = q/2;
}
}
void initialize()
{
char a[10],i=1;
int x,y;
scanf("%s",a);
while(a[0]!='#')
{
scanf("%d%d",&heap[i].id,&heap[i].period);
heap[i].time = heap[i].period;
heapup(i);
scanf("%s",a);
total++;
i++;
}
}
int heapdown()
{
int p=1;
while(2*p<=total)
{
int left,right;
left = 2*p;
right = 2*p+1;
if(right>total)
{
swap(p,left);
p = left;
}
else if(heap[left].time<heap[right].time)
{
swap(p,left);
p = left;
}
else if(heap[left].time==heap[right].time&&heap[left].id<heap[right].id)
{
swap(p,left);
p = left;
}
else
{
swap(p,right);
p = right;
}
}
heap[p].time += heap[p].period;
return p;
}
int main()
{
int j,k;
int n;
initialize();
scanf("%d",&k);
for(j=0;j<k;j++)
{
printf("%d\n",heap[1].id);
n = heapdown();
heapup(n);
}
}
初学,第一次接触最小堆
感触是insert和出堆的delete两个函数功能一定要简明。之前一次失败的尝试当中我为了方便把print和读入加入了insert和delete里后来导致了很多麻烦 这也应该是程序设计的标准吧
以及导致我time exceed 的一个很简单的bug出现在heapdown的循环当中
int heapdown()
{
int p=1;
while(2*p<=total)
{
int left,right;
left = 2*p;
right = 2*p+1;
if(right>total)
{
swap(p,left);
p = left;
}
else if(heap[left].time<heap[right].time)
{
swap(p,left);
p = left;
}
else if(heap[left].time==heap[right].time)
if(heap[left].id<heap[right].id)
{
swap(p,left);
p = left;
}
else
{
swap(p,right);
p = right;
}
}
heap[p].time += heap[p].period;
return p;
}
可以看出我的本意是如果不满足前面if中的条件则进入else
但根据else的优先结合性出现了问题,导致了死循环
timelimit exceed 的原因除了算法问题外还可能是在某种输入情况下程序内部循环出问题,在设置循环时出环的条件尽量独立性较强