Algorithm question-throwing pawns

Source: Internet
Author: User
The title is as follows: "There is a 100-storey building, and you have two identical glass pawns in your hands. Dropping the pawns from a certain layer of the building will break down. Use the two glass pawns in your hands to find an optimal strategy to find out the critical layer. "
Let's talk about expansion: N layers, K balls

This question has a DP solution because of recursion. Assume that the data is first thrown at the R layer, and if the data is broken, it is in the range of 1 ~ R between search, at this time there is a K-1 ball; not broken in R + 1 ~ Search between N, with K balls left.

The Code is as follows:

1 # include <iostream> 2 # include <fstream> 3 # include <sstream> 4 # include <string> 5 # include <cmath> 6 # include <iomanip> 7 # include <vector> 8 # include <deque> 9 # include <list> 10 # include <queue> 11 # include <stack> 12 # include <map> 13 # include <algorithm> 14 # include <limits> 15 # include <utility> 16 # include <ctime> 17 # include <bitset> 18 using namespace STD; 19 20 # define max_floor 51221 # define max_ball 10022 23 int dp (int n, int K) 24 {25 if (k <1 | n <1) Return-1; // incorrect input 26 27 if (k = 1) return n-1; // remove some trivial case28 if (n = 1) return 0; 29 30 int M [max_ball] [max_floor]; 31 int I, j, R; 32 int temp, min; 33 34 for (I = 0; I <= K; I ++) m [I] [0] = m [I] [1] = 0; // F (1, K) = f (0, K) = 035 for (j = 2; j <= N; j ++) m [1] [J] = J-1; // F (n, 1) = n-136 37/* 38 state transition equation: 39 f (n, k) = min {max {f (R, k-1) + 1, F (n-R, K) + 1}, 1 <= r <= n} 40 */41 for (I = 2; I <= K; I ++) 42 for (j = 2; j <= N; j ++) 43 {44 min = numeric_limits <int>: max (); 45 for (r = 1; r <= J; r ++) 46 {47 temp = max (M [I-1] [r], M [I] [J-R]) + 1; 48 if (temp <min) 49 min = temp; 50} 51 m [I] [J] = min; 52} 53 54 return M [k] [N]; // F (n, k) 55} 56 57 int main () 58 {59 int N, K; 60 61 CIN> N> K; 62 cout <dp (n, k) <Endl; 63 64 return 0; 65}

This question has another solution for the first question: the first ball first finds the segment where the critical layer is located, and the second ball finds the layer from the segment. Because the number of CIDR blocks increases, the number of layers is reduced. (I don't really understand it)

X + (x-1) + (X-2 )....

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.