Minimum interception system
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 20766 Accepted Submis Sion (s): 8213
Problem Description
A missile interception system has been developed by a country to defend against enemy missile attacks. But there is a flaw in the missile interception system: Although its first artillery shells can reach any height, each shot cannot exceed the height of the previous pitch. One day, the radar caught the enemy's missiles. As the system is still in the trial phase, So there is only one set of systems, so it is possible not to intercept all missiles.
What do we do? How many systems do you have? It's easy to say it. Cost is a big problem. So I came here to call for help, please figure out how many sets of interception systems you need at least.
Input
Enter several sets of data. Each group of data includes: Total number of missiles (positive integers), the height at which the missiles are flown (radar-given height data is a positive integer not greater than 30000, separated by a space)
Output
corresponding to each set of data output intercept all missiles with a minimum of how many sets of this missile interception system.
Sample Input
8 389 207 155 300 299 170 158 65
Sample Output
2
Analysis: This problem has a pit ... Never use the simplest greedy to think, because it must be wrong. There will be people directly think: Find out the number of two adjacent numbers to meet A[i] < a[i+1], is not the number of systems required, practice has proved that this method is wrong, because this system can not get the shells, but it is possible to receive the next one ~ ~ ~ so we have to move a little brain, We only need to maintain (update) the minimum value that each interception system can intercept. If all the system can intercept the minimum value is less than the current shell height, then there must be another system to receive it, otherwise, with the smallest possible and larger than the current shell of the system to receive, remember to update the system's minimum reception height oh. We can put the minimum value of each system in an array, and the number of the last array is the minimum number of interception systems.
PS: Because each time from a[0] first to detect whether there is a minimum height of the smallest system can receive the shells, and the number of systems starting from 0, so the last generation of a[], must be non-diminishing, so directly from the subscript 0 start sweep can meet the greedy requirements.
AC Code:
#include <algorithm> #include <iostream> #include <cstdio>using namespace Std;int a[100001];int Main ( {#ifdef sxk freopen ("In.txt", "R", stdin); #endif int n; while (~SCANF ("%d", &n)) {a[0] = 9999999; preprocessing int len = 0; int key; while (n--) {int flag = 0; scanf ("%d", &key); for (int i = 0; I <= len; i + +)//Scan all interception systems for minimum interception height {if (key <= a[i]) If you can use this system to intercept {a[i] = key; Update the system minimum intercept height flag = 1; Break }} if (flag = = 0)//If none of the current systems can intercept the shell {A[++len] = k ey Add a system, and update the interception system minimum intercept Height}} printf ("%d\n", Len + 1); Because a[] subscript is starting from 0, so the number of elements is len+1}}
HDU 1257 minimum interception system (greedy)