Advertisement
Guest User

Untitled

a guest
Apr 26th, 2020
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. //
  2. // Created by Ильдар Ялалов on 14.01.2020.
  3. //
  4. //#pragma GCC optimize("Ofast")
  5. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  6.  
  7. #include <bits/stdc++.h>
  8.  
  9. using namespace std;
  10. typedef long long ll;
  11. typedef unsigned long long ull;
  12. const int inf_int = 1e9 + 100;
  13. const ll inf_ll = 1e18;
  14. typedef pair<int, int> pii;
  15. typedef pair<ll, ll> pll;
  16. typedef double dbl;
  17. typedef unsigned int uint;
  18. #define pb push_back
  19. #define eb emplace_back
  20. const double pi = 3.1415926535898;
  21. #define dout if(debug) cout
  22. #define fi first
  23. #define se second
  24. #define sp setprecision
  25. #define sz(a) (int(a.size()))
  26. #define mp make_pair
  27. #define all(a) a.begin(),a.end()
  28.  
  29.  
  30. #ifdef zxc
  31.  
  32. #include "debug.h"
  33.  
  34. #define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  35. #else
  36. #define debug(...) 42
  37. #endif
  38.  
  39. bool debug = 0;
  40. const int MAXN = (4e5) + 100;
  41. const int LOG = 21;
  42. const int mod = 1e9 + 7;
  43. const int MX = (3e7 + 100);
  44.  
  45.  
  46. int a[MAXN];
  47.  
  48. struct vertex {
  49.     int pref;
  50.     int suf;
  51.     int total;
  52.  
  53.     vertex() {
  54.     }
  55. };
  56.  
  57. #define left v<<1,tl,tm
  58. #define right v<<1|1,tm+1,tr
  59.  
  60.  
  61. vertex t[4 * MAXN];
  62.  
  63. void upd(int v) {
  64.     int l = v << 1;
  65.     int r = v << 1 | 1;
  66.     t[v].total = t[l].total + t[r].total;
  67.     t[v].suf = min(t[r].suf, t[l].suf + t[r].total);
  68.     t[v].pref = min(t[l].pref, t[l].total + t[r].pref);
  69. }
  70.  
  71. void build(int v, int tl, int tr) {
  72.     if (tl == tr) {
  73.         t[v].pref = 0;
  74.         t[v].suf = 0;
  75.         t[v].total = 1;
  76.     } else {
  77.         int tm = (tl + tr) >> 1;
  78.         build(left);
  79.         build(right);
  80.         upd(v);
  81.     }
  82. }
  83.  
  84. void update(int v, int tl, int tr, int pos, bool open) {
  85.     if (tl == tr) {
  86.         if (open) {
  87.             t[v].suf = -1;
  88.             t[v].pref = -1;
  89.             t[v].total = -1;
  90.         } else {
  91.             t[v].suf = 0;
  92.             t[v].pref = 0;
  93.             t[v].total = 1;
  94.         }
  95.     } else {
  96.         int tm = (tl + tr) >> 1;
  97.         if (pos <= tm) {
  98.             update(left, pos, open);
  99.         } else {
  100.             update(right, pos, open);
  101.         }
  102.         upd(v);
  103.     }
  104. }
  105.  
  106. int used[MAXN];
  107. int sec[MAXN];
  108. int res[MAXN];
  109.  
  110. void solve() {
  111.     int n;
  112.     cin >> n;
  113.     for (int i = 1; i <= 2 * n; ++i) {
  114.         cin >> a[i];
  115.     }
  116.     n = n * 2;
  117.     build(1, 1, n);
  118.     for (int i = 1; i <= n; ++i) {
  119.         sec[a[i]] = i;
  120.     }
  121.     for (int i = 1; i <= n; ++i) {
  122.         if (used[a[i]])
  123.             continue;
  124.         int r = sec[a[i]];
  125.         used[a[i]] = 1;
  126.         if (r == n)
  127.             continue;
  128.         update(1, 1, n, i, true);
  129.         update(1, 1, n, r, true);
  130.         res[i] = res[r] = 1;
  131.         if (t[1].suf < 0) {
  132.             update(1, 1, n, i, false);
  133.             update(1, 1, n, r, false);
  134.             res[i] = res[r] = 0;
  135.         }
  136.     }
  137.     string ans = "";
  138.     int cnt = 0;
  139.     for(int i = 1;i<=n;++i){
  140.         debug(res[i]);
  141.     }
  142.     for (int i = 1; i <= n; ++i) {
  143.         if (res[i]) {
  144.             cnt++;
  145.             ans.pb('(');
  146.         } else {
  147.             cnt--;
  148.             ans.pb(')');
  149.         }
  150.         if (cnt < 0) {
  151.             cout << "(";
  152.             return;
  153.         }
  154.     }
  155.     if (cnt != 0) {
  156.         cout << "(";
  157.     } else {
  158.         cout << ans << "\n";
  159.     }
  160.  
  161. }
  162.  
  163. // CHECK LIMITS (n <= 10^5)
  164. // CHECK CORNER CASES ( n==1)
  165. signed main() {
  166.  
  167. #ifdef zxc
  168.     freopen("../input.txt", "r", stdin);
  169. //    freopen("../output.txt", "w", stdout);
  170. #else
  171. #endif //zxc
  172.     ios_base::sync_with_stdio(0);
  173.     cin.tie(0);
  174.     cout.tie(0);
  175.     cout.setf(ios::fixed);
  176.     cout.precision(2);
  177.  
  178.  
  179.     int t = 1;
  180.     while (t--) {
  181.         solve();
  182.     }
  183.     debug(1.0 * clock() / CLOCKS_PER_SEC);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement