Argus(完全二叉树--堆(head))

5人阅读 评论(0) 收藏 举报
分类:
Argus
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11822 Accepted: 5775

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


题意:若干任务,给出任务的id和执行间隔,要求按照执行的时间顺序来输出要求的前k个任务的id号,当两个任务同时执行时,先输出id小的。

题解:使用堆这种数据结构,堆可分为最大堆和最小堆。最小堆中,任一节点的值都

小于等于其任一子节点存储的值,根节点一定存储最小的值,堆可用来实现优先队

列。


#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

struct node
{
    int id,p,now;
}p[3001];
int k;
void down(node h[],int s,int m)
{
    node rc=h[s];
    for(int j=s*2;j<=m;j*=2)
    {
        if(j<m)
        {
            if(h[j].now>h[j+1].now)
                j++;
            else
            {
                if(h[j].now==h[j+1].now && h[j].id>h[j+1].id)
                    j++;
            }
        }
        if(rc.now<h[j].now || (rc.now==h[j].now && rc.id<h[j].id))
            break;
        h[s]=h[j];
        s=j;
    }
    h[s]=rc;
}

void build(node h[],int length)
{
    for(int i=length/2;i>0;i--)
        down(h,i,length);
}

int main()
{
    string str;
    cin>>str;
    int len,i=1;
    while(str!="#")
    {
        cin>>p[i].id>>p[i].p;
        p[i].now=p[i].p;
        i++;
        cin>>str;
    }
    len=i-1;
    cin>>k;
    build(p,len);
    for(int i=1;i<=k;i++)
    {
        cout<<p[1].id<<endl;
        p[1].now+=p[1].p;
        down(p,1,len);
    }
    return 0;
}

查看评论

完全二叉树,堆,堆排序,优先队列

/*堆的性质: 1 结构性:(二叉)堆是一个数组,他可以被看成一个近似的完全二叉树。 2.有序性:从任意一个点顺着路径到另一个点关键字权值总是非递增或者非递减的 兄弟之间并不存在特定的约束关系。 */...
  • wmaoshu
  • wmaoshu
  • 2015-11-25 21:19:34
  • 1004

堆优先用完全二叉树表示、堆的特性和最大最小堆

  • chy89224
  • chy89224
  • 2017-02-16 21:48:48
  • 170

数据结构 之 堆(完全二叉树、大根堆、小根堆)

堆是一种完全二叉树结构,这意味着它具有完全二叉树的性质,其中一点如下所示:设完全二叉树的一元素编号为i,1 n,则该元素无左孩子。否则,其左孩子的编号为2 * i; 3、如果1 + 2*i > n ...
  • u012925231
  • u012925231
  • 2017-11-29 22:55:35
  • 272

堆的实现-用数组表示的完全二叉树

堆时一种具有优先级的队列,取出元素的顺序按照元素的优先级 堆若用数组和链表,二者的插入时间复杂度为O(1),删除时间复杂度为O(N) 若用有序数组或有序链表,插入操作所需时间均为O(N),删除操作...
  • Gouhailiang
  • Gouhailiang
  • 2016-11-08 17:44:36
  • 443

完全二叉树的创建与堆排序代码实现

堆排序: 创建一棵完全二叉树。 利用完全二叉树进行堆排序。
  • zhangdoudou0626
  • zhangdoudou0626
  • 2015-12-15 21:05:09
  • 730

java 完全二叉树的构建与四种遍历方法

二叉树的遍历,先序,后序,中序,关键记录一下二叉树的层序遍历算法。
  • u010091003
  • u010091003
  • 2017-03-04 21:32:08
  • 244

树-------完全二叉树结点数

 完全二叉树结点数 给定一棵完全二叉树的头节点head,返回这棵树的节点个数。如果完全二叉树的节点数为N,请实现时间复杂度低于O(N)的解法。  /** public cl...
  • zlhzlh11
  • zlhzlh11
  • 2015-11-02 15:58:31
  • 281

完全二叉树和堆排序

堆排序算法复杂度虽然也是O(nlgn),但其常数因子较大,而且在实际测试中发现和快速排序、二路归并排序相比速度会慢不少,但堆有其独特的优势: (1) 堆排序仅需要O(1)的空间,这是其他O(nlgn...
  • wdq347
  • wdq347
  • 2013-04-09 09:48:23
  • 1271

c++自低向上构建堆源代码 原创

  • 2009年10月18日 17:44
  • 880KB
  • 下载
    个人资料
    持之以恒
    等级:
    访问量: 855
    积分: 619
    排名: 8万+
    文章存档
    最新评论