-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_7_1.cpp
55 lines (45 loc) · 1.11 KB
/
11_7_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// 11_7_1.cpp
//
//
// Created by Pengyan Qin on 7/16/15.
//
// Find the largest problem
#include <iostream>
#include <vector>
using namespace std;
// Given a sequence of number
// Find the length of the longest non_decreasing sequence
int LIS(int A[], int n){
int *l = new int[n];
vector<vector<int> > vvi;
vector<int> vec;
for(int i = 0; i < n; ++i)
vvi.push_back(vec);
int len = 1;
int ind = 0;
for(int i = 0; i < n; ++i){ // the longest subsequence with ends with A[i]
l[i] = 1;
for(int j = 0; j < i; ++j){ // go through A[0] to A[i-1]
if(A[i] >= A[j] && (l[j] + 1) > l[i]){
l[i] = l[j] + 1;
vvi[i] = vvi[j];
}
}
vvi[i].push_back(A[i]); // the sequence ends with A[i]
if(l[i] > len){
ind = i;
len = l[i];
}
}
for(int i = 0; i < vvi[ind].size(); ++i)
cout << vvi[ind][i] << " ";
cout << endl;
delete[] l;
return len;
}
int main(){
int A[] = {13, 14, 15, 11, 12};
int n = 5;
cout << LIS(A, n) << endl;
}