Half related

Dichotomous answer


 The basic template 
in an ordered array, a value of k binary search

1  // Find a monotonous array which half a certain value K 
2  int n-, K;
 . 3  int A [MAXN];
 . 4  
. 5  void Solve () {
 . 6      int LB = - 1 , UB = n-;
 . 7      the while (UB - LB> . 1 ) {
 . 8          int MID = (LB + UB) / 2 ;
 . 9          IF (A [MID]> = K) {
 10              UB = MID;
 . 11          } the else {
 12 is              LB = MID;
 13 is          }
 14     }
15     printf("%d\n", ub);
16 }

Analysis examples

POJ1064 

Suppose a solution and determine the feasibility of

Title: there are N rope, lengths of L. If the same cut length of rope from the K them, then this can be up to K how long each rope. After the answer to two decimal places

Solution: C (x) = k can be obtained as a rope length of the bar of x = floor (sum (Li / x) is larger than k)

code show as below:

Note that the program determines in the scope of our solution requires two decimal places, since the range of 1 cycle can be reduced to half the interval of 100 cycles may be up to 10 -30 accuracy range. In addition, an abort condition may be set as the (ub - lb)> EPS Thus, a specified size range

. 1  int N, K; 
 2  Double L [MAXN];
 . 3  
. 4  BOOL C ( Double X) {
 . 5      int NUM = 0 ;
 . 6      for ( int I = 0 ; I <N; I ++ ) {
 . 7          NUM + = ( int ) (L [I] / X);
 . 8      }
 . 9      return NUM> = K;
 10  }
 . 11  
12 is  void Solve () {
 13 is      // range initialise the 
14      Double LB = 0, UB = INF * 1.0 ;
 15  
16      // cycle is repeated until a sufficient range of solutions small 
. 17      for ( int I = 0 ; I < 100 ; I ++ ) {
 18 is          Double MID = (LB + UB) / 2 ;
 . 19          IF ( C (MID))
 20 is              LB = MID;
 21 is          the else 
22 is              UB = MID;
 23 is      }
 24      the printf ( " % .2f \ n- " , Floor (UB * 100 ) / 100 );
 25  }
 26 is 
27 int main() {
28 #ifndef ONLINE_JUDGE
29     freopen("input.txt", "r", stdin);
30 #endif
31     scanf("%d %d", &N, &K);
32     for (int i = 0; i < N; i++) scanf("%lf", &L[i]);
33     solve();
34     return 0;
35 }

POJ 2456 

Maximizing the minimum

topic:

Farmer John has played a barn between N hut. Barn row in a line, the i-th position x_i in the barn. But he was not satisfied with the M cow house, so often attack each other. John in order to prevent harm to each other between the cattle, it was decided to each head of cattle are placed as far away from other cattle barn. That is, to maximize the distance between the nearest two cows.

solution:

Position C (d) = cattle may be arranged such that the two nearest cow is not less than d. The biggest problem is transformed for the sake of d

greedy:

Barn sort position;

The first cow barn into $ $ x_0;

If the cow into the i $ a $ x_j words, the $ i + 1 $ into the cow must satisfy $ x_j + d <= x_k $ minimum of $ $ x_k

 1 int N, M;
 2 int x[MAXN];
 3 
 4 bool C(int d) {
 5     int last = 1;
 6     for (int i = 1; i < M; i++) {
 7         int crt = last + 1;
 8         while (crt <= N && x[crt] - x[last] < d) {
 9             crt++;
10         }
11         if (crt == N + 1) return false;
12         last = crt;
13     }
14     return true;
15 }
16 
17 void solve() {
18     sort(x + 1, x + 1 + N);
19     int lb = 0, ub = INF;
20     while (ub - lb > 1) {
21         int mid = (ub + lb) / 2;
22         if (C(mid))
23             lb = mid;
24         else
25             ub = mid;
26     }
27     printf("%d\n", lb);
28 }
29 
30 int main() {
31 #ifndef ONLINE_JUDGE
32     freopen("input.txt", "r", stdin);
33 #endif
34     scanf("%d %d", &N, &M);
35     for (int i = 1; i <= N; i++) scanf("%d", &x[i]);
36     solve();
37     return 0;
38 }

 

Guess you like

Origin www.cnblogs.com/romaLzhih/p/11619504.html