4eyes4u

Monotonic Queue [prvi '<']

Aug 31st, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N=1e5+10;
  5.  
  6. int a[N], f[N][2], n;
  7. stack<int> box;
  8.  
  9. int main()
  10. {
  11.     scanf ("%d", &n);
  12.     for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
  13.  
  14.     for (int i=1;i<=n;i++)
  15.     {
  16.         while (!box.empty())
  17.         {
  18.             int xt=box.top();
  19.             if (a[xt]>a[i])
  20.             {
  21.                 f[i][0]=xt;
  22.                 break;
  23.             }
  24.             else box.pop();
  25.         }
  26.  
  27.         box.push(i);
  28.     }
  29.  
  30.     while (!box.empty()) box.pop();
  31.  
  32.     for (int i=n;i>0;i--)
  33.     {
  34.         while (!box.empty())
  35.         {
  36.             int xt=box.top();
  37.             if (a[xt]>a[i])
  38.             {
  39.                 f[i][1]=xt;
  40.                 break;
  41.             }
  42.             else box.pop();
  43.         }
  44.  
  45.         box.push(i);
  46.     }
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment