Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <sstream>
  5. #include <unordered_set>
  6.  
  7. bool check(const std::vector<int>& seq) {
  8.     size_t n = (seq.size() + 1) / 2;
  9.     std::vector<bool> is_leave;
  10.     is_leave.reserve(n);
  11.     for (size_t it = 0; it < n; ++it) {
  12.         is_leave.push_back(false);
  13.     }
  14.  
  15.     std::vector<int> count;
  16.     count.reserve(n);
  17.     for (size_t it = 0; it < n; ++it) {
  18.         count.push_back(0);
  19.     }
  20.  
  21.     for (auto elem : seq) {
  22.         ++count[elem];
  23.     }
  24.  
  25.     for (size_t it = 0; it < n; ++it) {
  26.         if (count[it] == 1) {
  27.             is_leave[it] = true;
  28.         }
  29.     }
  30.  
  31.     std::stack<int> stack;
  32.     for (auto elem : seq) {
  33.         if (is_leave[elem]) {
  34.             continue;
  35.         }
  36.         if (!stack.empty() && stack.top() == elem) {
  37.             stack.pop();
  38.             continue;
  39.         }
  40.         stack.push(elem);
  41.     }
  42.     return stack.empty();
  43. }
  44.  
  45. bool Check(const std::vector<int>& seq) {
  46.     size_t n = (seq.size() + 1) / 2;
  47.     std::vector<bool> is_visited;
  48.     std::unordered_set<int> set;
  49.     is_visited.reserve(n);
  50.     for (size_t it = 0; it < n; ++it) {
  51.         is_visited.push_back(false);
  52.     }
  53.  
  54.     std::stack<int> stack;
  55.     stack.push(0);
  56.     for (auto elem : seq) {
  57.         if (is_visited[elem]) {
  58.             if (stack.top() == elem) {
  59.                 return false;
  60.             }
  61.             stack.pop();
  62.             if (stack.top() != elem) {
  63.                 return false;
  64.             }
  65.         } else {
  66.             stack.push(elem);
  67.             is_visited[elem] = true;
  68.             set.insert(elem);
  69.         }
  70.     }
  71.     return set.size() == n;
  72. }
  73.  
  74. std::vector<int> get_numbers(const std::string& str) {
  75.     std::vector<int> res;
  76.     res.reserve(200'000);
  77.    std::stringstream s;
  78.    s << str;
  79.    std::string temp;
  80.    int found;
  81.    while (!s.eof()) {
  82.        s >> temp;
  83.        if (std::stringstream(temp) >> found) {
  84.            res.push_back(found);
  85.        }
  86.    }
  87.    return res;
  88. }
  89.  
  90. int main() {
  91.    std::ios_base::sync_with_stdio(0);
  92.    std::vector<int> seq;
  93.    std::string str;
  94.    getline(std::cin, str);
  95.    seq = get_numbers(str);
  96.    if (Check(seq)) {
  97.        std::cout << "YES";
  98.    } else {
  99.        std::cout << "NO";
  100.    }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement