- #include <iostream>
- #include <iomanip>
- using namespace std;
- int longest_inc_sequence(int x[], int n)
- {
- int *last;
- int length = 0;
- int left;
- int right;
- int mid;
- int i;
- last = new int[n];
- last[0] = x[0];
- for (i = 1; i < n; ++i)
- {
- if (x[i] < last[0])
- {
- last[0] = x[i];
- }
- else if (x[i] > x[length])
- {
- ++length;
- x[length] = x[i];
- }
- else
- {
- left = 0;
- right = length;
- while (left <= right)
- {
- mid = (left + right) / 2;
- if (x[mid] > x[i])
- {
- right = mid - 1;
- }
- else
- {
- left = mid + 1;
- }
- }
- x[right] = x[i];
- }
- }
- delete(last);
- return length + 1;
- }
- int main(int argc, char *argv[])
- {
- int x[] = { 1, 3, 2, 1, 5, 7, 8, 6, 5, 9, 4, 10, 6 };
- int n = sizeof(x)/sizeof(int);
- int i;
- cout << "/nLongest Increasing Sequence Program";
- cout << "/n===================================/n";
- cout << "/nGiven Array : ";
- for (i = 0; i < n; i++)
- {
- cout << setw(4) << x[i];
- }
- cout << "/n/nLength of L.I.S. is " << longest_inc_sequence(x, n) << endl;
- return 0;
- }
- /*
- Longest Increasing Sequence Program
- ===================================
- Given Array : 1 3 2 1 5 7 8 6 5 9 4 10 6
- Length of L.I.S. is 7
- */