Dang_Quan_10_Tin

Untitled

Mar 7th, 2021
452
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. using ld = long double;
  5. using ull = unsigned long long;
  6.  
  7. const bool typetest = 0;
  8. const int N = 5e3 + 5;
  9. string s;
  10. int a[N], n, maxn[N][14], minn[N][14];
  11. int piv(n + 1), last;
  12.  
  13. void Read()
  14. {
  15.     cin >> s;
  16.     n = s.size();
  17.     s = " " + s;
  18.     piv = n + 1;
  19.     for (int i = 1; i <= n; ++i)
  20.     {
  21.         a[i] = a[i - 1] + (s[i] == '(' ? 1 : -1);
  22.         maxn[i][0] = minn[i][0] = a[i];
  23.         if (piv == n + 1 && a[i] < 0)
  24.             piv = i + 1;
  25.         if (a[i] < 0)
  26.             last = i;
  27.     }
  28. }
  29.  
  30. int Getmin(int l, int r)
  31. {
  32.     if (l > r)
  33.         return 10000000;
  34.     int loga = log2(r - l + 1);
  35.     return min(minn[l][loga], minn[r - (1 << loga) + 1][loga]);
  36. }
  37.  
  38. int Getmax(int l, int r)
  39. {
  40.     if (l > r)
  41.         return 0;
  42.     int loga = log2(r - l + 1);
  43.     return max(maxn[l][loga], maxn[r - (1 << loga) + 1][loga]);
  44. }
  45.  
  46. void Solve()
  47. {
  48.     for (int i = 1; i < 14; ++i)
  49.         for (int j = 1; j <= n - (1 << i) + 1; ++j)
  50.         {
  51.             maxn[j][i] = max(maxn[j][i - 1], maxn[j + (1 << (i - 1))][i - 1]);
  52.             minn[j][i] = min(minn[j][i - 1], minn[j + (1 << (i - 1))][i - 1]);
  53.         }
  54.     if (piv == n + 1 && a[n] == 0)
  55.     {
  56.         cout << "possible";
  57.         return;
  58.     }
  59.     for (int i = 1; i < piv; ++i)
  60.         for (int j = i; j <= n; ++j)
  61.         {
  62.             int maxx = Getmax(i, j);
  63.             maxx = maxx - 2 * (maxx - a[i - 1]);
  64.             int minx = Getmin(i, j);
  65.             minx = minx - 2 * (minx - a[i - 1]);
  66.             int sufmin = Getmin(j + 1, n);
  67.             sufmin = sufmin - a[j] + (a[j] - 2 * (a[j] - a[i - 1]));
  68.             //cout << i << " " << j << " " << maxx << " " << minx << " " << sufmin << " " << a[n] - a[j] + (a[j] - 2 * (a[j] - a[i - 1])) << "\n";
  69.             if (maxx >= 0 && minx >= 0 && sufmin >= 0 && a[n] - a[j] + (a[j] - 2 * (a[j] - a[i - 1])) == 0)
  70.             {
  71.                 cout << "possible";
  72.                 return;
  73.             }
  74.         }
  75.     cout << "impossible";
  76. }
  77.  
  78. int32_t main()
  79. {
  80.     ios::sync_with_stdio(0);
  81.     cin.tie(0);
  82.     cout.tie(0);
  83.     int t(1);
  84.     if (typetest)
  85.         cin >> t;
  86.     for (int _ = 1; _ <= t; ++_)
  87.     {
  88.         Read();
  89.         Solve();
  90.     }
  91. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×