SHARE
TWEET

Codeforces #527 D2

a guest Dec 18th, 2018 198 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #pragma comment(linker, "/stack:200000000")
  3. #pragma GCC optimize ("Ofast")
  4. #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <cmath>
  8. #include <cstring>
  9. #include <chrono>
  10. #include <complex>
  11.  
  12.  
  13.  
  14.  
  15. #define endl "\n"
  16. #define ll long long int
  17. #define vi vector<int>
  18. #define vll vector<ll>
  19. #define vvi vector < vi >
  20. #define pii pair<int,int>
  21. #define pll pair<long long, long long>
  22. #define vpll vector < pair<long long,long long> >
  23. #define mod 1000000007
  24. #define inf 1000000000000000001;
  25. #define all(c) c.begin(),c.end()
  26. #define fill(a)  memset(a, 0, sizeof (a))
  27. #define mp make_pair
  28. #define pb push_back
  29. #define mem(a,val) memset(a,val,sizeof(a))
  30. #define eb emplace_back
  31. #define f first
  32. #define s second
  33.  
  34. #define rep(i, n)    for(long long int i = 0; i < (n); ++i)
  35. #define repA(i, a, n)  for(int i = a; i <= (n); ++i)
  36. #define repD(i, a, n)  for(int i = a; i >= (n); --i)
  37. #define trav(a, x) for(auto& a : x)
  38.  
  39.  
  40. using namespace std;
  41.  
  42.  
  43. struct StackNode
  44. {
  45.     ll data;
  46.     struct StackNode* next;
  47. };
  48.  
  49. struct StackNode* newNode(ll data)
  50. {
  51.     struct StackNode* stackNode =
  52.               (struct StackNode*) malloc(sizeof(struct StackNode));
  53.     stackNode->data = data;
  54.     stackNode->next = NULL;
  55.     return stackNode;
  56. }
  57.  
  58. ll isEmpty(struct StackNode *root)
  59. {
  60.     return !root;
  61. }
  62.  
  63. void push(struct StackNode** root, ll data)
  64. {
  65.     struct StackNode* stackNode = newNode(data);
  66.     stackNode->next = *root;
  67.     *root = stackNode;
  68.     // printf("%d pushed to stack\n", data);
  69. }
  70.  
  71. int pop(struct StackNode** root)
  72. {
  73.     if (isEmpty(*root))
  74.         return INT_MIN;
  75.     struct StackNode* temp = *root;
  76.     *root = (*root)->next;
  77.     ll popped = temp->data;
  78.     free(temp);
  79.  
  80.     return popped;
  81. }
  82.  
  83. ll peek(struct StackNode* root)
  84. {
  85.     if (isEmpty(root))
  86.         return INT_MIN;
  87.     return root->data;
  88. }
  89.  
  90.  
  91.  
  92. int main(){
  93.     std::ios::sync_with_stdio(false);
  94.         cin.tie(0);
  95.         cin.exceptions(cin.failbit);
  96.     ll t,n,i,j,d,q,x,y,r,m;
  97.     vll v;
  98.     vpll v2;
  99.     struct StackNode* root = NULL;
  100.     cin>>n;
  101.     cin>>d;
  102.     push(&root, d);
  103.     ll max = d;
  104.     ll prev = 0;
  105.     ll spl = 0;
  106.     rep(i,n){
  107.         if(i==0) continue;
  108.         cin>>d;
  109.         if((peek(root)==d)){
  110.             prev = pop(&root);
  111.             if(!isEmpty(root))
  112.             if(peek(root)<prev){
  113.                 spl = 1;
  114.                 break;
  115.             }
  116.         }
  117.         else push(&root,d);
  118.     }
  119.     ll flag = 0;
  120.     if(isEmpty(root)) flag = 1;
  121.     else{
  122.         pop(&root);
  123.         if(isEmpty(root)) flag = 1;
  124.     }
  125.     if(spl==1) flag = 0;
  126.     if(flag==1) cout<<"YES";
  127.     else cout<<"NO";
  128. }
  129.  
  130. /*input
  131.  
  132.  
  133.  
  134.  
  135. Check the end cases
  136. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top