a problem
给出两个长度为 $n$ 的数组 $a, b$
对于任意的 $a_i + b_j$, 求第 $k$ 大
不妨设 $a_i < a_{i + 1}, b_i < b_{i + 1}$
对于任意的 $a_i + b_j$, 可以得到这样的 $n ^ 2$ 个数
$$
\begin{matrix}
a_1 + b_1 & a_1 + b_2 & \cdots & a_1 + b_n \\
a_2 + b_1 & a_2 + b_2 & \cdots & a_2 + b_n \\
\vdots & \vdots & \ddots & \vdots \\
a_n + b_1 & a_n + b_2 & \cdots & a_n + b_n \\
\end{matrix}
$$
显然最小的数一定在第一列中
可以用一个 pair 存储矩阵中的每个元素
pair <a_i + b_j, j>
这样的话可以由改元素推出同行下列的元素
$first = a_i + b_j - b_j + b_{j + 1}$
$second = j + 1$
首先将第一列的元素对应的 pair 放入 priority_queue
每次弹出 fisrt 最小的元素,放入该元素的同行的下列元素
直至找到 $k$ 大
时间复杂度 O(nlogn)
a problem的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
- PHP curl报错“Problem (2) in the Chunked-Encoded data”解决方案
$s = curl_init(); curl_setopt($s, CURLOPT_POST, true); curl_setopt($s, CURLOPT_POSTFIELDS, $queryStr ...
随机推荐
- PB笔记之调用数据窗口时的过滤条件添加方式
在PB查询数据窗口的数据时 通常可以有两种方式 一是在数据窗口事先写好查询条件,然后用retrieve()函数通过参数传递给数据窗口 这种方式适合查询条件较为简单,条件数较少的数据窗口 二是使用Set ...
- ArcGIS JS之 applyEdits之后要素符号更新
ArcGIS JS版本 ArcGIS JS 4.11 最近做一个地图服务,通过FeatureLayer.applyEdits()方法,更新唯一值的渲染字段,实现地图渲染根据用户的配置实时更新. 由于A ...
- VK Cup 2017 - Round 1 (CDE)
771C Bear and Tree Jumps 大意: 给定树,每步能走到距离不超过$k$的任意点,记$f(s,t)$为$s$到$t$的最少步数,求$\sum\limits_{s<t}f(s, ...
- java EE学习之数据库操作
jdbc开发流程 注册驱动 建立连接(Connection) 创建运行SQL的语句(Statement) 运行语句 处理运行结果(ResultSet) 释放资源 注冊驱动有三种方式: Class.fo ...
- windows下安装phpredis扩展
根据phpyinfo获取自己的php信息 x86,php5.6,TS,VC11 在pecl网站上找到对应的版本 5.6 Thread Safe (TS) x86 https://pecl.php.ne ...
- Springboot笔记01——Springboot简介
一.什么是微服务 在了解Springboot之前,首先我们需要了解一下什么是微服务. 微服务是一种架构风格(服务微化),是martin fowler在2014年提出来的.微服务简单地说就是:一个应用应 ...
- BeautifulSoup库的安装与使用
BeautifulSoup库的安装 Win平台:“以管理员身份运行” cmd 执行 pip install beautifulsoup4 演示HTML页面地址:http://python123.io/ ...
- Android面试题 描述一下android的系统架构
android系统架构从下往上为linux内核层.运行库.应用程序框架层和应用程序层. Linux Kernel:负责硬件的驱动程序.网络.电源.系统安全以及内存管理等功能. Libraries和an ...
- openwrt 切换overlay文件系统为根文件系统
http://blog.chinaunix.net/uid-27057175-id-4584360 openwrt的overlayfs 通过/etc/preinit调用 /sbin/mount_roo ...
- 小程序数据绑定和setData
我们wxml没有直接调用数据的能力,我们的逻辑是通过js调用数据,再由js传递给wxml才能够显示出来.那么怎么由js传递给wxml? 首先我的js里面有这样一段代码 process: funct ...