2018 Blue Bridge Cup --- the number of tests

Title: Testing times

Residents temper x planet is not very good, but fortunately they get angry only when abnormal behavior is: throw the phone.
The major manufacturers also have introduced a variety of shatterproof phone. x planet Bureau of Quality Supervision provides ruggedness phone must be tested and assessed by a shatterproof index before it allows the traded.
x planet there are many towering tower, just can be used for ruggedness testing. Each layer height of the tower are the same, and the earth is slightly different, they are not the first layer of the ground, but the equivalent of our second floor.
If the phone did not go broke from Layer 7 dropped, but the 8th floor broke, the phone ruggedness index = 7.
In particular, if the phone is dropped from the first layer 1 is broken opinions, the ruggedness index = 0.
If the tower to the top of the n-layer did not throw broken, the ruggedness index = n
order to reduce the number of tests, a sample from each of the three mobile phone manufacturers to take the test.
Tower high of a test of the 1000 level, if we always use the best strategy, most need to be tested in the worst luck in order to determine how many times the phone ruggedness index it?
Please fill in the maximum number of tests.
Note: You need to fill is an integer, do not fill out any extra content.

Answer: 19

Problem-solving ideas : This question is to see the first reaction many people would think to find a dichotomy, then the answer would be quite simple if log21000 = 10.
1. But the problem is to test out the phone's ruggedness index, if dichotomy for the first time in 500 layers did not fall to pieces, the next layer 250 is not broken, then the next layer 125 is broken, that our mobile phones ruggedness index is how much, and obviously we can not know, so the use of shatterproof index dichotomy phone, you need 10 phone to measure the ruggedness index in the worst case.
2. Therefore, the correct idea of the topic should be using dynamic programming method

a. List Grid
Here Insert Picture Description (The white space in the content under the best strategy for the worst luck when cell phone use is just i j floors are asking when the subject throw the phone number)

b. 1. fill data (when we only have a cell phone time)

//只有一个手机时,几层楼就要摔几次
		for(int i=1;i<=N;i++) {
			dp[i][1]=i;
		}

Here Insert Picture Description
2. When we have the phone number of two and three

Topic gives: In order to reduce the number of tests, a sample from each of the three mobile phone manufacturers to take the test. This passage should be how to understand it
such as a cell phone only when we fall in one layer, layer 1000 if the worst-case needs 1000

  • 1. When we have two phones in a 3-story building we will be able to reduce the number of test experience
  • 2. When we put a cell phone fell from the 2nd floor, there are two cases are either broken or not broken.
    At this time, only once and then throw the index can be obtained ruggedness. (1 floor to fall at once broken floor 2 case,
    broken ruggedness index represents 0, 1 is not broken in the layer 2 building the case did not fall to pieces in the same way) (when we have three mobile phone case three floors of empathy)
  • At this point we can only consider the case of a 3-storey phone, and at this time we can not go to the 2-storey dropped, because it is broken, we
  • We can not know his ruggedness index, and this time we can only obediently began to throw down from the first floor.

Specific tabular data
Here Insert Picture Description
. C summed up the law:

DP [i] [j]: j represents phone i floor to fall from the test case
because we can determine the maximum value for each empty before a floor +1 times (e.g.: find phone 2, F 3 when the frequency at the second floor seeking again fell in a 3 floor building a certain number of 3 can be obtained) i.e. dp [i-1] [j ] +1

Minimum of two situations:
Damage: Total continues to try to layer k-1, the number of mobile -1: dp [k-1] [j-1]
undamaged: continue to attempt to co-NK layer, the same phone number: dp [nk ] [j]

Minimum number is generated from the above two cases, i.e., due to the requirement worst luck: max (number of damage, the number of cases is not damaged)

And both try once in layer K max (+1 damage times, the number of times without damage +1)

即:max(dp[k-1][j-1]+1,dp[n-k][j]+1) => max(dp[k-1][j-1],dp[n-k][j])+1

max(dp[k-1][j-1],dp[n-k][j])+1 <= dp[i][j] <=dp[i-1][j]+1

Finally, there is the result dp [1000] [3], because the process of filling us in, is (according to the number of phone lines to fill certain priority cases, the floor changes, because doing so is to determine the maximum value of each blank depends on it in front of an empty (same phone number, floor -1)) so the code to achieve the same is true (the phone number in the outer loop) :( Note: our strategy must be optimal, even bad luck, poor get only option, It will not affect the optimal solution we choose from the options available, namely the least number of solutions, so we need to determine which one best current option ie: min ())

Specific code:

package 蓝桥杯_2018;

import java.util.Scanner;
/**
 * 题目理解:
 * 在最坏的运气下最多需要测试多少次才能确定手机的耐摔指数呢?
 * 1.这里的测试多少次是指需要摔多少次手机
 * 2.个人一开始疑惑的点是为了减少测试次数,从每个厂家抽样3部手机参加测试
 * (个人理解的偏差以为一次测试可以扔3部手机),1次测试还是只能扔1部手机
 *   其实题目增加手机数是为了尽量少摔几次手机
 *     1  2  3  4  5  6 ....1000
 *  1  1  2  3  4  5  6     1000      dp[i][j]:表示i层楼时使用j部手机需要摔的最大次数
 *  2  1  2  2
 *  3  
 *  例如只有一部手机的时候我们只能一层一层的摔,1000层的话最坏情况需要1000次
 *  1.当我们有两部手机时在有3层楼的情况下我们就能体会到减少测试次数
 *  2.当我们把一部手机从2层摔下时,有两种情况要么碎了,要么没碎。
 *         此时只需再摔一次就可以得到耐摔指数了.(在2层楼碎了的情况下往1层楼摔一次,
 *         碎了则代表指数是0,没坏就是1。在2层楼摔下去没碎的情况同理) 
 *   此时我们可以考虑只有一个手机3层楼的情况,我们此时并不能往2层楼扔下去, 因为它碎了,我们
 *   就无法得知他的耐摔指数了,此时我们只能乖乖的从1层开始往下扔.      
 */
public class 测试次数 {

	public static void main(String[] args) {
		
		Scanner s=new Scanner(System.in);
		int N=s.nextInt();  //表示楼层数
		int M=s.nextInt();  //表示手机数
		
		//dp[i][j]:表示i层楼时使用j部手机需要摔的最大次数
		int dp[][]=new int[N+1][M+1];
		
		//只有一个手机时,几层楼就要摔几次
		for(int i=1;i<=N;i++) {
			dp[i][1]=i;
		}
		
		//两个以及以上手机时(j代表剩余手机数,i代表剩余楼层数)
		for(int j=2;j<=M;j++) {
			for(int i=1;i<=N;i++) {
				
				//赋最大初值(楼层每增加一层,其需要摔的次数一定会小于等于其楼层数减一的次数+1)
				dp[i][j]=dp[i-1][j]+1;
				
				for(int k=2;k<i;k++) {
					//dp[k-1][j-1]表示在第k层摔坏了    
					//dp[i-k][j]表示第k层没摔坏
					dp[i][j]=Math.min(dp[i][j],
							Math.max(dp[k-1][j-1], dp[i-k][j])+1);
				}
			}
		}
		//输出表格右下角的值
		System.out.println(dp[N][M]);
		s.close();

	}

}

Published 19 original articles · won praise 2 · Views 423

Guess you like

Origin blog.csdn.net/TheWindOfSon/article/details/103181774