题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141

题目大意:查找是否又满足条件的x值。

这里简单介绍一个小算法,二分查找。

 /*

  x^2+6*x-7==y
输入y 求x 精确度为10^-5
0=<x<=10000 */
#include <iostream>
#include <cstdio>
using namespace std;
int main (void)
{
double y;
while(cin>>y)
{
double l,r,x;
l=;
r=;//在所给的区间定义边界,一左一右
while(r-l>0.00001)//精确度的问题
{
x=(l+r)/;//二分来节省计算的次数和时间
double yy;
yy=x*x+*x-;
if(yy<y)
l=x+0.00001;//选取右半部分区间
else
r=x;
}
cout<<l<<endl;
}
return ;
}

以下是对hdu2141的解法;

先把原式化为Ai+Bj = X-Ck.然后在对Ai+Bj 进行二分。

 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; __int64 s[]; int main ()
{
int l,n,m,T=;
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
int num=;
__int64 a[],b[],c[];
for (int i=; i<l; i++)
scanf("%I64d",&a[i]);
for (int j=; j<n; j++)
scanf("%I64d",&b[j]);
for (int k=; k<m; k++)
scanf("%I64d",&c[k]);
int t;
scanf("%d",&t);
printf("Case %d:\n", T++);
for (int i=; i<l; i++)
for (int j=; j<n; j++)
s[num++]=a[i]+b[j];
sort(s,s+num);
//sort(a,a+l);
//sort(b,b+n);
sort(c,c+m);
while (t--)
{
__int64 x;
scanf("%I64d",&x);
if(x>s[num-]+c[m-]||x<s[]+c[])
{
printf("NO\n");
continue;
}
int flag=;
__int64 p;
for (int k=; k<m; k++)
{
p=x-c[k];
int cc;
int lz=,r=num-;//cout<<r<<endl;
while(r>lz)
{
cc=(r+lz)/;
if (s[cc]<p)
lz=cc+;
else if(s[cc]==p)
{
flag=;
printf ("YES\n");
break;
}
else
r=cc-;
}
if(flag==)
break;//cout<<k<<endl;
if (p==s[r])
{ flag=;
printf("YES\n");
break;
} }
if(flag==)
printf ("NO\n"); }
}
}

hdu 2141 Can you find it?(二分查找)的更多相关文章

  1. C - 啥~ 渣渣也想找玩数字 HDU - 2141(有序序列枚举 + 二分优化查找)

    题目描述 可爱的演演又来了,这次他想问渣渣一题... 如果给你三个数列 A[],B[],C[],请问对于给定的数字 X,能否从这三个数列中各选一个,使得A[i]+B[j]+C[k]=X? 输入 多组数 ...

  2. hdu 4190 Distributing Ballot Boxes(贪心+二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/1000 ...

  3. HDU 2141 Can you find it? [二分]

    Can you find it? Give you three sequences of numbers A, B, C, then we give you a number X. Now you n ...

  4. Holedox Eating HDU - 4302 2012多校C 二分查找+树状数组/线段树优化

    题意 一个长度$n<=1e5$的数轴,$m<=1e5$个操作 有两种一些操作 $0$  $x$ 在$x$放一个食物 $1$ 一个虫子去吃最近的食物,如果有两个食物一样近,不转变方向的去吃 ...

  5. HDU 5265 pog loves szh II (二分查找)

    [题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog love ...

  6. HDU 3280 Equal Sum Partitions(二分查找)

    Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. hdu 2141:Can you find it?(数据结构,二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  8. hdu 2141 Can you find it?(二分查找变例)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

  9. Can you find it? HDU - 2141 (二分查找)

    Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate ...

随机推荐

  1. 从循环里面用QPixmap new对象很耗时联想到的

    1.在循环里面用QPixmap new图片对象延迟很高,这个是通过打时间日志得出的,深层原因还不清楚: 2.自制的图片浏览器在初始化的时候会初始化自己的一个图片列表,所以要用到上面的描述.所有图片的初 ...

  2. Linux上Makefile管理java项目

    前面文章讲到了Linux上通过.spec文件与rpmbuild命令将java程序打包为RPM安装包, 现阶段遇到新的需求: 使用Makefile来操纵java的编译.打包 该需求以前面的内容为基础 可 ...

  3. quartz 使用总结

    quartz是一个任务调度框架,具体的用途比如说,我想我的程序在每天的3点干什么事,每隔多长时间做一件什么事.quartz框架就可以完美地解决这些. 1.xml配置方式 首先我是用spring来管理的 ...

  4. hdu1506 Largest Rectangle in a Histogram

    Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...

  5. cocos2d-x 动作类

    动作类是Action IntervalAction是间隔动作,InstantAction是瞬时动作. 动作的管理是要由节点负责的,任何的节点都可以管理节点,如精灵.菜单.层.甚至场景都可以管理动作.节 ...

  6. Swiper 常用功能及配置清单

    内容来源于Swiper中文在线(http://www.swiper.com.cn/),由于Swiper功能强大,这里只将常用的功能列出来,方便开发. 这里统一使用Swiper最新版 4.0做为演示! ...

  7. java 读取配置文件 与更新

    笔记 public class Config { private static Properties props = new Properties(); static File configFile ...

  8. ECharts 上传图片Example

    前端 1.为ECharts准备一个div <div id="main" style="Height:400px"></div> 2.引入 ...

  9. Gym101981I Magic Potion(最大流)

    Problem I. Magic Potion There are n heroes and m monsters living in an island. The monsters became v ...

  10. sqlserver 找出字符第N次出现的位置

    [1编写函数]CREATE FUNCTION IndexOf(@str VARCHAR(500),@value VARCHAR(50),@posIndex INT)RETURNS int AS BEG ...