Argus--[优先队列]
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 解题思路:
假设绝对时间T,容易发现,对每一个query,每当T增加period时就会进行输出。我们不妨这样想,每次输出的query都是离下一次输出需要时间最少的。那么我们就可以采用优先队列存储query,记下一次输出时间为cnt,cnt越小的query优先级越高,每次取队列头元素,输出之后执行cnt+=period,再存入队列。
注意:
优先队列默认元素越大优先级越高,因此定义静态成员函数‘<’时按大于号来定义。 代码如下:
#include <iostream>
#include <time.h>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm> #define _clock (double(clock())/CLOCKS_PER_SEC) using namespace std; char buff[]; struct query{
int Q_num;
int Period;
int cnt;
query(int q=,int p=):Q_num(q),Period(p){cnt=Period;}
bool operator <(const query& a)const {
return (cnt>a.cnt)||((cnt==a.cnt)&&Q_num>a.Q_num);
}
}; priority_queue<query> u;
int q,p,k; int main(){
while(scanf("%s",buff)==&&buff[]!='#'){
scanf("%d%d",&q,&p);
u.push(query(q,p));
}
scanf("%d",&k);
while(k--){
query a=u.top();u.pop();
cout<<a.Q_num<<endl;
a.cnt+=a.Period;
u.push(a);
}
//cout<<"time : "<<_clock<<endl;
return ;
}
Argus--[优先队列]的更多相关文章
- [caffe]linux下安装caffe(无cuda)以及python接口
昨天在mac上折腾了一天都没有安装成功,晚上在mac上装了一个ParallelDesktop虚拟机,然后装了linux,十分钟就安装好了,我也是醉了=.= 主要过程稍微记录一下: 1.安装BLAS s ...
- [Swift]基础
[Swift]基础 一, 常用变量 var str = "Hello, playground" //变量 let str1="Hello xmj112288" ...
- [Ruby on Rails系列]4、专题:Rails应用的国际化[i18n]
1. 什么是internationalization(i18n)? 国际化,英文简称i18n,按照维基百科的定义:国际化是指在设计软件,将软件与特定语言及地区脱钩的过程.当软件被移植到不同的语言及地区 ...
- [译]一个灵活的 Trello 敏捷工作流
[译]一个灵活的 Trello 敏捷工作流 翻译自 An Agile Trello Workflow That Keeps Tasks Flexible Getting things done 可不只 ...
- iOS10收集IDFA,植入第三方广告[终结]--ADMob
[PS: 前段时间,公司做ASO推广,需要在应用中收集IDFA值,跟广告平台做交互!于是有了这个需求--] 1.首先,考虑了一下情况(自己懒 -_-#),就直接在首页上写了一个Banner,循环加载广 ...
- Java基础 之软引用、弱引用、虚引用 ·[转载]
Java基础 之软引用.弱引用.虚引用 ·[转载] 2011-11-24 14:43:41 Java基础 之软引用.弱引用.虚引用 浏览(509)|评论(1) 交流分类:Java|笔记分类: Ja ...
- CSU 1642 Problem B[难][前缀和]
Description 已知两个正整数a和b,求在a与b之间(包含a和b)的所有整数的十进制表示中1出现的次数. Input 多组数据(不超过100000组),每组数据2个整数a,b.(1≤a,b≤1 ...
- [ufldl]Supervised Neural Networks
要实现的部分为:forward prop, softmax函数的cost function,每一层的gradient,以及penalty cost和gradient. forwad prop forw ...
- [干货]2017已来,最全面试总结——这些Android面试题你一定需要
地址.http://blog.csdn.net/xhmj12/article/details/54730883 相关阅读: 吊炸天!74款APP完整源码! [干货精品,值得收藏]超全的一线互联 ...
随机推荐
- windbg双机调试
win10 测试,当出现下列情况 ,请使用管理员身份运行 设置添加系统环境变量_NT_SYMBOL_PATH 的值为:srv*c:\symbols*http://msdl.microsoft.com ...
- SPSS统计分析过程包括描述性统计、均值比较、一般线性模型、相关分析、回归分析、对数线性模型、聚类分析、数据简化、生存分析、时间序列分析、多重响应等几大类
https://www.zhihu.com/topic/19582125/top-answershttps://wenku.baidu.com/search?word=spss&ie=utf- ...
- jquery find 推荐
https://codeplayer.vip/p/j7soa 这篇写的还是不错的,备用. // 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id ...
- vue @click.native
1,给vue组件绑定事件时候,必须加上native ,不然不会生效(监听根元素的原生事件,使用 .native 修饰符) 2,等同于在自组件中: 子组件内部处理click事件然后向外发送click事件 ...
- 网络流24题 骑士共存(DCOJ8023)
题目描述 在一个 n*n 个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入. 对于给定的 n*n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以 ...
- 2019-10-21-WPF-多个-StylusPlugIn-的事件触发顺序
title author date CreateTime categories WPF 多个 StylusPlugIn 的事件触发顺序 lindexi 2019-10-21 08:33:15 +080 ...
- BZOJ 4551树题解
好吧,洛谷的数据比较水暴力就可以过....(而且跑到飞快) 不过(BZ水不过去)还是讲讲正规的做法. 其实一眼可以看出可以树剖,但是,码起来有点麻烦. 其实有一种更简单的离线做法. 我们很容易联想到并 ...
- HZOJ Silhouette
转化一下题意:给出矩阵每行每列的最大值,求满足条件的矩阵个数. 先将A,B按从大到小排序,显然没有什么影响.如果A的最大值不等于B的最大值那么无解否则一定有解. 考虑从大到小枚举A,B中出现的数s,那 ...
- spring data jpa 原生sql 别名字段无法注入
开发四年只会写业务代码,分布式高并发都不会还做程序员?->>> 在使用entityManager.createNativeQuery(sql,User.class)这个方法时, ...
- 使用jQuery的 autocomplete 实现输入框 自动提示补全
参考网址: https://www.cnblogs.com/jinzhiming/p/6768402.html 插件下载地址: 链接:https://pan.baidu.com/s/1SpP3hixZ ...