Advertisement
royalsflush

What Goes Up - UVa 481 (PD do Algorithmist)

Sep 18th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <string.h>
  4. #include <stack>
  5. using namespace std;
  6.  
  7. const int inf =0x3f3f3f3f;
  8. const int MAXN = 100010;
  9.  
  10. stack<pair<int, int> > dp[MAXN];
  11. int a[MAXN];
  12. int n;
  13.  
  14. int find(int x) {
  15.     int beg=0, end=n-1;
  16.  
  17.     while (beg<end) {
  18.         int mid=(beg+end)/2;
  19.  
  20.         if (dp[mid].top().first>=x) end=mid;
  21.         else beg=mid+1;
  22.     }
  23.  
  24.     return beg;
  25. }
  26.  
  27. int main() {
  28.     n=0;
  29.     while (scanf("%d", &a[n++])!=EOF);
  30.     for (int i=0; i<n; i++)
  31.         dp[i].push(make_pair(inf,-1));
  32.     n--;
  33.  
  34.     dp[1].push(make_pair(a[0],0));
  35.     for (int i=1; i<n; i++) {
  36.         int idx=find(a[i]);
  37.         dp[idx].push(make_pair(a[i],i));
  38.     }
  39.  
  40.     int sz;
  41.     for (int i=1; i<n+1; i++)
  42.         if (dp[i].top().first==inf) {
  43.             sz=i-1;
  44.             printf("%d\n-\n",i-1);
  45.             break;
  46.         }
  47.  
  48.     stack<int> s;
  49.     int prev=inf;
  50.  
  51.     for (sz; sz>0; sz--) {
  52.         while (prev<=dp[sz].top().second)
  53.             dp[sz].pop();
  54.         s.push(dp[sz].top().first);
  55.         prev=dp[sz].top().second;
  56.     }
  57.  
  58.    
  59.  
  60.     while (!s.empty()) {
  61.         printf("%d\n", s.top());
  62.         s.pop();
  63.     }
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement