hdu 2141 Can you find it?(二分查找)

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

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

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

 1 /*
 2
 3  x^2+6*x-7==y
 4  输入y   求x   精确度为10^-5
 5  0=<x<=10000
 6
 7  */
 8 #include <iostream>
 9 #include <cstdio>
10 using namespace std;
11 int main (void)
12 {
13     double y;
14     while(cin>>y)
15     {
16         double l,r,x;
17         l=0;
18         r=10000;//在所给的区间定义边界,一左一右
19         while(r-l>0.00001)//精确度的问题
20         {
21             x=(l+r)/2;//二分来节省计算的次数和时间
22             double yy;
23             yy=x*x+6*x-7;
24             if(yy<y)
25                 l=x+0.00001;//选取右半部分区间
26             else
27                 r=x;
28         }
29         cout<<l<<endl;
30     }
31     return 0;
32 }

以下是对hdu2141的解法;

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 __int64 s[250100];
 7
 8 int main ()
 9 {
10     int l,n,m,T=1;
11     while(scanf("%d%d%d",&l,&n,&m)!=EOF)
12     {
13         int num=0;
14         __int64 a[1000],b[1000],c[1000];
15         for (int i=0; i<l; i++)
16             scanf("%I64d",&a[i]);
17         for (int j=0; j<n; j++)
18             scanf("%I64d",&b[j]);
19         for (int k=0; k<m; k++)
20             scanf("%I64d",&c[k]);
21         int t;
22         scanf("%d",&t);
23         printf("Case %d:\n", T++);
24         for (int i=0; i<l; i++)
25             for (int j=0; j<n; j++)
26                 s[num++]=a[i]+b[j];
27         sort(s,s+num);
28         //sort(a,a+l);
29         //sort(b,b+n);
30         sort(c,c+m);
31         while (t--)
32         {
33             __int64 x;
34             scanf("%I64d",&x);
35             if(x>s[num-1]+c[m-1]||x<s[0]+c[0])
36             {
37                 printf("NO\n");
38                 continue;
39             }
40             int flag=0;
41             __int64 p;
42             for (int k=0; k<m; k++)
43             {
44                 p=x-c[k];
45                 int cc;
46                 int lz=0,r=num-1;//cout<<r<<endl;
47                 while(r>lz)
48                 {
49                     cc=(r+lz)/2;
50                     if (s[cc]<p)
51                         lz=cc+1;
52                     else if(s[cc]==p)
53                     {
54                         flag=1;
55                         printf ("YES\n");
56                         break;
57                     }
58                     else
59                         r=cc-1;
60                 }
61                 if(flag==1)
62                     break;//cout<<k<<endl;
63                 if (p==s[r])
64                 {
65
66                     flag=1;
67                     printf("YES\n");
68                     break;
69                 }
70
71             }
72             if(flag==0)
73                 printf ("NO\n");
74
75         }
76     }
77 }

hdu 2141 Can you find it?(二分查找)

时间: 2024-10-03 13:09:50

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

HDU 2141 Can you find it? 二分查找

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 21485    Accepted Submission(s): 5446 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

hdu 4938 Seeing People 排序+二分查找

Seeing People Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 61 Problem Description There are two kinds of people. If person i is the first kind of people, it

hdu 4715 Difference Between Primes (二分查找)

Problem Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, skywind present a new conjecture: every even integer can be expressed as the difference of two pr

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1065    Accepted Submission(s): 536 Problem Description Today, bes

HDU - 2141 Can you find it?(二分)

题目链接:点我点我 题意:给出L个A,N个B,M个C,然后S个X,求能否找出解使得Ai+Bj+Ck = X.成立 题解:TLE了一晚上.这道题其实就是个暴力二分,前把三组数中任意两组数先合并,只不过最后的时候转变一下思想,不要直接去求使X成立的条件, 而是反过去把X当作条件(但是有些大神直接set+set的find()函数过了,当我没说,(捂脸逃....) 1.巧妙暴力二分 1 #include <cstdio> 2 #include <algorithm> 3 using nam

HDU 3280 Equal Sum Partitions(二分查找)

Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the

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

[题目链接]click here~~ [题目大意]在给定 的数组里选两个数取模p的情况下和最大 [解题思路]: 思路见官方题解吧~~ 弱弱献上代码: Problem : 5265 ( pog loves szh II ) Judge Status : Accepted RunId : 13961817 Language : G++ Author : javaherongwei Code Render Status : Rendered By HDOJ G++ Code Render Versio

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 you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. Input There are many cases. Every data case

二分查找 HDOJ 2141 Can you find it?

题目传送门 1 /* 2 题意:给出一个数,问是否有ai + bj + ck == x 3 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAX