The usefulness of the two points is too large, whether it is to find a simple equation, or to find the best solution is a good problem-solving thinking.
As long as the linear, sequential or ordered data can be used to find the best answer in two, and the average time is O (log2 N). The topic seems to be hdu 4190, the limit of the problem is 10000ms, and two points to do just when 1000ms, its advantages can be imagined.
However, as the "programming Zhu Ji Nanxiong" said, although the two points of thinking and practice is very cool, but the preparation of the two-point program is always flawed. The first program of the two points appeared in 1942, but until 1962 there appeared the first no bug of the binary program, it is easy to write the correct difficulty imaginable.
Problem solving Skills:
(1) Plastic data title: L is the lower bound, R is the upper bound
The general shape of the data of the problem of its circulation is:
while (L < R) then l=mid+1,high=mid the various forms;
Or some of the subject boundary requirements are relatively strong to be
while (L <= R) then l=mid+1,r=mid-1 the various forms;
Also the problem is CF 371C that the boundary processing requirements higher is:
while (L+1 < R) and L=mid,r=mid
(2) floating point type title: #define EPS 1e-5
Big God said: "The general floating-point problem will be associated with precision, so it is bound to be related to EPS, because if the accuracy requires 0.01, then if you do so in l=mid+eps, here I set EPS for 0.00001, then the time complexity will be multiplied by 10^3, So since the second point is to reduce the time, this will increase the complexity of time, then how to avoid this problem it.
So in Hdu 1551 I fell into this hole, I put the precision in the l=mid+eps, and then directly tle. I write precision in the while inside the time drop a lot. Because each time is divided, this is not much related to EPS, as long as the best answer can be approached. so the technique is as follows:
while (r-l >eps) then L=mid, R=mid; ”。
The basic idea of solving problems is:
R) { mid=(l+r)/2;//If it is an integer use shift >>1 to speed up if (Gao (mid)<l=mid+1; The GAO function is a function that verifies whether the best answer is met after a binary enumeration r=mid;}
Here are the code and analysis for these questions:
HDU1551 Cable Master http://acm.hdu.edu.cn/showproblem.php?pid=1551
As my two-point first question, test instructions is to give you a string of n, let you into the length of equal m section, to find the longest length of the rope. I didn't know this question could be done in two points before, it was amazing.
The code is as follows:
#include <iostream>#include<cstdio>#include<cstring>#include<math.h>#include<algorithm>#defineEPS 1e-8using namespacestd;intn,m;Doublesum;Doublea[10001];BOOLJudgeDoubles) { intCnt=0; for(intI=0; i<n; i++) {CNT+=(int) (a[i]/s);//The string can be divided by the number of pieces}if(cnt>=m)return true; Else return false;}intMain () { while(SCANF ("%d%d", &n,&m)!=eof&& (n| |m)) {sum=0; for(intI=0; i<n; i++) {scanf ("%LF",&A[i]); Sum+=A[i]; } Sum/=m;//rope can be divided into the maximum lengthDoubleL=0, r=sum; while(Fabs (R-L) >EPS) { DoubleMid= (L+R)/2.0; if(judge (mid)) L=mid; ElseR=mid; } printf ("%.2lf\n", L); } return 0;}
。
Summary of the
Two-point topic (not to be continued)