有序数列第K小

题目描述

给出两个长度分别为\(n,m\)的单调非递减数列,求出它们合并后的第\(k\)小值。

输入输出格式

输入格式:

第一行三个数,\(n,m,k\)如题意所述;

第二行\(n\)个数,依次为数列1;

第三行\(m\)个数,依次为数列2;

输出格式:

一个数,表示合并后的第\(k\)小值。

说明

对于所有数据,\(k\le n+mk≤n+m , a_i\le 10^8\),时间限制200ms。


这个题其实考察的是\(logk\)的分治做法

对当前的两个序列,左指针为\(la,lb\),右指针为\(ra,rb\),求当前的第\(k\)小值。

把第\(k\)小值除2,取两个序列之一贡献这么多,得到子问题

注意边界情况


Code:

#include <cstdio>
int min(int x,int y){return x<y?x:y;}
const int N=1000010;
int n,m,k,a[N],b[N];
void divide(int la,int ra,int lb,int rb,int nk)
{
if(la>ra)
{
printf("%d\n",b[lb+nk-1]);
return;
}
if(lb>rb)
{
printf("%d\n",a[la+nk-1]);
return;
}
if(nk==1)
{
printf("%d\n",min(a[la],b[lb]));
return;
}
int lk=nk>>1;
lk=min(lk,min(ra+1-la,rb+1-lb));
if(a[la+lk-1]<b[lb+lk-1])
divide(la+lk,ra,lb,rb,nk-lk);
else
divide(la,ra,lb+lk,rb,nk-lk);
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++) scanf("%d",a+i);
for(int i=1;i<=m;i++) scanf("%d",b+i);
divide(1,n,1,m,k);
return 0;
}

有序数列第K小加强版

加上多次区间询问


Code:

#include <cstdio>
int min(int x,int y){return x<y?x:y;}
const int N=1000010;
int n,m,q,a[N],b[N];
void divide(int la,int ra,int lb,int rb,int nk)
{
if(la>ra)
{
printf("%d\n",b[lb+nk-1]);
return;
}
if(lb>rb)
{
printf("%d\n",a[la+nk-1]);
return;
}
if(nk==1)
{
printf("%d\n",min(a[la],b[lb]));
return;
}
int lk=nk>>1;
lk=min(lk,min(ra+1-la,rb+1-lb));
if(a[la+lk-1]<b[lb+lk-1])
divide(la+lk,ra,lb,rb,nk-lk);
else
divide(la,ra,lb+lk,rb,nk-lk);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i);
for(int i=1;i<=m;i++) scanf("%d",b+i);
scanf("%d",&q);
int l1,l2,r1,r2,k;
for(int i=1;i<=q;i++)
{
scanf("%d%d%d%d%d",&l1,&r1,&l2,&r2,&k);
divide(l1,r1,l2,r2,k);
}
return 0;
}

2018.7.26

有序数列第K小的更多相关文章

  1. 求两个有序数组的中位数或者第k小元素

    问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...

  2. 两个有序数列找第k小

    给定一个数组,数组中的数据无序,在一个数组中找出其第k个最小的数,例如对于数组x,x = {3,2,1,4,5,6},则其第2个最小的数为2  两个有序数组 找第k小 * 方案一 合并遍历 * 二:游 ...

  3. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  4. 现在有m组n个有序数组,例如{1,2,3,4},{2,3,4,6},{1,3,5,7},在这些数组中选择第k小的数据,然后返回这个值

    问题描述:现在有m组n个有序数组,例如{1,2,3,4},{2,3,4,6},{1,3,5,7},在这些数组中选择第k小的数据,然后返回这个值 思路:参照两个数组归并的过程,每次选取最小的数据进行比较 ...

  5. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  6. Ex 2_22 两个有序列表合并后的第k小元素..._第四次作业

    package org.xiu68.ch02; public class Ex2_22 { public static void main(String[] args) { // TODO Auto- ...

  7. 每天一道算法题目(18)——取等长有序数组的上中位数和不等长有序数组的第k小的数

    1.取上中位数 题目: 给定两个有序数组arr1和arr2,两个数组长度都为N,求两个数组中所有数的上中位数.要求:时间复杂度O(logN).      例如:          arr1 = {1, ...

  8. Leetcode 378.有序矩阵中第k小的元素

    有序矩阵中第k小的元素 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素.请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, ...

  9. [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

随机推荐

  1. 使用union

    QUERY: explain extended ) TRACE: { "steps": [ { "join_preparation": { "sele ...

  2. meta-data获取小结

    android 开发中:   在AndroidManifest.xml中,<meta-data>元素可以作为子元素,   被包含在<activity>.<applicat ...

  3. 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)

    题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...

  4. 适配chrome65最新selenium-chromedriver

    网盘地址:https://pan.baidu.com/s/1BmdwRgD96IL32-3FTFxPSg 密码: 2vg6

  5. Python2快速入门教程,只需要这十五张图片就够了!

    今天给大家分享的教程是适用于Python 2.7,但它可能适用于Python 2.Python 2.7将停止在2020中的支持. 与Python 2.7和3兼容的Python代码是完全可能的.通过使用 ...

  6. ajax 个人理解 学习笔记

    W:Ajax Q:异步网络请求.无刷新请求数据. W:ajax的实现流程如下: Q: 创建XHR对象 调用open()方法,创建请求 调用send()方法,发送请求 捕获请求状态,判断请求结果 获取数 ...

  7. Python中from module import *语法

    from module import *的语法在Python 3.X和Python 2.X中的使用稍有区别: 在Python 3.X中,from module import *无法在函数里面使用,而在 ...

  8. Line belt(三分镶嵌)

    In a two-dimensional plane there are two line belts, there are two segments AB and CD, lxhgww's spee ...

  9. Check the string

    A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend ...

  10. 词频统计 SPEC 20170914 1 1 1 1 1

    功能1 小文件输入,为表明程序能跑,结果真实而不是迫害老五,请他亲自键盘在控制台下输入命令. #include<stdio.h> #include<string.h> #inc ...