二分套二分 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 ...
随机推荐
- Studio for WPF:使用 C1TileView 创建图片库
C1TileView 提供了数据交互浏览的功能.允许我们设置最大化和最小化浏览模板,我们可以通过最小化模板快速定位详细浏览选项. 下面我们分步分享实现方法: 1.添加 C1TileView 到窗体,并 ...
- java ftp
FTPUtil import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import jav ...
- servlet中的转发和重定向问题
重定向和请求转发在学习servlet的时候很容易混淆,故在此特意记录. 1. 重定向---------sendRedirect()方法 Servlet响应请求有两种方式,一个是重定向,返回一个页面给客 ...
- python peewee.ImproperlyConfigured: MySQLdb or PyMySQL must be installed.
最近在学习Python,打算先看两个在线教程,再在github上找几个开源的项目练习一下,在学到“被解放的姜戈”时遇到django同步数据库时无法执行的错误,记录一下. 错误现象: 执行python ...
- 开启Windows Server 2008 R2上帝模式
TAG标签: 摘要:这个“God Mode” 应该大部分的网友都听过了,只是在 Windows Server 2008 R2 上也支持此一功能.启用方式非常简单,在桌面新建一个文件夹,命名为: God ...
- js事件绑定
事件绑定,常见的是odiv.onclick=function(){..........}; 这种方式绑定事件太单一,如果绑定多个,那么最后一个事件会覆盖掉之前的,也就是说只执行最后一次绑定的事件,这 ...
- CRUD Operations in MVC4 Using AngularJS and WCF REST Services
Now in this article I will show how to do Create, Retrieve, Update and Delete (CRUD) operations in M ...
- EntityFramework4.1开发
常见问题大概为这几个 一.ef4.1 codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题. 二.ef4.1 没有了edmx等复杂的东西 变得简单 干 ...
- 实验12:Problem I: 成绩排序
Home Web Board ProblemSet Standing Status Statistics Problem I: 成绩排序 Problem I: 成绩排序 Time Limit: 1 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 --索引目录
Sharepoint学习笔记—习题系列--70-573习题解析 为便于查阅,这里整理并列出了我前面播客中的关于70-573习题解析系列的所有问题,有些内容可能会在以后更新, ...