POJ 2051 Argus
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 8782 | Accepted: 3985 |
Description
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 second part is your task. This part contains only one line, which is one positive integer K (<= 10000).
Output
Sample Input
Register 2004 200
Register 2005 300
#
5
Sample Output
2004
2005
2004
2004
2005
题目大意:输入一串用户的命令,id,周期,没过一个周期输出一次id,输入以“#”结束,最后一行输入一个整数n,表示打印n个用户id。
解题方法:堆排序,用最小堆,每次将时间值最小的放到堆顶,然后输出,加上时间周期之后在进行堆调制,重复n次即可。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std; typedef struct
{
int time;
int period;
int id;
}Node; Node user[]; void sift(int low, int high)
{
int i = low;
int j = * i;
Node temp = user[i];
while(j <= high)
{
if (j < high && (user[j].time > user[j + ].time || (user[j].time == user[j + ].time && user[j].id > user[j + ].id)))
{
j++;
}
if (temp.time > user[j].time || (temp.time == user[j].time && temp.id > user[j].id))
{
user[i].id = user[j].id;
user[i].period = user[j].period;
user[i].time = user[j].time;
i = j;
j = i * ;
}
else
{
break;
}
}
user[i] = temp;
} int main()
{
char cmd[];
int id, period, n;
int nCount = ;
while(true)
{
cin>>cmd;
if (strcmp(cmd, "#") == )
{
break;
}
cin>>id>>period;
user[++nCount].id = id;
user[nCount].period = period;
user[nCount].time = period;
}
cin>>n;
while(n--)
{
for (int i = nCount / ; i >= ; i--)
{
sift(i, nCount);
}
user[].time += user[].period;
cout<<user[].id<<endl;
}
return ;
}
POJ 2051 Argus的更多相关文章
- poj 2051.Argus 解题报告
题目链接:http://poj.org/problem?id=2051 题目意思:题目有点难理解,所以结合这幅图来说吧---- 有一个叫Argus的系统,该系统支持一个 Register 命令,输入就 ...
- poj 2051 Argus(优先队列)
题目链接: http://poj.org/problem?id=2051 思路分析: 优先级问题,使用优先队列求解:当执行某个任务后,再增加一个任务到队列中, 该任务的优先级为执行任务的时间加上其时间 ...
- POJ 2051 argus(简单题,堆排序or优先队列)
又是一道数据结构题,使用堆来进行权值调整和排序,每次调整都是o(n)的复杂度,非常高效. 第一眼看题觉得可以用优先队列来做,应该也很简单. 事实上多数优先队列都是通过堆来实现的. 写的时候还是出了一些 ...
- 算法手记 之 数据结构(堆)(POJ 2051)
一篇读书笔记 书籍简评:<ACM/ICPC 算法训练教程>这本书是余立功主编的,代码来自南京理工大学ACM集训队代码库,所以小编看过之后发现确实很实用,适合集训的时候刷题啊~~,当时是听了 ...
- POJ 2051
http://poj.org/problem?id=2051 这个题目的大题意思就是给你一些ID,和ID所对应的周期,每隔它所对应的周期,它的任务就会执行,就会输出所对应的ID Register 20 ...
- 最小堆的维护,POJ(2051)
题目链接:http://poj.org/problem?id=2051 ///维持最小堆(优先队列)POJ2051 #include <iostream> #include <str ...
- 优先队列(priority_queue)的cmp,POJ(2051)
sort()函数的cmp为函数,priority_queue的cmp为类,具体写法是: struct Node { int i,j; } node[]; struct cmp { bool opera ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
随机推荐
- AutoHotKey设置ide的光标功能键
CapsLock:: SetCapsLockState,off ;锁定为小写 CapsLock & d:: flag_C=1 ;小写d代替control键,实现跨词移动CapsLock &am ...
- Winform C# 编程 1
http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL01Wmfc0B-QfB2pS1y13peTbq
- ssh整合思想初步 struts2与Spring的整合 struts2-spring-plugin-2.3.4.1.jar下载地址 自动加载Spring中的XML配置文件 Struts2下载地址
首先需要JAR包 Spring整合Structs2的JAR包 struts2-spring-plugin-2.3.4.1.jar 下载地址 链接: https://pan.baidu.com/s/1o ...
- 基于matlab的蓝色车牌定位与识别---定位
接着昨天的工作继续.定位的过程有些是基于车牌的颜色进行定位的,自己则根据数字图像一些形态学的方法进行定位的. 合着代码进行相关讲解. 1.相对彩色图像进行灰度化,然后对图像进行开运算.再用小波变换获取 ...
- 【动态规划】bzoj1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
思路自然的巧妙dp Description 有N头奶牛,每头那牛都有一个标号Pi,1 <= Pi <= M <= N <= 40000.现在Farmer John要把这些奶牛分 ...
- MySQL - INSERT 集合
范例1: INSERT INTO t_table SELECT ot.* FROM t_other_table ot WHERE ot.is_sent = ? and ot.insert_time & ...
- 【Python学习之五】高级特性2(切片、迭代、列表生成器、生成器、迭代器)
2.迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration).在Python中,迭代是通过for ... in来完成的. ...
- 《Java并发编程实战》读书笔记一 -- 简介
<Java并发编程实战>读书笔记一 -- 简介 并发的历史 并发的历史,也是人类利用有限的资源去提高生产效率的一个的例子. 设想现在有台计算机,这台计算机具有以下的资源: 单核CPU一个 ...
- JAVA基础篇—接口实现动态创建对象
Scanner在控制台输入内容 package com.Fruit; public interface Fruit {//提供接口 } package com.Fruit; public class ...
- console_init()分析
启动阶段初始化控制台流程分析, start_kernel console_init(); -->tty_ldisc_begin(); /* Setup the default TTY line ...