fork download
  1. // #pragma once // huu khanh chy
  2. #include <bits/stdc++.h>
  3.  
  4. // #pragma GCC optimize("Ofast")
  5. // #pragma GCC optimize("O3")
  6. // #pragma GCC optimize("unroll-loops")
  7. // #pragma GCC target("avx,avx2")
  8. // #pragma GCC target("bmi,bmi2")
  9. // #pragma GCC target("popcnt,lzcnt")
  10.  
  11. // #pragma GCC optimize("Os")
  12. // #pragma GCC optimize("O2")
  13. // #pragma GCC optimize("inline")
  14. // #pragma GCC optimize("fast-math")
  15. // #pragma GCC target("fma")
  16. // #pragma GCC target("sse,sse2")
  17. // #pragma GCC target("sse3,ssse3")
  18. // #pragma GCC target("sse4.1,sse4.2")
  19.  
  20. #define FOR(i, n) for(long long (i) = 0; (i) < (n); ++(i))
  21. #define forr(i, l, r, k) for (long long i = (l); i <= (r); i += (k))
  22. #define rfor(i, r, l, k) for (long long i = (r); i >= (l); i -= (k))
  23. #define SORT(a) sort((a).begin(), (a).end())
  24. #define RSORT(a) sort((a).begin(), (a).end(), greater<long long>())
  25. #define sortt(a, type) sort((a).begin(), (a).end(), type)
  26. #define for_Cout(a, char) for (auto c : (a)) cout << c << char;
  27. #define REV(s) reverse((s).begin(), (s).end())
  28. #define mset(a, valueptr) memset(a, valueptr, sizeof a)
  29. #define biton(x, i) ((x) >> (i) & 1)
  30. #define MASK(i) (1ll << (i))
  31. #define TIME cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"
  32. #define int long long
  33. #define nn "\n"
  34. #define fi first
  35. #define se second
  36. #define pb push_back
  37. #define ins insert
  38. #define All(x) (x).begin(), (x).end()
  39. #define pii pair<int, int>
  40. #define pli pair<long long, int>
  41. #define pll pair<long long, long long>
  42. #define vll vector<long long>
  43. #define vit vector<int>
  44. #define vbl vector<bool>
  45. #define vstr vector<string>
  46. #define v(datatype) vector<datatype>
  47. #define vvll vector<vector<long long>>
  48. #define TASK "name"
  49.  
  50. template<typename... value> void inall(value&... valueofvalue) { ((std::cin >> valueofvalue), ...); }
  51. template<typename... value> void outall(char valueofchar, const value&... valueofvalue) { ((std::cout << valueofvalue << valueofchar), ...); std::cout << valueofchar; }
  52. template<class X, class Y> bool maximize(X& x, const Y& y) { if (x < y) { x = y; return true; } return false; }
  53. template<class X, class Y> bool minimize(X& x, const Y& y) { if (x > y) { x = y; return true; } return false; }
  54.  
  55. using namespace std;
  56.  
  57. inline void fastIO() noexcept(true) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }
  58. inline long long ucln(long long a, long long b) { while (a != 0) { long long uc = a; a = b % a ; b = uc; } return b; }
  59. inline long long bcnn(long long a, long long b) { long long res = (a * b) / ucln(a, b); return res; }
  60. inline long long luythua(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res *= a; } a = a * a; b >>= 1; } return res; }
  61. inline long long giathua(long long num) { unsigned long long res = 1; for (unsigned long long i = 2; i <= num; ++i) res *= i; return res; }
  62. inline long long luythualaydu (long long a, long long b, long long mod) { long long res = 1; a = a % mod; while (b > 0) { if (b & 1) { res = (res * a) % mod; } a = (a * a) % mod; b >>= 1; } return res; }
  63. inline long long giathualaydu (long long num, long long mod) { unsigned long long res = 1; for (unsigned long long i = 2; i <= num; ++i) res = (res * i) % mod; return res; }
  64.  
  65. typedef char cr;
  66. typedef string str;
  67. typedef long long ll;
  68. typedef unsigned long long ull;
  69. typedef double db;
  70. typedef bool bl;
  71. typedef long double ldb;
  72.  
  73. constexpr long long MOD1 = 1000000007LL;
  74. constexpr long long MOD2 = 1000000009LL;
  75. constexpr long long MOD3 = 2147483647LL;
  76. constexpr long long INF = 1000000000000000000LL;
  77. constexpr int base1= 310;
  78. constexpr int base2 = 256;
  79. constexpr long long N = 1e4 + 5;
  80.  
  81. struct node {
  82. int u, v, w;
  83. };
  84. // int n, m, h[MAXn], dp[MAXn][17], nhim[MAXn], nguyet[MAXn];
  85. // ll ans = 0; v(pii) a[MAXn]; v(node) tmp;
  86. // void input() noexcept(true) {
  87. // inall(n, m);
  88. // forr(i, 1, m, 1) {
  89. // int u, v, w; inall(u, v, w);
  90. // a[u].pub({v, w});
  91. // a[v].pub({u, w});
  92. // tmp.pub({u, v, w});
  93. // }
  94. // TIME;
  95. // }
  96. // void dfs(int u, int fu, int hei) noexcept(true) {
  97. // dp[u][0] = fu; h[u] = hei;
  98. // for (int i = 1; i < 17; ++i) {
  99. // dp[u][i] = dp[dp[u][i - 1]][i - 1];
  100. // }
  101. // for (auto [v, w] : a[u]) {
  102. // if (v != fu) dfs(v, u, hei + 1);
  103. // }
  104. // }
  105. // int lca(int u, int v) {
  106. // if (h[u] < h[v]) swap(u, v);
  107. // int d = h[u] - h[v];
  108. // for (int i = 0; i < 17; ++i) {
  109. // if (biton(d, i)) u = dp[u][i];
  110. // }
  111. // if (u == v) return u;
  112. // for (int i = 16; i >= 0; --i) {
  113. // if (dp[u][i] != dp[v][i]) {
  114. // u = dp[u][i]; v = dp[v][i];
  115. // }
  116. // }
  117. // return dp[u][0];
  118. // }
  119. // void dfs2(int u, int fu) noexcept(true) {
  120. // for (auto [v, w] : a[u]) {
  121. // if (v == fu) continue;
  122. // dfs2(v, u);
  123. // if (nhim[v] >= w) ans += 1LL * (nhim[v] - w);
  124. // nhim[u] = max(nhim[u], nhim[v]);
  125. // }
  126. // if (nhim[u] <= nguyet[u]) nhim[u] = 0;
  127. // }
  128. // void output() noexcept(true) {
  129. // dfs(1, 1, 0);
  130. // for (int i = 0; i < m; ++i) {
  131. // node k = tmp[i];
  132. // int l = lca(k.u, k.v);
  133. // nguyet[l] = max(nguyet[l], k.w);
  134. // nhim[k.u] = max(nhim[k.u], k.w);
  135. // nhim[k.v] = max(nhim[k.v], k.w);
  136. // }
  137. // dfs2(1, 1);
  138. // cout << ans;
  139. // TIME;
  140. // }
  141. // struct node {
  142. // int u, v, w;
  143. // };
  144. // int n, m, h[MAXn], dp[MAXn][17], nhim[MAXn], nguyet[MAXn];
  145. // int par[MAXn], par_w[MAXn], order_arr[MAXn], ord_cnt;
  146. // ll ans = 0; v(pii) a[MAXn]; v(node) tmp;
  147. //
  148. // void input() noexcept(true) {
  149. // inall(n, m);
  150. // forr(i, 1, m, 1) {
  151. // int u, v, w; inall(u, v, w);
  152. // a[u].pub({v, w});
  153. // a[v].pub({u, w});
  154. // tmp.pub({u, v, w});
  155. // }
  156. // TIME;
  157. // }
  158. //
  159. // void bfs() noexcept(true) {
  160. // queue<int> q;
  161. // v(bool) vis(n + 1, false);
  162. // par[1] = 1; par_w[1] = 0; h[1] = 0;
  163. // vis[1] = true; q.push(1);
  164. // while (!q.empty()) {
  165. // int u = q.front(); q.pop();
  166. // order_arr[ord_cnt++] = u;
  167. // dp[u][0] = par[u];
  168. // for (int i = 1; i < 17; ++i)
  169. // dp[u][i] = dp[dp[u][i-1]][i-1];
  170. // for (auto [v, w] : a[u]) {
  171. // if (!vis[v]) {
  172. // vis[v] = true;
  173. // par[v] = u;
  174. // par_w[v] = w;
  175. // h[v] = h[u] + 1;
  176. // q.push(v);
  177. // }
  178. // }
  179. // }
  180. // }
  181. //
  182. // int lca(int u, int v) {
  183. // if (h[u] < h[v]) swap(u, v);
  184. // int d = h[u] - h[v];
  185. // for (int i = 0; i < 17; ++i) {
  186. // if (biton(d, i)) u = dp[u][i];
  187. // }
  188. // if (u == v) return u;
  189. // for (int i = 16; i >= 0; --i) {
  190. // if (dp[u][i] != dp[v][i]) {
  191. // u = dp[u][i]; v = dp[v][i];
  192. // }
  193. // }
  194. // return dp[u][0];
  195. // }
  196. //
  197. // void solve() noexcept(true) {
  198. // for (int i = ord_cnt - 1; i >= 1; --i) {
  199. // int v = order_arr[i];
  200. //
  201. // if (nhim[v] <= nguyet[v]) nhim[v] = 0;
  202. //
  203. // int u = par[v];
  204. // int w = par_w[v];
  205. //
  206. // if (nhim[v] > w) ans += 1LL * (nhim[v] - w);
  207. // nhim[u] = max(nhim[u], nhim[v]);
  208. // }
  209. // }
  210. //
  211. // void output() noexcept(true) {
  212. // bfs();
  213. // for (int i = 0; i < m; ++i) {
  214. // node k = tmp[i];
  215. // int l = lca(k.u, k.v);
  216. // nguyet[l] = max(nguyet[l], k.w);
  217. // nhim[k.u] = max(nhim[k.u], k.w);
  218. // nhim[k.v] = max(nhim[k.v], k.w);
  219. // }
  220. // solve();
  221. // cout << ans;
  222. // TIME;
  223. // }
  224.  
  225. // struct node {
  226. // int u, v, w;
  227. // };
  228. // int n, m, h[MAXn], dp[MAXn][17], nhim[MAXn], nguyet[MAXn];
  229. // int par[MAXn], par_w[MAXn], order_arr[MAXn], ord_cnt;
  230. // int dsu[MAXn];
  231. // ll ans = 0;
  232. // v(pii) a[MAXn]; // chỉ chứa cạnh MST
  233. // v(node) tmp; // tất cả cạnh gốc
  234. //
  235. // // ─── DSU ─────────────────────────────────────────────
  236. // int find(int x) { return dsu[x] == x ? x : dsu[x] = find(dsu[x]); }
  237. // bool unite(int x, int y) {
  238. // x = find(x); y = find(y);
  239. // if (x == y) return false;
  240. // dsu[x] = y; return true;
  241. // }
  242. //
  243. // void input() noexcept(true) {
  244. // inall(n, m);
  245. // forr(i, 1, m, 1) {
  246. // int u, v, w; inall(u, v, w);
  247. // tmp.pub({u, v, w}); // KHÔNG add vào a[] ở đây
  248. // }
  249. // TIME;
  250. // }
  251. //
  252. // // ─── Kruskal MAX spanning tree → add vào a[] ─────────
  253. // void build_mst() noexcept(true) {
  254. // forr(i, 1, n, 1) dsu[i] = i;
  255. // sort(tmp.begin(), tmp.end(), [](const node& x, const node& y){
  256. // return x.w > y.w; // giảm dần → MAX spanning tree
  257. // });
  258. // for (auto& k : tmp) {
  259. // if (unite(k.u, k.v)) {
  260. // a[k.u].pub({k.v, k.w});
  261. // a[k.v].pub({k.u, k.w});
  262. // }
  263. // }
  264. // }
  265. //
  266. // // ─── BFS thay dfs ────────────────────────────────────
  267. // void bfs() noexcept(true) {
  268. // queue<int> q;
  269. // v(bool) vis(n + 1, false);
  270. // par[1] = 1; par_w[1] = 0; h[1] = 0;
  271. // vis[1] = true; q.push(1);
  272. // while (!q.empty()) {
  273. // int u = q.front(); q.pop();
  274. // order_arr[ord_cnt++] = u;
  275. // dp[u][0] = par[u];
  276. // for (int i = 1; i < 17; ++i)
  277. // dp[u][i] = dp[dp[u][i-1]][i-1];
  278. // for (auto [v, w] : a[u]) {
  279. // if (!vis[v]) {
  280. // vis[v] = true;
  281. // par[v] = u;
  282. // par_w[v] = w;
  283. // h[v] = h[u] + 1;
  284. // q.push(v);
  285. // }
  286. // }
  287. // }
  288. // }
  289. //
  290. // int lca(int u, int v) {
  291. // if (h[u] < h[v]) swap(u, v);
  292. // int d = h[u] - h[v];
  293. // for (int i = 0; i < 17; ++i) {
  294. // if (biton(d, i)) u = dp[u][i];
  295. // }
  296. // if (u == v) return u;
  297. // for (int i = 16; i >= 0; --i) {
  298. // if (dp[u][i] != dp[v][i]) {
  299. // u = dp[u][i]; v = dp[v][i];
  300. // }
  301. // }
  302. // return dp[u][0];
  303. // }
  304. //
  305. // // ─── Solve thay dfs2 ─────────────────────────────────
  306. // void solve() noexcept(true) {
  307. // for (int i = ord_cnt - 1; i >= 1; --i) {
  308. // int v = order_arr[i];
  309. //
  310. // if (nhim[v] <= nguyet[v]) nhim[v] = 0;
  311. //
  312. // int u = par[v];
  313. // int w = par_w[v];
  314. //
  315. // if (nhim[v] > w) ans += 1LL * (nhim[v] - w);
  316. // nhim[u] = max(nhim[u], nhim[v]);
  317. // }
  318. // }
  319. //
  320. // void output() noexcept(true) {
  321. // build_mst(); // build MST trước
  322. // bfs(); // BFS trên MST
  323. //
  324. // // dùng TẤT CẢ m cạnh gốc (tmp đã sort nhưng không ảnh hưởng)
  325. // for (auto& k : tmp) {
  326. // int l = lca(k.u, k.v);
  327. // nguyet[l] = max(nguyet[l], k.w);
  328. // nhim[k.u] = max(nhim[k.u], k.w);
  329. // nhim[k.v] = max(nhim[k.v], k.w);
  330. // }
  331. // solve();
  332. // cout << ans;
  333. // TIME;
  334. // }
  335. // int n, m, h[MAXn];
  336. // int dp[MAXn][17]; // binary lifting cha
  337. // int mn[MAXn][17]; // min edge trên path lên 2^i bước
  338. // int par[MAXn], par_w[MAXn], order_arr[MAXn], ord_cnt;
  339. // int dsu[MAXn];
  340. // ll ans = 0;
  341. // v(pii) a[MAXn]; // chỉ cạnh MST
  342. // v(node) tmp;
  343.  
  344. // // DSU
  345. // int find(int x) { return dsu[x] == x ? x : dsu[x] = find(dsu[x]); }
  346. // bool unite(int x, int y) {
  347. // x = find(x); y = find(y);
  348. // if (x == y) return false;
  349. // dsu[x] = y; return true;
  350. // }
  351.  
  352.  
  353. // Kruskal MAX spanning tree → build a[]
  354. // void build_mst() noexcept(true) {
  355. // forr(i, 1, n, 1) dsu[i] = i;
  356. // sort(tmp.begin(), tmp.end(), [](const node& x, const node& y){
  357. // return x.w > y.w;
  358. // });
  359. // for (auto& k : tmp)
  360. // if (unite(k.u, k.v)) {
  361. // a[k.u].pub({k.v, k.w});
  362. // a[k.v].pub({k.u, k.w});
  363. // }
  364. // }
  365. //
  366. // // BFS build LCA + min-edge sparse table
  367. // void bfs() noexcept(true) {
  368. // queue<int> q;
  369. // v(bool) vis(n + 1, false);
  370. // par[1] = 1; par_w[1] = 0; h[1] = 0;
  371. // vis[1] = true; q.push(1);
  372. // while (!q.empty()) {
  373. // int u = q.front(); q.pop();
  374. // order_arr[ord_cnt++] = u;
  375. // dp[u][0] = par[u];
  376. // mn[u][0] = par_w[u]; // cạnh u -> cha
  377. // for (int i = 1; i < 17; ++i) {
  378. // dp[u][i] = dp[dp[u][i-1]][i-1];
  379. // mn[u][i] = min(mn[u][i-1], mn[dp[u][i-1]][i-1]);
  380. // }
  381. // for (auto [v, w] : a[u])
  382. // if (!vis[v]) {
  383. // vis[v] = true;
  384. // par[v] = u; par_w[v] = w;
  385. // h[v] = h[u] + 1;
  386. // q.push(v);
  387. // }
  388. // }
  389. // }
  390. //
  391. // int lca_query(int u, int v) {
  392. // int res = INT_MAX;
  393. // if (h[u] < h[v]) swap(u, v);
  394. // int d = h[u] - h[v];
  395. // for (int i = 0; i < 17; ++i)
  396. // if (biton(d, i)) { res = min(res, mn[u][i]); u = dp[u][i]; }
  397. // if (u == v) return res;
  398. // for (int i = 16; i >= 0; --i)
  399. // if (dp[u][i] != dp[v][i]) {
  400. // res = min(res, mn[u][i]);
  401. // res = min(res, mn[v][i]);
  402. // u = dp[u][i]; v = dp[v][i];
  403. // }
  404. // res = min(res, mn[u][0]);
  405. // res = min(res, mn[v][0]);
  406. // return res;
  407. // }
  408. int n;
  409. int a[N];
  410. char c[4 * N];
  411. void input() noexcept(true) {
  412. cin >> n;
  413. for(int i = 1; i <= n; i++){
  414. cin >> a[i];
  415. }
  416. }
  417.  
  418. void output() noexcept(true) {
  419. int j = 1;
  420. for(int i = 1; i <= 2 * n; i++){
  421. if(c[i] == 0){
  422. c[i] = '(';
  423. c[i + a[j] + 1] = ')';
  424. j++;
  425. }
  426. }
  427. for(int i = 1; i <= 2 * n; i++){
  428. cout << c[i];
  429. }
  430. }
  431. signed main() {
  432. fastIO();
  433. if (fopen(TASK".INP", "r")) {
  434. freopen(TASK".INP", "r", stdin);
  435. freopen(TASK".OUT", "w", stdout);
  436. }
  437.  
  438. input(), output();
  439. return 0;
  440. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Standard output is empty