GastonFontenla

UVa: 497 - Strategic Defense Initiative

May 26th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stack>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. string s;
  9.  
  10. int toInt()
  11. {
  12.     int n = 0;
  13.     for(int i=0; i<s.size(); i++)
  14.         n = n*10 + (s[i]-'0');
  15.     return n;
  16. }
  17.  
  18. int main()
  19. {
  20.     int tc;
  21.     cin >> tc;
  22.  
  23.     ///Tengo que leer dos fines de línea
  24.     getline(cin, s);
  25.     getline(cin, s);
  26.     for(int w=0; w<tc; w++)
  27.     {
  28.         int n = 0;
  29.         vector <int> a;
  30.         getline(cin, s);
  31.         while(s.size())
  32.         {
  33.             a.push_back(toInt());
  34.             getline(cin, s);
  35.             n++;
  36.         }
  37.         vector <int> lis(n, 1);
  38.         vector <int> from(n, -1);
  39.  
  40.         int p = 0; ///Posición final de LIS
  41.         int m = 1; ///LIS
  42.         for(int i=1; i<n; i++)
  43.         {
  44.             for(int j=0; j<i; j++)
  45.             {
  46.                 if(a[j] < a[i] && lis[i] < lis[j]+1)
  47.                 {
  48.                     lis[i] = lis[j]+1;
  49.                     from[i] = j;
  50.                 }
  51.             }
  52.             if(lis[i] > m)
  53.             {
  54.                 m = lis[i];
  55.                 p = i;
  56.             }
  57.         }
  58.  
  59.         stack <int> r; ///Para mostrarlos invertidos
  60.  
  61.         if(w)
  62.             cout << endl;
  63.         cout << "Max hits: " << m << endl;
  64.         r.push(a[p]);
  65.         while(from[p] != -1)
  66.         {
  67.             r.push(a[from[p]]);
  68.             p = from[p];
  69.         }
  70.         while(r.size())
  71.         {
  72.             cout << r.top() << endl;
  73.             r.pop();
  74.         }
  75.     }
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment