Description
A country develops 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
Enter 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)
Output
The minimum number of such missile interception systems 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. Solution: first look at a data item 7, 3, 6, 2, 5, and 4. greedy: Save the shortest height. If the shortest height is smaller than the shortest height, update the shortest height. If no one can surpass the shortest height, save the shortest height, in fact, it is greedy. Alas, your brain is too creative, not to ensure that the maximum number of Missiles intercepted by a system is less. This is my code # include <iostream>
Using namespace STD;
# Deprecision Max 30005
Int A [10005]; //
Int main ()
{
Int N; // a total of N Missiles
Int S = 0; // There are currently s sets of systems
Int height, temp, index, Judge; // The input height to determine whether to enable another system.
While (CIN> N)
{
S = 0; // There are currently S Systems
Memset (A, 0, sizeof ());
For (INT I = 0; I <n; I ++)
{
Judge = 0; // determine whether to enable another system. If not, enable another system. If not, 1 cannot.
Temp = max;
Cin> height;
For (Int J = 0; j <s; j ++)
{
If (A [J]> = height)
{
Judge = 1; // no longer enable the system
If (A [J]-height <temp) // use this system to get greedy.
{
Index = J;
Temp = A [J]-height;
}
}
}
If (1 = judge) // no longer open a new system
{
A [Index] = height;
}
Else if (0 = judge) // open another System
{
S ++;
A [s-1] = height;
}
}
Cout <S <Endl;
}
}