Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- vector <int> LIS(vector <int> v)
- {
- vector <int> lis(v.size(), 1), padre(v.size(), -1);
- int posMaxLis = 0;
- for(int i=0; i<v.size(); i++)
- {
- for(int j=i; j<v.size(); j++)
- {
- if(v[j] > v[i])
- {
- if(lis[i]+1 > lis[j])
- {
- lis[j] = lis[i]+1;
- padre[j] = i;
- }
- }
- if(lis[j] > lis[posMaxLis])
- {
- posMaxLis = j;
- }
- }
- }
- ///Hacer backtracking
- vector <int> resultado;
- do
- {
- resultado.push_back(v[posMaxLis]);
- posMaxLis = padre[posMaxLis];
- }while(posMaxLis >= 0);
- reverse(resultado.begin(), resultado.end());
- return resultado;
- }
- int main()
- {
- vector <int> v = {1, 2, 3, 4, 1, 2, 5 ,87, 34, 1, 4, 5, 6, 7};
- vector <int> res = LIS(v);
- for(int i=0; i<res.size(); i++)
- cout << res[i] << " ";
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment