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 Just do this question, thought very simple, the missile came on the dozen, the height is not enough to change a system. Later thought, this should be done with greed, every time the missile comes to the test before the system can be used, can not blindly use the new system. Therefore, the correct way of thinking about this problem should be: The missile came-"
Pre-Test systemCan intercept height-"found one or more can be used-" used with the missile height
Minimum DifferenceSystem (greedy, cost-effective)-"Update the height of the system just used to the height of the missile just intercepted." If the missiles come
greater than allThe minimum height of an existing interception system is to use the new interception system to update the minimum height of the system to the height of this missile.
1#include <stdio.h>2 #defineM 99999993 4 intMain ()5 {6 inti,k,n,high,cnt,mini,min[10010],minnum;7 intFlag1,flag2;8 9 while(SCANF ("%d", &n)! =EOF)Ten { OneCNT =0; AFlag1 =0; - for(k=0; k<n;k++) - { thescanf"%d",&High ); - if(flag1==0) - { -CNT =1; +Flag1 =1; -MIN[CNT] =High ; + Continue; A } atFlag2 =0; -Minnum =M; - for(i=1; i<=cnt;i++) - { - if(min[i]-high<0) - Continue; in if(min[i]<minnum) - { toMinnum =Min[i]; +Mini =i; - } the if(i==CNT) * { $min[mini]=High ;Panax NotoginsengFlag2 =1; - } the } + if(flag2==0) A { thecnt++; +min[cnt]=High ; - } $ } $printf"%d\n", CNT); - } - return 0; the}
HDU 1257: Minimum interception system