acdreamoj1108(The kth number)
题目链接:http://acdream.info/problem?
pid=1108
题意:n个数的数列,m次查询某个区间出现次数第k多的数出现的次数。n,m<=100000
解法:这个由于是离线的所以能够先统一处理,然后再输出。能够维护一个left和right指针。pre,pre[i]表示此时区间内出现次数大于等于i的数的种类。
为了降低复杂度,关键是left和right的移动方式,即查询区间怎样排序,假设紧靠区间左端点排序,那么右端点每次一定最大回是n。假设依照右端点排序,左端点每次一定最大是n。这里有个非常好的处理办法,就是模糊排序。先左端点非严格排序。即除以sqrt(n)再排序,这样复杂度最大是n*sqrt(n)
代码:
- /******************************************************
- * @author:xiefubao
- *******************************************************/
- #pragma comment(linker, "/STACK:102400000,102400000")
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <cstdio>
- #include <queue>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <map>
- #include <set>
- #include <stack>
- #include <string.h>
- //freopen ("in.txt" , "r" , stdin);
- using namespace std;
- #define eps 1e-8
- #define zero(_) (abs(_)<=eps)
- const double pi=acos(-1.0);
- typedef long long LL;
- const int Max=100010;
- const int INF=1e9+7;
- int num[Max];
- int help[Max];
- int r[Max];
- int l[Max];
- int k[Max];
- int pre[Max];
- int cnt[Max];
- int tool;
- bool cmp(int i,int j)
- {
- if(l[i]/tool==l[j]/tool&&r[i]!=r[j])
- return r[i]<r[j];
- return l[i]<l[j];
- }
- int ans[Max];
- int n,m;
- int findans(int t)
- {
- int l=1,r=n;
- while(l<=r)
- {
- int middle=(l+r)/2;
- if(pre[middle]>=t)
- l=middle+1;
- else
- r=middle-1;
- }
- return l-1;
- }
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- scanf("%d%d",&n,&m);
- tool=sqrt(n);
- for(int i=0; i<n; i++)
- scanf("%d",num+i),help[i]=i;
- for(int i=0; i<m; i++)
- scanf("%d%d%d",l+i,r+i,k+i),l[i]--,r[i]--;
- sort(help,help+m,cmp);
- memset(cnt,0,sizeof cnt);
- memset(pre,0,sizeof pre);
- int left=0,right=-1;
- for(int i=0;i<m;i++)
- {
- int L=l[help[i]],R=r[help[i]];
- while(left<L){ pre[cnt[num[left++]]--]--;}
- while(L<left){ pre[++cnt[num[--left]]]++;}
- while(right<R){ pre[++cnt[num[++right]]]++;}
- while(R<right){ pre[cnt[num[right--]]--]--;}
- ans[help[i]]=findans(k[help[i]]);
- }
- for(int i=0;i<m;i++)
- printf("%d\n",ans[i]);
- }
- return 0;
- }
acdreamoj1108(The kth number)的更多相关文章
- 协议端口号(protocol port number)
协议端口号(protocol port number) 先来个注意事项 (-> ->) 这种在协议层间的抽象的协议端口是软件端口,和硬件端口是完全不同的概念.硬件端口是不同设备进行交互的接 ...
- 整体二分(SP3946 K-th Number ZOJ 2112 Dynamic Rankings)
SP3946 K-th Number (/2和>>1不一样!!) #include <algorithm> #include <bitset> #include & ...
- C++之路进阶——poj2104(K-th Number)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 44537 Accepted: 14781 Ca ...
- 图的最短路径-----------Dijkstra算法详解(TjuOj2870_The Kth City)
做OJ需要用到搜索最短路径的题,于是整理了一下关于图的搜索算法: 图的搜索大致有三种比较常用的算法: 迪杰斯特拉算法(Dijkstra算法) 弗洛伊德算法(Floyd算法) SPFA算法 Dijkst ...
- 一个快速double转int的方法(利用magic number)
代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...
- javascript弹出框打印某个数值时,弹出NaN?(not a number)
一.NaN:表示not a number null 未定义或空字符串 undefined 对象属性不存在 或是声明了变量但从未赋值. 二.出现这种情况有(1)此常数的值是零被零除所得到的结果. (2) ...
- C#LeetCode刷题之#414-第三大的数(Third Maximum Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3710 访问. 给定一个非空数组,返回此数组中第三大的数.如果不存 ...
- 非数值(Not a Number)NaN的解释
它是一个特殊的数值.它用于表示一个本来要返回数值的操作数未返回数值的情况. 在ECMAScript中,任何数值除以0会返回NaN,而不会导致错误,不会停止代码的执行,因此不会影响其他代码的执行. Na ...
- PAT-1144(The Missing Number)set的使用,简单题
The Missing Number PAT-1144 #include<iostream> #include<cstring> #include<string> ...
随机推荐
- B-Tree、B+Tree和B*Tree
B-Tree(这儿可不是减号,就是常规意义的BTree) 是一种多路搜索树: 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点 ...
- 闲置小U盘变身最强大路由器
小容量 U 盘,用起来嫌容量太少,丢了好像又觉得太可惜.不过现在将它进行一番小改造后,配合我们的电脑 ,就能得到一台强大的路由器,不仅省了买路由的钱,而且这台路由器在市面上基本买不到 ! DD ...
- [转]eoe社区cocos2d-x游戏引擎知识大汇总
[eoeAndroid 社区]特意为大家汇总了cocos2d-x知识贴,分量十足,纯正干或.从基础教程到游戏应用的开发,我们不让知识流失,我们要做知识的搬运工还有加工 师.希望大家能够一起的学习,和大 ...
- Flex里的命名空间,fx、mx、s【转】
Flex 4带给我们的,是全新的命名空间.了解这些命名空间必定是一件好事情.Flex 4有三个非常重要的命名空间,分别是: xmlns:fx=”http://ns.adobe.com/mxml/200 ...
- 项目 erlang启动时死循环
机子里的otp是新装的 看了一下main 是在util:ensure_started一堆app的时候死讯了, 按照顺序是sasl crypto asn1 public_key ssl 发现是publi ...
- python用法笔记(数组(list、touple、dict)、字符串)
1.产生n个全为1的数组a=[1]*n2.字符数字转化int('12')float('12.5')str(123.45)ASCII码转为相应的字符:chr(97)字符转化为相应的ASCII码:ord( ...
- xcode 6.4 安装Alcatraz失败解决方法
Alcatraz Xcode6.4安装不了解决方法http://www.cocoachina.com/bbs/read.php?tid=310380 版权声明:本文为博主原创文章,未经博主允许不得转载 ...
- SCAU 10893 Spiral
10893 Spiral 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description Given an odd number n, we can ar ...
- gpgcheck
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Public key for co ...
- 两年内从零到每月十亿 PV 的发展来谈 Pinterest 的架构设计(转)
原文:Scaling Pinterest - From 0 To 10s Of Billions Of Page Views A Month In Two Years 译文:两年内从零到每月十亿 PV ...