导弹拦截系统中,求一个系统能拦截导弹的最多个数,就是求最长递减子序列。需要的最少的系统个数=最长增长子序列长度。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
vector<int> heigh,result,result1;
int main(int argc, const char * argv[])
{
int temp;
while (scanf("%d", &temp)!=EOF) {
heigh.push_back(temp);
}
result.push_back(heigh[0]);
result1.push_back(heigh[0]);
for(int i = 1; i < heigh.size(); i++){
int j;
for(j = 0; j < result.size(); j++){
if(heigh[i]<result[j]) {result[j] = heigh[i]; break;}
}
if(j==result.size()) result.push_back(heigh[i]);
for(j = 0; j < result1.size(); j++){
if(heigh[i]>result1[j]) {result1[j] = heigh[i]; break;}
}
if(j==result1.size()) result1.push_back(heigh[i]);
}
printf("%d\n", (int)result1.size());
printf("%d\n", (int)result.size());
}