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> ...
随机推荐
- C++11 之 " = delete "
1 缺省函数 设计一个类,没有成员函数 (member function),只有成员数据 (member data) class DataOnly { private: std::string st ...
- js数组依据下标删除元素
最近在项目中遇到了一些问题,基础性的东西记得不牢固,就总结一下放在这里备再次查找,对操作js数组的一些问题一些常用的记录! 1.创建数组 var array = new Array(); var ar ...
- 《学习OpenCV》练习题第四章第七题abc
题外话:一直是打算把这本书的全部课后编程题写完的,中间断了几个月,一直忙于其他事.现在开始补上. 这道题我不清楚我理解的题意是不是正确的,这道题可以练习用OpenCV实现透视变换(可以用于矫正在3维环 ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- cisco tftp 备份/恢复
使用tftp服务器对cisco 3560 配置备份及恢复 Switch#copy running-config tftp:Address or name of remote host []? 192. ...
- [iOS微博项目 - 3.0] - 手动刷新微博
github: https://github.com/hellovoidworld/HVWWeibo A.下拉刷新微博 1.需求 在“首页”界面,下拉到一定距离的时候刷新微博数据 刷新数据的时候使 ...
- String.Format格式说明(转)
C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0 ...
- 异常:exception和error的区别
Throwable 是所有 Java 程序中错误处理的父类 ,有两种子类: Error 和 Exception . Error :表示由 JVM 所侦测到的无法预期的错误,由于这是属于 JVM ...
- window.print打印指定div
window.print可以打印网页,但有时候我们只希望打印特定控件或内容,怎么办呢? 首先我们可以把要打印的内容放在div中,然后用下面的代码进行打印. <html> <head& ...
- 移动端轮播图插件(支持Zepto和jQuery)
一. 效果图 二. 功能介绍 1. 支持图片自动轮播和非自动轮播 2. 支持点击和滑动. 三. 简单介绍 代码都有注释,逻辑简单,不做更多赘述. 1. 在你的html中添加一行. <sectio ...