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 ...
随机推荐
- vpn,可以连接上,但是不能访问局域网内共享的文件怎么办
不选用VPN的上网关,就可以识别域用户访问共享文件的权限了,在VPN连接的属性里双击TCP/IP协议-高级-去掉勾选"在远程网络上使用默认网关".
- 数据结构和算法 – 4.字符串、 String 类和 StringBuilder 类
4.1.String类的应用 class String类应用 { static void Main(string[] args) { string astring = "Now is The ...
- 与你相遇好幸运,Tippecanoe在Centos下の安装
全新的CentOS 7 x86_64 安装编译工具 yum install -y gcc automake autoconf libtool make yum insyall -y gcc gcc-c ...
- 在ubuntu上搭建开发环境4---ubuntu简单的搭建LAMP环境和配置
最近重新安装了Ubuntu,但是之前的LAMP环境自然也就没有了,实在是不想再去编译搭建LAMP环境(这种方法实在是太费时间,而且太容易遇到各种不知道为什么的错误),所以,就去查查有没有什么简单的搭建 ...
- Delphi中uses在interfeace和implementation中的区别
use单元引入分为在interface中引入,如 interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Co ...
- UML- 模型图介绍
第一类 用例图 第二类 静态图 类图 对象图 包图第三类 行为图 状态图 活动图第四类 交互图 序列图 协助图第五类 实现图 构件图 部署图 1 用例图:从用户角度描述系统功能,以及每个系统功 ...
- Android图像处理实例教程
Android图像处理实例教程 原始出处 http://vaero.blog.51cto.com/4350852/856750
- loj 1377 (bfs)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1377 思路:这道题只要处理好遇到"*"这种情况就可以搞定了.我们可 ...
- 简单解释Windows如何使用FS段寄存器
详见附件 jpg改rar
- Arduino101学习笔记(九)—— 中断函数
1.设置中断函数 //***************************************************************************************** ...