ACM: 限时训练题解-Runtime Error-二分查找
- Runtime Error
Bahosain was trying to solve this simple problem, but he got a Runtime Error on one of the test cases, can you help him by solving it?
Given an array of N non-negative integers and an integer K, your task is to find two integers X and Y from the given array such that X × Y = K.
The chosen numbers must have different indices in the array.
Input
The first line of input contains T (1 ≤ T ≤ 128), the number of test cases.
The first line of each test case contains two integers: N (2 ≤ N ≤ 100,000) and K (1 ≤ K ≤ 100,000). The next line contains N space-separated integers, each between 0 and 100,000.
Output
For each test case, if there is no solution, print -1 on a single line. Otherwise print a single line with two space-separated integers X Y (X ≤ Y), where X and Y are two numbers from the given array and X × Y = K.
If there is more than one possible solution, print the one with the minimum X.
|
Sample Input |
Sample Output |
||
|
4 |
2 6 |
||
|
6 12 |
-1 |
||
|
3 6 2 |
4 2 |
9 |
3 12 |
|
2 1 |
1 12 |
||
|
1 2 |
|||
|
4 36 |
|||
|
12 18 |
3 36 |
||
|
4 12 |
|||
|
1 2 6 |
12 |
||
/*
题意:
从给出的N个数中找出两个数,乘积为 K; 枚举x 二分搜索 y
*/ #include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define MX 100000 + 50
using namespace std; int a[MX]; int main() {
int T,k,n;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&k);
for(int i=0; i<n; i++) {
scanf("%d",&a[i]);
}
sort(a,a+n);
int ans1=0,ans=0;
for(int i=0; i<n-1; i++) {
ans1=i; int l=i+1,r=n-1,mid;
while(l<=r) {
mid=(r+l)/2;
if(a[i]*a[mid]>k) {
r=mid-1;
} else if(a[i]*a[mid]<k) {
l=mid+1;
} else if(a[i]*a[mid]==k){
ans=mid;
break;
}
}
if(ans)break; }
if(ans)
printf("%d %d\n",a[ans1],a[ans]);
else printf("-1\n");
}
return 0;
}
ACM: 限时训练题解-Runtime Error-二分查找的更多相关文章
- ACM: 限时训练题解-Rock-Paper-Scissors-前缀和
Rock-Paper-Scissors Rock-Paper-Scissors is a two-player game, where each player chooses one of Roc ...
- ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举
Heavy Coins Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always ...
- ACM: 限时训练题解- Travelling Salesman-最小生成树
Travelling Salesman After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...
- ACM: 限时训练题解-Epic Professor-水题
Epic Professor Dr. Bahosain works as a professor of Computer Science at HU (Hadramout Universit ...
- ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】
Street Lamps Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- [ACM] poj 1064 Cable master (二分查找)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
- what the fuck!(二分查找 / 暴力模拟)
what the fuck! Description 现在有一家公司有nnn个员工(nnn为奇数),他们的工资发放是基本工资+提成,现在这家公司计划再招一批人.要写一篇招聘启事,但是对于这个招聘启事中 ...
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- MVC – 4.mvc初体验(2)
5.显示学员列表 效果 数据表 5.1 首先,在文件夹Models新建一个新建项(W),选择ADO.NET 实体数据模型 (SingleTest.edmx) 5.2 建一个控制器,StudentsCo ...
- Windows 8使用这半年(企业版)
2014-06-23 硬件:thinkpad e430c core i5-3210m 4g/500g 1.Windows 8出现开机引导问题 主要开机的时候提示缺少引导文件,按ctrl+alt+del ...
- WMSYS.WM_CONCAT 函數的用法
select t.rank, t.Name from t_menu_item t; 10 CLARK 10 KING 10 MILLER 20 ADAMS 20 FORD ...
- ArcGIS图层和要素的过滤显示
ArcGIS可以设置动态地图服务(ArcGISDynamicMapServiceLayer)显示哪些图层,也可以设置每个图层根据某个属性字段的某些条件来进行过滤显示. 1.设置显示的图层 主要是通过A ...
- MD5与Base64的思考
MD5加密是对任意长的数据使用MD5哈稀算法散列为4个32位组,若格式化为ASCII字符则为16字符,若格式化16进制表示,则为32字符. (MD5的具体算法请参阅相关书籍和资料) MD5广泛用于数 ...
- 1080P、720P、4CIF、CIF所需要的理论带宽
转自:http://blog.sina.com.cn/s/blog_64684bf30101hdl7.html 在视频监控系统中,对存储空间容量的大小需求是与画面质量的高低.及视频线路等都有很大关系. ...
- WPF之MVVM(Step1)——自己实现ICommand接口
开发WPF应用程序,就不得不提MVVM.下面偶将展示MVVM中简单的实现,其中主要在于ICommand的实现上,不过这种实现方式,应该不会有多少人在开发中使用,在此仅作学习使用. 准备: 界面绘制,简 ...
- Java Server returned HTTP response code: 401
今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的.写好之后请求,返回异常Java Server returned HTTP response code: ...
- WPF标准控件模板查看程序(文件里面)
xaml <Window x:Class="ControlTemplateBrowser.MainWindow" xmlns="http://schemas.mic ...
- Shadow SSDT详解、WinDbg查看Shadow SSDT
一.获取ShadowSSDT 好吧,我们已经在R3获取SSDT的原始地址及SDT.SST.KiServiceTbale的关系里面提到:所有的SST都保存在系统服务描述表(SDT)中.系统中一共有两个S ...