Minimum interception system
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 24770 Accepted Submission (s): 9719
Problem description A missile interception system developed by a country to defend against enemy missile attacks. But there is a flaw in this missile interception system: Although its first shells can reach any height, each shot cannot exceed the height of the previous one. Someday, The radar captures the enemy's missiles. Since the system is still in trial, there is only one set of systems that may not 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 enters several sets of data. Each group of data includes: Total number of missiles (positive integers), the height at which missiles fly (radar-given height data is a positive integer not greater than 30000, separated by a space)
Output corresponds to each set of data outputs to intercept all missiles with a minimum of how many sets of such missile interception systems.
Sample INPUT8 389 207 155 300 299 170 158 65
Sample Output2
Puzzle: Greedy from the current interception system to find the smallest height difference to intercept if the height is greater than all the interception system to increase the new interception system
Code implementation:
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intINF =0x3f3f3f;7 Const intMAXN =30011;8 intDP[MAXN];9 Ten intMain () { One intA, B, N; A intans; - BOOLFlag; - while(SCANF ("%d", &n)! =EOF) { theAns =0; - while(n--){ -A =inf; -scanf"%d", &b); +Flag =false; - inttemp; + for(inti =0; i < ans; ++i) {//If you can select the interception system with the smallest height difference A if(b <= dp[i] && a > dp[i]-b) { atA = Dp[i]-b; -temp =i; -Flag =true; - } - } - in if(!flag) {//Update the interception system -Dp[ans] =b; to++ans; + } - Else{ theDp[temp] =b; * } $ }Panax Notoginseng -printf"%d\n", ans); the } + return 0; A}
Alpha
"HDU1257" minimum interception system (greedy)