[51NOD1105]第k大的数(二分答案)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105
先排序,二分上下界分别是最小的两个数和最大的两个数的乘积。注意到一个性质,就是a[i]*b[j],i从左到右,j从右到左。假如遇到一组a[i]*b[j]<=x,j就不必再减小了。这时候比x小的数一定是j个。下一次继续找i,这个b[j]一定不可能大于b[j+1]。问题就由找第k大的数,转换成找第n*n-k+1小的数。每次二分这个乘积x,看看是否符合条件即可。
/*
━━━━━┒ギリギリ♂ eye!
┓┏┓┏┓┃キリキリ♂ mind!
┛┗┛┗┛┃\○/
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f, sizeof(a))
#define lp p << 1
#define rp p << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb; const int maxn = ;
LL n, k, ans;
LL a[maxn], b[maxn]; bool ok(LL x) {
LL ret = ;
LL j = n;
For(i, , n+) {
while(j) {
if(a[i] * b[j] > x) j--;
else break;
}
ret += j;
}
return ret >= k;
} int main() {
// FRead();
while(cin >> n >> k) {
k = n * n - k + ;
For(i, , n+) {
cin >> a[i] >> b[i];
}
sort(a+, a+n+); sort(b+, b+n+);
LL l = a[] * b[], r = a[n] * b[n];
while(l <= r) {
LL m = (l + r) >> ;
if(ok(m)) {
r = m - ;
ans = m;
}
else l = m + ;
}
cout << ans << endl;
}
RT ;
}
[51NOD1105]第k大的数(二分答案)的更多相关文章
- 1105 第K大的数(二分)
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * B[0],A[0 ...
- 树状数组+二分答案查询第k大的数 (团体程序设计天梯赛 L3-002. 堆栈)
前提是数的范围较小 1 数据范围:O(n) 2 查第k大的数i:log(n)(树状数组查询小于等于i的数目)*log(n)(二分找到i) 3 添加:log(n) (树状数组) 4 删除:log(n) ...
- 51nod 1105 第K大的数 【双重二分/二分套二分/两数组任意乘积后第K大数】
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- 51NOD 1105 第K大的数
数组A和数组B,里面都有n个整数. 数组C共有n^2个整数,分别是: A[0] * B[0],A[0] * B[1] ...... A[0] * B[n-1] A[1] * B[0],A[1] * B ...
- 51 nod 1105 第K大的数
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- 1105 第K大的数
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * B[0],A[0] * B[1] ...... ...
- 第k大的数,前k大的数
1.排序后去出前k个,o(n*log(n)) 如果k<log(n),可以考虑直接选择排序,因为只需要执行找到第k个就可以结束 o(n*k) 2.o(nlog(k))快排把数分为了两个部分, ...
- 寻找第K大的数
在一堆数据中查找到第k个大的值. 名称是:设计一组N个数,确定其中第k个最大值,这是一个选择问题,解决这个问题的方法很多. 所谓“第(前)k大数问题”指的是在长度为n(n>=k)的乱序数组中S找 ...
随机推荐
- composer的安装
HomeGetting StartedDownloadDocumentationBrowse Packages Dependency management Declaring dependencies ...
- iOS10和Xcode8适配
1 Xib文件的注意事项 使用Xcode8打开xib文件后,会出现下图的提示. 大家选择Choose Device即可. 之后大家会发现布局啊,frame乱了,只需要更新一下frame即可.如下图 注 ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D. Little Artem and Dance
题目链接: http://codeforces.com/contest/669/problem/D 题意: 给你一个初始序列:1,2,3,...,n. 现在有两种操作: 1.循环左移,循环右移. 2. ...
- 数字式PID控制的应用总结
PID控制是一个二阶线性闭环控制器,通过调整比例.积分和微分三项参数,使得大多数的工业控制系统获得良好的闭环控制性能.PID控制优点:a. 技术成熟,b. 易被人们熟悉和掌握,c. 不需要建立数学模型 ...
- 【POJ】【2096】Collecting Bugs
概率DP/数学期望 kuangbin总结中的第二题 大概题意:有n个子系统,s种bug,每次找出一个bug,这个bug属于第 i 个子系统的概率为1/n,是第 j 种bug的概率是1/s,问在每个子系 ...
- Leetcode#80 Remove Duplicates from Sorted Array II
原题地址 简单模拟题. 从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去 代码: int removeDuplicates(int A[], int n) { ) return n; ; ...
- MemSQL Start[c]UP 2.0 - Round 1
A. Eevee http://codeforces.com/contest/452/problem/A 字符串水题 #include<cstdio> #include<cstrin ...
- HTML/CSS中常遇到的bug 一些注意事项总结
1.IE6下横向双倍margin bug (触发条件:块属性标签:float:横向margin设置:IE6下.解决办法:css中加入display:inline.) 2.css中公用属性首先声明:如对 ...
- UVALive 6533
哈夫曼树 倒过来思考 ~ 最深的叶子 值为1 所以最深的先出队列 #include <iostream> #include <cstdio> #include <cs ...
- properties配置应用,为什么需要使用properties文件
在项目中我们常常会使用Constants常量类,达到系统全局配置的目的. 但是有些常量需要动态的配置,如果项目上线后,每次修改Constants.java然后再编译,再上传Constants.clas ...