GastonFontenla

UVa: 231 - Testing the CATCHER

May 28th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. vector <int> a, t, r;
  8. int n;
  9.  
  10. ///La idea es que ceil me busque el menor valor que es mayor al dado
  11. int ceil(int v)
  12. {
  13.     if(v > a[t[n]])
  14.         return -1;
  15.     int s = 0, e = n;
  16.     while(s+1 < e)
  17.     {
  18.         int m = (s+e)/2;
  19.         if(a[t[m]] > v)
  20.             e = m;
  21.         else
  22.             s = m;
  23.     }
  24.     if(a[t[s]] > v)
  25.         return s;
  26.     return e;
  27. }
  28.  
  29. int main()
  30. {
  31.     int k, test = 0;
  32.     cin >> k;
  33.     while(k != -1)
  34.     {
  35.         a.clear();
  36.  
  37.         while(k != -1)
  38.         {
  39.             a.push_back(k);
  40.             cin >> k;
  41.         }
  42.  
  43.         reverse(a.begin(), a.end());
  44.  
  45.         r = vector <int> (a.size(), -1);
  46.         t = vector <int> (1, 0);
  47.         n = 0;
  48.  
  49.         for(int i=1; i<a.size(); i++)
  50.         {
  51.             if(a[i] > a[t[n]])
  52.             {
  53.                 r[i] = t[n];
  54.                 t.push_back(i);
  55.                 n++;
  56.             }
  57.             else
  58.             {
  59.                 int p = ceil(a[i]);
  60.                 if(p >= 0 && p <= n)
  61.                 {
  62.                     t[p] = i;
  63.                     if(p)
  64.                         r[i] = t[p-1];
  65.                 }
  66.             }
  67.         }
  68.  
  69.         if(test)
  70.             cout << endl;
  71.  
  72.         cout << "Test #" << test+1 << ":" << endl;
  73.         cout << "  maximum possible interceptions: " << n+1 << endl;
  74.  
  75.         cin >> k;
  76.         test++;
  77.     }
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment