Advertisement
Ritam_C

hackerearth stack operations

Feb 21st, 2021
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1.     #include <bits/stdc++.h>
  2.     #define ll long long
  3.     #define ld long double
  4.     #define pb push_back
  5.     #define p_b pop_back
  6.     #define si stack<int>
  7.     #define sll stack<ll>
  8.     #define sc stack<char>
  9.     #define vi vector<int>
  10.     #define vll vector<ll>
  11.     #define mii map<int, int>
  12.     #define msi map<string, int>
  13.     #define mci map<char, int>
  14.     #define qc queue<char>
  15.     #define qi queue<int>
  16.     #define qll queue<ll>
  17.     using namespace std;
  18.      
  19.     int main(){
  20.         ios_base::sync_with_stdio(false);
  21.         cin.tie(NULL);
  22.         ll n, k;
  23.         cin >> n >> k;
  24.         sll s, t, buf;
  25.         for(int i = 0; i < n; i++){
  26.             ll x;
  27.             cin >> x;
  28.             buf.push(x);
  29.         }
  30.      
  31.         while(buf.size() > 0){
  32.             s.push(buf.top());
  33.             buf.pop();
  34.         }
  35.      
  36.         t.push(0);
  37.         if(k > n && n > 1){
  38.             while(s.size() > 0){
  39.                 if(t.top() <= s.top()){
  40.                     t.push(s.top());
  41.                     s.pop();
  42.                 } else{
  43.                     s.pop();
  44.                 }
  45.                 k--;
  46.             }
  47.            
  48.             s.push(t.top());
  49.             k--;
  50.      
  51.             if(k%2 == 0){
  52.                 cout << s.top() << "\n";
  53.             } else{
  54.                 cout << t.top() << "\n";
  55.             }
  56.      
  57.         } else if(n == 1){
  58.             if(k%2 == 0){
  59.                 cout << s.top() << "\n";
  60.             } else{
  61.                 cout << "-1\n";
  62.             }
  63.         } else{
  64.             while(k-- > 1){
  65.                 if(t.top() <= s.top()){
  66.                     t.push(s.top());
  67.                     s.pop();
  68.                 } else{
  69.                     s.pop();
  70.                 }
  71.             }
  72.      
  73.             s.push(t.top());
  74.             cout << s.top() << "\n";
  75.         }
  76.         return 0;
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement