1260:【例9.4】拦截导弹(Noip1999)
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 4063 通过数: 1477
【题目描述】
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入导弹依次飞来的高度(雷达给出的高度数据是不大于30000的正整数,导弹数不超过1000),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。
【输入】
n (一共n个导弹)
输入导弹依次飞来的高度。
【输出】
第一行:最多能拦截的导弹数;
第二行:要拦截所有导弹最少要配备的系统数。
【输入样例】
389 207 155 300 299 170 158 65
【输出样例】
6
2
```java
public class Main {
static int[] MaxLength,
height,
MaxCount;
static int max1, max2;
public static void LTS(int k){
for (int i = 0; i < k; i++) {
MaxLength[i] = 1;
MaxCount[i] = 1;
for (int j = 0; j < i; j++) {
if(height[i] <= height[j]){
MaxLength[i] = Math.max(MaxLength[i], MaxLength[j]+1);
}else{
MaxCount[i] = Math.max(MaxCount[i], MaxCount[j]+1);
}
}
max1 = Math.max(max1, MaxLength[i]);
max2 = Math.max(max2, MaxCount[i]);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
MaxLength = new int[n];
MaxCount = new int[n];
height = new int[n];
for (int i = 0; i < n; i++) {
height[i] = scan.nextInt();
}
LTS(n);
System.out.println(max1);
System.out.println(max2);
}
}