What is the optimal strategy for random targets in Team Fortress 2 basic training? I speedrun the basic training mode in Team Fortress 2 (Valve, 2007). At one point, the player is required to destroy a series of targets that pop up randomly. These targets are the only random part of the run; everything else is consistent and optimized.
The targets are laid out in two rows of three and one row of one, as shown in the following diagram. The site tells me I'm not allowed to post images, so: 

Primary targets can only be destroyed by a primary weapon, secondary targets by a secondary weapon, and melee targets by a melee weapon.
8 groups of targets appear, randomly and independently, during the practice. Except for the first group, no group will appear until the previous one has been destroyed. A group of targets may consist of:


*

*The melee target;

*Any one or two secondary targets; or

*Any one or two primary targets.


We complete this section as the "Scout" character, who has the following weapons:


*

*Primary: Scattergun. Hitscan, meaning the bullets travel instantaneously. Can fire a shot every 0.625 seconds.

*Secondary: Pistol. Hitscan. Can fire a shot every 0.17 seconds.

*Melee: Bat. Sends a hitscan attack 0.25 seconds after the player attacks. Can fire a shot every 0.5 seconds. (This is irrelevant as there is at most one melee target, and the time between groups of targets exceeds 0.5 seconds.)


In Team Fortress 2, you can only hold one weapon at a time, and after you switch to another weapon you must wait 0.5 seconds before you can shoot again. Since the player does not know what target is going to appear next, they must guess and hold out a weapon. If they guess correctly, they can destroy the targets with the weapon they are holding. If they guess incorrectly, they lose 0.5 seconds waiting to shoot.
Given this information, what weapon should I hold out to optimize my average completion time?
Throughout this post, we make the simplifying assumption that the player has perfect aim and can snap from target to target instantly.
 A: If:


*

*the choice of which row to pop up is uniformly random and independent

*the delay between a new popup is long enough that you can carry out whatever strategy you like

*the popups appear at perfectly predictable times

*you can switch weapons immediately after firing


then the best choice is to always switch to the bat, and swing the bat 0.25 seconds before the target pops up.
The reason is that no matter what choice you make, you will always have


*

*a 66% chance of needing to switch weapons

*to destroy the targets as fast as possible


Since they're the same in all cases, they're irrelevant to the choice of which weapon to hold out.
The only other possible cost you might have to pay in addition is the 0.25 second delay between swinging the bat and hitting the target, and this cost is eliminated by selecting the bat and preswinging. (and with perfect timing, you suffer no delay if you guess wrong and need to switch weapons)

If your timing is not perfect enough, or the appearance times are not predictable, so that you lose on average if you try preswinging the bat (and thus don't want to try that), then you should get the same average result no matter which weapon you pick.
Incidentally, if you don't have instant reactions, there is yet another reason to pick the bat.  You don't have to aim at the melee target and can fire immediately... and if the ranged targets appear you can use the weapon switch times to take aim.
A: Even following @Hurkyll's advice to swing the bat 0.25 seconds before the targets appear, likely impossible for a human, I conclude that you should hold out either your primary or secondary weapon.
Here is a table of the time losses depending on what targets appear and what weapon you are holding.
+-------------+---------+-----------+-------+
|             | Primary | Secondary | Melee |
+-------------+---------+-----------+-------+
| 1 Primary   | 0       | 0.5       | 0.5   |
| 2 Primary   | 0.625   | 1.125     | 1.125 |
| 1 Secondary | 0.5     | 0         | 0.5   |
| 2 Secondary | 0.67    | 0.17      | 0.67  |
| 1 Melee     | 0.75    | 0.75      | 0     |
+-------------+---------+-----------+-------+

Average time loss for a primary weapon is $(0+0.625+0.5+0.67+0.75) / 5 = 0.509$ seconds.
Average time loss for a secondary weapon is $(0.5+1.125+0+0.17+0.75) / 5 = 0.509$ seconds.
But average time loss for a melee weapon is $(0.5+1.125+0.5+0.67 + 0)/5 = 0.559$ seconds.
A: After I posted to the forum reddit.com/r/tf2 on the subject, a user named "starblaster64" examined the decompiled map and discovered exactly how the targets are calculated. An array of the numbers from 1 to 15 is created and randomly shuffled. The game iterates through the first 8 entries in the array. Let i equal the current position of the iteration.


*

*If the number at position i is 7, 8, 9, 10, 11, or 14: some combination of primary targets appears.

*If the number at position i is 4, 5, 6, 12, 13, or 15: some combination of secondary targets appears.

*If the number at position i is 1, 2, or 3: the melee target appears.


Thus, the optimal strategy is:


*

*Hold out either your primary or secondary weapon, whichever has appeared the fewest times.

*Never hold out your melee. Even if 7 primary/secondary targets have appeared, there is a 5/8 chance another one will.


starblaster64 also notes that every target can appear up to 3 times, so:


*

*If a target has appeared 3 times, don't aim at it.

