1686 第K大区间(尺取+二分)
定义一个区间的值为其众数出现的次数。
现给出n个数,求将所有区间的值排序后,第K大的值为多少。
第一行两个数n和k(1<=n<=100000,k<=n*(n-1)/2)
第二行n个数,0<=每个数<2^31
一个数表示答案。
4 2
1 2 3 2
2 //首先,考虑区间的值的性质,如果区间的值为 x ,那么,区间无论怎么左右扩展,区间的值必然大于等于 x 。
那么,计算区间的值大于等于 x ,可以用尺取在 O(n) 时间算出,显然,这是有单调性的,二分即可
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
# define MX
/**************************/ int n,k;
int a[MX];
int b[MX];
int num[MX]; bool check(int x)
{
memset(num,,sizeof(num));
int l=,r=,flag=;
LL tot=;
while (r<=n+)
{
if (!flag)
{
if (r==n+) break;
num[a[r]]++;
if (num[a[r]]>=x) flag=r;
r++;
}
else
{
while (flag)
{
tot += n-r+;
num[a[l]]--;
if (num[a[flag]]<x) flag=;
l++;
}
}
}
if (tot>=k) return ;
return ;
} int main()
{
scanf("%d%d",&n,&k);
for (int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i] = a[i];
}
sort(b+,b++n);
for (int i=;i<=n;i++)
a[i] = lower_bound(b+,b++n,a[i])-b; int l=, r=n;
int ans;
while (l<=r)
{
int mid = (l+r)>>;
if (check(mid)) ans=mid,l=mid+;
else r=mid-;
}
printf("%d\n",ans);
return ;
}
1686 第K大区间(尺取+二分)的更多相关文章
- 51nod 1686 第K大区间【离散化+二分】
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 定义一个区间的值为其众数出现的次数. 现给出n ...
- 51nod 1686 第k大区间
1686 第K大区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. ...
- 1686 第K大区间
1686 第K大区间 时间限制:1 秒 空间限制:131072 KB 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. 众数(统计学/数学名词)_百度百 ...
- 51NOD 1686 第K大区间 二分
第k大区间 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. Input 第一行两个数n和k(1<=n<=100000,k<=n* ...
- 51nod 1686 第K大区间2
1685 第K大区间2 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. 众数(统计学/数学名词)_百度百科 Input 第一行两个数n和k(1<=n ...
- 51nod 1686 第K大区间 二分瞎搞
题目: 定义一个区间的值为其众数出现的次数. 现给出n个数,求将所有区间的值排序后,第K大的值为多少. 题解: 答案明显单调,我们考虑二分答案. 转化为判定问题后我们需要观察到一个性质: 如果一个区间 ...
- 51Nod 1686 第K大区间(离散化+尺取法)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 思路: 第K大值,所以可以考虑二分法,然后用尺取法去扫描, ...
- 51Nod——T 1686 第K大区间
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...
- 【题解】51nod 1686第K大区间
成功的秘诀,在于克服自己看题解的冲动……[笑哭].自己A掉这题还是灰常开心的~ 以及爱死 two - pointer ! two - pointer 大法是真的好哇……这个题目有上一题的经验:求第\( ...
随机推荐
- Unity3D使用TCP/IP协议,传递protocol buffer消息protobuf-net
原文:http://my.oschina.net/faint/blog/296785 第一部分 dll 1 下面大多数内容,都是使用c#编译的dll来实现的. 2 编译为dll后,要拖放到unity3 ...
- jQuery--百度百科
JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Oper ...
- dubbo笔记
使用Maven打包依赖项,启动时从本地jar中读取dubbo.xsd 最近项目用到dubbo,打包启动时报错 Failed to read schema document from http://co ...
- JavaScript完整性检查
1.7个“坑” <!DOCTYPE html> <html lang="zh"> <head> <meta charset="U ...
- Android Shape 详解
1 http://blog.csdn.net/feng88724/article/details/6398193 2 <shape xmlns:android="http://sche ...
- 使用dynamic类型来优化反射
什么是dynamic类型?微软给出的官方文档中这样解释:在通过 dynamic 类型实现的操作中,该类型的作用是绕过编译时类型检查. 改为在运行时解析这些操作. dynamic 类型简化了对 COM ...
- Mac 常用命令介绍
1.查看所有shell cat /etc/shells 2.查看当前使用的shell类型 $ echo $SHELL 3.
- JQuery最佳实践及常见错误(转自 简书)
1 使用JQuery的ready处理器 如果你的代码操作DOM,则需要DOM加载完成后再运行代码.推荐使用如下的第一种写法,第二种写法在JQuery3.x中已经不推荐使用了. $(function ( ...
- C# Activator.CreateInstance 动态创建类的实例(一)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- mysql workbench 导出表结构
Server->Data Export 选择数据库(我的是 lhc库) -> 选择对应表(我的是 device表), Dump Structre and Data 导出表数据和表结构 D ...