ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NC14301 K-th Number

2022-06-28 19:03:29  阅读:159  来源: 互联网

标签:cnt int Number mid NC14301 th each array


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 \log 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;
}

标签:cnt,int,Number,mid,NC14301,th,each,array
来源: https://www.cnblogs.com/BlankYang/p/16420703.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有