Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int BinarySearch(std::vector<int> &v, int l, int r, int key) {
  5.     while (r-l > 1) {
  6.         int m = l + (r-l)/2;
  7.         if (v[m] >= key) {
  8.             std::cout << v[m] << " ";
  9.             r = m;
  10.         } else {
  11.             std::cout << v[m] << " ";
  12.             l = m;
  13.         }
  14.     }
  15.  
  16.     return r;
  17. }
  18.  
  19. void  LongestIncreasingSequence(std::vector<int> &test) {
  20.     if (test.size() == 0)
  21.         return;
  22.     for (int i = 0; i < test.size(); i++)
  23.         std::cout << test[i] << " ";
  24.     std::cout << "\n";
  25.    
  26.     std::vector<int> gosho(test.size(), 0);
  27.     int length = 1; // always points empty slot in tail
  28.  
  29.     gosho[0] = test[0];
  30.     for (size_t i = 1; i < test.size(); i++) {
  31.         if (test[i] < gosho[0])
  32.             gosho[0] = test[i];
  33.         else if (test[i] > gosho[length-1])
  34.             gosho[length++] = test[i];
  35.         else {
  36.             gosho[BinarySearch(gosho, -1, length, test[i])] = test[i];
  37.             std::cout << "\n tail = ";
  38.             for (int i = 0; i < length; i++)
  39.                 std::cout << gosho[i] << " ";
  40.             std::cout << "\n";
  41.         }
  42.     }
  43.     std::cout << "\n";
  44.     for (int i = 0; i < length; i++)
  45.         std::cout << gosho[i] << " ";
  46. }
  47.  
  48. int main() {
  49.     std::vector<int> test{ 11, 12, 13, 3, 14, 4, 15, 5, 6, 7, 8, 7, 16, 9, 8 };
  50.     LongestIncreasingSequence(test);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement