Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2141
Search for the X value that meets the condition.
Here is a simple introduction to a small algorithm, binary search.
1/* 2 3 x ^ 2 + 6 * x-7 = Y 4 input Y x accuracy is 10 ^-5 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; // define the boundary in the given range, one left and one right 19 while (R-l> 0.00001) // Accuracy Problem 20 {21 x = (L + r)/2; // second point to save the calculation times and time 22 double YY; 23 YY = x * x + 6 * x-7; 24 if (yy <Y) 25 L = x + 0.00001; // select the right half of the range 26 else27 r = x; 28} 29 cout <L <Endl; 30} 31 return 0; 32}
View code
The following is a solution to hdu2141;
First, convert the original form into ai + bj = x-ck. Then, divide the 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 else59 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 }