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> ...
随机推荐
- Mysql字符串截取函数SUBSTRING的用法说明
感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始截取字符串 left(str, length) 说明:le ...
- kali2 source
deb http://http.kali.org/kali sana main non-free contrib deb http://security.kali.org/kali-security/ ...
- STL源码剖析读书笔记--第四章--序列式容器
1.什么是序列式容器?什么是关联式容器? 书上给出的解释是,序列式容器中的元素是可序的(可理解为可以按序索引,不管这个索引是像数组一样的随机索引,还是像链表一样的顺序索引),但是元素值在索引顺序的方向 ...
- 【24点游戏】cocos2dx 源码
1. 4个数字 24点判断 double Calc(double a, double b, string oper) { double result = 0; const char *p = ope ...
- Chapter 7 Windows下pycaffe的使用之draw_net.py
Chapter 6 中完成了在Windows下,对pycaffe的编译,如果编译存在问题,请参考:http://www.cnblogs.com/xiaopanlyu/p/6158902.html 本文 ...
- CenotOS ip a
- HTML5每日一练之figure新标签的应用
igure元素是一种元素的组合,可带有标题(可选).figure标签用来表示网页上一块独立的内容,将其从网页上移除后不会对网页上的其他内容产生影响.figure所表示的内容可以是图片.统计图或代码示例 ...
- ZOJ 3810 Pretty Poem 分类: ACM 2015-05-17 14:40 83人阅读 评论(0) 收藏
Pretty Poem Time Limit: 2 Seconds Memory Limit:65536 KB Poetry is a form of literature that uses ...
- Mysql SQL优化&执行计划
SQL优化准则 禁用select * 使用select count(*) 统计行数 尽量少运算 尽量避免全表扫描,如果可以,在过滤列建立索引 尽量避免在where子句对字段进行null判断 尽量避免在 ...
- 【转载】Windows/Office“神key的来源”(附win8神key)
凡是没有经过微软授权的渠道激活Windows/Office的全部是"D版"!但由于密钥激活更方便快捷,因此很受欢迎.从百度博客到现在,很多网友询问:"神key&q ...