Professional Documents
Culture Documents
Binary Search PDF
Binary Search PDF
1
, , .. a[1] a[2]
. . . a[N ].
. , ,
, , , . ,
, , , ,
. , .
, :
while (l<r) {
m=(l+r)/2;
if (a[m]<k) l=m+1;
else r=m;
}
if (a[r]==k) printf("%d", r);
else printf("-1");
l r 0 n 1 .
, 1.
O(log N ), N .
2
,
( ) . ,
, x1 , x2 : x1 > x2 f (x1 ) > f (x2 ) ( x1 x2 , x1 > x2 , f (x1 )
f (x2 )).
, , ,
, . , ,
, .. ,
.
. x (
3
x) y , y 3 = x.
: x (x 1)
5 .
x 1, x, . ,
1, x. ,
x, , .
:
r = x;
l = 1;
while (fabs(l-r)>eps) {
m=(l+r)/2;
if (m*m*m<x) l=m;
else r=m;
}
fabs, math.h.
3
- .
, , , , ,
.
, .
.
2006-2007
, .
,
N . ,
, y . ( , .
, .) ,
.
N , x y , (1 N 2 108 ,
1 x, y 10).
, N .
411
3
512
4
(), , ,
. .
min(x, y) , , N 1 .
l - , r - . 0 , ,
(N 1) x ( ).
, , .
N 1, , .
int main(void)
{
int n, x, y, i, j, l, r, now;
double speed;
scanf("%d%d%d", &n, &i, &j);
x=i<j?i:j;
y=i>j?i:j;
l=0;
r = (n-1)*y;
while (l != r) {
now = (l+r)/2;
j = now / x + now / y;
if (j < n-1) l = now+1;
else r = now;
}
printf("%d", r+x);
return 0;
}
13
, , .
, .
.
. .
: , ,
.
N
M . i- N i
i + 1 (N + 1- ), K , i- ,
(1 M 2000, 1 N, K 200000).
,
.
35
4
1201
112
140234
, .
, ,
( ,
).
, (.. ,
) . .
, (,
, , ,
, , ).
. , x = (min + max)/2. ,
, , x ,
, x + a[1], a[1] ,
x+a[1]+a[2] ..
, x (min + x)/2, x = (x + max)/2.
: x , (x 1)
, x.
. , ,
. ,
. , ,
x = (x + max)/2, , x = (min + x)/2. :
, .
, .