給定一個順序存盤的線性表,請設計一個演算法查找該線性表中最長的連續遞增子序列,例如,(1,9,2,5,7,3,4,6,8,0)中最長的遞增子序列為(3,4,6,8),
輸入格式:
輸入第1行給出正整數n(≤10?5);第2行給出n個整數,其間以空格分隔,
輸出格式:
在一行中輸出第一次出現的最長連續遞增子序列,數字之間用空格分隔,序列結尾不能有多余空格,
輸入樣例:
15
1 9 2 5 7 3 4 6 8 0 11 15 17 17 10
輸出樣例:
3 4 6 8
這道題好難,,倒不是思路難想,就是測驗點和對一些測驗的資料處理的不好,改了一些測驗點,改一回多兩分,最后全順序還是在別人的幫助下找出的,唉,20分的題,按理說不應該被卡的這么慘,但事實就是這樣,它只是說了n的范圍,沒說輸入的整數的范圍,防止麻煩,我一開始就直接定義了long long型別

#include <iostream>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin >> n;
ll a[n];
for(int i = 0;i<n;i++){
cin >> a[i];
}
if(n==1){
cout << a[0];
return 0;
}
int maxn = -1;
int start,end;
for(int i = 0;i<n;i++){
int len = 0;
for(int j = i+1;j<n;j++){
if(a[j]>a[j-1]){
len++;
if(j == n-1){
for(int k = i;k<=j;k++){
cout << a[k];
if(k<j)
cout <<" ";
}
return 0;
}
} else{
if(len>maxn){
maxn = len;
start = i;
end = j-1;
}
break;
}
}
}
for(int i = start;i<=end;i++){
cout << a[i];
if(i<end)
cout <<" ";
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/99506.html
標籤:其他
