Number of tests (Blue Bridge Cup)

foreword

I stumbled upon such a topic when I was brushing the questions. At first glance, it was quite simple. I didn’t expect it to be so simple
. Sorry!

topic

Title: The inhabitants of planet x test times are not very good-natured, but fortunately, the only abnormal behavior when they are angry is: drop their mobile phones. Major manufacturers have also launched a variety of drop-resistant mobile phones. The Quality Supervision Bureau of Planet X stipulates that the mobile phone must pass the drop test and be assessed a drop resistance index before it is allowed to be listed for circulation.
Planet X has a lot of towering towers that are just right for a drop test. Each floor of the tower is the same height, with a slight difference on Earth, their first floor is not the ground, but is equivalent to our 2nd floor.
If the phone is dropped from the 7th layer without breaking, but the 8th layer is broken, the phone's drop resistance index = 7. In particular, if the phone is broken when dropped from the first layer, the drop resistance index = 0.
If the nth floor of the top floor of the tower is not broken, then the drop resistance index = n In order to reduce the number of tests, 3 mobile phones are sampled from each manufacturer to participate in the test.
In one test, the tower height is 1000 layers. If we always adopt the best strategy, how many times does it take to determine the drop resistance index of the mobile phone under the worst luck? Please fill in this maximum number of tests.
Note: What needs to be filled is an integer, do not fill in any redundant content.

card point

How should I say this question? At first, I read the title carefully, because this is the title of the JAVA Blue Bridge Cup in Group B in 2019, and it is the fourth question. So I thought about it for a while and wrote it, and found that the answer was wrong. After checking it, I didn't understand how others came up with it for a long time, and finally I typed the watch myself and typed it out.

Why didn't I respond to this question at the time, and I was wrong. The main reason was that my understanding of the language was relatively poor, and I always understood the meaning of his title.
I will also repeat the words here, that is to ask you to drop the phone to test the anti-drop value of the phone. If the first layer is not broken, then the value is at least 1, and the second layer is not broken and then +1, until the phone is broken, test The drop resistance value. Now suppose there are three mobile phones, and ask, how many times the three mobile phones are dropped to get the result if they are unlucky.

The more hateful thing about this question is that it is hard to think about how the phone fell. If you know how he fell, you can actually come up with ideas directly and quickly. I am also stuck here, and I don’t understand how this happened. A throw, and then the worst luck.

However, the hints about dp here are quite obvious. One is the word "most", and the title is equivalent to directly telling you the factors that affect dp, the number of layers and the number of mobile phones. So this piece of dp is very good to think. First, the value is naturally what we ask for, and then dp[i][j] where i represents the floor and j represents the mobile phone. Of course you can do it the other way around, but I prefer it that way.

By meter

At this time, let's think about how this phone fell. This piece, let's make a table
insert image description here
first. This is the first point, we can easily think of it.

Next is the case of having two mobile phones.

If there is only one floor, then my two mobile phones are also dropped once. If it is two floors, I can directly drop it on the second floor. If it is broken, I will go to the first floor to test. If it is not broken, I will test it directly. out, then I only need 1 time. In other words, I also continued to measure from the first floor and then to the second floor. If he wants the largest, then I will also give him a 2 at this time
insert image description here
. If there are 2 mobile phones, then there will be 3, 4, and 5 floors. If it’s on the third floor, obviously it will be dropped directly on the third floor first, and it will be broken once, but the second time it will not be broken, and there will be a phone left, then I can’t drop it directly from the second floor at this time, I can only start from 1. , that is how much it takes for a mobile phone to measure 2 layers at most, obviously it is 2 at this time and then add the previous one. so 3

So if two mobile phones have 4 layers, if I continue to lose them directly from the 4th layer, then according to our assumption, the worst is probably 4 times, then what if I start to lose them from the 3rd layer.

So here we can summarize a table, for two mobile phones, throw them from different floors, and then measure the best and worst times.
Among them, good, broken refers to whether the current layer is broken
insert image description here

So at this time, we found that if we follow the most strategic and the worst luck, we can't start throwing directly from the top floor, but obviously according to our common sense, it is better to throw directly from a high place than directly from the first floor. But obviously we can't be directly subjective, so at this time we need to choose an appropriate strategy.

problem transformation

So at this time, we have determined that there are two points to be dealt with in this problem, one is to determine the dp, and the other is to determine the strategy, which is the start of the first layer.


Then the question here is obviously to ask you how many times you need to spend under the best strategy, that is, the most and the smallest in the table below . Then, the most of each layer is determined by whether the phone is broken or not at the current layer. If it is not broken
, then dp[ni][j] i is the number of layers, and i is the number of mobile phones. If it is broken, it is 1+dp[i-1][j-1] and then determine the best strategy, and fill in the corresponding "most" and it will be ok

insert image description here

coding

It may be a bit confusing to say this, but you will understand directly when you look at the code next.

public class 测试次数 {
    
    
    static int N = 1000;
    static int M = 3;
    static int[][] dp = new int[N+1][M+1];
    public static void main(String[] args) {
    
    
        //初始化,第一台手机
        for (int i=1;i<N;i++){
    
    
            dp[i][1] = i;
        }

        //中间状态
        for (int i=2;i<=3;i++){
    
    
            for (int j=1;j<=1000;j++){
    
    
                //选择出最好的方案
                int min_val = Integer.MAX_VALUE;
                for(int k=1;k<=j;k++){
    
    
                    int max = Math.max(1 + dp[j - k][i], (dp[k - 1][i - 1])+1);
                    min_val = Math.min(max, min_val);
                }
                dp[j][i] = min_val;
            }
        }

        System.out.println(dp[N][M]);
    }
}

Summarize

The interesting part of this question is how the mobile phone is dropped. It is actually very easy to figure out the intermediate process. This is also an interesting place.

Guess you like

Origin blog.csdn.net/FUTEROX/article/details/123669007