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 ...
随机推荐
- iis 7.5应用程序池自动停止
今天在我的windows7旗舰版上配置iis7 (Internet Information Server)失败,一直未能启动服务,访问本地网络提示"Service Unavailable H ...
- 【转载】python super的用法
转载地址: http://blog.csdn.net/cxm19830125/article/details/20610533 super的用法是调用继承类的初始化方法,如下面的代码: class A ...
- HDU1899 Sum the K-th's(树状数组)
枚举,每次增加点,删除点 #include<cstdio> #include<iostream> #include<cstdlib> #include<cst ...
- php的时间输出格式
php中时间一般分为两种格式,一种是标准时间格式timestamp,即Y-m-d G:i:s.另一种就是时间戳. 例如: 一.标准时间与时间戳转换: //获得服务端系统时间 date_default_ ...
- apache https配置步骤
转自:http://www.cnblogs.com/best-jobs/p/3298258.html 1. 确认是否安装ssl模块 是否有mod_ssl.so文件 2. 生成证书和密钥 linux ...
- AIX下禁止crs随ha启动而启动
/etc/init.crs enable /etc/init.crs disable 查看目前crs是enable还是disable状态 状态记录在一个文本文件里 /etc/oracle/scls_ ...
- 第三篇:用SOUI能做什么?
SOUI-DEMO界面预览 在回答SOUI能做什么之前,先看看SVN中demo工程的界面截图: 使用SOUI实现上面的界面主要的工作全在配置几个XML文件,基本不需要写C++代码.(如何配置XML布局 ...
- [unity3d插件]2dtoolkit系列一 创建精灵
从今天开始要做一个2d游戏,由于之前都是做cocos2dx的,然后接触了一段时间的unity3d,都是做3D方面的东西,得知要做2d游戏还是有点开心的,或许因为不想丢失之前的2d游戏的一些思想,然后接 ...
- Cygwin的安装,卸载,以及安装gdb
转载来源 http://10000001.blog.51cto.com/4600383/1341484 1.安装 其实Cygwin的安装时很简单的,需要的安装相应的就可以了,要详细的去网上找,很多 ...
- 三层+MVC导出Excel(2)
背景: 出门在外,一切以健康为主,学习为辅,健康搞好了,学习也不能拉下,在外工作期间,我们在做数据导出的时候,自己封了一个类,利用NPOI进行数据导出Excel,自我感觉良好,特给大家分享一下,希望对 ...