alexpak

5 - Накопитель

Apr 14th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define DB(v) cerr << #v << ' ' << v << endl
  4. #define sz(v) int(v.size())
  5. #define For(i, a, b) for(int i = a;i <= b; ++i)
  6.  
  7. const int N = 1e5;
  8.  
  9. template <typename T>
  10. inline ostream &operator << (ostream &out, const vector <T> &v) {
  11.     for(auto to: v)
  12.         out << to << ' ';
  13.     out << '\n';
  14.     return out;
  15. }
  16.  
  17. vector <int> arr, index_arr;
  18. string symbol;
  19.  
  20. string s, t;
  21.  
  22. void make_arr () {
  23.     arr.clear();
  24.     index_arr.resize(sz(s));
  25.     symbol = "";
  26.     for(int i = 0;i < sz(s); ++i) {
  27.         int j = i;
  28.         while(j < sz(s) - 1 && s[j + 1] == s[j])
  29.             j++;
  30.         arr.push_back(j - i + 1);
  31.         for(int k = i;k <= j; ++k)
  32.             index_arr[k] = sz(arr) - 1;
  33.         symbol += s[i];
  34.         i = j;
  35.     }
  36. }
  37.  
  38. int oper = 0;
  39.  
  40. bool can(int l_border, int r_border, int i, char need) {
  41.     if(symbol[i] != need)
  42.         return false;
  43.  
  44.     int l = i - 1, r = i + 1, amount = arr[i];
  45.     while(l >= l_border || r <= r_border) {
  46.         oper++;
  47.         bool found = false;
  48.         if(l >= l_border && amount > arr[l]) {
  49.             found = true;
  50.             amount += arr[l];
  51.             if(l > l_border)
  52.                 amount += arr[l - 1];
  53.             l -= 2;
  54.         }
  55.         if(r <= r_border && amount > arr[r]) {
  56.             found = true;
  57.             amount += arr[r];
  58.             if(r + 1 <= r_border)
  59.                 amount += arr[r + 1];
  60.             r += 2;
  61.         }
  62.         if(!found)
  63.             return false;
  64.     }
  65.     return true;
  66. }
  67.  
  68. bool good (int l, int r) {
  69.     if(l != 0 && s[l] != t[l])
  70.         return false;
  71.     if(r != sz(s) - 1 && s[r] != t[r])
  72.         return false;
  73.     int l_border = index_arr[l], r_border = index_arr[r];
  74.     char need = t[l];
  75.  
  76.     DB(l_border); DB(r_border);
  77.     for(int i = l_border; i <= r_border; ++i) {
  78.         if(can(l_border, r_border, i, need)) {
  79.             return true;
  80.         }
  81.     }
  82.     return false;
  83. }
  84.  
  85. void generate_input() {
  86.     s = "";
  87.     string add = "++-";
  88.     while(sz(s) + 3 < N / 2) {
  89.         s += add;
  90.     }
  91.     for(int i = 0;i < N / 2; ++i)
  92.         s += '-';
  93.  
  94.     t = "";
  95.     for(int i = 0;i < sz(s); ++i)
  96.         t += '+';
  97. }
  98.  
  99. int main()
  100. {
  101. #ifdef HOME
  102.     freopen("input.txt","r",stdin);
  103. #endif // HOME
  104.     ios::sync_with_stdio(NULL); cin.tie(NULL);
  105.     int Q; cin >> Q;
  106.     For(q, 1, Q) {
  107.         //cin >> s >> t;
  108.         generate_input();
  109.        // cout << s << endl << t << endl;
  110.         DB(sz(s)); DB(sz(t));
  111.         make_arr();
  112.  
  113.         bool ans = true;
  114.         for(int i = 0;i < sz(t); ++i) {
  115.             int j = i;
  116.             while(j + 1 < sz(t) && t[j + 1] == t[j]) {
  117.                 j++;
  118.             }
  119.             if(!good(i, j)) {
  120.                 ans = false;
  121.                 break;
  122.             }
  123.             i = j;
  124.         }
  125.         DB(oper);
  126.  
  127.         if(ans)
  128.             cout << "Yes";
  129.         else
  130.             cout << "No";
  131.         cout << '\n';
  132.     }
  133.     return 0;
  134. }
Add Comment
Please, Sign In to add comment