P1020 导弹拦截(动归)

题目描述

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。

输入导弹依次飞来的高度(雷达给出的高度数据是≤50000 的正整数),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。

输入输出格式

输入格式:

1行,若干个整数(个数≤100000 )

输出格式:

2行,每行一个整数,第一个数字表示这套系统最多能拦截多少导弹,第二个数字表示如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。

输入输出样例

输入样例#1: 复制

389 207 155 300 299 170 158 65

输出样例#1: 复制

6
2
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>

using namespace std ;
const int MAX = 100005 ;
int a[MAX] ;
int dp[MAX] ;
int low[MAX];
int hei[MAX] ;
int cnt ;
struct cmp{
	bool operator()(int a,int b){
		return a>b;
	}
};

int main() {
	int ans1 , ans2 ;
	while(~scanf("%d",&a[cnt++])){

	}
	cnt-- ;
	int len = 1 ;
	int s = 1 ;
	hei[s] = a[0] ;
	low[len] = a[0] ;
	for(int i = 1 ; i<cnt ;i ++ ){
		if(a[i]<=low[len]){
			low[++len] = a[i] ;
		}
		else{
			int mid = upper_bound(low+1,low+1+len,a[i],cmp())-low ;
			// 在一个递减数列中 ,查找比a[i] 小的第一个数 ;
			low[mid] = a[i] ;
		}
		if(a[i]>hei[s]){
			hei[++s] = a[i] ;
		}
		else{
			int mid = lower_bound(hei+1,hei+1+s,a[i])-hei;
			hei[mid] = a[i] ;
		}

	}
	ans1 = len ;
	ans2 = s ;
	cout<<ans1<<endl<<ans2 ;



	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/88673348