二分套二分 hrbeu.acm.1211Kth Largest
Kth Largest
TimeLimit: 1 Second MemoryLimit: 32 Megabyte
Description
There are two sequences A and B with N (1<=N<=10000) elements each. All of the elements are positive integers. Given C=A*B, where '*' representing Cartesian product, c = a*b, where c belonging to C, a belonging to A and b belonging to B. Your job is to find the K'th largest element in C, where K begins with 1.
Input
Input file contains multiple test cases. The first line is the number of test cases. There are three lines in each test case. The first line of each case contains two integers N and K, then the second line represents elements in A and the following line represents elements in B. All integers are positive and no more than 10000. K may be more than 10000.
Output
For each case output the K'th largest number.
Sample Input
2
2 1
3 4
5 6
2 3
2 1
4 8
Sample Output
24
8
解题思路
双重二分。先对两个序列A,B从大到小排序,然后可以我们进行第一重二分:对要求取的答案二分,即求取的答案在[A[n]*B[n],A[1]*B[1]]之间,取s1=A[n]*B[n],e1=A[1]*B[1],mid=(s1+e1)/2,那么我们去计算在序列C中大于等于这个mid值的个数是多少,当然不是算出这个序列来,而是进行第二次二分。我们对于两个序列可以这样处理,枚举序列A,二分序列B,也就是说对于每个A[i],我们去二分序列B,来计算大于等于mid值的个数。那么我们可以得到结束条件,当大于等于mid值的个数大于等于k,且大于mid值的个数小于k,那么答案就是这个mid。那么时间复杂度就为n*log(n)*log(n^2)了。
#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#define N 10101
#include<algorithm>
int a[N],b[N],tast,k,n;
bool cmp(int a,int b)
{
return a>b;
}
int read()
{
int ans=,ff=;
char s;
s=getchar();
while(s<''||s>'')
{
if(s=='-') ff=-;
s=getchar();
}
while(s>=''&&s<='')
{
ans=ans*+s-'';
s=getchar();
}
return ans;
}
int find(int l,int r,int t,int m)
{
int mid;
while(l<=r)
{
mid=(l+r)>>;
if(m*b[mid]>t)
l=mid+;
else r=mid-;
}
return r;
}
int count(int t)
{
int ret=;
for(int i=;i<=n;++i)
{/*枚举A中的每一个数字,寻找a[i]*b[j]>t的边界*/
ret+=find(,n,t,a[i]);
}
return ret;
}
int solve(int l,int r)
{
int mid,ans;
int t1,t2;
while(l<=r)
{
mid=(l+r)>>;
t1=count(mid);//统计大于mid值的个数
t2=count(mid-);//统计大于等于mid值的个数
if(t1<k&&t2>=k)/*为什么不是t1==k-1&&t2==k,是为了处理数据重复的情况,这时一定符合t1<k&&t2>=k*/
{
ans=mid;/*找到答案直接跳出*/
break;
}
else if(t2<k) r=mid-;
else l=mid+;
}
return ans;
}
int main()
{
tast=read();
while(tast--)
{
n=read();k=read();
for(int i=;i<=n;++i)
a[i]=read();
for(int i=;i<=n;++i)
b[i]=read();
sort(a+,a+n+,cmp);
sort(b+,b+n+,cmp);
printf("%d\n",solve(a[n]*b[n],a[]*b[]));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
}
return ;
}
二分套二分 hrbeu.acm.1211Kth Largest的更多相关文章
- poj3579 二分套二分
和poj3685类似,都是二分答案然后在判断时再二分 这题的内层二分可以用stl代替 /* 二分套二分,思路:升序排序数据,先二分答案x进行判断,判断时枚举每个元素,二分找到和其之差小于等于x的所有值 ...
- POJ-3579 Median---二分第k大(二分套二分)
题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m ...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- poj 3685 Matrix 二分套二分 经典题型
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 5724 Accepted: 1606 Descriptio ...
- poj3685 二分套二分
F - 二分二分 Crawling in process... Crawling failed Time Limit:6000MS Memory Limit:65536KB 64bit ...
- Matrix [POJ3685] [二分套二分]
Description 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. Input 第一行输 ...
- 51nod 1105(第K大数 二分套二分)
题目链接:http://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=620811 参考自:https://blog.csdn.net/f_ ...
- POJ 3685 Matrix (二分套二分)
Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8674 Accepted: 2634 Descriptio ...
- Gym 101064 D Black Hills golden jewels 【二分套二分/给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合】
D. Black Hills golden jewels time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- jQuery Flipping Gallery 翻转画廊
在线实例 简单配置 翻转方向 鼠标滚动 自动播放 绑定事件 使用方法 <div class="main"> <div class="page_conta ...
- [原创]html5_PC游戏_图片俄罗斯方块
PC游戏_图片俄罗斯方块 以前的了,快一年了... 使用了离线canvas复制的方法,启动预览效果需要服务器支持 另外,AC娘图片可以自己做加载功能,这样游戏图片显示更顺畅 效果: --- 代码: h ...
- copy 和 strong(或retain)的区别
http://stackoverflow.com/questions/18526909/whether-i-should-use-propertynonatomic-copy-or-propertyn ...
- Vue混合
gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson13 一 定位 混合以一种灵活的方式为组件提供分布复用功能.混合对象 ...
- Win10的分辨率问题
个人觉得win10扁平化的界面给人全新的感觉,但安装后,发现分辨率只有1280x720.1152x864.1024x768(推荐).800x600,不管调整哪一个,都觉得分辨率还是有问题,看起来字体. ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q111-Q114)
Question 111You create a custom page layout that contains the following code segment. (Line numbers ...
- 适配iPhone6和iPhone6 Plus
先对比所有市面上的iPhone设备,然后分析如何适配新的设备, iPhone4,iPhone4s 分辨率960*640 长宽比1.5iPhone5,iPhone5s 分辨率1136*640 ...
- Android中TextView中的文字设置为不同颜色
questionDesTextView=(TextView)findViewById(R.id.question_des); SpannableStringBuilder builder = new ...
- iOS-RegexKitLite导入错误
RegexKitLite是什么? RegexKitLite是一个非常方便的处理正则表达式的第三方类库. 本身只有一个RegexKitLite.h和RegexKitLite.m 导入RegexKitLi ...
- runtime之消息转发
前言 在上一篇文章中我们初尝了runtime的黑魔法,可以在程序编译阶段就获取到成员变量的名字,特性以及动态的给对象增加属性等等,在接下来中我们进一步了解OC的消息发送机制.如果之前没接触过runti ...