Advanced Search Techniques

1,329 views

Published on

Advanced Search Techniques

Published in: Education
6 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total views
1,329
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
81
Comments
6
Likes
2
Embeds 0
No embeds

No notes for slide

Advanced Search Techniques

  1. 1. Advanced Search TechniquesMd. Shakil Ahmed.
  2. 2. Topic• Binary search• Bisection• Ternary Search
  3. 3. Binary search algorithm• binary search or half-interval search finds the position of a specified value within a sorted array.• In each step, the algorithm compares the input key value with the key value of the middle element of the array.• If the keys match, then a matching element has been found so its index, or position, is returned.• Otherwise, if the sought key is less than the middle elements key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the input key is greater, on the sub-array to the right.• If the remaining array to be searched is reduced to zero, then the key cannot be found in the array and a special "Not found" indication is returned.
  4. 4. Iterativeint binary_search(int A[], int key, int imin, int imax){while (imax >= imin) {int imid = (imin + imax) / 2;if (A[imid] < key)imin = imid + 1;else if (A[imid] > key )imax = imid - 1;else return imid;}return KEY_NOT_FOUND;}
  5. 5. Recursiveint binary_search(int A[], int key, int imin, int imax) {if (imax < imin) return KEY_NOT_FOUND;else {int imid = (imin + imax) / 2;if (A[imid] > key)return binary_search(A, key, imin, imid-1);else if (A[imid] < key)return binary_search(A, key, imid+1, imax);Elsereturn imid;}}
  6. 6. Performance• The worst case is Log2(N).
  7. 7. 1088 - Points in Segments• Given n points (1 dimensional) and q segments, you have to find the number of points that lie in each of the segments. A point pi will lie in a segment A B if A ≤ pi ≤ B.• For example if the points are 1, 4, 6, 8, 10. And the segment is 0 to 5. Then there are 2 points that lie in the segment.
  8. 8. Input• input starts with an integer T (≤ 5), denoting the number of test cases.• Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers denoting the points in ascending order. All the integers are distinct and each of them range in [0, 108].• Each of the next q lines contains two integers Ak Bk (0 ≤ Ak ≤ Bk ≤ 108) denoting a segment.
  9. 9. Output• For each case, print the case number in a single line. Then for each segment, print the number of points that lie in that segment.
  10. 10. Solutionint binary_search (int key){ int start, end,middle; while(middle+1<=n && A[middle+1]<= key)start = 0; middle++;end = n;middle = (start+ end)/2; while(middle-1>=0 && A[middle-1]> key) middle--;while(start<= end){ return middle;if(Array[middle]== key) }break;else if(A[middle]> key)end = middle - 1;elsestart = middle + 1;middle = (start+ end)/2;}
  11. 11. Solutionscanf("%d %d",&n,&q);A[0]=-1000;for(i=1;i<=n;i++)scanf("%d",&A[i]);for(q1=1;q1<=q;q1++){scanf("%d %d",&x,&y);printf("%dn", binary_search(y)- binary_search(x-1));}
  12. 12. 1043 - Triangle Partitioning• You are given AB, AC and BC. DE is parallel to BC. You are also given the area ratio between ADE and BDEC. You have to find the value of AD.
  13. 13. Input• Input starts with an integer T (≤ 25), denoting the number of test cases.• Each case begins with four real numbers denoting AB, AC, BC and the ratio of ADE and BDEC (ADE / BDEC). You can safely assume that the given triangle is a valid triangle with positive area. OutputFor each case of input you have to print the casenumber and AD. Errors less than 10-6 will be ignored.
  14. 14. Solutiondouble Area(double x1, double y1, double z1){ double s = (x1 + y1 + z1)/2; return sqrtl(s * (s-x1) * (s-y1) * (s-z1));}scanf("%lf %lf %lf %lf",&ab,&ac,&bc,&ratio);area = Area(ab,ac,bc);start = 0;end = ab;previous = -100;middle = (start+end)/2;
  15. 15. Solutionwhile(start<=end) previous = middle - previous;{ if(previous<0)ab1 = middle; previous = - previous;ac1 = (ac * middle)/ab;bc1 = (bc * middle)/ab; if(previous<0.000001) break;area1 = Area(ab1,ac1,bc1);area2 = area - area1; previous = middle; }if(area1/area2>= ratio) printf("Case %d: %lfn",caseNumber,middle);end = middle;elsestart = middle;middle = (start+end)/2;
  16. 16. Similar• 1048 - Conquering Keokradong• 1056 - Olympics• 1076 - Get the Containers• 1138 - Trailing Zeroes (III)• 1276 - Very Lucky Numbers
  17. 17. Bisection• Bisection -> two section• Divide elements to 2 part.• Then sort one part & use binary search.
  18. 18. 1127 - Funny Knapsack• Given n integers and a knapsack of weight W, you have to count the number of combinations for which you can add the items in the knapsack without overflowing the weight.• Input• Input starts with an integer T (≤ 100), denoting the number of test cases.• Each case contains two integers n (1 ≤ n ≤ 30) and W (1 ≤ W ≤ 2 * 109) and the next line will contain n integers separated by spaces. The integers will be non negative and less than 109.• Output• For each set of input, print the case number and the number of possible combinations.
  19. 19. Solutionscanf("%d %d",&n,&k); for(i=0;i<n;i++) for(i=m;i<n;i++) scanf("%lld",&X[i]); { N3=N2; m = n/2; for(j=0;j<N2;j++) A[0]=0; { N1=1; B[N3]=B[j]+X[i]; B[0]=0; N3++; N2=1; } for(i=0;i<m;i++) N2=N3; { } N3=N1; for(j=0;j<N1;j++) sort(B,B+N2); { A[N3]=A[j]+X[i]; N3++; } N1=N3; }
  20. 20. Solutiontotal = 0; while(B[mid]>t) mid--; for(i=0;i<N1;i++) if(k>=A[i]) { while(mid+1<N2 && B[mid+1]<=t) t = k-A[i]; mid++; int left = 0; int right = N2 - 1; total += (mid+1); mid = (left+right)/2; } while(left<=right) { printf("Case %d: %dn",caseNumber,total); if(B[mid]==t) break; else if(B[mid]>t) right=mid-1; else left = mid+1; mid = (left+right)/2; }
  21. 21. Similar• 1235 - Coin Change (IV)
  22. 22. Ternary Search• A ternary search algorithm is a technique for finding the minimum or maximum of a unimodal function.
  23. 23. Recursive algorithmdef ternarySearch(f, left, right, absolutePrecision):if (right - left) < absolutePrecision:return (left + right)/2leftThird = (2*left + right)/3rightThird = (left + 2*right)/3if f(leftThird) < f(rightThird):return ternarySearch(f, leftThird, right, absolutePrecision)else:return ternarySearch(f, left, rightThird, absolutePrecision)
  24. 24. 1146 - Closest Distance• Two men are moving concurrently, one man is moving from A to B and other man is moving from C to D. Initially the first man is at A, and the second man is at C. They maintain constant velocities such that when the first man reaches B, at the same time the second man reaches D. You can assume that A, B, C and D are 2D Cartesian co-ordinates. You have to find the minimum Euclidean distance between them along their path.
  25. 25. • Input• Input starts with an integer T (≤ 1000), denoting the number of test cases.• Each case will contain eight integers: Ax, Ay, Bx, By, Cx, Cy, Dx, Dy. All the co-ordinates are between 0 and 100. (Ax, Ay) denotes A. (Bx, By) denotes B and so on.• Output• For each case, print the case number and the minimum distance between them along their path. Errors less than 10-6 will be ignored.
  26. 26. Solutionvoid makePoint(double h1, double h2){double h3 = (ax - h1)*(ax - h1) + (ay - h2) * (ay - h2);h3 = sqrt(h3);double h4 = dis - h3;mx = (cx * h4 + dx * h3)/dis;my = (cy * h4 + dy * h3)/dis;}double distance(double h1, double h2, double h3, double h4){double h5 = (h1-h3)*(h1-h3) + (h2-h4)*(h2-h4);return sqrt(h5);}
  27. 27. Solutiondis = (ax - bx)*(ax - bx) + (ay - by) * (ay - by);dis = sqrt(dis);min = (ax - cx)*(ax - cx) + (ay - cy) * (ay - cy);min = sqrt(min);leftx = ax;lefty = ay;rightx = bx;righty = by;while(1){ rightThirdx = (leftx + 2*rightx)/3;leftThirdx = (2*leftx + rightx)/3; rightThirdy = (lefty + 2*righty)/3;leftThirdy = (2*lefty + righty)/3; makePoint(rightThirdx,rightThirdy);makePoint(leftThirdx,leftThirdy); disRight =disLeft = distance(leftThirdx,leftThirdy,mx,my); distance(rightThirdx,rightThirdy,mx,my);
  28. 28. Solutionif(disLeft<=disRight) min2 = min - min1; { if(min2<0) min2=-min2; rightx = rightThirdx; if(min2<0.0000000001) righty = rightThirdy; break; min1 = disLeft; min = min1; } } else printf("Case %ld: %lfn",cas1,min); { leftx = leftThirdx; lefty = leftThirdy; min1 = disRight; }
  29. 29. Similar• 1240 - Point Segment Distance (3D)
  30. 30. Thanks

×