KPI

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1160    Accepted Submission(s): 488

Problem Description
你工作以后, KPI 就是你的全部了. 我开发了一个服务,取得了很大的知名度。数十亿的请求被推到一个大管道后同时服务从管头拉取请求。让我们来定义每个请求都有一个重要值。我的KPI是由当前管道内请求的重要值的中间值来计算。现在给你服务记录,有时我想知道当前管道内请求的重要值得中间值。
 
Input
有大约100组数据。

每组数据第一行有一个n(1≤n≤10000)

,代表服务记录数。

接下来有n行,每一行有3种形式
  "in x": 代表重要值为x(0≤x≤109)

的请求被推进管道。
  "out": 代表服务拉取了管道头部的请求。
  "query: 代表我想知道当前管道内请求重要值的中间值. 那就是说,如果当前管道内有m条请求, 我想知道,升序排序后第floor(m/2)+1th

条请求的重要值.

为了让题目简单,所有的x都不同,并且如果管道内没有值,就不会有"out"和"query"操作。

 
Output
对于每组数据,先输出一行

Case #i:
然后每一次"query",输出当前管道内重要值的中间值。

 
Sample Input
6
in 874
query
out
in 24622
in 12194
query
 
Sample Output
Case #1:
874
24622
 
Source
题意: 三种操作 ”in  x“ 将x 加入到队列
                      ”out“ 删除队头元素
                      ”query“ 队列中现有元素升序排列后 输出第 ( len/2+1) 大元素
题解:1.将所有元素离散化处理 这里的方法比较麻烦 具体看代码有多次映射
         2.根据操作  将离散化后的元素加入 树状数组 in~add(x,1),out~add(x,-1);
         3.树状数组求第k大 
         f[n] 树状数组
         f[1]=a1
         f[2]=a1+a2
         f[3]=a3
 
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
struct node
{
int x;
int y;
} s[];
int t;
int jishu=;
char a[][];
int b[];
int exm;
int f[];
int coun=;
map<int,int> mp;
map<int,int> mpp;
queue<int> st;
int n;
bool cmp(struct node aa,struct node bb )
{
return aa.x<bb.x;
}
void add(int x,int y,int n)
{
for (;x<=n;x+=x&(-x)) f[x]+=y;
}
int sum(int x)
{
int ret=;
for (;x;x-=x&(-x)) ret+=f[x];
return ret;
}
int find_k(int k) //求第k大 板子
{
int i,w=;
for (i=;i>=;i--)
if (w+(<<i)<=n&&f[w+(<<i)]<k)
{
k-=f[w+(<<i)];w+=<<i;
}
return w+;
}
int k_num(int k){
int w=,cnt=;
for(int i=;i>=;i--)
{ w+=(<<i);
if(w>n || k<=f[w])
w-=(<<i);
else
{
k-=f[w];
}
}
return w+;
}
int main()
{
while(scanf("%d",&t)!=EOF)
{ jishu++;
memset(s,,sizeof(s));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(f,,sizeof(f));
mp.clear();
mpp.clear();
while(st.size()>)
st.pop();
coun=;
for(int i=;i<=t;i++)
{
scanf("%s",&a[i]);
if(a[i][]=='i')
{
scanf("%d",&exm);
s[++coun].x=exm;//存插入的元素
s[coun].y=coun;//struct 存储对应位置
}
}
cout<<"Case #"<<jishu<<":"<<endl;
sort(s+,s++coun,cmp);//结构体排序 x为主键升序排列
b[s[].y]=;
int k=;
mp[s[].x]=;
mpp[]=s[].x;
for(int i=;i<=coun;i++)//for循环完成离散化处理
{
if(s[i].x==s[i-].x)
{
b[s[i].y]=k;
mp[s[i].x]=k;// 与元素的相互映射
mpp[k]=s[i].x;//
}
else
{
b[s[i].y]=++k;
mp[s[i].x]=k;
mpp[k]=s[i].x;
}
}
n=k;
k=;
for(int i=;i<=t;i++)
{
if(a[i][]=='i')
{
st.push(mpp[b[++k]]);
add(b[k],,n);
}
if(a[i][]=='o')
{
int now=st.front();
st.pop();
add(mp[now],-,n);
}
if(a[i][]=='q')
{
int len=st.size();
int location=k_num((len/)+);
printf("%d\n",mpp[location]); }
}
}
return ;
}
/*
10
in 3
in 1
out
in 4
query
in 2
out
in 100
in 1
query
*/
 
 

HDU 5249 离线树状数组求第k大+离散化的更多相关文章

  1. poj 2985 The k-th Largest Group 树状数组求第K大

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted ...

  2. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  3. 树状数组求第K大(From CLJ)

    ; <<log2[n];p;p>>=) if(a[ret+p]<=kth) kth-=a[ret+=p]; return ret;

  4. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  5. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  6. 区间的关系的计数 HDU 4638 离线+树状数组

    题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...

  7. HDU - 4777 离线树状数组

    离线树状数组搞一搞. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #de ...

  8. HDU 4417 离线+树状数组

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

随机推荐

  1. Visaul Studio 密钥

    vs professional 2015 简体中文版  :HMGNV-WCYXV-X7G9W-YCX63-B98R2

  2. ubuntu16.04图形界面安装中文输入法,中文展示

    打开system Settings 设置   打开设置语言   安装Language Support   点击installed languages 选择chinese 打勾,安装   安装IBus框 ...

  3. 从零开始的Python学习Episode 3——字符串格式化与for循环

    一.字符串格式化 利用一段注释记录想要输出的字符串格式,并用 %s . %d 或 %f 依次代替要输出的数据(%s代表字符串,%d代表数字,%f代表浮点数),然后在这段注释之后依次加上要输出的数据. ...

  4. 剑指offer-包含min函数的栈20

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). class Solution: def __init__(self): self.st ...

  5. Map Reduce Application(Top 10 IDs base on their value)

    Top 10 IDs base on their value First , we need to set the reduce to 1. For each map task, it is not ...

  6. Some good articles

    https://alligator.io/vuejs/introduction-render-functions/ https://alligator.io/vuejs/vue-jwt-pattern ...

  7. Linux error:No space left on device

    一台Oracle数据库服务器在关机重启后,Oracle监听无法启动,提示错误 Linux error:no space left on device 提示可知:问题是出在磁盘空间不足 但是初步查看分区 ...

  8. 物联网常见通信协议RFID、NFC、Bluetooth、ZigBee等梳理

    1  概述 在上一篇文章<物联网常见通信协议与通讯协议梳理[上]-通讯协议>中,对物联网常用通信协议和通讯协议作了区分,并对通讯协议进行了分享:本文将对常用的通信协议进行剖析,重点面向市场 ...

  9. jquery.validate 中文乱码解决方法

    第一种.就是所说的引用jquery.validate.messages_cn.js                    下载地址:http://files.cnblogs.com/files/pin ...

  10. 【Linux】CentOS安装redis

    CENTOS7下安装REDIS 安装完成之后使用:redis-cli命令连接,如图: 提示:/var/run/redis_6379.pid exists, process is already run ...