Greedy C language-Minimum interception system (Hdu 1257)
Problem Description a country has developed a missile interception system to defend against missile attacks by the enemy. however, this missile interception system has a defect: although its first shell can reach any height, it cannot exceed the height of the previous one. one day, the radar captured the enemy's missiles. because the system is still in the trial phase, there is only one system, so it may not be able to intercept all missiles.
What should we do? How many more systems are involved! It's easy to talk about. What about the cost? The cost is a big problem, so I am here for help. Please help calculate the minimum number of interception systems required.
Input several groups of data. each set of data includes the total number of missiles (positive integers), and the height of missiles flying here (the height data given by the radar is a positive integer not greater than 30000, separated by spaces)
The Output corresponds to the minimum number of missile interceptions required for each set of data Output to intercept all missiles.
Sample Input
8 389 207 155 300 299 170 158 65
Sample Output
2
This is a greedy question.
The specific greedy process is:
Each time you read a missile height, you must first determine whether a gun can intercept it. If not, you must add an intercept system. If yes, you need to select an interception system with the minimum difference between the current height and the target missile height. In this way, the high loss can be minimized. in other words, there are currently three interception systems with the highest heights of 100, 90, and 80 respectively. To intercept an 85 shell, you can only select one at and 90, because the difference between 90 and 85 is close and the height loss is small, the interception system of 90 is selected for interception.
Specific Code Implementation ideas:
Use an arrayF []It indicates that the current number of interception systems has already reached the current interception height. At first there was only one interception height. Naturally, the interception height was the strike height of the first missile, and then each time it was read into the strike height of a missileH. ThenF []Array from leftF [0]Start to find a locationIMakeF [I]> H> f [I-1]Of course, ifF [0]RatioHLarge, soINaturally0, Found that the current I interception system is the interception system that can minimize the high loss, and then change the height of the interception systemHIndicates that the current interception system has intercepted the missile of H. If the above-mentioned position is not found, I said, H has exceeded the height of all interception systems, then we need to add another interception system and change the height of the interception systemHAddF []Finally, it indicates that it has been intercepted.HThis missile.
Most important:
When searching for the I position, use:
Binary Search !!
# Include
# Include using namespace std; # include
Int main () {int f [100000]; int I, a, n, t = 0; int l, r, mid; while (scanf ("% d ", & n )! = EOF) {t = 1; while (n --) {scanf ("% d", & a); l = 0, r = t; while (l <= r) // Binary Search {mid = (l + r)/2; if (f [mid] = a) {l = mid; break ;} else if (f [mid] <= a) l = mid + 1; else r = mid-1;} if (l