Given N crystals with distinct strengths and a hidden target at rank K, minimize the worst-case number of swings to break it using forces up to P without risking an explosion (force gap W).
Hard8Binary searchGame theoryDynamic programmingNo attempts yetTime limit1sMemory limit512 MBM mana crystals fell on New Namustrum. The dead rose after the crystals landed, and Marko, the head of the village, captured Kul'den, the wizard who raised them. Under questioning, Kul'den admitted that the M mana crystals feed mana to the risen dead and that destroying every crystal stops them. The village owns a blunt hammer, and one swing applies force to exactly one crystal.
While Marko was breaking the crystals one at a time, a large explosion went off. Medivh, the village archmage, analyzed the cause and found that a crystal holding a large amount of condensed mana overloads and explodes when it is struck with a force far greater than the force needed to break it. Tassadar, the village shaman, appraised the N remaining crystals and determined that exactly one of them holds condensed mana and that this crystal is the K-th strongest of the N. So K−1 crystals are stronger than it and N−K crystals are weaker. The remaining crystals all have different strengths and the full order of strengths is known, but nobody knows the actual strength values.
The strengths are distinct positive real numbers, all at most P. Lindhol the blacksmith fixes a positive real number p at most P in his head and applies exactly that force to a crystal. A crystal of strength X struck with a force of X or more breaks and disappears, and a crystal that is gone cannot be struck again. A force below X does nothing. The crystal with condensed mana is the exception: striking it with a force stronger than its strength by W or more, that is with a force of at least X+W, makes it explode.
You tell Lindhol which crystal by strength rank to hit and with how much force, one order at a time, and you learn at once whether that crystal broke. You may never give an order that carries any chance of an explosion. Whatever the true strengths are, you want to break the condensed crystal without an explosion, using a strategy that makes the largest possible number of swings as small as it can be. Find the number of swings in the worst case under that strategy.
The first line contains four integers N, K, P, W separated by spaces. In order they are the number of remaining mana crystals N, the strength rank of the crystal with condensed mana K, the maximum power of the blunt hammer P, and the force gap that causes an explosion W. (1≤N≤1000, 1≤K≤N, 1≤P≤2000, 1≤W≤P)
Inputs for which no assignment of strengths satisfies the conditions are not given.
Print on one line the number of hammer swings needed to break the crystal with condensed mana in the worst case, under a strategy that minimizes that number.