NC14301 K-th Number
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的更多相关文章
- 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 ...
- [POJ2104] K – th Number (可持久化线段树 主席树)
题目背景 这是个非常经典的主席树入门题--静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个正整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 K Supreme Number(规律)
https://nanti.jisuanke.com/t/31452 题意 给出一个n (2 ≤ N ≤ 10100 ),找到最接近且小于n的一个数,这个数需要满足每位上的数字构成的集合的每个非空子集 ...
- Count the number of possible triangles
From: http://www.geeksforgeeks.org/find-number-of-triangles-possible/ Given an unsorted array of pos ...
- 大数据热点问题TOP K
1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...
- 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 ...
- C++经典编程题#1:含k个3的数
总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个正整数 m 和 k,其中1 < m < 100000,1 < k < 5 ,判断 m 能否被19整除, ...
- K Best(最大化平均数)_二分搜索
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- 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 ...
- POJ3111 K Best
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
随机推荐
- 2021.07.19 BZOJ2654 tree(生成树)
2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...
- Rust如何开发eBPF应用(一)?
前言 eBPF是一项革命性的技术,可以在Linux内核中运行沙盒程序,而无需重新编译内核或加载内核模块.它能够在许多内核 hook 点安全地执行字节码,主要应用在云原生网络.安全.跟踪监控等方面. e ...
- 《手写Mybatis》第5章:数据源的解析、创建和使用
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 管你吃几碗粉,有流量就行! 现在我们每天所接收的信息量越来越多,但很多的个人却没有多 ...
- 带你了解极具弹性的Spark架构的原理
摘要:相比MapReduce僵化的Map与Reduce分阶段计算相比,Spark的计算框架更加富有弹性和灵活性,运行性能更佳. 本文分享自华为云社区<Spark架构原理>,作者:JavaE ...
- 封闭的一个多月,老菜鸟的 机械手和AGV 自动搬运小项目总结
最近上海疫情严重,闲赋在家无事可做,手机里不断的推送一些无脑的谩骂声音,索性找点事情做,将3月份实施的一个自动搬运小项目做一个简单的汇总,便于今后项目实施中积累一些经验.项目需求非常简单,因为能力有限 ...
- Unity实现A*寻路算法学习2.0
二叉树存储路径节点 1.0中虽然实现了寻路的算法,但是使用List<>来保存节点性能并不够强 寻路算法学习1.0在这里:https://www.cnblogs.com/AlphaIcaru ...
- [STL] vector 可变数组
点击查看代码 #include<iostream> #include<vector> using namespace std; int main() { // 初始化 a 为 ...
- 一文带你读懂zookeeper在大数据生态的应用
一个执着于技术的公众号 一.简述 在一群动物掌管的世界中,动物没有人类聪明的思想,为了保持动物世界的生态平衡,这时,动物管理员-zookeeper诞生了. 打开Apache zookeeper的官网, ...
- 关于background-*的一些属性
1.盒模型 盒模型从外到内一次为:margin-box.border-box.padding-box.content-box. 2.一些属性设置的相对位置 ⑴background-position的属 ...
- 第一个MVC程序
配置版 添加web的支持! 确定导入了SpringMVC 的依赖! 配置web.xml , 注册DispatcherServlet <?xml version="1.0" e ...