NC14301 K-th Number

题目

题目描述

Alice are given an array A[1..N] with N numbers.

Now Alice want to build an array B by a parameter K as following rules:

Initially, the array B is empty. Consider each interval in array A. If the length of this interval is less than K, then ignore this interval. Otherwise, find the K-th largest number in this interval and add this number into array B.

In fact Alice doesn't care each element in the array B. She only wants to know the M-th largest element in the array B. Please help her to fi nd this number.

输入描述

The first line is the number of test cases. For each test case, the first line contains three positive numbers \(N(1≤N≤10^5)\) ; $K(1≤K≤N) $; \(M\) .

The second line contains \(N\) numbers \(A_i(1≤A_i≤10^9)\) .It's guaranteed that M is not greater than the length of the array B.

输出描述

For each test case, output a single line containing the M-th largest element in the array B.

示例1

输入

2
5 3 2
2 3 1 5 4
3 3 1
5 8 2

输出

3
2

题解

思路

知识点:二分。

二分 \(B\) 的第 \(m\) 大,其在 \(B\) 序列中的位置具有单调性,检验大于等于这个数的 \(A\) 中所以子区间第 \(k\) 大的数量,如果数量大于等于 \(m\) ,说明这个数小于等于答案;反之,肯定大于。

计算大于等于这个数的 \(A\) 中所以子区间第 \(k\) 大的数量,通过对 \(A\) 尺取法快速计算区间总数,得到这个数在 \(B\) 中的排位。

注意的是,对答案二分时,不需要严格要求 \(mid\) 是在数组中的数,因为 \(l\) 和 \(r\) 最终会收敛到一个分界点上,而分界点只可能出现在数组中的数,所以不必担心。

坑点是 \(B\) 数组的长度是会超 \(int\) 的,因此 \(m\) 也会超 \(int\) 而题目没给范围,是最坑的。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; int n, k;
ll m;///可能到 1e10,坑死了
int a[100007]; bool check(int mid) {/// 查询大于等于mid的第k大的数的数量是否大于等于m
ll sum = 0;
int i = 0, j = 0, cnt = 0;
while (i < n) {
while (j < n && cnt < k) {
if (a[j] >= mid) cnt++;
j++;
}
if (cnt == k) sum += n - j + 1;
if (a[i] >= mid) cnt--;
i++;
}
return sum >= m;
} bool solve() {
cin >> n >> k >> m;
for (int i = 0;i < n;i++) cin >> a[i];
int l = 1, r = 1e9;///不必担心mid不是数组中的数,因为最后收敛点一定在数组的某个数上
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) l = mid + 1; ///check为真说明 mid 小于等于答案
else r = mid - 1;
}
cout << r << '\n';///返回比最后一次小于等于答案的数的位置
return true;
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
if (!solve()) cout << -1 << '\n';
}
return 0;
}

NC14301 K-th Number的更多相关文章

  1. ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

    A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ...

  2. [POJ2104] K – th Number (可持久化线段树 主席树)

    题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)

    https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...

  4. Count the number of possible triangles

    From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...

  5. 大数据热点问题TOP K

    1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...

  6. Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

    F. Restore a Number   Vasya decided to pass a very large integer n to Kate. First, he wrote that num ...

  7. C++经典编程题#1:含k个3的数

    总时间限制:  1000ms 内存限制:  65536kB 描述 输入两个正整数 m 和 k,其中1 < m < 100000,1 < k < 5 ,判断 m 能否被19整除, ...

  8. K Best(最大化平均数)_二分搜索

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

  9. LintCode-Kth Prime Number.

    Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eli ...

  10. POJ3111 K Best

    Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...

随机推荐

  1. 初踩坑JS加载与audio接口:点击头像开始/暂停背景音乐

    背景 封楼期间难得空闲,也静不下心学习,空闲之余萌生了重做引导单页的想法.因为之前都是扒站(某大公司游戏官网)+小改,一来虽然很炫酷,但本人水平有限,仍有很大一部分JS无从下手,甚至是看不懂|-_-| ...

  2. Vue Router的简单了解

    Vue Router Vue Router官方文档 传统Web项目开发往往采用超链接实现页面之间的切换和跳转.Vue开发的是单页面应用(Single Page Application,SPA),不能使 ...

  3. VSCode 前端常用插件集合

    Visual Studio Code 是由微软开发的一款免费.跨平台的文本编辑器.由于其卓越的性能和丰富的功能,它很快就受到了大家的喜爱. 但工欲善其事必先利其器,以下是本人为前端开发收集的常用的vs ...

  4. C3P0反序列化链学习

    C3P0 c3p0第一次听闻是用于fastjson的回显上,大佬们总结三种方法,后面两种主要就是用于fastjson和jackjson的回显利用(注入内存马) http base jndi hex序列 ...

  5. 一文读懂原子操作、内存屏障、锁(偏向锁、轻量级锁、重量级锁、自旋锁)、Disruptor、Go Context之上半部分

    我不想卷,我是被逼的 在做了几年前端之后,发现互联网行情比想象的差,不如赶紧学点后端知识,被裁之后也可接个私活不至于饿死.学习两周Go,如盲人摸象般不知重点,那么重点谁知道呢?肯定是使用Go的后端工程 ...

  6. PDCA循环——快速提升软件质量的必备工具

    近年来,软件项目的规模及其复杂性正在以空前的速度增长,互联网用户市场庞大,互联网公司和相应的软件产品层出不穷.快速响应需求变化往往是互联网行业的常态,软件产品的快速开发迭代对于公司迅速占领市场.抢占商 ...

  7. shiro550反序列学习

    Shiro550 shiro550和fastjson作为攻防演练的利器,前面学习了fastjson的相关利用和回显,本篇主要来学习一下shiro550的漏洞原理. 1.漏洞原因 在 Shiro < ...

  8. 《Streaming Systems》第三章: Watermarks

    定义 对于一个处理无界数据流的 pipeline 而言,非常需要一个衡量数据完整度的指标,用于标识什么时候属于某个窗口的数据都已到齐,窗口可以执行聚合运算并放心清理,我们暂且就给它起名叫 waterm ...

  9. ucore lab6 调度管理机制 学习笔记

    这节虽叫调度管理机制,整篇下来主要就讲了几个调度算法.兴许是考虑到LAB5难,LAB6就仁慈了一把,难度大跳水.平常讲两节原理做一个实验,这次就上了一节原理.权当大战后的小憩吧. schedule函数 ...

  10. screen使用小结

    目录 安装 shell-screen-window关系 常用参数 快捷键 离开当前screen 打开一个新的窗口 查看窗口列表 窗口的快速切换 回到行首 关闭窗口 关闭所有窗口 关闭screen 删除 ...