二分套二分 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 ...
随机推荐
- Hadoop Pipes Exception: Illegal text protocol command
Hadoop Pipes Exception: Illegal text protocol command 对于Hadoop pipes 出现这样的错误,基本上编译代码依赖的.so和.a 版本不匹配 ...
- activiti 工作流
1. 工作流的概念 工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实 ...
- 一个SpringMVC简单Demo中出现的错误
最近在学springmvc 一个简答的Springmvc配置包括如下步骤: 1.在 web.xml 文件中配置 DispatcherServlet (该中央控制器相当于 MVC 模式中的 C),还可以 ...
- pygame for python3.3
pygame的更新慢的令人发指,我最初使用的python是3.4版本的,无何奈何pygame不支持3.4,甚至官网只有3.2版本的.我于是将各种版本试了一遍,出现各种问题,同时我比较钟爱3.x版本,最 ...
- java.util.TimeZone 新加方法 getTimeZone(ZoneId zoneId) 导致的问题
最近接受的项目中用spring配置了一个TimeZone对象: <bean id ="timeZone4Job" class="java.util.TimeZone ...
- C#枚举类型和结构体
注意:枚举类型和结构体都属于值类型. 结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 一.定义的方法: struct student { public int nianl ...
- ASP.NET MVC下判断用户登录和授权的方法
日常开发的绝大多数系统中,都涉及到管理用户的登录和授权问题.登录功能(Authentication),针对于所有用户都开放:而授权(Authorization),则对于某种用户角色才开放. 在asp. ...
- SHAREPOINT 工作流审批权限问题
继续我们上次的工作流,我们发现所有人都有审批权限,这和我们正常的逻辑相反,正常应该是只有审批人才有权限,其它人只能查看,如下 这样解决,同样为SpecialPermissions 绑定到新成员 > ...
- arcgis union 0x80040218
我利用ArcGis中的union工具将两张图层叠加时,系统总是提示失败,这是什么原因?希望高手能够解决这个问题.图片是系统提示,表示看不懂哪里出错了? 你必须使用数据管理-要素-修复几何,源数据存在不 ...
- 实验12:Problem D: 判断两个圆之间的关系
Home Web Board ProblemSet Standing Status Statistics Problem D: 判断两个圆之间的关系 Problem D: 判断两个圆之间的关系 T ...