时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
给出n个整数和x,请问这n个整数中是否存在三个数a,b,c使得ax2+bx+c=0,数字可以重复使用。
输入描述:
第一行两个整数n,x
第二行n个整数a[i]表示可以用的数
1 <= n <= 1000, -1000 <= a[i], x <= 1000输出描述:
YES表示可以
NO表示不可以
示例1
输入2 1
1 -2
输出YES
虽然只AC了这么一道,能AC一道是一道了。
刚开始想到是用三重暴力循环来求,不过很明显超时了。所以就用二重循环,然后用二分找方程中与c相反的值
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int search_BIN(int a[],int key,int n)//二分
{
int low=1;
int high=n;
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
return 1;
else if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
return 0;
}
int main()
{
int n,x;
int sum=0;
int flag=0;
int c;
int a[9999]={0};
cin>>n>>x;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
sum=a[i]*x*x+a[j]*x;
if(-sum>a[n-1]||-sum<a[0])
continue;
c=search_BIN(a,-sum,n);
if(c==1)
{
flag=1;
break;
}
}
if(flag)
break;
}
if(flag==0)
{
cout<<"NO\n";
}
else{
cout<<"YES\n";
}
return 0;
}
本文介绍了一种在给定整数集合中寻找三个数以满足特定方程的方法。通过使用二分查找技术优化了原始的暴力搜索算法,有效提高了算法效率。
529

被折叠的 条评论
为什么被折叠?



