[51nod1786]数据流中的算法
来源:互联网 发布:python kgram算法 编辑:程序博客网 时间:2024/06/14 17:33
题目描述
数据流统计功能上线后,为51nod提升用户体验做出了很大的贡献。但是新问题随之而来,夹克老爷还想知道在一个窗口内,访问次数最多用户(即窗口内的众数)。如果有多个众数,取用户ID最小的一个。(窗口的意思是一个固定长度的区间!)
(因为数据流是实时的、在线的,所以不允许使用离线算法^_^)
做法
一个map+一个set
#include<cstdio>#include<algorithm>#include<set>#include<map>#define fo(i,a,b) for(i=a;i<=b;i++)using namespace std;struct dong{ int x,y; friend bool operator <(dong a,dong b){ return a.x>b.x||a.x==b.x&&a.y<b.y; }} zlt;map<int,int> f;multiset<dong> s;int a[5000000+10],sta[80];int i,j,k,l,t,n,m,tot,top;int read(){ int x=0,f=1; char ch=getchar(); while (ch<'0'||ch>'9'){ if (ch=='-') f=-1; ch=getchar(); } while (ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } return x*f;}void ins(int x){ if (f[x]){ zlt.x=f[x]; zlt.y=x; s.erase(s.find(zlt)); } f[x]++; zlt.x=f[x]; zlt.y=x; s.insert(zlt);}void del(int x){ zlt.x=f[x]; zlt.y=x; s.erase(s.find(zlt)); f[x]--; if (f[x]){ zlt.x=f[x]; zlt.y=x; s.insert(zlt); }}void write(int x){ if (!x){ putchar('0'); putchar('\n'); return; } top=0; while (x){ sta[++top]=x%10; x/=10; } while (top) putchar('0'+sta[top--]); putchar('\n');}int main(){ n=read();k=read(); tot=0; fo(i,1,n){ t=read(); if (t==1){ a[++tot]=read(); if (tot>k) del(a[tot-k]); ins(a[tot]); } else write((*s.begin()).y); }}
阅读全文
1 0
- [51nod1786]数据流中的算法
- 51Nod 数据流中的算法
- 51NOD1785 数据流中的算法 【水】
- 51Nod-1785-数据流中的算法
- 51nod-1785:数据流中的算法
- 51nod 1785 数据流中的算法
- 51nod 1786 数据流中的算法
- 【51nod 数据流中的算法】+ vector
- 51Nod数据流中的算法—众数
- 51nod 1785 数据流中的算法
- 1786 数据流中的算法
- 51Nod 1786 数据流中的算法-众数 题解
- 51nod 1785 数据流中的算法 (方差计算公式)
- 1785 数据流中的算法(模拟,乱搞)
- 51Nod-数据流算法-1785
- 数据流算法
- 51nod 1785 数据流中的算法 【D(x)与E(x)+队列+线段树】
- 51Nod-TalkingData数据科学精英夏令营挑战赛-D-数据流中的算法-众数
- 二叉树相关(抄袭不能
- Kafka整体结构图、Consumer与topic关系、Kafka消息分发、Consumer的负载均衡、Kafka文件存储机制、Kafka partition segment等(来自学习资料)
- interlij idea创建给Web项目添加Mybatis
- 技术性网址
- 拉取种子用户的4种方法
- [51nod1786]数据流中的算法
- getAttribute、setAttribute 、 removeAttribute
- leetcode--476--Number Complement(我的解法)
- 关于网络中的端口号
- [codeforces815D] Karen and Cards
- 第一人称《AI》
- 简易版WireShark实现-相关网络知识(1)
- activity和fragment的转场动画的实现
- Docker跨主机容器访问通信-Open vSwitch