给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。
输入格式:
输入第1行给出正整数n(≤105);第2行给出n个整数,其间以空格分隔。
输出格式:
在一行中输出第一次出现的最长连续递增子序列,数字之间用空格分隔,序列结尾不能有多余空格。
输入样例:
15
1 9 2 5 7 3 4 6 8 0 11 15 17 17 10
输出样例:
3 4 6 8
简单模拟
#include<algorithm>
#include<iostream>
using namespace std;
int n;
const int N = 1e6;
int a[N], res = 1;
int main(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int cnt = 1, r = 1;
for(int i = 2; i <= n; i++){
if(a[i] > a[i - 1]){
cnt++;
if(cnt > res){
res = cnt;
r = i;
}
}else{
cnt = 1;
}
}
//output
for(int i = r - res + 1; i <= r; i++){
cout << a[i];
if(i != r) cout << ' ';
}
return 0;
}