output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template <class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for (auto e : v) out << e << ' '; return out; } template <class T> ostream& operator<<(ostream& out, vector<T> v) { return prnt(out, v); } template <class T> ostream& operator<<(ostream& out, set<T> v) { return prnt(out, v); } template <class T1, class T2> ostream& operator<<(ostream& out, map<T1, T2> v) { return prnt(out, v); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } const int N = 1001000; int n, a[N], nxt[N], k, pos[N]; stack<int> s; vector<int> euler; const int INF = 1e9; int lazy[4 * N], first[4 * N]; pair<int, int> leaf[N]; vector<int> v[N]; void push(int node, int l, int r) { if (lazy[node]) { first[node] += lazy[node]; if (l != r) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } } inline int value(int node) { return first[node] + lazy[node]; } void upd(int node, int l, int r, int x, int y, int val) { push(node, l, r); if (x <= l && r <= y) return void(lazy[node] = val); int mid = (l + r) / 2; if (x <= mid) upd(2 * node, l, mid, x, y, val); if (mid < y) upd(2 * node + 1, mid + 1, r, x, y, val); first[node] = max(value(2 * node), value(2 * node + 1)); } int query(int node, int l, int r, int x, int y) { push(node, l, r); if (x <= l && r <= y) return first[node]; int mid = (l + r) / 2, ret = 0; if (x <= mid) ret = query(2 * node, l, mid, x, y); if (mid < y) ret = max(ret, query(2 * node + 1, mid + 1, r, x, y)); return ret; } void dfs(int node) { euler.push_back(node); pos[node] = euler.size(); leaf[node] = {pos[node], pos[node]}; for (auto i : v[node]) { dfs(i); leaf[node].first = min(leaf[node].first, leaf[i].first); leaf[node].second = max(leaf[node].second, leaf[i].second); } } void mark(int node, int val) { upd(1, 1, n + 1, leaf[node].first, leaf[node].second, val); } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; s.push(n + 1); a[n + 1] = n + 100; for (int i = n; i > 0; i--) { while (a[s.top()] <= a[i]) s.pop(); nxt[i] = s.top(); s.push(i); v[nxt[i]].push_back(i); } dfs(n + 1); for (int i = 1; i <= k; i++) mark(i, 1); cout << query(1, 1, n + 1, 1, n + 1) << ' '; for (int i = k + 1; i <= n; i++) { mark(i, 1); mark(i - k, -1); cout << query(1, 1, n + 1, 1, n + 1) << ' '; } }
### Prompt Develop a solution in cpp to the problem described below: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template <class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for (auto e : v) out << e << ' '; return out; } template <class T> ostream& operator<<(ostream& out, vector<T> v) { return prnt(out, v); } template <class T> ostream& operator<<(ostream& out, set<T> v) { return prnt(out, v); } template <class T1, class T2> ostream& operator<<(ostream& out, map<T1, T2> v) { return prnt(out, v); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } const int N = 1001000; int n, a[N], nxt[N], k, pos[N]; stack<int> s; vector<int> euler; const int INF = 1e9; int lazy[4 * N], first[4 * N]; pair<int, int> leaf[N]; vector<int> v[N]; void push(int node, int l, int r) { if (lazy[node]) { first[node] += lazy[node]; if (l != r) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } } inline int value(int node) { return first[node] + lazy[node]; } void upd(int node, int l, int r, int x, int y, int val) { push(node, l, r); if (x <= l && r <= y) return void(lazy[node] = val); int mid = (l + r) / 2; if (x <= mid) upd(2 * node, l, mid, x, y, val); if (mid < y) upd(2 * node + 1, mid + 1, r, x, y, val); first[node] = max(value(2 * node), value(2 * node + 1)); } int query(int node, int l, int r, int x, int y) { push(node, l, r); if (x <= l && r <= y) return first[node]; int mid = (l + r) / 2, ret = 0; if (x <= mid) ret = query(2 * node, l, mid, x, y); if (mid < y) ret = max(ret, query(2 * node + 1, mid + 1, r, x, y)); return ret; } void dfs(int node) { euler.push_back(node); pos[node] = euler.size(); leaf[node] = {pos[node], pos[node]}; for (auto i : v[node]) { dfs(i); leaf[node].first = min(leaf[node].first, leaf[i].first); leaf[node].second = max(leaf[node].second, leaf[i].second); } } void mark(int node, int val) { upd(1, 1, n + 1, leaf[node].first, leaf[node].second, val); } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; s.push(n + 1); a[n + 1] = n + 100; for (int i = n; i > 0; i--) { while (a[s.top()] <= a[i]) s.pop(); nxt[i] = s.top(); s.push(i); v[nxt[i]].push_back(i); } dfs(n + 1); for (int i = 1; i <= k; i++) mark(i, 1); cout << query(1, 1, n + 1, 1, n + 1) << ' '; for (int i = k + 1; i <= n; i++) { mark(i, 1); mark(i - k, -1); cout << query(1, 1, n + 1, 1, n + 1) << ' '; } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int n, k, a[maxn]; vector<int> g[maxn]; int par[maxn], h[maxn]; void dfs(int cur) { for (auto nxt : g[cur]) { h[nxt] = h[cur] + 1; dfs(nxt); } } int p[maxn]; multiset<int> res[maxn]; multiset<int> ans; int pa(int x) { return x == p[x] ? x : p[x] = pa(p[x]); } inline int cal_ans(int id) { if (res[id].empty()) return 0; return *res[id].rbegin() - *res[id].begin() + 1; } inline void remove(int id) { int tmp = cal_ans(id); if (tmp) { ans.erase(ans.find(tmp)); } } inline void add(int id) { int tmp = cal_ans(id); if (tmp) { ans.insert(tmp); } } void merge(int x, int y) { x = pa(x), y = pa(y); if (x != y) { if (res[x].size() < res[y].size()) swap(x, y); remove(x); remove(y); p[y] = x; for (const auto& e : res[y]) res[x].insert(e); res[y].clear(); add(x); } } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", a + i); int root = n + 1; a[root] = root; stack<int> stk; for (int i = 1; i <= root; ++i) { while (!stk.empty() && a[stk.top()] < a[i]) { g[i].push_back(stk.top()); par[stk.top()] = i; stk.pop(); } stk.push(i); } assert(stk.size() == 1 && stk.top() == root); h[root] = 0; dfs(root); for (int i = 1; i <= root; ++i) p[i] = i; for (int i = 1; i <= k; ++i) { res[i].insert(h[i]); ans.insert(1); for (auto child : g[i]) merge(i, child); } printf("%d", *ans.rbegin()); for (int i = k + 1; i <= n; ++i) { res[i].insert(h[i]); ans.insert(1); for (auto child : g[i]) merge(i, child); int tmp = pa(i - k); remove(tmp); res[tmp].erase(res[tmp].find(h[i - k])); add(tmp); printf(" %d", *ans.rbegin()); } puts(""); }
### Prompt Your challenge is to write a CPP solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int n, k, a[maxn]; vector<int> g[maxn]; int par[maxn], h[maxn]; void dfs(int cur) { for (auto nxt : g[cur]) { h[nxt] = h[cur] + 1; dfs(nxt); } } int p[maxn]; multiset<int> res[maxn]; multiset<int> ans; int pa(int x) { return x == p[x] ? x : p[x] = pa(p[x]); } inline int cal_ans(int id) { if (res[id].empty()) return 0; return *res[id].rbegin() - *res[id].begin() + 1; } inline void remove(int id) { int tmp = cal_ans(id); if (tmp) { ans.erase(ans.find(tmp)); } } inline void add(int id) { int tmp = cal_ans(id); if (tmp) { ans.insert(tmp); } } void merge(int x, int y) { x = pa(x), y = pa(y); if (x != y) { if (res[x].size() < res[y].size()) swap(x, y); remove(x); remove(y); p[y] = x; for (const auto& e : res[y]) res[x].insert(e); res[y].clear(); add(x); } } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", a + i); int root = n + 1; a[root] = root; stack<int> stk; for (int i = 1; i <= root; ++i) { while (!stk.empty() && a[stk.top()] < a[i]) { g[i].push_back(stk.top()); par[stk.top()] = i; stk.pop(); } stk.push(i); } assert(stk.size() == 1 && stk.top() == root); h[root] = 0; dfs(root); for (int i = 1; i <= root; ++i) p[i] = i; for (int i = 1; i <= k; ++i) { res[i].insert(h[i]); ans.insert(1); for (auto child : g[i]) merge(i, child); } printf("%d", *ans.rbegin()); for (int i = k + 1; i <= n; ++i) { res[i].insert(h[i]); ans.insert(1); for (auto child : g[i]) merge(i, child); int tmp = pa(i - k); remove(tmp); res[tmp].erase(res[tmp].find(h[i - k])); add(tmp); printf(" %d", *ans.rbegin()); } puts(""); } ```
#include <bits/stdc++.h> using namespace std; int n, k; int a[1000005], p[1000005]; basic_string<int> e[1000005], st; int l[1000005], r[1000005], dub[1000005], lrt; void dfs(int x) { l[x] = ++lrt; for (int y : e[x]) { dub[y] = dub[x] + 1; dfs(y); } r[x] = lrt; } const int MAXN = 1 << 20; int b[2 * MAXN]; void update(int x, int v) { x += MAXN; b[x] = v; while (x > 1) { x >>= 1; b[x] = max(b[2 * x], b[2 * x + 1]); } } int query(int l, int r, int x = 1, int xl = 0, int xr = MAXN - 1) { if (r < xl || xr < l) return 0; if (l <= xl && xr <= r) return b[x]; int xm = (xl + xr) >> 1; return max(query(l, r, 2 * x, xl, xm), query(l, r, 2 * x + 1, xm + 1, xr)); } bool alive[1000005]; multiset<int> root_vals; set<pair<int, int>> root_ls; void add_node(int x) { for (int y : e[x]) { if (alive[y]) { int v = query(l[y], r[y]) - dub[y]; root_vals.erase(root_vals.find(v)); root_ls.erase({l[y], y}); } } update(l[x], dub[x]); int v = query(l[x], r[x]) - dub[x]; root_vals.insert(v); root_ls.insert({l[x], x}); alive[x] = 1; } void remove_node(int x) { auto it = prev(root_ls.upper_bound({l[x], 123123123})); int root = it->second; int v = query(l[root], r[root]) - dub[root]; root_vals.erase(root_vals.find(v)); update(l[x], 0); if (x != root) { v = query(l[root], r[root]) - dub[root]; root_vals.insert(v); } else { root_ls.erase({l[x], x}); } alive[x] = 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; n++; a[n] = n; for (int i = 1; i <= n; i++) { while (st.size() && a[st.back()] < a[i]) { p[st.back()] = i; st.pop_back(); } st += i; } for (int i = 1; i < n; i++) e[p[i]] += i; dfs(n); for (int i = 1; i <= k; i++) add_node(i); cout << *root_vals.rbegin() + 1 << ' '; for (int i = 2; i <= n - k; i++) { add_node(i + k - 1); remove_node(i - 1); cout << *root_vals.rbegin() + 1 << ' '; } cout << '\n'; }
### Prompt Please create a solution in Cpp to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, k; int a[1000005], p[1000005]; basic_string<int> e[1000005], st; int l[1000005], r[1000005], dub[1000005], lrt; void dfs(int x) { l[x] = ++lrt; for (int y : e[x]) { dub[y] = dub[x] + 1; dfs(y); } r[x] = lrt; } const int MAXN = 1 << 20; int b[2 * MAXN]; void update(int x, int v) { x += MAXN; b[x] = v; while (x > 1) { x >>= 1; b[x] = max(b[2 * x], b[2 * x + 1]); } } int query(int l, int r, int x = 1, int xl = 0, int xr = MAXN - 1) { if (r < xl || xr < l) return 0; if (l <= xl && xr <= r) return b[x]; int xm = (xl + xr) >> 1; return max(query(l, r, 2 * x, xl, xm), query(l, r, 2 * x + 1, xm + 1, xr)); } bool alive[1000005]; multiset<int> root_vals; set<pair<int, int>> root_ls; void add_node(int x) { for (int y : e[x]) { if (alive[y]) { int v = query(l[y], r[y]) - dub[y]; root_vals.erase(root_vals.find(v)); root_ls.erase({l[y], y}); } } update(l[x], dub[x]); int v = query(l[x], r[x]) - dub[x]; root_vals.insert(v); root_ls.insert({l[x], x}); alive[x] = 1; } void remove_node(int x) { auto it = prev(root_ls.upper_bound({l[x], 123123123})); int root = it->second; int v = query(l[root], r[root]) - dub[root]; root_vals.erase(root_vals.find(v)); update(l[x], 0); if (x != root) { v = query(l[root], r[root]) - dub[root]; root_vals.insert(v); } else { root_ls.erase({l[x], x}); } alive[x] = 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; n++; a[n] = n; for (int i = 1; i <= n; i++) { while (st.size() && a[st.back()] < a[i]) { p[st.back()] = i; st.pop_back(); } st += i; } for (int i = 1; i < n; i++) e[p[i]] += i; dfs(n); for (int i = 1; i <= k; i++) add_node(i); cout << *root_vals.rbegin() + 1 << ' '; for (int i = 2; i <= n - k; i++) { add_node(i + k - 1); remove_node(i - 1); cout << *root_vals.rbegin() + 1 << ' '; } cout << '\n'; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; struct Tree { int l, r, mx, laz; } t[maxn << 2]; struct Node { int v, next; } e[maxn << 1]; int head[maxn], dfn[maxn], siz[maxn], tot, num; int sta[maxn], top; long long a[maxn]; void Add(int x, int y) { e[++tot].v = y; e[tot].next = head[x]; head[x] = tot; } void dfs(int x) { dfn[x] = ++num; siz[x] = 1; for (int i = head[x]; i; i = e[i].next) { int v = e[i].v; dfs(v); siz[x] += siz[v]; } } void pushup(int rt) { t[rt].mx = max(t[rt << 1].mx, t[rt << 1 | 1].mx); } void pushdown(int rt) { if (t[rt].laz) { t[rt << 1].laz += t[rt].laz; t[rt << 1 | 1].laz += t[rt].laz; t[rt << 1].mx += t[rt].laz; t[rt << 1 | 1].mx += t[rt].laz; t[rt].laz = 0; } } void build(int rt, int l, int r) { t[rt].l = l; t[rt].r = r; if (l == r) { return; } int mid = l + r >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); pushup(rt); } void modify(int rt, int l, int r, int val) { if (t[rt].l >= l && t[rt].r <= r) { t[rt].laz += val; t[rt].mx += val; return; } pushdown(rt); int mid = t[rt].l + t[rt].r >> 1; if (l <= mid) modify(rt << 1, l, r, val); if (r > mid) modify(rt << 1 | 1, l, r, val); pushup(rt); } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); a[n + 1] = 10000000000; for (int i = 1; i <= n + 1; ++i) { while (top && a[sta[top]] < a[i]) { Add(i, sta[top]); top--; } sta[++top] = i; } dfs(n + 1); build(1, 1, n + 1); for (int i = 1; i <= k; ++i) { modify(1, dfn[i], dfn[i] + siz[i] - 1, 1); } printf("%d ", t[1].mx); for (int i = 2; i + k - 1 <= n; ++i) { modify(1, dfn[i - 1], dfn[i - 1] + siz[i - 1] - 1, -1); modify(1, dfn[i + k - 1], dfn[i + k - 1] + siz[i + k - 1] - 1, 1); printf("%d ", t[1].mx); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; struct Tree { int l, r, mx, laz; } t[maxn << 2]; struct Node { int v, next; } e[maxn << 1]; int head[maxn], dfn[maxn], siz[maxn], tot, num; int sta[maxn], top; long long a[maxn]; void Add(int x, int y) { e[++tot].v = y; e[tot].next = head[x]; head[x] = tot; } void dfs(int x) { dfn[x] = ++num; siz[x] = 1; for (int i = head[x]; i; i = e[i].next) { int v = e[i].v; dfs(v); siz[x] += siz[v]; } } void pushup(int rt) { t[rt].mx = max(t[rt << 1].mx, t[rt << 1 | 1].mx); } void pushdown(int rt) { if (t[rt].laz) { t[rt << 1].laz += t[rt].laz; t[rt << 1 | 1].laz += t[rt].laz; t[rt << 1].mx += t[rt].laz; t[rt << 1 | 1].mx += t[rt].laz; t[rt].laz = 0; } } void build(int rt, int l, int r) { t[rt].l = l; t[rt].r = r; if (l == r) { return; } int mid = l + r >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); pushup(rt); } void modify(int rt, int l, int r, int val) { if (t[rt].l >= l && t[rt].r <= r) { t[rt].laz += val; t[rt].mx += val; return; } pushdown(rt); int mid = t[rt].l + t[rt].r >> 1; if (l <= mid) modify(rt << 1, l, r, val); if (r > mid) modify(rt << 1 | 1, l, r, val); pushup(rt); } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); a[n + 1] = 10000000000; for (int i = 1; i <= n + 1; ++i) { while (top && a[sta[top]] < a[i]) { Add(i, sta[top]); top--; } sta[++top] = i; } dfs(n + 1); build(1, 1, n + 1); for (int i = 1; i <= k; ++i) { modify(1, dfn[i], dfn[i] + siz[i] - 1, 1); } printf("%d ", t[1].mx); for (int i = 2; i + k - 1 <= n; ++i) { modify(1, dfn[i - 1], dfn[i - 1] + siz[i - 1] - 1, -1); modify(1, dfn[i + k - 1], dfn[i + k - 1] + siz[i + k - 1] - 1, 1); printf("%d ", t[1].mx); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1000005; const int unit = 450; int n, m, a[maxn], b[maxn], l[maxn], r[maxn]; long long t[maxn << 2], lz[maxn << 2], tim; vector<int> v[maxn]; void dfs(int x) { l[x] = ++tim; for (auto u : v[x]) dfs(u); r[x] = tim; } void update(int L, int R, int d, int l, int r, int rt) { if (L <= l && r <= R) { lz[rt] += 1ll * d; return; } int mid = l + r >> 1; if (L <= mid) update(L, R, d, l, mid, rt << 1); if (mid < R) update(L, R, d, mid + 1, r, rt << 1 | 1); t[rt] = max(t[((rt << 1) | 1)] + lz[((rt << 1) | 1)], t[(rt << 1)] + lz[(rt << 1)]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = (1); i < (n + 1); i++) cin >> a[i]; for (int i = n; i > 0; i--) { for (b[i] = i + 1; b[i] <= n && a[b[i]] <= a[i]; b[i] = b[b[i]]) ; v[b[i]].push_back(i); } dfs(n + 1); for (int i = (1); i < (m + 1); i++) update(l[i], r[i], 1, 1, tim, 1); cout << t[1] + lz[1] << " "; for (int i = (m + 1); i < (n + 1); i++) { update(l[i], r[i], 1, 1, tim, 1); update(l[i - m], r[i - m], -n, 1, tim, 1); cout << t[1] + lz[1] << " "; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1000005; const int unit = 450; int n, m, a[maxn], b[maxn], l[maxn], r[maxn]; long long t[maxn << 2], lz[maxn << 2], tim; vector<int> v[maxn]; void dfs(int x) { l[x] = ++tim; for (auto u : v[x]) dfs(u); r[x] = tim; } void update(int L, int R, int d, int l, int r, int rt) { if (L <= l && r <= R) { lz[rt] += 1ll * d; return; } int mid = l + r >> 1; if (L <= mid) update(L, R, d, l, mid, rt << 1); if (mid < R) update(L, R, d, mid + 1, r, rt << 1 | 1); t[rt] = max(t[((rt << 1) | 1)] + lz[((rt << 1) | 1)], t[(rt << 1)] + lz[(rt << 1)]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = (1); i < (n + 1); i++) cin >> a[i]; for (int i = n; i > 0; i--) { for (b[i] = i + 1; b[i] <= n && a[b[i]] <= a[i]; b[i] = b[b[i]]) ; v[b[i]].push_back(i); } dfs(n + 1); for (int i = (1); i < (m + 1); i++) update(l[i], r[i], 1, 1, tim, 1); cout << t[1] + lz[1] << " "; for (int i = (m + 1); i < (n + 1); i++) { update(l[i], r[i], 1, 1, tim, 1); update(l[i - m], r[i - m], -n, 1, tim, 1); cout << t[1] + lz[1] << " "; } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")"; } template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) cout << ", "; cout << v[i]; } return cout << "]"; } int t, n, k, itr[8000005], lz[8000005], a[1000005], cmp, par[1000005], tin[1000005], tout[1000005]; int sz; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> ndpar; vector<int> e[1000005]; void psh(int z, int l, int r) { itr[z] += lz[z]; if (l != r) { lz[2 * z] += lz[z]; lz[2 * z + 1] += lz[z]; } lz[z] = 0; } void iupd(int z, int l, int r, int lb, int rb, int b) { psh(z, l, r); if (r < lb || l > rb) return; if (l >= lb && r <= rb) { lz[z] += b; psh(z, l, r); return; } int mid = (l + r) >> 1; iupd(2 * z, l, mid, lb, rb, b); iupd(2 * z + 1, mid + 1, r, lb, rb, b); itr[z] = max(itr[2 * z + 1], itr[2 * z]); } void upd(int l, int r, int b) { iupd(1, 1, sz, l, r, b); } int ige(int z, int l, int r, int lb, int rb) { psh(z, l, r); if (r < lb || l > rb) return 0; if (l >= lb && r <= rb) return itr[z]; int mid = (l + r) >> 1; return max(ige(2 * z, l, mid, lb, rb), ige(2 * z + 1, mid + 1, r, lb, rb)); } int ge(int l, int r) { return ige(1, 1, sz, l, r); } void dfs(int g) { sz++; tin[g] = sz; for (auto u : e[g]) dfs(u); sz++; tout[g] = sz; } void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; cmp = 1; for (int i = 1; i <= n; i++) { while (!ndpar.empty() && ndpar.top().first < a[i]) { par[ndpar.top().second] = i; ndpar.pop(); } ndpar.push({a[i], i}); } sz = 0; for (int i = 1; i <= n; i++) { e[par[i]].push_back(i); } dfs(0); for (int i = 1; i <= k; i++) upd(tin[i], tout[i], 1); cout << ge(1, sz) << ' '; for (int st = 2; st + k - 1 <= n; st++) { upd(tin[st - 1], tout[st - 1], -1); upd(tin[st + k - 1], tout[st + k - 1], 1); cout << ge(1, sz) << ' '; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); t = 1; for (int i = 1; i <= t; i++) solve(); cout.flush(); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename A, typename B> ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")"; } template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) cout << ", "; cout << v[i]; } return cout << "]"; } int t, n, k, itr[8000005], lz[8000005], a[1000005], cmp, par[1000005], tin[1000005], tout[1000005]; int sz; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> ndpar; vector<int> e[1000005]; void psh(int z, int l, int r) { itr[z] += lz[z]; if (l != r) { lz[2 * z] += lz[z]; lz[2 * z + 1] += lz[z]; } lz[z] = 0; } void iupd(int z, int l, int r, int lb, int rb, int b) { psh(z, l, r); if (r < lb || l > rb) return; if (l >= lb && r <= rb) { lz[z] += b; psh(z, l, r); return; } int mid = (l + r) >> 1; iupd(2 * z, l, mid, lb, rb, b); iupd(2 * z + 1, mid + 1, r, lb, rb, b); itr[z] = max(itr[2 * z + 1], itr[2 * z]); } void upd(int l, int r, int b) { iupd(1, 1, sz, l, r, b); } int ige(int z, int l, int r, int lb, int rb) { psh(z, l, r); if (r < lb || l > rb) return 0; if (l >= lb && r <= rb) return itr[z]; int mid = (l + r) >> 1; return max(ige(2 * z, l, mid, lb, rb), ige(2 * z + 1, mid + 1, r, lb, rb)); } int ge(int l, int r) { return ige(1, 1, sz, l, r); } void dfs(int g) { sz++; tin[g] = sz; for (auto u : e[g]) dfs(u); sz++; tout[g] = sz; } void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; cmp = 1; for (int i = 1; i <= n; i++) { while (!ndpar.empty() && ndpar.top().first < a[i]) { par[ndpar.top().second] = i; ndpar.pop(); } ndpar.push({a[i], i}); } sz = 0; for (int i = 1; i <= n; i++) { e[par[i]].push_back(i); } dfs(0); for (int i = 1; i <= k; i++) upd(tin[i], tout[i], 1); cout << ge(1, sz) << ' '; for (int st = 2; st + k - 1 <= n; st++) { upd(tin[st - 1], tout[st - 1], -1); upd(tin[st + k - 1], tout[st + k - 1], 1); cout << ge(1, sz) << ' '; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); t = 1; for (int i = 1; i <= t; i++) solve(); cout.flush(); return 0; } ```
#include <bits/stdc++.h> using namespace std; mt19937 rng(static_cast<unsigned int>( chrono::steady_clock::now().time_since_epoch().count())); template <typename num_t> inline void add_mod(num_t& a, const int& b, const int& m) { a = (a + b); if (a >= m) a -= m; } template <typename num_t> inline bool update_max(num_t& a, const num_t& b) { return a < b ? a = b, true : false; } template <typename num_t> inline bool update_min(num_t& a, const num_t& b) { return a > b ? a = b, true : false; } template <typename num_t> num_t gcd(num_t lhs, num_t rhs) { return !lhs ? rhs : gcd(rhs % lhs, lhs); } template <typename num_t> num_t pw(num_t n, num_t k, const num_t& mod) { if (k == -1) k = mod - 2; num_t res = 1; for (; k > 0; k >>= 1) { if (k & 1) res = 1ll * res * n % mod; n = 1ll * n * n % mod; } return res % mod; } const int inf = 1e9 + 7; const int mod = 998244353; const long long ll_inf = 9ll * inf * inf + 7; const int MAX_N = 5000 + 7; template <typename num_t> class SegmentTree { public: void init(int l, int r) { tree.resize((r - l + 1) << 1); node_index = 0; build_tree(tree[node_index++], l, r); } void update(int left_index, int right_index, const num_t& val) { update_(tree[0], left_index, right_index, val); } num_t get(int left_index, int right_index) { if (right_index < left_index) return 0; return get(tree[0], left_index, right_index); } private: struct node { static const num_t nil = 0; node *left_child, *right_child; int left_index, right_index; num_t value; num_t lazy; inline void push_down(num_t updated_value) { value += updated_value; lazy += updated_value; } inline void merge_node() { const node &lhs = *left_child, &rhs = *right_child; value = max(lhs.value, rhs.value); } inline void lazy_update() { if (lazy) { if (left_child != nullptr) left_child->push_down(lazy); if (right_child != nullptr) right_child->push_down(lazy); lazy = 0; } } inline void init_value(num_t v = 0) { value = v; lazy = 0; } inline void init(int left, int right, node* left_child_, node* right_child_) { left_index = left; right_index = right; left_child = left_child_; right_child = right_child_; } int size() const { return right_index - left_index + 1; } }; vector<node> tree; int node_index = 0; void build_tree(node& root, int left, int right) { if (left == right) { root.init(left, right, nullptr, nullptr); root.init_value(0); return; } int mid = (left + right) >> 1, lhs = node_index++, rhs = node_index++; build_tree(tree[lhs], left, mid); build_tree(tree[rhs], mid + 1, right); root.init(left, right, &tree[lhs], &tree[rhs]); root.init_value(); root.merge_node(); } void update_(node& root, int u, int v, const num_t& value) { if (u > root.right_index || v < root.left_index) return; if (u <= root.left_index && root.right_index <= v) { root.push_down(value); } else { root.lazy_update(); update_(*root.left_child, u, v, value); update_(*root.right_child, u, v, value); root.init_value(0); root.merge_node(); } } num_t get(node& root, int u, int v) { if (u <= root.left_index && root.right_index <= v) return root.value; if (u > root.right_index || v < root.left_index) return node::nil; root.lazy_update(); auto lhs = get(*root.left_child, u, v); auto rhs = get(*root.right_child, u, v); return max(lhs, rhs); } }; SegmentTree<int> tree; int n, k; vector<int> a; void solve() { cin >> n >> k; a.resize(n); for (int& x : a) cin >> x; tree.init(0, n - 1); vector<int> stack_; for (int i = 0; i < n; ++i) { while (!stack_.empty() && a[stack_.back()] < a[i]) stack_.pop_back(); int last = 0; if (!stack_.empty()) last = stack_.back() + 1; stack_.push_back(i); tree.update(last, i, 1); if (i + 1 >= k) cout << tree.get(i - k + 1, i) << ' '; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); const bool multiple_test = false; int test = 1; if (multiple_test) cin >> test; for (int i = 0; i < test; ++i) { solve(); } }
### Prompt Please create a solution in CPP to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; mt19937 rng(static_cast<unsigned int>( chrono::steady_clock::now().time_since_epoch().count())); template <typename num_t> inline void add_mod(num_t& a, const int& b, const int& m) { a = (a + b); if (a >= m) a -= m; } template <typename num_t> inline bool update_max(num_t& a, const num_t& b) { return a < b ? a = b, true : false; } template <typename num_t> inline bool update_min(num_t& a, const num_t& b) { return a > b ? a = b, true : false; } template <typename num_t> num_t gcd(num_t lhs, num_t rhs) { return !lhs ? rhs : gcd(rhs % lhs, lhs); } template <typename num_t> num_t pw(num_t n, num_t k, const num_t& mod) { if (k == -1) k = mod - 2; num_t res = 1; for (; k > 0; k >>= 1) { if (k & 1) res = 1ll * res * n % mod; n = 1ll * n * n % mod; } return res % mod; } const int inf = 1e9 + 7; const int mod = 998244353; const long long ll_inf = 9ll * inf * inf + 7; const int MAX_N = 5000 + 7; template <typename num_t> class SegmentTree { public: void init(int l, int r) { tree.resize((r - l + 1) << 1); node_index = 0; build_tree(tree[node_index++], l, r); } void update(int left_index, int right_index, const num_t& val) { update_(tree[0], left_index, right_index, val); } num_t get(int left_index, int right_index) { if (right_index < left_index) return 0; return get(tree[0], left_index, right_index); } private: struct node { static const num_t nil = 0; node *left_child, *right_child; int left_index, right_index; num_t value; num_t lazy; inline void push_down(num_t updated_value) { value += updated_value; lazy += updated_value; } inline void merge_node() { const node &lhs = *left_child, &rhs = *right_child; value = max(lhs.value, rhs.value); } inline void lazy_update() { if (lazy) { if (left_child != nullptr) left_child->push_down(lazy); if (right_child != nullptr) right_child->push_down(lazy); lazy = 0; } } inline void init_value(num_t v = 0) { value = v; lazy = 0; } inline void init(int left, int right, node* left_child_, node* right_child_) { left_index = left; right_index = right; left_child = left_child_; right_child = right_child_; } int size() const { return right_index - left_index + 1; } }; vector<node> tree; int node_index = 0; void build_tree(node& root, int left, int right) { if (left == right) { root.init(left, right, nullptr, nullptr); root.init_value(0); return; } int mid = (left + right) >> 1, lhs = node_index++, rhs = node_index++; build_tree(tree[lhs], left, mid); build_tree(tree[rhs], mid + 1, right); root.init(left, right, &tree[lhs], &tree[rhs]); root.init_value(); root.merge_node(); } void update_(node& root, int u, int v, const num_t& value) { if (u > root.right_index || v < root.left_index) return; if (u <= root.left_index && root.right_index <= v) { root.push_down(value); } else { root.lazy_update(); update_(*root.left_child, u, v, value); update_(*root.right_child, u, v, value); root.init_value(0); root.merge_node(); } } num_t get(node& root, int u, int v) { if (u <= root.left_index && root.right_index <= v) return root.value; if (u > root.right_index || v < root.left_index) return node::nil; root.lazy_update(); auto lhs = get(*root.left_child, u, v); auto rhs = get(*root.right_child, u, v); return max(lhs, rhs); } }; SegmentTree<int> tree; int n, k; vector<int> a; void solve() { cin >> n >> k; a.resize(n); for (int& x : a) cin >> x; tree.init(0, n - 1); vector<int> stack_; for (int i = 0; i < n; ++i) { while (!stack_.empty() && a[stack_.back()] < a[i]) stack_.pop_back(); int last = 0; if (!stack_.empty()) last = stack_.back() + 1; stack_.push_back(i); tree.update(last, i, 1); if (i + 1 >= k) cout << tree.get(i - k + 1, i) << ' '; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); const bool multiple_test = false; int test = 1; if (multiple_test) cin >> test; for (int i = 0; i < test; ++i) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5, mod = 1e9 + 7; int t, tree[4 * N], tmin[N], tmout[N], timer, lazy[4 * N], r[N], n, k, a[N]; vector<int> V[N]; void update(int u, int start, int end, int l, int r, int val) { if (lazy[u]) { tree[u] += lazy[u]; if (l != r) { lazy[2 * u] += lazy[u]; lazy[2 * u + 1] += lazy[u]; } lazy[u] = 0; } if (l > end || r < start) return; if (start <= l && r <= end) { lazy[u] = val; tree[u] += lazy[u]; if (l != r) { lazy[2 * u] += lazy[u]; lazy[2 * u + 1] += lazy[u]; } lazy[u] = 0; return; } int mid = (l + r) / 2; update(2 * u, start, end, l, mid, val); update(2 * u + 1, start, end, mid + 1, r, val); tree[u] = max(tree[2 * u], tree[2 * u + 1]); } void dfs(int u) { timer++; tmin[u] = timer; for (int i = 0; i < V[u].size(); i++) dfs(V[u][i]); tmout[u] = timer; } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = n; i >= 1; i--) { int x = i + 1; while (x <= n && a[x] <= a[i]) { x = r[x]; } r[i] = x; V[r[i]].push_back(i); } for (int i = 1; i <= n; i++) { if (r[i] == n + 1) dfs(i); } for (int i = 1; i <= k; i++) { update(1, tmin[i], tmout[i], 1, n, 1); } cout << tree[1] << " "; for (int i = 2; i <= n - k + 1; i++) { update(1, tmin[i + k - 1], tmout[i + k - 1], 1, n, 1); update(1, tmin[i - 1], tmin[i - 1], 1, n, -2 * n); cout << tree[1] << " "; } }
### Prompt Your challenge is to write a CPP solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5, mod = 1e9 + 7; int t, tree[4 * N], tmin[N], tmout[N], timer, lazy[4 * N], r[N], n, k, a[N]; vector<int> V[N]; void update(int u, int start, int end, int l, int r, int val) { if (lazy[u]) { tree[u] += lazy[u]; if (l != r) { lazy[2 * u] += lazy[u]; lazy[2 * u + 1] += lazy[u]; } lazy[u] = 0; } if (l > end || r < start) return; if (start <= l && r <= end) { lazy[u] = val; tree[u] += lazy[u]; if (l != r) { lazy[2 * u] += lazy[u]; lazy[2 * u + 1] += lazy[u]; } lazy[u] = 0; return; } int mid = (l + r) / 2; update(2 * u, start, end, l, mid, val); update(2 * u + 1, start, end, mid + 1, r, val); tree[u] = max(tree[2 * u], tree[2 * u + 1]); } void dfs(int u) { timer++; tmin[u] = timer; for (int i = 0; i < V[u].size(); i++) dfs(V[u][i]); tmout[u] = timer; } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = n; i >= 1; i--) { int x = i + 1; while (x <= n && a[x] <= a[i]) { x = r[x]; } r[i] = x; V[r[i]].push_back(i); } for (int i = 1; i <= n; i++) { if (r[i] == n + 1) dfs(i); } for (int i = 1; i <= k; i++) { update(1, tmin[i], tmout[i], 1, n, 1); } cout << tree[1] << " "; for (int i = 2; i <= n - k + 1; i++) { update(1, tmin[i + k - 1], tmout[i + k - 1], 1, n, 1); update(1, tmin[i - 1], tmin[i - 1], 1, n, -2 * n); cout << tree[1] << " "; } } ```
#include <bits/stdc++.h> using namespace std; int n, k; int arr[1000008]; int l[1000008]; int mx[1000008 << 2], lazy[1000008 << 2]; void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } void build(int rt, int l, int r) { if (l > r) return; if (l == r) { mx[l] = arr[l]; return; } int mid = (l + r) >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); pushup(rt); } void pushdown(int rt) { mx[rt << 1] += lazy[rt]; mx[rt << 1 | 1] += lazy[rt]; lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } void update(int rt, int l, int r, int L, int R, int C) { if (L <= l && r <= R) { lazy[rt] += C; mx[rt] += C; return; } pushdown(rt); int mid = (l + r) >> 1; if (L <= mid) update(rt << 1, l, mid, L, R, C); if (mid < R) update(rt << 1 | 1, mid + 1, r, L, R, C); pushup(rt); } int query(int rt, int l, int r, int L, int R) { if (L <= l && r <= R) return mx[rt]; int mid = (l + r) >> 1; pushdown(rt); int ret = -1; if (L <= mid) ret = max(ret, query(rt << 1, l, mid, L, R)); if (mid < R) ret = max(ret, query(rt << 1 | 1, mid + 1, r, L, R)); return ret; } stack<int> sta; int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &arr[i]); for (int i = n; i >= 1; --i) { while (!sta.empty() && arr[sta.top()] <= arr[i]) { l[sta.top()] = i + 1; sta.pop(); } sta.push(i); } while (!sta.empty()) { l[sta.top()] = 1; sta.pop(); } for (int i = 1; i <= n; ++i) { update(1, 1, n, l[i], i, 1); if (i >= k) { printf("%d ", query(1, 1, n, i - k + 1, i)); update(1, 1, n, l[i - k + 1], i - k + 1, -1); } } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, k; int arr[1000008]; int l[1000008]; int mx[1000008 << 2], lazy[1000008 << 2]; void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } void build(int rt, int l, int r) { if (l > r) return; if (l == r) { mx[l] = arr[l]; return; } int mid = (l + r) >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); pushup(rt); } void pushdown(int rt) { mx[rt << 1] += lazy[rt]; mx[rt << 1 | 1] += lazy[rt]; lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } void update(int rt, int l, int r, int L, int R, int C) { if (L <= l && r <= R) { lazy[rt] += C; mx[rt] += C; return; } pushdown(rt); int mid = (l + r) >> 1; if (L <= mid) update(rt << 1, l, mid, L, R, C); if (mid < R) update(rt << 1 | 1, mid + 1, r, L, R, C); pushup(rt); } int query(int rt, int l, int r, int L, int R) { if (L <= l && r <= R) return mx[rt]; int mid = (l + r) >> 1; pushdown(rt); int ret = -1; if (L <= mid) ret = max(ret, query(rt << 1, l, mid, L, R)); if (mid < R) ret = max(ret, query(rt << 1 | 1, mid + 1, r, L, R)); return ret; } stack<int> sta; int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &arr[i]); for (int i = n; i >= 1; --i) { while (!sta.empty() && arr[sta.top()] <= arr[i]) { l[sta.top()] = i + 1; sta.pop(); } sta.push(i); } while (!sta.empty()) { l[sta.top()] = 1; sta.pop(); } for (int i = 1; i <= n; ++i) { update(1, 1, n, l[i], i, 1); if (i >= k) { printf("%d ", query(1, 1, n, i - k + 1, i)); update(1, 1, n, l[i - k + 1], i - k + 1, -1); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; struct SegementTree { int mx, lz; }; SegementTree tr[maxn * 4]; int L[maxn], a[maxn]; void pushup(int x) { tr[x].mx = max(tr[(x << 1)].mx, tr[((x << 1) | 1)].mx); } void maintain(int x, int val) { tr[x].mx += val; tr[x].lz += val; } void pushdown(int x) { if (tr[x].lz) { maintain((x << 1), tr[x].lz); maintain(((x << 1) | 1), tr[x].lz); tr[x].lz = 0; } } void build(int x, int l, int r) { if (l == r) { tr[x].mx = 0; tr[x].lz = 0; return; } int mid = (l + r) >> 1; build((x << 1), l, mid); build(((x << 1) | 1), mid + 1, r); pushup(x); } void update(int x, int l, int r, int ql, int qr, int val) { if (l >= ql && r <= qr) { maintain(x, val); return; } int mid = (l + r) >> 1; pushdown(x); if (ql <= mid) update((x << 1), l, mid, ql, qr, val); if (qr > mid) update(((x << 1) | 1), mid + 1, r, ql, qr, val); pushup(x); } int query(int x, int l, int r, int ql, int qr) { if (l >= ql && r <= qr) { return tr[x].mx; } int mid = (l + r) >> 1, ans = 0; pushdown(x); if (ql <= mid) ans = max(ans, query((x << 1), l, mid, ql, qr)); if (qr > mid) ans = max(ans, query(((x << 1) | 1), mid + 1, r, ql, qr)); return ans; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); a[0] = 0x3f3f3f3f; for (int i = 1; i <= n; i++) { L[i] = i - 1; while (a[i] > a[L[i]]) { L[i] = L[L[i]]; } } build(1, 1, n); for (int i = 1; i < m; i++) update(1, 1, n, L[i] + 1, i, 1); for (int i = 1, j = m; j <= n; i++, j++) { update(1, 1, n, L[j] + 1, j, 1); printf("%d ", query(1, 1, n, i, j)); } }
### Prompt Create a solution in cpp for the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1000010; struct SegementTree { int mx, lz; }; SegementTree tr[maxn * 4]; int L[maxn], a[maxn]; void pushup(int x) { tr[x].mx = max(tr[(x << 1)].mx, tr[((x << 1) | 1)].mx); } void maintain(int x, int val) { tr[x].mx += val; tr[x].lz += val; } void pushdown(int x) { if (tr[x].lz) { maintain((x << 1), tr[x].lz); maintain(((x << 1) | 1), tr[x].lz); tr[x].lz = 0; } } void build(int x, int l, int r) { if (l == r) { tr[x].mx = 0; tr[x].lz = 0; return; } int mid = (l + r) >> 1; build((x << 1), l, mid); build(((x << 1) | 1), mid + 1, r); pushup(x); } void update(int x, int l, int r, int ql, int qr, int val) { if (l >= ql && r <= qr) { maintain(x, val); return; } int mid = (l + r) >> 1; pushdown(x); if (ql <= mid) update((x << 1), l, mid, ql, qr, val); if (qr > mid) update(((x << 1) | 1), mid + 1, r, ql, qr, val); pushup(x); } int query(int x, int l, int r, int ql, int qr) { if (l >= ql && r <= qr) { return tr[x].mx; } int mid = (l + r) >> 1, ans = 0; pushdown(x); if (ql <= mid) ans = max(ans, query((x << 1), l, mid, ql, qr)); if (qr > mid) ans = max(ans, query(((x << 1) | 1), mid + 1, r, ql, qr)); return ans; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); a[0] = 0x3f3f3f3f; for (int i = 1; i <= n; i++) { L[i] = i - 1; while (a[i] > a[L[i]]) { L[i] = L[L[i]]; } } build(1, 1, n); for (int i = 1; i < m; i++) update(1, 1, n, L[i] + 1, i, 1); for (int i = 1, j = m; j <= n; i++, j++) { update(1, 1, n, L[j] + 1, j, 1); printf("%d ", query(1, 1, n, i, j)); } } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } const int inf = 2e9; const int maxn = 1e6 + 5; struct MaxLazySegmentTree { vector<int> t, o; void init(int n) { n += 10; t.resize(4 * n); o.resize(4 * n); } void push(int v) { t[2 * v] += o[v]; t[2 * v + 1] += o[v]; o[2 * v] += o[v]; o[2 * v + 1] += o[v]; o[v] = 0; } int queryMax(int v, int tl, int tr, int l, int r) { if (l > tr || r < tl) return -inf; if (l <= tl && tr <= r) { return t[v]; } else { push(v); int tm = (tl + tr) / 2; return max(queryMax(2 * v, tl, tm, l, r), queryMax(2 * v + 1, tm + 1, tr, l, r)); } } void rangeAdd(int v, int tl, int tr, int l, int r, int dx) { if (l > r) return; if (l > tr || r < tl) return; if (l <= tl && tr <= r) { t[v] += dx; o[v] += dx; } else { push(v); int tm = (tl + tr) / 2; rangeAdd(2 * v, tl, tm, l, r, dx); rangeAdd(2 * v + 1, tm + 1, tr, l, r, dx); t[v] = max(t[2 * v], t[2 * v + 1]); } } }; int n, k; int a[maxn]; int nl[maxn]; int tin[maxn], tout[maxn]; vector<int> g[maxn]; void add_edge(int u, int v) { g[u].push_back(v); g[v].push_back(u); } int cloc = 1; void dfs(int at, int p) { tin[at] = cloc++; for (int to : g[at]) { if (to == p) continue; dfs(to, at); } tout[at] = cloc++; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } a[++n] = inf; nl[n] = n; for (int i = n - 1; i >= 1; i--) { nl[i] = i + 1; while (nl[i] < n && a[i] >= a[nl[i]]) { nl[i] = nl[nl[i]]; } } for (int i = 1; i <= n; i++) { g[nl[i]].push_back(i); } dfs(n, -1); int N = cloc; MaxLazySegmentTree tree; tree.init(N); auto upd = [&](int i, int dx) { tree.rangeAdd(1, 1, N, i, N, dx); }; auto get = [&]() { return tree.queryMax(1, 1, N, 1, N); }; for (int i = 1; i <= k; i++) { upd(tin[i], +1); upd(tout[i], -1); } cout << get() << " "; for (int i = k + 1; i < n; i++) { upd(tin[i - k], -1); upd(tout[i - k], +1); upd(tin[i], +1); upd(tout[i], -1); cout << get() << " "; } cout << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } const int inf = 2e9; const int maxn = 1e6 + 5; struct MaxLazySegmentTree { vector<int> t, o; void init(int n) { n += 10; t.resize(4 * n); o.resize(4 * n); } void push(int v) { t[2 * v] += o[v]; t[2 * v + 1] += o[v]; o[2 * v] += o[v]; o[2 * v + 1] += o[v]; o[v] = 0; } int queryMax(int v, int tl, int tr, int l, int r) { if (l > tr || r < tl) return -inf; if (l <= tl && tr <= r) { return t[v]; } else { push(v); int tm = (tl + tr) / 2; return max(queryMax(2 * v, tl, tm, l, r), queryMax(2 * v + 1, tm + 1, tr, l, r)); } } void rangeAdd(int v, int tl, int tr, int l, int r, int dx) { if (l > r) return; if (l > tr || r < tl) return; if (l <= tl && tr <= r) { t[v] += dx; o[v] += dx; } else { push(v); int tm = (tl + tr) / 2; rangeAdd(2 * v, tl, tm, l, r, dx); rangeAdd(2 * v + 1, tm + 1, tr, l, r, dx); t[v] = max(t[2 * v], t[2 * v + 1]); } } }; int n, k; int a[maxn]; int nl[maxn]; int tin[maxn], tout[maxn]; vector<int> g[maxn]; void add_edge(int u, int v) { g[u].push_back(v); g[v].push_back(u); } int cloc = 1; void dfs(int at, int p) { tin[at] = cloc++; for (int to : g[at]) { if (to == p) continue; dfs(to, at); } tout[at] = cloc++; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } a[++n] = inf; nl[n] = n; for (int i = n - 1; i >= 1; i--) { nl[i] = i + 1; while (nl[i] < n && a[i] >= a[nl[i]]) { nl[i] = nl[nl[i]]; } } for (int i = 1; i <= n; i++) { g[nl[i]].push_back(i); } dfs(n, -1); int N = cloc; MaxLazySegmentTree tree; tree.init(N); auto upd = [&](int i, int dx) { tree.rangeAdd(1, 1, N, i, N, dx); }; auto get = [&]() { return tree.queryMax(1, 1, N, 1, N); }; for (int i = 1; i <= k; i++) { upd(tin[i], +1); upd(tout[i], -1); } cout << get() << " "; for (int i = k + 1; i < n; i++) { upd(tin[i - k], -1); upd(tout[i - k], +1); upd(tin[i], +1); upd(tout[i], -1); cout << get() << " "; } cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, k; vector<int> a, bit; bool par[1000005]; vector<int> in, out; int cnt(0); struct it { int COUNT, LAZY; it *l, *r; void Update(int l, int r, int L, int R, int a); }; it *add() { it *t = new it; t->COUNT = 0; t->LAZY = 0; t->l = t->r = NULL; return t; } void it::Update(int l, int r, int L, int R, int a) { if (r < L || R < l) return; if (L <= l && r <= R) { this->COUNT += a; this->LAZY += a; return; } if (this->r == NULL) this->r = add(); if (this->l == NULL) this->l = add(); if (this->LAZY) { this->l->COUNT += this->LAZY; this->r->COUNT += this->LAZY; this->l->LAZY += this->LAZY; this->r->LAZY += this->LAZY; this->LAZY = 0; } int m = (l + r) / 2; this->l->Update(l, m, L, R, a); this->r->Update(m + 1, r, L, R, a); this->COUNT = max(this->l->COUNT, this->r->COUNT); } it *root; vector<vector<int> > P; template <class T> T MAX(T a, T b) { return (a > b) ? a : b; }; template <class T> T MIN(T a, T b) { return (a < b) ? a : b; }; int GetBit(int u) { int dapan = 1e9; for (int i = n - u + 1; i >= 1; i -= i & (-i)) { if (bit[i]) dapan = MIN(dapan, bit[i]); } return dapan; } void Update(int u, int v) { for (int i = n - u + 1; i <= n; i += i & (-i)) { if (bit[i]) bit[i] = MIN(bit[i], v); else bit[i] = v; } } void Dfs(int u) { in[u] = ++cnt; for (int v : P[u]) { Dfs(v); } out[u] = cnt; } int main() { scanf("%d%d", &n, &k); P.resize(n + 3); a.resize(n + 3); in.resize(n + 3); out.resize(n + 3); bit.resize(n + 3); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = n; i >= 1; i--) { int x = GetBit(a[i] + 1); if (x != 1e9) { P[x].push_back(i); par[i] = 1; } Update(a[i], i); } for (int i = 1; i <= n; i++) { if (!par[i]) { P[n + 1].push_back(i); } } Dfs(n + 1); root = add(); for (int i = 1; i < k; i++) { root->Update(1, cnt, in[i], out[i], 1); } for (int i = k; i <= n; i += 1) { root->Update(1, cnt, in[i], out[i], 1); printf("%d ", root->COUNT); root->Update(1, cnt, in[i - k + 1], out[i - k + 1], -1); } }
### Prompt Your task is to create a CPP solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, k; vector<int> a, bit; bool par[1000005]; vector<int> in, out; int cnt(0); struct it { int COUNT, LAZY; it *l, *r; void Update(int l, int r, int L, int R, int a); }; it *add() { it *t = new it; t->COUNT = 0; t->LAZY = 0; t->l = t->r = NULL; return t; } void it::Update(int l, int r, int L, int R, int a) { if (r < L || R < l) return; if (L <= l && r <= R) { this->COUNT += a; this->LAZY += a; return; } if (this->r == NULL) this->r = add(); if (this->l == NULL) this->l = add(); if (this->LAZY) { this->l->COUNT += this->LAZY; this->r->COUNT += this->LAZY; this->l->LAZY += this->LAZY; this->r->LAZY += this->LAZY; this->LAZY = 0; } int m = (l + r) / 2; this->l->Update(l, m, L, R, a); this->r->Update(m + 1, r, L, R, a); this->COUNT = max(this->l->COUNT, this->r->COUNT); } it *root; vector<vector<int> > P; template <class T> T MAX(T a, T b) { return (a > b) ? a : b; }; template <class T> T MIN(T a, T b) { return (a < b) ? a : b; }; int GetBit(int u) { int dapan = 1e9; for (int i = n - u + 1; i >= 1; i -= i & (-i)) { if (bit[i]) dapan = MIN(dapan, bit[i]); } return dapan; } void Update(int u, int v) { for (int i = n - u + 1; i <= n; i += i & (-i)) { if (bit[i]) bit[i] = MIN(bit[i], v); else bit[i] = v; } } void Dfs(int u) { in[u] = ++cnt; for (int v : P[u]) { Dfs(v); } out[u] = cnt; } int main() { scanf("%d%d", &n, &k); P.resize(n + 3); a.resize(n + 3); in.resize(n + 3); out.resize(n + 3); bit.resize(n + 3); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = n; i >= 1; i--) { int x = GetBit(a[i] + 1); if (x != 1e9) { P[x].push_back(i); par[i] = 1; } Update(a[i], i); } for (int i = 1; i <= n; i++) { if (!par[i]) { P[n + 1].push_back(i); } } Dfs(n + 1); root = add(); for (int i = 1; i < k; i++) { root->Update(1, cnt, in[i], out[i], 1); } for (int i = k; i <= n; i += 1) { root->Update(1, cnt, in[i], out[i], 1); printf("%d ", root->COUNT); root->Update(1, cnt, in[i - k + 1], out[i - k + 1], -1); } } ```
#include <bits/stdc++.h> using namespace std; const int MX = 1e6; int st[MX * 4], la[MX * 4], tin[MX], tout[MX], t, n; vector<int> chd[MX]; void dfs(int u) { tin[u] = ++t; for (auto c : chd[u]) { dfs(c); } tout[u] = t; } void push(int v) { st[v * 2] += la[v]; la[v * 2] += la[v]; st[v * 2 + 1] += la[v]; la[v * 2 + 1] += la[v]; la[v] = 0; } void upd(int a, int b, int val, int v = 1, int l = 1, int r = n) { if (b < l || r < a) return; if (a <= l && r <= b) { st[v] += val; la[v] += val; } else { push(v); int m = (l + r) / 2; upd(a, b, val, v * 2, l, m); upd(a, b, val, v * 2 + 1, m + 1, r); st[v] = max(st[v * 2], st[v * 2 + 1]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> n >> k; vector<pair<int, int>> stk; for (int i = 0; i < n; ++i) { int a; cin >> a; while (!stk.empty() && stk.back().first < a) { chd[i].push_back(stk.back().second); stk.pop_back(); } stk.emplace_back(a, i); } for (auto [a, i] : stk) { dfs(i); } for (int i = 0; i < n; ++i) { upd(tin[i], tout[i], 1); if (i >= k - 1) { if (i >= k) { upd(tin[i - k], tout[i - k], -1); } cout << st[1] << ' '; } } cout << '\n'; }
### Prompt Please create a solution in cpp to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MX = 1e6; int st[MX * 4], la[MX * 4], tin[MX], tout[MX], t, n; vector<int> chd[MX]; void dfs(int u) { tin[u] = ++t; for (auto c : chd[u]) { dfs(c); } tout[u] = t; } void push(int v) { st[v * 2] += la[v]; la[v * 2] += la[v]; st[v * 2 + 1] += la[v]; la[v * 2 + 1] += la[v]; la[v] = 0; } void upd(int a, int b, int val, int v = 1, int l = 1, int r = n) { if (b < l || r < a) return; if (a <= l && r <= b) { st[v] += val; la[v] += val; } else { push(v); int m = (l + r) / 2; upd(a, b, val, v * 2, l, m); upd(a, b, val, v * 2 + 1, m + 1, r); st[v] = max(st[v * 2], st[v * 2 + 1]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int k; cin >> n >> k; vector<pair<int, int>> stk; for (int i = 0; i < n; ++i) { int a; cin >> a; while (!stk.empty() && stk.back().first < a) { chd[i].push_back(stk.back().second); stk.pop_back(); } stk.emplace_back(a, i); } for (auto [a, i] : stk) { dfs(i); } for (int i = 0; i < n; ++i) { upd(tin[i], tout[i], 1); if (i >= k - 1) { if (i >= k) { upd(tin[i - k], tout[i - k], -1); } cout << st[1] << ' '; } } cout << '\n'; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int seg[4 * maxn], lazy[4 * maxn]; void propagate(int, int, int); int get(int id, int L, int R, int l, int r) { if (L == l and R == r) return seg[id]; propagate(id, L, R); int mid = (L + R) >> 1, ret = 0; if (l < mid) ret = max(ret, get(2 * id + 0, L, mid, l, min(mid, r))); if (mid < r) ret = max(ret, get(2 * id + 1, mid, R, max(l, mid), r)); return ret; } void add(int id, int L, int R, int l, int r, int val = 1) { if (L == l and R == r) { seg[id] += val; lazy[id] += val; return; } propagate(id, L, R); int mid = (L + R) >> 1; if (l < mid) add(2 * id + 0, L, mid, l, min(mid, r)); if (mid < r) add(2 * id + 1, mid, R, max(l, mid), r); seg[id] = max(seg[2 * id + 0], seg[2 * id + 1]); } void propagate(int id, int L, int R) { if (lazy[id] == 0) return; int mid = (L + R) >> 1; add(2 * id + 0, L, mid, L, mid, lazy[id]); add(2 * id + 1, mid, R, mid, R, lazy[id]); lazy[id] = 0; } int a[maxn], pre[maxn]; int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; a[n] = n + 1; stack<int> st; st.push(n); for (int i = 0; i < n; i++) { while (a[st.top()] < a[i]) st.pop(); pre[i] = st.top(); if (pre[i] == n) pre[i] = -1; st.push(i); } int ptr = 0; for (int i = 0; i < n - k + 1; i++) { while (ptr - i < k) { add(1, 0, n, pre[ptr] + 1, ptr + 1); ptr++; } cout << get(1, 0, n, i, i + k) << " "; } }
### Prompt Your challenge is to write a CPP solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int seg[4 * maxn], lazy[4 * maxn]; void propagate(int, int, int); int get(int id, int L, int R, int l, int r) { if (L == l and R == r) return seg[id]; propagate(id, L, R); int mid = (L + R) >> 1, ret = 0; if (l < mid) ret = max(ret, get(2 * id + 0, L, mid, l, min(mid, r))); if (mid < r) ret = max(ret, get(2 * id + 1, mid, R, max(l, mid), r)); return ret; } void add(int id, int L, int R, int l, int r, int val = 1) { if (L == l and R == r) { seg[id] += val; lazy[id] += val; return; } propagate(id, L, R); int mid = (L + R) >> 1; if (l < mid) add(2 * id + 0, L, mid, l, min(mid, r)); if (mid < r) add(2 * id + 1, mid, R, max(l, mid), r); seg[id] = max(seg[2 * id + 0], seg[2 * id + 1]); } void propagate(int id, int L, int R) { if (lazy[id] == 0) return; int mid = (L + R) >> 1; add(2 * id + 0, L, mid, L, mid, lazy[id]); add(2 * id + 1, mid, R, mid, R, lazy[id]); lazy[id] = 0; } int a[maxn], pre[maxn]; int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; a[n] = n + 1; stack<int> st; st.push(n); for (int i = 0; i < n; i++) { while (a[st.top()] < a[i]) st.pop(); pre[i] = st.top(); if (pre[i] == n) pre[i] = -1; st.push(i); } int ptr = 0; for (int i = 0; i < n - k + 1; i++) { while (ptr - i < k) { add(1, 0, n, pre[ptr] + 1, ptr + 1); ptr++; } cout << get(1, 0, n, i, i + k) << " "; } } ```
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1000025; int in[MAX_N], out[MAX_N], arr[MAX_N], dep, s[MAX_N << 2], col[MAX_N << 2], ans[MAX_N]; stack<int> st; vector<int> vt[MAX_N]; void dfs(int rt, int fa) { in[rt] = ++dep; for (int i = 0; i < vt[rt].size(); i++) { int to = vt[rt][i]; if (to == fa) continue; dfs(to, rt); } out[rt] = dep; } void up(int rt) { s[rt] = max(s[rt << 1], s[rt << 1 | 1]); } void build(int rt, int l, int r) { col[rt] = 0; if (l == r) { s[rt] = 0; return; } int mid = (l + r) >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); up(rt); } void down(int rt, int l, int r) { if (col[rt]) { int mid = (l + r) >> 1; col[rt << 1] += col[rt]; col[rt << 1 | 1] += col[rt]; s[rt << 1] += col[rt]; s[rt << 1 | 1] += col[rt]; col[rt] = 0; } } void update(int rt, int l, int r, int x, int y, int v) { if (x <= l && r <= y) { col[rt] += v; s[rt] += v; return; } int mid = (l + r) >> 1; down(rt, l, r); if (x <= mid) update(rt << 1, l, mid, x, y, v); if (mid < y) update(rt << 1 | 1, mid + 1, r, x, y, v); up(rt); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &arr[i]); build(1, 1, n + 1); for (int i = n; i >= 1; i--) { while (!st.empty() && arr[st.top()] <= arr[i]) st.pop(); if (st.empty()) { vt[0].push_back(i); } else { vt[st.top()].push_back(i); } st.push(i); } dfs(0, -1); for (int i = n - m + 1; i <= n; ++i) update(1, 1, n + 1, in[i], out[i], 1); ans[n - m + 1] = s[1]; for (int i = n - m; i >= 1; i--) { update(1, 1, n + 1, in[i + m], out[i + m], -1); update(1, 1, n + 1, in[i], out[i], 1); ans[i] = s[1]; } for (int i = 1; i <= n - m + 1; ++i) i == n - m + 1 ? printf("%d\n", ans[i]) : printf("%d ", ans[i]); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX_N = 1000025; int in[MAX_N], out[MAX_N], arr[MAX_N], dep, s[MAX_N << 2], col[MAX_N << 2], ans[MAX_N]; stack<int> st; vector<int> vt[MAX_N]; void dfs(int rt, int fa) { in[rt] = ++dep; for (int i = 0; i < vt[rt].size(); i++) { int to = vt[rt][i]; if (to == fa) continue; dfs(to, rt); } out[rt] = dep; } void up(int rt) { s[rt] = max(s[rt << 1], s[rt << 1 | 1]); } void build(int rt, int l, int r) { col[rt] = 0; if (l == r) { s[rt] = 0; return; } int mid = (l + r) >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); up(rt); } void down(int rt, int l, int r) { if (col[rt]) { int mid = (l + r) >> 1; col[rt << 1] += col[rt]; col[rt << 1 | 1] += col[rt]; s[rt << 1] += col[rt]; s[rt << 1 | 1] += col[rt]; col[rt] = 0; } } void update(int rt, int l, int r, int x, int y, int v) { if (x <= l && r <= y) { col[rt] += v; s[rt] += v; return; } int mid = (l + r) >> 1; down(rt, l, r); if (x <= mid) update(rt << 1, l, mid, x, y, v); if (mid < y) update(rt << 1 | 1, mid + 1, r, x, y, v); up(rt); } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &arr[i]); build(1, 1, n + 1); for (int i = n; i >= 1; i--) { while (!st.empty() && arr[st.top()] <= arr[i]) st.pop(); if (st.empty()) { vt[0].push_back(i); } else { vt[st.top()].push_back(i); } st.push(i); } dfs(0, -1); for (int i = n - m + 1; i <= n; ++i) update(1, 1, n + 1, in[i], out[i], 1); ans[n - m + 1] = s[1]; for (int i = n - m; i >= 1; i--) { update(1, 1, n + 1, in[i + m], out[i + m], -1); update(1, 1, n + 1, in[i], out[i], 1); ans[i] = s[1]; } for (int i = 1; i <= n - m + 1; ++i) i == n - m + 1 ? printf("%d\n", ans[i]) : printf("%d ", ans[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int i, j, l, cnt = 0; int ar[1000050], tin[1000050], tout[1000050], ans[1000050], ta = 0; int co[1000050] = {0}; vector<int> g[1000050]; void dfs(int x) { tin[x] = ++ta; for (auto it : g[x]) dfs(it); tout[x] = ta; } struct Segment { int l, r; int im, _im; } seg[1000050 * 4]; void pp(int rt) { seg[rt].im = max(seg[2 * rt + 1].im, seg[2 * rt + 2].im); } void pd(int rt) { if (seg[rt].im) { seg[2 * rt + 1]._im += seg[rt]._im; seg[2 * rt + 2]._im += seg[rt]._im; seg[2 * rt + 1].im += seg[rt]._im; seg[2 * rt + 2].im += seg[rt]._im; seg[rt]._im = 0; } } void bt(int rt, int l, int r) { seg[rt]._im = 0, seg[rt].l = l, seg[rt].r = r; if (l == r) seg[rt].im = 0; else { bt(2 * rt + 1, l, l + r >> 1); bt(2 * rt + 2, (l + r) / 2 + 1, r); pp(rt); } } void ud(int rt, int ul, int ur, int val) { int r = seg[rt].r, l = seg[rt].l; if (ur < l || r < ul) return; if (ul <= l && r <= ur) { seg[rt].im += val; seg[rt]._im += val; return; } pd(rt); ud(2 * rt + 1, ul, ur, val); ud(2 * rt + 2, ul, ur, val); pp(rt); } int qy(int rt, int ql, int qr) { int r = seg[rt].r, l = seg[rt].l; if (qr < l || r < ql) return 0; if (ql <= l && r <= qr) { return seg[rt].im; } pd(rt); return max(qy(2 * rt + 1, ql, qr), qy(2 * rt + 2, ql, qr)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; for (i = 1; i < n + 1; i++) cin >> ar[i]; for (i = n + 1 - 1; i >= 1; i--) { for (l = i, j = i + 1; j <= n && !co[l];) { if (ar[j] > ar[l]) co[l] = j, l = j; else j = co[j]; } if (!co[l]) co[l] = n + 1; } for (i = 1; i < n + 1; i++) g[co[i]].push_back(i); dfs(n + 1); bt(0, 1, n + 1); for (i = 1; i < k; i++) ud(0, tin[i], tout[i], 1); for (i = k; i < n + 1; i++) { ud(0, tin[i], tout[i], 1); ans[cnt++] = seg[0].im; ud(0, tin[i - k + 1], tout[i - k + 1], -1); } for (i = 0; i < cnt; i++) cout << ans[i] << (n - i - 1 ? ' ' : '\n'); return ~~(0 ^ 0 ^ 0); }
### Prompt Construct a CPP code solution to the problem outlined: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int i, j, l, cnt = 0; int ar[1000050], tin[1000050], tout[1000050], ans[1000050], ta = 0; int co[1000050] = {0}; vector<int> g[1000050]; void dfs(int x) { tin[x] = ++ta; for (auto it : g[x]) dfs(it); tout[x] = ta; } struct Segment { int l, r; int im, _im; } seg[1000050 * 4]; void pp(int rt) { seg[rt].im = max(seg[2 * rt + 1].im, seg[2 * rt + 2].im); } void pd(int rt) { if (seg[rt].im) { seg[2 * rt + 1]._im += seg[rt]._im; seg[2 * rt + 2]._im += seg[rt]._im; seg[2 * rt + 1].im += seg[rt]._im; seg[2 * rt + 2].im += seg[rt]._im; seg[rt]._im = 0; } } void bt(int rt, int l, int r) { seg[rt]._im = 0, seg[rt].l = l, seg[rt].r = r; if (l == r) seg[rt].im = 0; else { bt(2 * rt + 1, l, l + r >> 1); bt(2 * rt + 2, (l + r) / 2 + 1, r); pp(rt); } } void ud(int rt, int ul, int ur, int val) { int r = seg[rt].r, l = seg[rt].l; if (ur < l || r < ul) return; if (ul <= l && r <= ur) { seg[rt].im += val; seg[rt]._im += val; return; } pd(rt); ud(2 * rt + 1, ul, ur, val); ud(2 * rt + 2, ul, ur, val); pp(rt); } int qy(int rt, int ql, int qr) { int r = seg[rt].r, l = seg[rt].l; if (qr < l || r < ql) return 0; if (ql <= l && r <= qr) { return seg[rt].im; } pd(rt); return max(qy(2 * rt + 1, ql, qr), qy(2 * rt + 2, ql, qr)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; for (i = 1; i < n + 1; i++) cin >> ar[i]; for (i = n + 1 - 1; i >= 1; i--) { for (l = i, j = i + 1; j <= n && !co[l];) { if (ar[j] > ar[l]) co[l] = j, l = j; else j = co[j]; } if (!co[l]) co[l] = n + 1; } for (i = 1; i < n + 1; i++) g[co[i]].push_back(i); dfs(n + 1); bt(0, 1, n + 1); for (i = 1; i < k; i++) ud(0, tin[i], tout[i], 1); for (i = k; i < n + 1; i++) { ud(0, tin[i], tout[i], 1); ans[cnt++] = seg[0].im; ud(0, tin[i - k + 1], tout[i - k + 1], -1); } for (i = 0; i < cnt; i++) cout << ans[i] << (n - i - 1 ? ' ' : '\n'); return ~~(0 ^ 0 ^ 0); } ```
#include <bits/stdc++.h> using namespace std; int n, m, tot, num[1000010], L[1000010]; struct node { int l, r, mx, sum; } t[1000010 << 1]; void build(int d, int l, int r) { if (l == r) return; int mid = l + r >> 1; t[d].l = ++tot, build(tot, l, mid), t[d].r = ++tot, build(tot, mid + 1, r); } void add(int d, int l, int r, int L, int R, int x) { if (L <= l && r <= R) { t[d].mx += x, t[d].sum += x; return; } int mid = l + r >> 1; if (L <= mid) add(t[d].l, l, mid, L, R, x); if (R > mid) add(t[d].r, mid + 1, r, L, R, x); t[d].mx = max(t[t[d].l].mx, t[t[d].r].mx) + t[d].sum; } int query(int d, int l, int r, int L, int R) { if (L <= l && r <= R) return t[d].mx; int mid = l + r >> 1, res = 0; if (L <= mid) res = max(res, query(t[d].l, l, mid, L, R)); if (mid < R) res = max(res, query(t[d].r, mid + 1, r, L, R)); return res + t[d].sum; } int main() { scanf("%d%d", &n, &m), num[0] = 1000010; for (int i = 1; i <= n; i++) scanf("%d", &num[i]); for (int i = 1; i <= n; i++) { L[i] = i - 1; for (; num[i] > num[L[i]]; L[i] = L[L[i]]) ; } build(tot = 1, 1, n); for (int i = 1; i < m; i++) add(1, 1, n, L[i] + 1, i, 1); for (int i = 1, j = m; j <= n; i++, j++) add(1, 1, n, L[j] + 1, j, 1), printf("%d ", query(1, 1, n, i, j)); }
### Prompt Develop a solution in CPP to the problem described below: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, tot, num[1000010], L[1000010]; struct node { int l, r, mx, sum; } t[1000010 << 1]; void build(int d, int l, int r) { if (l == r) return; int mid = l + r >> 1; t[d].l = ++tot, build(tot, l, mid), t[d].r = ++tot, build(tot, mid + 1, r); } void add(int d, int l, int r, int L, int R, int x) { if (L <= l && r <= R) { t[d].mx += x, t[d].sum += x; return; } int mid = l + r >> 1; if (L <= mid) add(t[d].l, l, mid, L, R, x); if (R > mid) add(t[d].r, mid + 1, r, L, R, x); t[d].mx = max(t[t[d].l].mx, t[t[d].r].mx) + t[d].sum; } int query(int d, int l, int r, int L, int R) { if (L <= l && r <= R) return t[d].mx; int mid = l + r >> 1, res = 0; if (L <= mid) res = max(res, query(t[d].l, l, mid, L, R)); if (mid < R) res = max(res, query(t[d].r, mid + 1, r, L, R)); return res + t[d].sum; } int main() { scanf("%d%d", &n, &m), num[0] = 1000010; for (int i = 1; i <= n; i++) scanf("%d", &num[i]); for (int i = 1; i <= n; i++) { L[i] = i - 1; for (; num[i] > num[L[i]]; L[i] = L[L[i]]) ; } build(tot = 1, 1, n); for (int i = 1; i < m; i++) add(1, 1, n, L[i] + 1, i, 1); for (int i = 1, j = m; j <= n; i++, j++) add(1, 1, n, L[j] + 1, j, 1), printf("%d ", query(1, 1, n, i, j)); } ```
#include <bits/stdc++.h> using namespace std; int st[1000007]; int top; int s[1000007], t[1000007]; int mx[4000007]; int sum[4000007]; int head[1000007], to[2000007], nex[2000007]; int n, k; int a[10000077]; int dfn; int tot; void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } void pushdown(int rt) { if (sum[rt]) { sum[rt << 1] += sum[rt]; sum[rt << 1 | 1] += sum[rt]; mx[rt << 1] += sum[rt]; mx[rt << 1 | 1] += sum[rt]; sum[rt] = 0; } } void change(int rt, int l, int r, int L, int R, int x) { if (L <= l && r <= R) { sum[rt] += x; mx[rt] += x; return; } pushdown(rt); int mid = (l + r) >> 1; if (L <= mid) change(rt << 1, l, mid, L, R, x); if (R > mid) change(rt << 1 | 1, mid + 1, r, L, R, x); pushup(rt); } void add(int x, int y) { nex[++tot] = head[x]; head[x] = tot; to[tot] = y; } void dfs(int x) { s[x] = ++dfn; for (int i = head[x]; i; i = nex[i]) dfs(to[i]); t[x] = dfn; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); while (top && a[st[top]] < a[i]) { add(i, st[top]); top--; } st[++top] = i; } while (top) { add(n + 1, st[top]); top--; } dfs(n + 1); for (int i = 1; i <= k; i++) change(1, 1, n + 1, s[i], t[i], 1); printf("%d ", mx[1]); for (int i = k + 1; i <= n; i++) { change(1, 1, n + 1, s[i], t[i], 1); change(1, 1, n + 1, s[i - k], t[i - k], -1); printf("%d ", mx[1]); } }
### Prompt Please create a solution in CPP to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int st[1000007]; int top; int s[1000007], t[1000007]; int mx[4000007]; int sum[4000007]; int head[1000007], to[2000007], nex[2000007]; int n, k; int a[10000077]; int dfn; int tot; void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } void pushdown(int rt) { if (sum[rt]) { sum[rt << 1] += sum[rt]; sum[rt << 1 | 1] += sum[rt]; mx[rt << 1] += sum[rt]; mx[rt << 1 | 1] += sum[rt]; sum[rt] = 0; } } void change(int rt, int l, int r, int L, int R, int x) { if (L <= l && r <= R) { sum[rt] += x; mx[rt] += x; return; } pushdown(rt); int mid = (l + r) >> 1; if (L <= mid) change(rt << 1, l, mid, L, R, x); if (R > mid) change(rt << 1 | 1, mid + 1, r, L, R, x); pushup(rt); } void add(int x, int y) { nex[++tot] = head[x]; head[x] = tot; to[tot] = y; } void dfs(int x) { s[x] = ++dfn; for (int i = head[x]; i; i = nex[i]) dfs(to[i]); t[x] = dfn; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); while (top && a[st[top]] < a[i]) { add(i, st[top]); top--; } st[++top] = i; } while (top) { add(n + 1, st[top]); top--; } dfs(n + 1); for (int i = 1; i <= k; i++) change(1, 1, n + 1, s[i], t[i], 1); printf("%d ", mx[1]); for (int i = k + 1; i <= n; i++) { change(1, 1, n + 1, s[i], t[i], 1); change(1, 1, n + 1, s[i - k], t[i - k], -1); printf("%d ", mx[1]); } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1000005; int n, k, ara[N], to[N]; vector<int> g[N]; int tree[4 * N], lazy[4 * N]; void upd(int node, int val) { lazy[node] += val; tree[node] += val; } void push(int node) { if (lazy[node] != 0) { upd(2 * node, lazy[node]); upd(2 * node + 1, lazy[node]); lazy[node] = 0; } } void update(int node, int L, int R, int pos, int val) { if (pos < L or pos > R) return; if (L == R) { tree[node] = val; return; } push(node); int mid = (L + R) >> 1; update(2 * node, L, mid, pos, val); update(2 * node + 1, mid + 1, R, pos, val); tree[node] = max(tree[node * 2], tree[node * 2 + 1]); } void updateRange(int node, int L, int R, int l, int r, int val) { if (R < l or r < L) return; if (L >= l and R <= r) { upd(node, val); return; } int mid = (L + R) >> 1; push(node); updateRange(2 * node, L, mid, l, r, val); updateRange(2 * node + 1, mid + 1, R, l, r, val); tree[node] = max(tree[node * 2], tree[node * 2 + 1]); } int query(int node, int L, int R, int l, int r) { if (R < l or r < L) return 0; if (L >= l and R <= r) { return tree[node]; } push(node); int mid = (L + R) >> 1; int x = query(2 * node, L, mid, l, r); int y = query(2 * node + 1, mid + 1, R, l, r); return max(x, y); } int st[N], ed[N]; void dfs(int node, int pre) { static int timer = 0; st[node] = ++timer; for (int i : g[node]) { if (i == pre) continue; dfs(i, node); } ed[node] = timer; } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> ara[i]; ara[n + 1] = 1e9; stack<int> stk; stk.push(n + 1); for (int i = n; i >= 1; i--) { while (!stk.empty() and ara[stk.top()] <= ara[i]) { stk.pop(); } to[i] = stk.top(); stk.push(i); g[to[i]].push_back(i); } dfs(n + 1, 0); for (int i = 1; i < k; i++) { updateRange(1, 1, n + 1, st[i], ed[i], 1); } for (int i = k; i <= n; i++) { updateRange(1, 1, n + 1, st[i], ed[i], 1); int ans = query(1, 1, n + 1, 1, n + 1); cout << ans << " "; update(1, 1, n + 1, st[i - k + 1], -1e9); } }
### Prompt Your task is to create a Cpp solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1000005; int n, k, ara[N], to[N]; vector<int> g[N]; int tree[4 * N], lazy[4 * N]; void upd(int node, int val) { lazy[node] += val; tree[node] += val; } void push(int node) { if (lazy[node] != 0) { upd(2 * node, lazy[node]); upd(2 * node + 1, lazy[node]); lazy[node] = 0; } } void update(int node, int L, int R, int pos, int val) { if (pos < L or pos > R) return; if (L == R) { tree[node] = val; return; } push(node); int mid = (L + R) >> 1; update(2 * node, L, mid, pos, val); update(2 * node + 1, mid + 1, R, pos, val); tree[node] = max(tree[node * 2], tree[node * 2 + 1]); } void updateRange(int node, int L, int R, int l, int r, int val) { if (R < l or r < L) return; if (L >= l and R <= r) { upd(node, val); return; } int mid = (L + R) >> 1; push(node); updateRange(2 * node, L, mid, l, r, val); updateRange(2 * node + 1, mid + 1, R, l, r, val); tree[node] = max(tree[node * 2], tree[node * 2 + 1]); } int query(int node, int L, int R, int l, int r) { if (R < l or r < L) return 0; if (L >= l and R <= r) { return tree[node]; } push(node); int mid = (L + R) >> 1; int x = query(2 * node, L, mid, l, r); int y = query(2 * node + 1, mid + 1, R, l, r); return max(x, y); } int st[N], ed[N]; void dfs(int node, int pre) { static int timer = 0; st[node] = ++timer; for (int i : g[node]) { if (i == pre) continue; dfs(i, node); } ed[node] = timer; } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> ara[i]; ara[n + 1] = 1e9; stack<int> stk; stk.push(n + 1); for (int i = n; i >= 1; i--) { while (!stk.empty() and ara[stk.top()] <= ara[i]) { stk.pop(); } to[i] = stk.top(); stk.push(i); g[to[i]].push_back(i); } dfs(n + 1, 0); for (int i = 1; i < k; i++) { updateRange(1, 1, n + 1, st[i], ed[i], 1); } for (int i = k; i <= n; i++) { updateRange(1, 1, n + 1, st[i], ed[i], 1); int ans = query(1, 1, n + 1, 1, n + 1); cout << ans << " "; update(1, 1, n + 1, st[i - k + 1], -1e9); } } ```
#include <bits/stdc++.h> int N, K, a[1000002], q[1000002], next[1000002]; int RMQ[20][1000001], *dep = RMQ[0], LOG[1000001]; struct heap { std::priority_queue<int> add, rmv; void push(int x) { add.push(x); } void del(int x) { rmv.push(x); } int top() { while (!rmv.empty() && add.top() == rmv.top()) { add.pop(); rmv.pop(); } return add.top(); } } answer; int max_dep(int l, int r) { int Log = LOG[r - l + 1]; return std::max(RMQ[Log][l], RMQ[Log][r - (1 << Log) + 1]); } int main() { scanf("%d%d", &N, &K); for (int i = 1; i <= N; i++) scanf("%d", a + i); for (int i = N, D = 0; i; i--) { while (D && a[q[D]] <= a[i]) D--; next[i] = D ? q[D] : N + 1; dep[i] = dep[next[i]] + 1; q[++D] = i; } for (int i = 1; 1 << i <= N; i++) for (int j = 1; j <= N - (1 << i) + 1; j++) RMQ[i][j] = std::max(RMQ[i - 1][j], RMQ[i - 1][j + (1 << i - 1)]); for (int i = 2; i <= N; i++) LOG[i] = LOG[i >> 1] + 1; for (int i = 1, H = 1, T = 0, l = 1, r = 0; i <= N - K + 1; i++) { while (r < i + K - 1) { while (H <= T && a[q[T]] <= a[r + 1]) { answer.del(max_dep(H == T ? l : q[T - 1] + 1, q[T]) - dep[next[q[T]]]); T--; } q[++T] = ++r; answer.push(max_dep(H == T ? l : q[T - 1] + 1, q[T]) - dep[next[q[T]]]); } while (l < i) { int before = max_dep(l, q[H]); l++; if (q[H] < l) { answer.del(before - dep[next[q[H]]]); H++; } else { int after = max_dep(l, q[H]); if (before != after) { answer.del(before - dep[next[q[H]]]); answer.push(after - dep[next[q[H]]]); } } } printf("%d\n", answer.top()); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> int N, K, a[1000002], q[1000002], next[1000002]; int RMQ[20][1000001], *dep = RMQ[0], LOG[1000001]; struct heap { std::priority_queue<int> add, rmv; void push(int x) { add.push(x); } void del(int x) { rmv.push(x); } int top() { while (!rmv.empty() && add.top() == rmv.top()) { add.pop(); rmv.pop(); } return add.top(); } } answer; int max_dep(int l, int r) { int Log = LOG[r - l + 1]; return std::max(RMQ[Log][l], RMQ[Log][r - (1 << Log) + 1]); } int main() { scanf("%d%d", &N, &K); for (int i = 1; i <= N; i++) scanf("%d", a + i); for (int i = N, D = 0; i; i--) { while (D && a[q[D]] <= a[i]) D--; next[i] = D ? q[D] : N + 1; dep[i] = dep[next[i]] + 1; q[++D] = i; } for (int i = 1; 1 << i <= N; i++) for (int j = 1; j <= N - (1 << i) + 1; j++) RMQ[i][j] = std::max(RMQ[i - 1][j], RMQ[i - 1][j + (1 << i - 1)]); for (int i = 2; i <= N; i++) LOG[i] = LOG[i >> 1] + 1; for (int i = 1, H = 1, T = 0, l = 1, r = 0; i <= N - K + 1; i++) { while (r < i + K - 1) { while (H <= T && a[q[T]] <= a[r + 1]) { answer.del(max_dep(H == T ? l : q[T - 1] + 1, q[T]) - dep[next[q[T]]]); T--; } q[++T] = ++r; answer.push(max_dep(H == T ? l : q[T - 1] + 1, q[T]) - dep[next[q[T]]]); } while (l < i) { int before = max_dep(l, q[H]); l++; if (q[H] < l) { answer.del(before - dep[next[q[H]]]); H++; } else { int after = max_dep(l, q[H]); if (before != after) { answer.del(before - dep[next[q[H]]]); answer.push(after - dep[next[q[H]]]); } } } printf("%d\n", answer.top()); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct edge { int to, next; } e[1000010]; struct Seg { int c, add; } tr[4000010]; int n, K, a[1000010], head[1000010], siz[1000010], dfn[1000010], cnt, tot, top, S[1000010]; inline void add(int u, int v) { cnt++; e[cnt].to = v; e[cnt].next = head[u]; head[u] = cnt; } inline void dfs(int u) { dfn[u] = ++tot; siz[u] = 1; for (int i = head[u]; i; i = e[i].next) { int v = e[i].to; dfs(v); siz[u] += siz[v]; } } inline void push_up(int p) { tr[p].c = max(tr[p << 1].c, tr[p << 1 | 1].c); } inline void push_down(int p) { if (tr[p].add == 0) return; tr[p << 1].c += tr[p].add; tr[p << 1 | 1].c += tr[p].add; tr[p << 1].add += tr[p].add; tr[p << 1 | 1].add += tr[p].add; tr[p].add = 0; } inline void update(int p, int l, int r, int L, int R, int val) { if (l >= L && r <= R) { tr[p].c += val; tr[p].add += val; return; } push_down(p); int mid = (l + r) >> 1; if (L <= mid) update(p << 1, l, mid, L, R, val); if (R > mid) update(p << 1 | 1, mid + 1, r, L, R, val); push_up(p); } int main() { scanf("%d%d", &n, &K); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); n++; a[n] = n; for (int i = 1; i <= n; i++) { while (top && a[S[top]] < a[i]) { add(i, S[top]); top--; } S[++top] = i; } dfs(n); for (int i = 1; i <= K; i++) update(1, 1, n, dfn[i], dfn[i] + siz[i] - 1, 1); printf("%d ", tr[1].c); for (int i = K + 1; i < n; i++) { update(1, 1, n, dfn[i - K], dfn[i - K] + siz[i - K] - 1, -1); update(1, 1, n, dfn[i], dfn[i] + siz[i] - 1, 1); printf("%d ", tr[1].c); } return 0; }
### Prompt Generate a Cpp solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct edge { int to, next; } e[1000010]; struct Seg { int c, add; } tr[4000010]; int n, K, a[1000010], head[1000010], siz[1000010], dfn[1000010], cnt, tot, top, S[1000010]; inline void add(int u, int v) { cnt++; e[cnt].to = v; e[cnt].next = head[u]; head[u] = cnt; } inline void dfs(int u) { dfn[u] = ++tot; siz[u] = 1; for (int i = head[u]; i; i = e[i].next) { int v = e[i].to; dfs(v); siz[u] += siz[v]; } } inline void push_up(int p) { tr[p].c = max(tr[p << 1].c, tr[p << 1 | 1].c); } inline void push_down(int p) { if (tr[p].add == 0) return; tr[p << 1].c += tr[p].add; tr[p << 1 | 1].c += tr[p].add; tr[p << 1].add += tr[p].add; tr[p << 1 | 1].add += tr[p].add; tr[p].add = 0; } inline void update(int p, int l, int r, int L, int R, int val) { if (l >= L && r <= R) { tr[p].c += val; tr[p].add += val; return; } push_down(p); int mid = (l + r) >> 1; if (L <= mid) update(p << 1, l, mid, L, R, val); if (R > mid) update(p << 1 | 1, mid + 1, r, L, R, val); push_up(p); } int main() { scanf("%d%d", &n, &K); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); n++; a[n] = n; for (int i = 1; i <= n; i++) { while (top && a[S[top]] < a[i]) { add(i, S[top]); top--; } S[++top] = i; } dfs(n); for (int i = 1; i <= K; i++) update(1, 1, n, dfn[i], dfn[i] + siz[i] - 1, 1); printf("%d ", tr[1].c); for (int i = K + 1; i < n; i++) { update(1, 1, n, dfn[i - K], dfn[i - K] + siz[i - K] - 1, -1); update(1, 1, n, dfn[i], dfn[i] + siz[i] - 1, 1); printf("%d ", tr[1].c); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-13; const long double PI = acos(-1); const int INF = (int)1e9 + 7; const long long INFF = (long long)1e18; const int mod = (int)1e9 + 7; const int MXN = (int)1e6 + 7; int a[MXN], siz[MXN], gp[MXN], pos[MXN]; vector<int> mem[MXN]; int fd(int v) { return gp[v] == v ? v : gp[v] = fd(gp[v]); } void go(int v, int p) { reverse(begin(mem[v]), end(mem[v])); for (int x : mem[v]) { pos[x] = p; go(x, p - 1); p -= siz[x]; } } struct Seg { int ll[MXN << 2], rr[MXN << 2], v[MXN << 2], d[MXN << 2]; void bd(int l, int r, int p = 1) { ll[p] = l, rr[p] = r, v[p] = -INF; if (l == r) return; int mid = (l + r) >> 1; bd(l, mid, p << 1); bd(mid + 1, r, p << 1 | 1); } inline void pushdown(int p) { d[p << 1] += d[p]; d[p << 1 | 1] += d[p]; v[p << 1] += d[p]; v[p << 1 | 1] += d[p]; d[p] = 0; } int qy() { return v[1]; } void md1(int pp, int val, int p = 1) { if (ll[p] == rr[p]) { v[p] = val; return; } pushdown(p); int mid = (ll[p] + rr[p]) >> 1; if (pp <= mid) md1(pp, val, p << 1); else md1(pp, val, p << 1 | 1); v[p] = max(v[p << 1], v[p << 1 | 1]); } void md2(int l, int r, int p = 1) { if (ll[p] > r || rr[p] < l) return; if (l <= ll[p] && rr[p] <= r) { d[p]++; v[p]++; return; } pushdown(p); md2(l, r, p << 1); md2(l, r, p << 1 | 1); v[p] = max(v[p << 1], v[p << 1 | 1]); } } seg; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) { siz[i] = 1; gp[i] = i; } set<pair<int, int> > st; for (int i = 1; i <= n; i++) { auto rr = st.lower_bound({a[i], -INF}); for (auto it = st.begin(); it != rr; it++) { siz[i] += siz[-it->second]; mem[i].push_back(-it->second); } while (!st.empty() && st.begin()->first < a[i]) st.erase(st.begin()); st.insert({a[i], -i}); } int cntp = 0; for (auto it : st) { cntp += siz[-it.second]; pos[-it.second] = cntp; go(-it.second, cntp - 1); } seg.bd(1, n); for (int i = 1; i <= k; i++) { seg.md1(pos[i], 0); seg.md2(1, pos[i]); } printf("%d ", seg.qy()); for (int i = k + 1; i <= n; i++) { seg.md1(pos[i - k], -INF); seg.md1(pos[i], 0); seg.md2(1, pos[i]); printf("%d ", seg.qy()); } puts(""); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long double eps = 1e-13; const long double PI = acos(-1); const int INF = (int)1e9 + 7; const long long INFF = (long long)1e18; const int mod = (int)1e9 + 7; const int MXN = (int)1e6 + 7; int a[MXN], siz[MXN], gp[MXN], pos[MXN]; vector<int> mem[MXN]; int fd(int v) { return gp[v] == v ? v : gp[v] = fd(gp[v]); } void go(int v, int p) { reverse(begin(mem[v]), end(mem[v])); for (int x : mem[v]) { pos[x] = p; go(x, p - 1); p -= siz[x]; } } struct Seg { int ll[MXN << 2], rr[MXN << 2], v[MXN << 2], d[MXN << 2]; void bd(int l, int r, int p = 1) { ll[p] = l, rr[p] = r, v[p] = -INF; if (l == r) return; int mid = (l + r) >> 1; bd(l, mid, p << 1); bd(mid + 1, r, p << 1 | 1); } inline void pushdown(int p) { d[p << 1] += d[p]; d[p << 1 | 1] += d[p]; v[p << 1] += d[p]; v[p << 1 | 1] += d[p]; d[p] = 0; } int qy() { return v[1]; } void md1(int pp, int val, int p = 1) { if (ll[p] == rr[p]) { v[p] = val; return; } pushdown(p); int mid = (ll[p] + rr[p]) >> 1; if (pp <= mid) md1(pp, val, p << 1); else md1(pp, val, p << 1 | 1); v[p] = max(v[p << 1], v[p << 1 | 1]); } void md2(int l, int r, int p = 1) { if (ll[p] > r || rr[p] < l) return; if (l <= ll[p] && rr[p] <= r) { d[p]++; v[p]++; return; } pushdown(p); md2(l, r, p << 1); md2(l, r, p << 1 | 1); v[p] = max(v[p << 1], v[p << 1 | 1]); } } seg; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) { siz[i] = 1; gp[i] = i; } set<pair<int, int> > st; for (int i = 1; i <= n; i++) { auto rr = st.lower_bound({a[i], -INF}); for (auto it = st.begin(); it != rr; it++) { siz[i] += siz[-it->second]; mem[i].push_back(-it->second); } while (!st.empty() && st.begin()->first < a[i]) st.erase(st.begin()); st.insert({a[i], -i}); } int cntp = 0; for (auto it : st) { cntp += siz[-it.second]; pos[-it.second] = cntp; go(-it.second, cntp - 1); } seg.bd(1, n); for (int i = 1; i <= k; i++) { seg.md1(pos[i], 0); seg.md2(1, pos[i]); } printf("%d ", seg.qy()); for (int i = k + 1; i <= n; i++) { seg.md1(pos[i - k], -INF); seg.md1(pos[i], 0); seg.md2(1, pos[i]); printf("%d ", seg.qy()); } puts(""); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 3; const int MAXM = 2048 * 1024 + 3; int t[MAXN]; int nas[MAXN]; int tree[MAXM]; int lazy[MAXM]; pair<int, int> zak[MAXN]; vector<int> vec[MAXN]; int ile = 0; void DFS(int x) { zak[x].first = ile + 1; for (auto it : vec[x]) DFS(it); if (vec[x].empty()) ile++; zak[x].second = ile; } void zepchnij(int v) { tree[v * 2] += lazy[v]; tree[v * 2 + 1] += lazy[v]; lazy[v * 2] += lazy[v]; lazy[v * 2 + 1] += lazy[v]; lazy[v] = 0; } void dodaj(int v, int x, int y, int a, int b, int war) { if (y < a || x > b) return; if (x >= a && y <= b) { tree[v] += war; lazy[v] += war; return; } zepchnij(v); dodaj(v * 2, x, (x + y) / 2, a, b, war); dodaj(v * 2 + 1, (x + y) / 2 + 1, y, a, b, war); tree[v] = max(tree[v * 2], tree[v * 2 + 1]); } int main() { int n, k; int pot = 1; scanf("%d %d", &n, &k); while (pot < n) pot *= 2; for (int i = 1; i <= n; i++) scanf("%d", &t[i]); t[n + 1] = MAXN; for (int i = n; i > 0; i--) { nas[i] = i + 1; while (t[i] >= t[nas[i]]) nas[i] = nas[nas[i]]; vec[nas[i]].push_back(i); } DFS(n + 1); for (int i = 1; i <= k; i++) dodaj(1, 1, pot, zak[i].first, zak[i].second, 1); printf("%d ", tree[1]); for (int i = k + 1; i <= n; i++) { dodaj(1, 1, pot, zak[i - k].first, zak[i - k].second, -1); dodaj(1, 1, pot, zak[i].first, zak[i].second, 1); printf("%d ", tree[1]); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 3; const int MAXM = 2048 * 1024 + 3; int t[MAXN]; int nas[MAXN]; int tree[MAXM]; int lazy[MAXM]; pair<int, int> zak[MAXN]; vector<int> vec[MAXN]; int ile = 0; void DFS(int x) { zak[x].first = ile + 1; for (auto it : vec[x]) DFS(it); if (vec[x].empty()) ile++; zak[x].second = ile; } void zepchnij(int v) { tree[v * 2] += lazy[v]; tree[v * 2 + 1] += lazy[v]; lazy[v * 2] += lazy[v]; lazy[v * 2 + 1] += lazy[v]; lazy[v] = 0; } void dodaj(int v, int x, int y, int a, int b, int war) { if (y < a || x > b) return; if (x >= a && y <= b) { tree[v] += war; lazy[v] += war; return; } zepchnij(v); dodaj(v * 2, x, (x + y) / 2, a, b, war); dodaj(v * 2 + 1, (x + y) / 2 + 1, y, a, b, war); tree[v] = max(tree[v * 2], tree[v * 2 + 1]); } int main() { int n, k; int pot = 1; scanf("%d %d", &n, &k); while (pot < n) pot *= 2; for (int i = 1; i <= n; i++) scanf("%d", &t[i]); t[n + 1] = MAXN; for (int i = n; i > 0; i--) { nas[i] = i + 1; while (t[i] >= t[nas[i]]) nas[i] = nas[nas[i]]; vec[nas[i]].push_back(i); } DFS(n + 1); for (int i = 1; i <= k; i++) dodaj(1, 1, pot, zak[i].first, zak[i].second, 1); printf("%d ", tree[1]); for (int i = k + 1; i <= n; i++) { dodaj(1, 1, pot, zak[i - k].first, zak[i - k].second, -1); dodaj(1, 1, pot, zak[i].first, zak[i].second, 1); printf("%d ", tree[1]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct xy { int x, y; }; int a[1212121], par[1212121], depth[1212121]; vector<int> C[1212121]; void dfs(int w, int dep) { depth[w] = dep; for (int nxt : C[w]) dfs(nxt, dep + 1); } int Hn = 0, myH[1212121], sz[1212121], root[1212121], ppar[1212121]; int find(int x) { if (ppar[x] == x) return x; return ppar[x] = find(ppar[x]); } multiset<int> S[1212121], ansS; void push(int x) { sz[x] = 1; int mx = 0, mxw; for (int child : C[x]) { sz[x] += sz[child]; if (mx < sz[child] && S[myH[child]].size() > 0) mx = sz[child], mxw = child; } if (mx == 0) { int hIdx = ++Hn; myH[x] = hIdx; root[hIdx] = x; S[hIdx].insert(depth[x]); ppar[hIdx] = hIdx; ansS.insert(0); } else { int hIdx = myH[x] = myH[mxw]; root[hIdx] = x; int dep = (*S[hIdx].rbegin()) - depth[mxw]; ansS.erase(ansS.find(dep)); S[hIdx].insert(depth[x]); for (int child : C[x]) if (child != mxw) { if (!S[myH[child]].empty()) { for (auto &v : S[myH[child]]) S[hIdx].insert(v); ppar[myH[child]] = hIdx; int dep = (*S[myH[child]].rbegin()) - depth[child]; ansS.erase(ansS.find(dep)); } S[myH[child]].clear(); } dep = (*S[hIdx].rbegin()) - depth[x]; ansS.insert(dep); } } void pop(int x) { int hIdx = find(myH[x]); int dep = (*S[hIdx].rbegin()) - depth[root[hIdx]]; ansS.erase(ansS.find(dep)); S[hIdx].erase(S[hIdx].find(depth[x])); if (S[hIdx].empty()) return; dep = (*S[hIdx].rbegin()) - depth[root[hIdx]]; ansS.insert(dep); } int main() { int n, k, i, j; scanf("%d%d", &n, &k); for (i = 1; i <= n; i++) scanf("%d", &a[i]); vector<xy> ST; ST.push_back({n + 1, n + 1}); for (i = n; i >= 1; i--) { while (ST.back().x <= a[i]) ST.pop_back(); par[i] = ST.back().y; C[par[i]].push_back(i); ST.push_back({a[i], i}); } dfs(n + 1, 0); for (i = 1; i <= n; i++) { push(i); if (k <= i) { printf("%d ", *ansS.rbegin() + 1); if (i != n) pop(i - k + 1); } } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct xy { int x, y; }; int a[1212121], par[1212121], depth[1212121]; vector<int> C[1212121]; void dfs(int w, int dep) { depth[w] = dep; for (int nxt : C[w]) dfs(nxt, dep + 1); } int Hn = 0, myH[1212121], sz[1212121], root[1212121], ppar[1212121]; int find(int x) { if (ppar[x] == x) return x; return ppar[x] = find(ppar[x]); } multiset<int> S[1212121], ansS; void push(int x) { sz[x] = 1; int mx = 0, mxw; for (int child : C[x]) { sz[x] += sz[child]; if (mx < sz[child] && S[myH[child]].size() > 0) mx = sz[child], mxw = child; } if (mx == 0) { int hIdx = ++Hn; myH[x] = hIdx; root[hIdx] = x; S[hIdx].insert(depth[x]); ppar[hIdx] = hIdx; ansS.insert(0); } else { int hIdx = myH[x] = myH[mxw]; root[hIdx] = x; int dep = (*S[hIdx].rbegin()) - depth[mxw]; ansS.erase(ansS.find(dep)); S[hIdx].insert(depth[x]); for (int child : C[x]) if (child != mxw) { if (!S[myH[child]].empty()) { for (auto &v : S[myH[child]]) S[hIdx].insert(v); ppar[myH[child]] = hIdx; int dep = (*S[myH[child]].rbegin()) - depth[child]; ansS.erase(ansS.find(dep)); } S[myH[child]].clear(); } dep = (*S[hIdx].rbegin()) - depth[x]; ansS.insert(dep); } } void pop(int x) { int hIdx = find(myH[x]); int dep = (*S[hIdx].rbegin()) - depth[root[hIdx]]; ansS.erase(ansS.find(dep)); S[hIdx].erase(S[hIdx].find(depth[x])); if (S[hIdx].empty()) return; dep = (*S[hIdx].rbegin()) - depth[root[hIdx]]; ansS.insert(dep); } int main() { int n, k, i, j; scanf("%d%d", &n, &k); for (i = 1; i <= n; i++) scanf("%d", &a[i]); vector<xy> ST; ST.push_back({n + 1, n + 1}); for (i = n; i >= 1; i--) { while (ST.back().x <= a[i]) ST.pop_back(); par[i] = ST.back().y; C[par[i]].push_back(i); ST.push_back({a[i], i}); } dfs(n + 1, 0); for (i = 1; i <= n; i++) { push(i); if (k <= i) { printf("%d ", *ansS.rbegin() + 1); if (i != n) pop(i - k + 1); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int arr[N]; int par[N]; vector<int> adj[N]; int start[N], ending[N]; int ctr = 0; int tree[4 * N] = {0}; int lazy[4 * N] = {0}; int ans[N]; inline int maxi(int a, int b) { return a > b ? a : b; } void dfs(int node) { start[node] = ctr++; for (int x : adj[node]) { dfs(x); } ending[node] = ctr - 1; } inline void upd(int node, int l, int r) { if (lazy[node] == 0 or l == r) { lazy[node] = 0; return; } tree[2 * node] += lazy[node]; lazy[2 * node] += lazy[node]; tree[2 * node + 1] += lazy[node]; lazy[2 * node + 1] += lazy[node]; lazy[node] = 0; } void update(int node, int l, int r, int left, int right, int val) { upd(node, l, r); if (right < l or r < left) { return; } if (l == r) { tree[node] += val; return; } if (left <= l and r <= right) { tree[node] += val; lazy[node] += val; return; } int m = (l + r) / 2; update(node * 2, l, m, left, right, val); update(2 * node + 1, m + 1, r, left, right, val); tree[node] = maxi(tree[2 * node], tree[2 * node + 1]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> arr[i]; } par[n] = 0; stack<int> s; s.push(n); for (int i = n - 1; i >= 1; i--) { while (!s.empty() and arr[s.top()] <= arr[i]) s.pop(); if (s.empty()) { par[i] = 0; } else par[i] = s.top(); s.push(i); } for (int i = 1; i <= n; i++) { adj[par[i]].push_back(i); } dfs(0); for (int i = 1; i <= k; i++) { update(1, 0, n, start[i], ending[i], 1); } ans[k] = tree[1]; for (int i = k + 1; i <= n; i++) { update(1, 0, n, start[i], ending[i], 1); update(1, 0, n, start[i - k], ending[i - k], -1); ans[i] = tree[1]; } for (int i = k; i <= n; i++) cout << ans[i] << " "; cout << endl; return 0; }
### Prompt Please create a solution in Cpp to the following problem: For some array c, let's denote a greedy subsequence as a sequence of indices p_1, p_2, ..., p_l such that 1 ≀ p_1 < p_2 < ... < p_l ≀ |c|, and for each i ∈ [1, l - 1], p_{i + 1} is the minimum number such that p_{i + 1} > p_i and c[p_{i + 1}] > c[p_i]. You are given an array a_1, a_2, ..., a_n. For each its subsegment of length k, calculate the length of its longest greedy subsequence. Input The first line contains two integers n and k (1 ≀ k ≀ n ≀ 10^6) β€” the length of array a and the length of subsegments. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ n) β€” array a. Output Print n - k + 1 integers β€” the maximum lengths of greedy subsequences of each subsegment having length k. The first number should correspond to subsegment a[1..k], the second β€” to subsegment a[2..k + 1], and so on. Examples Input 6 4 1 5 2 5 3 6 Output 2 2 3 Input 7 6 4 5 2 5 3 6 6 Output 3 3 Note In the first example: * [1, 5, 2, 5] β€” the longest greedy subsequences are 1, 2 ([c_1, c_2] = [1, 5]) or 3, 4 ([c_3, c_4] = [2, 5]). * [5, 2, 5, 3] β€” the sequence is 2, 3 ([c_2, c_3] = [2, 5]). * [2, 5, 3, 6] β€” the sequence is 1, 2, 4 ([c_1, c_2, c_4] = [2, 5, 6]). In the second example: * [4, 5, 2, 5, 3, 6] β€” the longest greedy subsequences are 1, 2, 6 ([c_1, c_2, c_6] = [4, 5, 6]) or 3, 4, 6 ([c_3, c_4, c_6] = [2, 5, 6]). * [5, 2, 5, 3, 6, 6] β€” the subsequence is 2, 3, 5 ([c_2, c_3, c_5] = [2, 5, 6]). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int arr[N]; int par[N]; vector<int> adj[N]; int start[N], ending[N]; int ctr = 0; int tree[4 * N] = {0}; int lazy[4 * N] = {0}; int ans[N]; inline int maxi(int a, int b) { return a > b ? a : b; } void dfs(int node) { start[node] = ctr++; for (int x : adj[node]) { dfs(x); } ending[node] = ctr - 1; } inline void upd(int node, int l, int r) { if (lazy[node] == 0 or l == r) { lazy[node] = 0; return; } tree[2 * node] += lazy[node]; lazy[2 * node] += lazy[node]; tree[2 * node + 1] += lazy[node]; lazy[2 * node + 1] += lazy[node]; lazy[node] = 0; } void update(int node, int l, int r, int left, int right, int val) { upd(node, l, r); if (right < l or r < left) { return; } if (l == r) { tree[node] += val; return; } if (left <= l and r <= right) { tree[node] += val; lazy[node] += val; return; } int m = (l + r) / 2; update(node * 2, l, m, left, right, val); update(2 * node + 1, m + 1, r, left, right, val); tree[node] = maxi(tree[2 * node], tree[2 * node + 1]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> arr[i]; } par[n] = 0; stack<int> s; s.push(n); for (int i = n - 1; i >= 1; i--) { while (!s.empty() and arr[s.top()] <= arr[i]) s.pop(); if (s.empty()) { par[i] = 0; } else par[i] = s.top(); s.push(i); } for (int i = 1; i <= n; i++) { adj[par[i]].push_back(i); } dfs(0); for (int i = 1; i <= k; i++) { update(1, 0, n, start[i], ending[i], 1); } ans[k] = tree[1]; for (int i = k + 1; i <= n; i++) { update(1, 0, n, start[i], ending[i], 1); update(1, 0, n, start[i - k], ending[i - k], -1); ans[i] = tree[1]; } for (int i = k; i <= n; i++) cout << ans[i] << " "; cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n, a = 1, b; string s, t; cin >> n >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (n % 2 != 0 || s[0] != '(' || s[n - 1] != ')') { cout << ":("; return 0; } for (int i = 1; i < n; i++) { if (s[i] == '(') a++; } b = n / 2 - a; a = 1; for (int i = 1; i < n; i++) { if (s[i] == '(') a++; else if (s[i] == ')') a--; else { if (b-- > 0) { s[i] = '('; a++; } else { s[i] = ')'; a--; } } if (a <= 0 && i != n - 1) { a = 1; break; } } if (a == 0) cout << s; else cout << ":("; return 0; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n, a = 1, b; string s, t; cin >> n >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (n % 2 != 0 || s[0] != '(' || s[n - 1] != ')') { cout << ":("; return 0; } for (int i = 1; i < n; i++) { if (s[i] == '(') a++; } b = n / 2 - a; a = 1; for (int i = 1; i < n; i++) { if (s[i] == '(') a++; else if (s[i] == ')') a--; else { if (b-- > 0) { s[i] = '('; a++; } else { s[i] = ')'; a--; } } if (a <= 0 && i != n - 1) { a = 1; break; } } if (a == 0) cout << s; else cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int i; if (n % 2 == 1) { cout << ":(" << endl; return 0; } int a = 0, b = 0, c = 0; int curr = 0; for (i = 0; i < n; i++) if (s[i] == '(') curr++; else if (s[i] == ')') curr--; else c++; if (curr > 0) { b = curr; } else if (curr < 0) { a = abs(curr); } bool flag = true; if (a > c || b > c) flag = false; else { int rest = c - a - b; if (rest % 2 == 1) flag = false; else { b += rest / 2; a += rest / 2; } } if (flag == false) { cout << ":(" << endl; return 0; } for (i = 0; i < n; i++) if (s[i] == '?') if (a) { s[i] = '('; a--; } else s[i] = ')'; curr = 0; for (i = 0; i < n; i++) { if (s[i] == '(') curr++; else curr--; if (i != n - 1 && curr <= 0) { flag = false; break; } else if (i == n - 1 && curr != 0) { flag = false; break; } } if (flag) cout << s; else cout << ":(" << endl; }
### Prompt Please provide a Cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int i; if (n % 2 == 1) { cout << ":(" << endl; return 0; } int a = 0, b = 0, c = 0; int curr = 0; for (i = 0; i < n; i++) if (s[i] == '(') curr++; else if (s[i] == ')') curr--; else c++; if (curr > 0) { b = curr; } else if (curr < 0) { a = abs(curr); } bool flag = true; if (a > c || b > c) flag = false; else { int rest = c - a - b; if (rest % 2 == 1) flag = false; else { b += rest / 2; a += rest / 2; } } if (flag == false) { cout << ":(" << endl; return 0; } for (i = 0; i < n; i++) if (s[i] == '?') if (a) { s[i] = '('; a--; } else s[i] = ')'; curr = 0; for (i = 0; i < n; i++) { if (s[i] == '(') curr++; else curr--; if (i != n - 1 && curr <= 0) { flag = false; break; } else if (i == n - 1 && curr != 0) { flag = false; break; } } if (flag) cout << s; else cout << ":(" << endl; } ```
#include <bits/stdc++.h> using namespace std; long long sigma(long long n) { return (n * (n + 1) / 2); } long long MOD(long long x) { if (x >= 0) return x; return (-x); } bool great(long long a, long long b) { return a > b; } bool p_sm(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) { return (a.second < b.second); } return (a.first < b.first); } long long max3(long long a, long long b, long long c) { return (max(a, max(b, c))); } long long max4(long long a, long long b, long long c, long long d) { return (max(max(a, b), max(c, d))); } long long min3(long long a, long long b, long long c) { return (min(a, min(b, c))); } long long min4(long long a, long long b, long long c, long long d) { return (min(min(a, b), min(c, d))); } vector<long long> factors(long long x) { vector<long long> v; for (long long i = 1; i * i <= x; i++) { if (x % i == 0) { v.push_back(i); if (i != x / i) { v.push_back(x / i); } } } return v; } vector<long long> ispr; vector<long long> min_prime; void strt(long long nn) { min_prime.resize(nn); ispr.resize(nn); for (int i = 0; i < nn; i++) { min_prime[i] = -1; }; for (int i = 0; i < nn; i++) { ispr[i] = true; }; for (long long i = 2; i * i <= nn; i++) { if (!ispr[i]) { continue; } for (long long j = i * i; j <= nn; j += i) { if (min_prime[j] == -1) min_prime[j] = i; ispr[j] = false; } } for (long long i = 2; i < nn; i++) { if (ispr[i]) { min_prime[i] = i; } } } unordered_map<long long, long long> prime_fact(long long x) { unordered_map<long long, long long> mp; while (x != 1) { mp[min_prime[x]]++; x /= min_prime[x]; } return mp; } map<long long, long long> factorizee(long long n) { map<long long, long long> mp; for (long long i = 2; i * i <= n; ++i) { while (n % i == 0) { mp[i]++; n /= i; } } if (n != 1) { mp[n]++; } return mp; } long long power(long long a, long long b, long long m) { long long ans = 1; while (b > 0) { if (b % 2) { ans = (ans * a) % m; } a = (a * a) % m; b = b / 2; } return ans; } int main() { long long n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << endl; exit(0); ; } if (s[0] == ')' || s[n - 1] == '(') { cout << ":(" << endl; exit(0); ; } long long ob, cb; ob = cb = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { ob++; } if (s[i] == ')') { cb++; } } long long cu = 0; long long ob1 = 0; long long lim = n / 2 - ob; if (lim == 0) { if (s[0] == '?') { cout << ":(" << endl; exit(0); ; } } string res; for (long long i = 0; i < n - 1; i++) { if (s[i] == ')') { cu--; res.push_back(s[i]); if (cu == 0) { cout << ":(" << endl; exit(0); ; } } else if (s[i] == '(') { res.push_back(s[i]); cu++; } else { if (ob1 < lim) { res.push_back('('); ob1++; cu++; } else { res.push_back(')'); cu--; if (cu == 0) { cout << ":(" << endl; exit(0); ; } } } } if ((ob1 + ob) != (n / 2)) { cout << ":(" << endl; exit(0); ; } if (res[0] == ')' || res[n - 1] == '(') { cout << ":(" << endl; exit(0); ; } cout << res << ")" << endl; }
### Prompt Please create a solution in CPP to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long sigma(long long n) { return (n * (n + 1) / 2); } long long MOD(long long x) { if (x >= 0) return x; return (-x); } bool great(long long a, long long b) { return a > b; } bool p_sm(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) { return (a.second < b.second); } return (a.first < b.first); } long long max3(long long a, long long b, long long c) { return (max(a, max(b, c))); } long long max4(long long a, long long b, long long c, long long d) { return (max(max(a, b), max(c, d))); } long long min3(long long a, long long b, long long c) { return (min(a, min(b, c))); } long long min4(long long a, long long b, long long c, long long d) { return (min(min(a, b), min(c, d))); } vector<long long> factors(long long x) { vector<long long> v; for (long long i = 1; i * i <= x; i++) { if (x % i == 0) { v.push_back(i); if (i != x / i) { v.push_back(x / i); } } } return v; } vector<long long> ispr; vector<long long> min_prime; void strt(long long nn) { min_prime.resize(nn); ispr.resize(nn); for (int i = 0; i < nn; i++) { min_prime[i] = -1; }; for (int i = 0; i < nn; i++) { ispr[i] = true; }; for (long long i = 2; i * i <= nn; i++) { if (!ispr[i]) { continue; } for (long long j = i * i; j <= nn; j += i) { if (min_prime[j] == -1) min_prime[j] = i; ispr[j] = false; } } for (long long i = 2; i < nn; i++) { if (ispr[i]) { min_prime[i] = i; } } } unordered_map<long long, long long> prime_fact(long long x) { unordered_map<long long, long long> mp; while (x != 1) { mp[min_prime[x]]++; x /= min_prime[x]; } return mp; } map<long long, long long> factorizee(long long n) { map<long long, long long> mp; for (long long i = 2; i * i <= n; ++i) { while (n % i == 0) { mp[i]++; n /= i; } } if (n != 1) { mp[n]++; } return mp; } long long power(long long a, long long b, long long m) { long long ans = 1; while (b > 0) { if (b % 2) { ans = (ans * a) % m; } a = (a * a) % m; b = b / 2; } return ans; } int main() { long long n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << endl; exit(0); ; } if (s[0] == ')' || s[n - 1] == '(') { cout << ":(" << endl; exit(0); ; } long long ob, cb; ob = cb = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') { ob++; } if (s[i] == ')') { cb++; } } long long cu = 0; long long ob1 = 0; long long lim = n / 2 - ob; if (lim == 0) { if (s[0] == '?') { cout << ":(" << endl; exit(0); ; } } string res; for (long long i = 0; i < n - 1; i++) { if (s[i] == ')') { cu--; res.push_back(s[i]); if (cu == 0) { cout << ":(" << endl; exit(0); ; } } else if (s[i] == '(') { res.push_back(s[i]); cu++; } else { if (ob1 < lim) { res.push_back('('); ob1++; cu++; } else { res.push_back(')'); cu--; if (cu == 0) { cout << ":(" << endl; exit(0); ; } } } } if ((ob1 + ob) != (n / 2)) { cout << ":(" << endl; exit(0); ; } if (res[0] == ')' || res[n - 1] == '(') { cout << ":(" << endl; exit(0); ; } cout << res << ")" << endl; } ```
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") #pragma GCC optimize("-fno-defer-pop") long long int power(long long int a, long long int b, long long int m) { if (b == 0) return 1; if (b == 1) return a % m; long long int t = power(a, b / 2, m) % m; t = (t * t) % m; if (b & 1) t = ((t % m) * (a % m)) % m; return t; } long long int modInverse(long long int a, long long int m) { return power(a, m - 2, m); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, l, n; cin >> n; char ar[n + 1]; cin >> ar; if (n % 2 == 1) { cout << ":("; return 0; } k = (n / 2); long long int a = 0, b = 0; for (i = 0; i < n; i++) { if (ar[i] == '(') a++; else if (ar[i] == ')') b++; } a = k - a; b = k - b; for (i = 0; i < n; i++) { if (ar[i] == '?') { if (a > 0) { ar[i] = '('; a--; } else { ar[i] = ')'; b--; } } } int f = 1; stack<char> st; for (i = 0; i < n; i++) { if (ar[i] == '(') { st.push(ar[i]); } else { if (st.size() > 0 && st.top() == '(') { st.pop(); } else { f = 0; break; } } if (st.size() == 0 && i > 0 && i < n - 1) { f = 0; break; } } if (f == 0 || st.size() > 0) { cout << ":("; } else cout << ar; return 0; }
### Prompt In Cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-ffloat-store") #pragma GCC optimize("-fno-defer-pop") long long int power(long long int a, long long int b, long long int m) { if (b == 0) return 1; if (b == 1) return a % m; long long int t = power(a, b / 2, m) % m; t = (t * t) % m; if (b & 1) t = ((t % m) * (a % m)) % m; return t; } long long int modInverse(long long int a, long long int m) { return power(a, m - 2, m); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, l, n; cin >> n; char ar[n + 1]; cin >> ar; if (n % 2 == 1) { cout << ":("; return 0; } k = (n / 2); long long int a = 0, b = 0; for (i = 0; i < n; i++) { if (ar[i] == '(') a++; else if (ar[i] == ')') b++; } a = k - a; b = k - b; for (i = 0; i < n; i++) { if (ar[i] == '?') { if (a > 0) { ar[i] = '('; a--; } else { ar[i] = ')'; b--; } } } int f = 1; stack<char> st; for (i = 0; i < n; i++) { if (ar[i] == '(') { st.push(ar[i]); } else { if (st.size() > 0 && st.top() == '(') { st.pop(); } else { f = 0; break; } } if (st.size() == 0 && i > 0 && i < n - 1) { f = 0; break; } } if (f == 0 || st.size() > 0) { cout << ":("; } else cout << ar; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, num, top; string a; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> a; for (int i = 0; i < n; i++) if (a[i] == ')') num++; for (int i = n - 1; i >= 0; i--) { if (a[i] == '?') { if (num < n / 2) a[i] = ')', num++; else a[i] = '('; } } for (int i = 0; i < n; i++) { if (a[i] == '(') top++; else top--; if (top <= 0 && i != n - 1) { cout << ":(\n"; return 0; } } if (top) { cout << ":(\n"; return 0; } cout << a << "\n"; }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, num, top; string a; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> a; for (int i = 0; i < n; i++) if (a[i] == ')') num++; for (int i = n - 1; i >= 0; i--) { if (a[i] == '?') { if (num < n / 2) a[i] = ')', num++; else a[i] = '('; } } for (int i = 0; i < n; i++) { if (a[i] == '(') top++; else top--; if (top <= 0 && i != n - 1) { cout << ":(\n"; return 0; } } if (top) { cout << ":(\n"; return 0; } cout << a << "\n"; } ```
#include <bits/stdc++.h> using namespace std; int main() { int i, n, p, q, x, flag; string s; cin >> n >> s; if (n & 1) { cout << ":(" << endl; return 0; } x = n / 2; p = 0; for (i = 0; i < n; i += 1) if (s[i] == '(') p += 1; p = x - p; i = 0; while (p > 0 && i < n) { if (s[i] == '?') { s[i] = '('; p -= 1; } i += 1; } for (i = 0; i < n; i += 1) if (s[i] == '?') s[i] = ')'; p = q = 0; flag = 1; for (i = 0; i < n - 1; i += 1) { if (s[i] == '(') p += 1; else q += 1; if (q >= p) { flag = 0; break; } } if (flag == 0) cout << ":(" << endl; else { if (s[i] == '(') p += 1; else q += 1; if (p == q) cout << s << endl; else cout << ":(" << endl; } }
### Prompt Please create a solution in CPP to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int i, n, p, q, x, flag; string s; cin >> n >> s; if (n & 1) { cout << ":(" << endl; return 0; } x = n / 2; p = 0; for (i = 0; i < n; i += 1) if (s[i] == '(') p += 1; p = x - p; i = 0; while (p > 0 && i < n) { if (s[i] == '?') { s[i] = '('; p -= 1; } i += 1; } for (i = 0; i < n; i += 1) if (s[i] == '?') s[i] = ')'; p = q = 0; flag = 1; for (i = 0; i < n - 1; i += 1) { if (s[i] == '(') p += 1; else q += 1; if (q >= p) { flag = 0; break; } } if (flag == 0) cout << ":(" << endl; else { if (s[i] == '(') p += 1; else q += 1; if (p == q) cout << s << endl; else cout << ":(" << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 6; int n, c1, c2, v[N], mn = 1e9; string s; int main() { ios::sync_with_stdio(0); cin >> n; cin >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (s[0] != '(' || s[n - 1] != ')') { puts(":("); return 0; } if (n & 1) { puts(":("); return 0; } if (n == 2) { cout << s << endl; return 0; } for (int i = 1; i < n - 1; i++) { if (s[i] == '(') ++c1; if (s[i] == ')') ++c2; } if (c1 + 1 > n / 2 || c2 + 1 > n / 2) { puts(":("); return 0; } for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (c1 + 1 < (n >> 1)) { ++c1; s[i] = '('; } else { ++c2; s[i] = ')'; } } } int x = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') x++; else { x--; if (x < 0) { puts(":("); return 0; } } } cout << s << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 6; int n, c1, c2, v[N], mn = 1e9; string s; int main() { ios::sync_with_stdio(0); cin >> n; cin >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (s[0] != '(' || s[n - 1] != ')') { puts(":("); return 0; } if (n & 1) { puts(":("); return 0; } if (n == 2) { cout << s << endl; return 0; } for (int i = 1; i < n - 1; i++) { if (s[i] == '(') ++c1; if (s[i] == ')') ++c2; } if (c1 + 1 > n / 2 || c2 + 1 > n / 2) { puts(":("); return 0; } for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (c1 + 1 < (n >> 1)) { ++c1; s[i] = '('; } else { ++c2; s[i] = ')'; } } } int x = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') x++; else { x--; if (x < 0) { puts(":("); return 0; } } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; inline void Boost() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } void NO() { cout << ":(" << endl; exit(0); } int main() { Boost(); int n; cin >> n; string s; cin >> s; if (n % 2 == 1) { NO(); } int op = 0; for (auto x : s) { if (x == '(') { ++op; } } if (op > n / 2) { NO(); } for (int i = 0; i < n && op < n / 2; ++i) { if (s[i] == '?') { s[i] = '('; ++op; } } for (auto &x : s) { if (x == '?') { x = ')'; } } deque<char> DQ; for (int i = 0; i < n; ++i) { if (s[i] == '(') { DQ.push_back('('); } else { if (DQ.empty()) { NO(); } else { DQ.pop_back(); } } if (i != n - 1 && DQ.empty()) { NO(); } } cout << s; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline void Boost() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } void NO() { cout << ":(" << endl; exit(0); } int main() { Boost(); int n; cin >> n; string s; cin >> s; if (n % 2 == 1) { NO(); } int op = 0; for (auto x : s) { if (x == '(') { ++op; } } if (op > n / 2) { NO(); } for (int i = 0; i < n && op < n / 2; ++i) { if (s[i] == '?') { s[i] = '('; ++op; } } for (auto &x : s) { if (x == '?') { x = ')'; } } deque<char> DQ; for (int i = 0; i < n; ++i) { if (s[i] == '(') { DQ.push_back('('); } else { if (DQ.empty()) { NO(); } else { DQ.pop_back(); } } if (i != n - 1 && DQ.empty()) { NO(); } } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, i, x, f = 1, y, req; string s; cin >> n; cin >> s; if ((s[0] == ')') || (n % 2)) { cout << ":("; exit(0); } y = count(s.begin(), s.end(), '('); req = n / 2; if (y > req) { cout << ":("; exit(0); } x = 0; for (i = 0; i < n; i++) { if (s[i] == '(') x++; else if (s[i] == ')') x--; else { if (y < req) { s[i] = '('; x++; y++; } else { s[i] = ')'; x--; } } if (i == (n - 1)) break; if (x == 0) { f = 0; break; } } if (f == 0) { cout << ":("; } else { if (x == 0) cout << s; else cout << ":("; } }
### Prompt Create a solution in cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, i, x, f = 1, y, req; string s; cin >> n; cin >> s; if ((s[0] == ')') || (n % 2)) { cout << ":("; exit(0); } y = count(s.begin(), s.end(), '('); req = n / 2; if (y > req) { cout << ":("; exit(0); } x = 0; for (i = 0; i < n; i++) { if (s[i] == '(') x++; else if (s[i] == ')') x--; else { if (y < req) { s[i] = '('; x++; y++; } else { s[i] = ')'; x--; } } if (i == (n - 1)) break; if (x == 0) { f = 0; break; } } if (f == 0) { cout << ":("; } else { if (x == 0) cout << s; else cout << ":("; } } ```
#include <bits/stdc++.h> using namespace std; void dbg_out() { cerr << "\b\b]\n"; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ", "; dbg_out(T...); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string s; cin >> s; if ((n & 1) or s[0] == ')' or s[n - 1] == '(') { cout << ":("; return 0; } int curr = 0, open = 0, close = 0; for (int i = 0; i < n; ++i) { if (s[i] == ')') close++; else if (s[i] == '(') open++; } if (open > (n / 2) or close > n / 2) { cout << ":("; return 0; } int extra = n / 2 - open; for (int i = 0; i < n; ++i) { if (s[i] == '?') { if (extra > 0) { s[i] = '('; extra--; } else s[i] = ')'; } } for (int i = 0; i < n; ++i) { curr += (s[i] == '(' ? 1 : -1); if (i < n - 1 and curr <= 0) { cout << ":("; return 0; } } cout << (curr != 0 ? ":(" : s); return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; void dbg_out() { cerr << "\b\b]\n"; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ", "; dbg_out(T...); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string s; cin >> s; if ((n & 1) or s[0] == ')' or s[n - 1] == '(') { cout << ":("; return 0; } int curr = 0, open = 0, close = 0; for (int i = 0; i < n; ++i) { if (s[i] == ')') close++; else if (s[i] == '(') open++; } if (open > (n / 2) or close > n / 2) { cout << ":("; return 0; } int extra = n / 2 - open; for (int i = 0; i < n; ++i) { if (s[i] == '?') { if (extra > 0) { s[i] = '('; extra--; } else s[i] = ')'; } } for (int i = 0; i < n; ++i) { curr += (s[i] == '(' ? 1 : -1); if (i < n - 1 and curr <= 0) { cout << ":("; return 0; } } cout << (curr != 0 ? ":(" : s); return 0; } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; string s; cin >> s; if (n & 1) { cout << ":(" << '\n'; return 0; } long long o = n / 2, c = n / 2; for (char ch : s) { if (ch == '(') o--; else if (ch == ')') c--; } long long cur = 0; bool good = true; for (long long i = 0; i < n; i++) { if (s[i] == '(') cur++; else if (s[i] == ')') cur--; else { if (o > 0) { o--; cur++; s[i] = '('; } else { c--; s[i] = ')'; cur--; } } if (i < n - 1 && cur <= 0) { good = false; break; } else if (i == n - 1 && cur < 0) { good = false; break; } } if (cur != 0) good = false; if (good) { cout << s << '\n'; } else cout << ":(" << '\n'; }
### Prompt Your challenge is to write a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; string s; cin >> s; if (n & 1) { cout << ":(" << '\n'; return 0; } long long o = n / 2, c = n / 2; for (char ch : s) { if (ch == '(') o--; else if (ch == ')') c--; } long long cur = 0; bool good = true; for (long long i = 0; i < n; i++) { if (s[i] == '(') cur++; else if (s[i] == ')') cur--; else { if (o > 0) { o--; cur++; s[i] = '('; } else { c--; s[i] = ')'; cur--; } } if (i < n - 1 && cur <= 0) { good = false; break; } else if (i == n - 1 && cur < 0) { good = false; break; } } if (cur != 0) good = false; if (good) { cout << s << '\n'; } else cout << ":(" << '\n'; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; string s; cin >> n >> s; map<char, int> count; for (char c : s) count[c]++; string ans; int lefts = (count['?'] - count['('] + count[')']) / 2; for (char c : s) { if (c == '?') { ans += lefts > 0 ? '(' : ')'; lefts--; } else ans += c; } int acc = 0; for (int i = 0; i < n; i++) { acc += (ans[i] == '(') - (ans[i] == ')'); int ok = 1; if (i == n - 1) { if (acc) ok = 0; } else if (acc <= 0) ok = 0; if (!ok) { cout << ":(" << endl; return 0; } } cout << ans << endl; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; string s; cin >> n >> s; map<char, int> count; for (char c : s) count[c]++; string ans; int lefts = (count['?'] - count['('] + count[')']) / 2; for (char c : s) { if (c == '?') { ans += lefts > 0 ? '(' : ')'; lefts--; } else ans += c; } int acc = 0; for (int i = 0; i < n; i++) { acc += (ans[i] == '(') - (ans[i] == ')'); int ok = 1; if (i == n - 1) { if (acc) ok = 0; } else if (acc <= 0) ok = 0; if (!ok) { cout << ":(" << endl; return 0; } } cout << ans << endl; } ```
#include <bits/stdc++.h> using namespace std; bool is_balanced_and_not_prefix_balanced(string x) { stack<char> s; for (int i = 0; i < x.size(); i++) { if (x[i] == '(') { s.push(x[i]); } else if (x[i] == ')' && !s.empty()) { s.pop(); if (i != x.size() - 1 && s.empty()) return false; } else { return false; } } if (s.empty()) return true; return false; } int main() { int n; while (cin >> n) { string str; cin >> str; int count = n / 2; for (int i = 0; i < str.size(); i++) { if (str[i] == '(') { count--; } } for (int i = 0; i < str.size(); i++) { if (str[i] == '?') { if (count > 0) { str[i] = '('; count--; } else { str[i] = ')'; } } } if (is_balanced_and_not_prefix_balanced(str)) { cout << str << endl; } else { cout << ":(" << endl; } } return 0; }
### Prompt Generate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool is_balanced_and_not_prefix_balanced(string x) { stack<char> s; for (int i = 0; i < x.size(); i++) { if (x[i] == '(') { s.push(x[i]); } else if (x[i] == ')' && !s.empty()) { s.pop(); if (i != x.size() - 1 && s.empty()) return false; } else { return false; } } if (s.empty()) return true; return false; } int main() { int n; while (cin >> n) { string str; cin >> str; int count = n / 2; for (int i = 0; i < str.size(); i++) { if (str[i] == '(') { count--; } } for (int i = 0; i < str.size(); i++) { if (str[i] == '?') { if (count > 0) { str[i] = '('; count--; } else { str[i] = ')'; } } } if (is_balanced_and_not_prefix_balanced(str)) { cout << str << endl; } else { cout << ":(" << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; string s, ans; long long n, d; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> s; if (n % 2) return cout << ":(", 0; long long a = n / 2; for (long long i = 0; i < n; i++) a -= (s[i] == '('); for (long long i = 0; i < n; i++) { if (s[i] == '(') { d++; ans += '('; } else if (s[i] == ')') { d--; ans += ')'; } else { if (a > 0) { ans += '('; d++; a--; } else { ans += ')'; d--; } } if (d < 0 || (d == 0 && i != n - 1)) return cout << ":(", 0; } if (d != 0 || a != 0) return cout << ":(", 0; return cout << ans, 0; }
### Prompt Your task is to create a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s, ans; long long n, d; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> s; if (n % 2) return cout << ":(", 0; long long a = n / 2; for (long long i = 0; i < n; i++) a -= (s[i] == '('); for (long long i = 0; i < n; i++) { if (s[i] == '(') { d++; ans += '('; } else if (s[i] == ')') { d--; ans += ')'; } else { if (a > 0) { ans += '('; d++; a--; } else { ans += ')'; d--; } } if (d < 0 || (d == 0 && i != n - 1)) return cout << ":(", 0; } if (d != 0 || a != 0) return cout << ":(", 0; return cout << ans, 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int main() { int n; scanf("%d", &n); string s; cin >> s; if (n % 2 == 1) { cout << ":("; return 0; } int no_open = 0; int no_clos = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') no_open++; else if (s[i] == ')') no_clos++; } for (int i = 0; i < n; i++) { if (s[i] == '(') ; else if (s[i] == ')') ; else if (no_open < (n / 2)) { no_open++; s[i] = '('; } else { no_clos++; s[i] = ')'; } } stack<char> st; int flag = 0; for (int i = 0; i < n; i++) { if (s[i] == ')') { if (st.empty()) { flag = 1; break; } else st.pop(); if (st.empty() && i != n - 1) { flag = 1; break; } } else { st.push(s[i]); } } if (!st.empty()) { flag = 1; } if (flag == 1) { cout << ":("; } else cout << s; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int main() { int n; scanf("%d", &n); string s; cin >> s; if (n % 2 == 1) { cout << ":("; return 0; } int no_open = 0; int no_clos = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') no_open++; else if (s[i] == ')') no_clos++; } for (int i = 0; i < n; i++) { if (s[i] == '(') ; else if (s[i] == ')') ; else if (no_open < (n / 2)) { no_open++; s[i] = '('; } else { no_clos++; s[i] = ')'; } } stack<char> st; int flag = 0; for (int i = 0; i < n; i++) { if (s[i] == ')') { if (st.empty()) { flag = 1; break; } else st.pop(); if (st.empty() && i != n - 1) { flag = 1; break; } } else { st.push(s[i]); } } if (!st.empty()) { flag = 1; } if (flag == 1) { cout << ":("; } else cout << s; return 0; } ```
#include <bits/stdc++.h> const int N = 3e5 + 5; int n, cnt1, cnt2, cnt; char s[N]; int sum[N], a[N]; int char_to_int(char ch) { if (ch == '(') return -1; if (ch == ')') return 1; return 0; } char int_to_char(int x) { if (x == -1) return '('; if (x == 1) return ')'; return '?'; } int main() { scanf("%d%s", &n, s + 1); if (n % 2 == 1) { printf(":(\n"); return 0; } for (int i = 1; i <= n; i++) if (s[i] == '(') cnt1++; else if (s[i] == ')') cnt2++; for (int i = 1; i <= n; i++) a[i] = char_to_int(s[i]); cnt1 = n / 2 - cnt1; cnt2 = n / 2 - cnt2; if (cnt1 < 0 || cnt2 < 0) { printf(":(\n"); return 0; } for (int i = n; i; i--) { if (a[i] == 0) { if (cnt2) cnt2--, a[i] = 1; else cnt1--, a[i] = -1; } } sum[n] = a[n]; for (int i = n - 1; i; i--) sum[i] = sum[i + 1] + a[i]; for (int i = 1; i <= n; i++) if (sum[i] <= 0) cnt++; if (cnt >= 2) printf(":(\n"); else for (int i = 1; i <= n; i++) printf("%c", int_to_char(a[i])); puts(""); return 0; }
### Prompt Create a solution in cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> const int N = 3e5 + 5; int n, cnt1, cnt2, cnt; char s[N]; int sum[N], a[N]; int char_to_int(char ch) { if (ch == '(') return -1; if (ch == ')') return 1; return 0; } char int_to_char(int x) { if (x == -1) return '('; if (x == 1) return ')'; return '?'; } int main() { scanf("%d%s", &n, s + 1); if (n % 2 == 1) { printf(":(\n"); return 0; } for (int i = 1; i <= n; i++) if (s[i] == '(') cnt1++; else if (s[i] == ')') cnt2++; for (int i = 1; i <= n; i++) a[i] = char_to_int(s[i]); cnt1 = n / 2 - cnt1; cnt2 = n / 2 - cnt2; if (cnt1 < 0 || cnt2 < 0) { printf(":(\n"); return 0; } for (int i = n; i; i--) { if (a[i] == 0) { if (cnt2) cnt2--, a[i] = 1; else cnt1--, a[i] = -1; } } sum[n] = a[n]; for (int i = n - 1; i; i--) sum[i] = sum[i + 1] + a[i]; for (int i = 1; i <= n; i++) if (sum[i] <= 0) cnt++; if (cnt >= 2) printf(":(\n"); else for (int i = 1; i <= n; i++) printf("%c", int_to_char(a[i])); puts(""); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = (long long)(1e6 + 7); const long long INF = (long long)(1e18 + 7); const long long MOD = (long long)(1e9 + 7); signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n; cin >> n; string str; cin >> str; long long op[n + 1], q[n + 1]; op[n] = q[n] = 0; for (long long i = n - 1; i >= 0; i--) { if (str[i] == ')') { q[i] = q[i + 1]; op[i] = op[i + 1] - 1; } else if (str[i] == '(') { q[i] = q[i + 1]; op[i] = op[i + 1] + 1; } else { q[i] = q[i + 1] + 1; op[i] = op[i + 1]; } } if (str[0] == ')' || n % 2 == 1) { cout << ":("; exit(0); } long long cnt = 0; string res = ""; if (str[0] == '?') { cnt++; res += '('; } else { cnt++; res += str[0]; } for (long long i = 1; i < n; i++) { if (str[i] == '(') { cnt++; res += str[i]; } else if (str[i] == ')') { cnt--; res += str[i]; } else { if (cnt < (q[i] - op[i])) { cnt++; res += '('; } else { cnt--; res += ')'; } } if (cnt == 0 && i != n - 1) { cout << ":("; exit(0); } } cout << (cnt == 0 ? res : ":("); }
### Prompt Construct a cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = (long long)(1e6 + 7); const long long INF = (long long)(1e18 + 7); const long long MOD = (long long)(1e9 + 7); signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long n; cin >> n; string str; cin >> str; long long op[n + 1], q[n + 1]; op[n] = q[n] = 0; for (long long i = n - 1; i >= 0; i--) { if (str[i] == ')') { q[i] = q[i + 1]; op[i] = op[i + 1] - 1; } else if (str[i] == '(') { q[i] = q[i + 1]; op[i] = op[i + 1] + 1; } else { q[i] = q[i + 1] + 1; op[i] = op[i + 1]; } } if (str[0] == ')' || n % 2 == 1) { cout << ":("; exit(0); } long long cnt = 0; string res = ""; if (str[0] == '?') { cnt++; res += '('; } else { cnt++; res += str[0]; } for (long long i = 1; i < n; i++) { if (str[i] == '(') { cnt++; res += str[i]; } else if (str[i] == ')') { cnt--; res += str[i]; } else { if (cnt < (q[i] - op[i])) { cnt++; res += '('; } else { cnt--; res += ')'; } } if (cnt == 0 && i != n - 1) { cout << ":("; exit(0); } } cout << (cnt == 0 ? res : ":("); } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n; string s; cin >> n >> s; long long cnt = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') cnt++; } for (long long i = 0; i < n; i++) { if (s[i] == '?') { if (cnt < n / 2) { s[i] = '('; cnt++; } else s[i] = ')'; } } long long ans = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') ans++; else ans--; if (ans < 1 and i < n - 1 or (i == n - 1 and ans != 0)) { cout << ":(" << endl; return 0; } } cout << s << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { long long n; string s; cin >> n >> s; long long cnt = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') cnt++; } for (long long i = 0; i < n; i++) { if (s[i] == '?') { if (cnt < n / 2) { s[i] = '('; cnt++; } else s[i] = ')'; } } long long ans = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') ans++; else ans--; if (ans < 1 and i < n - 1 or (i == n - 1 and ans != 0)) { cout << ":(" << endl; return 0; } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int read() { int x = 0, f = 1; char ch; while (!isdigit(ch = getchar())) (ch == '-') && (f = -f); for (x = ch ^ 48; isdigit(ch = getchar()); x = (x << 3) + (x << 1) + (ch ^ 48)) ; return x * f; } template <class T> T Max(T a, T b) { return a > b ? a : b; } template <class T> T Min(T a, T b) { return a < b ? a : b; } char s[N], ch[N]; int n, cnt, tot; int main() { n = read(); scanf("%s", s + 1); for (int i = 1; i <= n; ++i) ch[i] = s[i]; for (int i = 1; i <= n; ++i) if (ch[i] == ')') cnt++; if (cnt > n / 2) return puts(":("), 0; for (int i = n; i; --i) { if (cnt == n / 2) break; if (ch[i] == '?') ch[i] = ')', cnt++; if (cnt == n / 2) break; } for (int i = 1; i <= n; ++i) if (ch[i] == '?') ch[i] = '('; for (int i = 1; i <= n; ++i) { if (ch[i] == '(') tot++; else tot--; if (tot < 0 || (i != n && tot == 0) || (i == n && tot != 0)) return puts(":("), 0; } printf("%s\n", ch + 1); return 0; }
### Prompt Create a solution in cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int read() { int x = 0, f = 1; char ch; while (!isdigit(ch = getchar())) (ch == '-') && (f = -f); for (x = ch ^ 48; isdigit(ch = getchar()); x = (x << 3) + (x << 1) + (ch ^ 48)) ; return x * f; } template <class T> T Max(T a, T b) { return a > b ? a : b; } template <class T> T Min(T a, T b) { return a < b ? a : b; } char s[N], ch[N]; int n, cnt, tot; int main() { n = read(); scanf("%s", s + 1); for (int i = 1; i <= n; ++i) ch[i] = s[i]; for (int i = 1; i <= n; ++i) if (ch[i] == ')') cnt++; if (cnt > n / 2) return puts(":("), 0; for (int i = n; i; --i) { if (cnt == n / 2) break; if (ch[i] == '?') ch[i] = ')', cnt++; if (cnt == n / 2) break; } for (int i = 1; i <= n; ++i) if (ch[i] == '?') ch[i] = '('; for (int i = 1; i <= n; ++i) { if (ch[i] == '(') tot++; else tot--; if (tot < 0 || (i != n && tot == 0) || (i == n && tot != 0)) return puts(":("), 0; } printf("%s\n", ch + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; int fmin(int a, int b) { if (a < b) { return a; } return b; } int main() { int s1, s2 = 0, i, j, o = 0, c = 0, q = 0, temp = 0, flag = 0; cin >> s1; int B[s1 + 1]; char ch[s1 + 1], ch2[s1 + 1]; cin >> ch; B[0] = 0; for (i = 0; i < s1; i++) { if (ch[i] == '(') { o++; } else if (ch[i] == ')') { c++; } else { q++; } } if (o < c) { i = 0; while (o < c && i < s1) { if (ch[i] == '?') { ch[i] = '('; o++; q--; } i++; } } else if (o > c) { i = s1 - 1; while (o > c && i >= 0) { if (ch[i] == '?') { ch[i] = ')'; c++; q--; } i--; } } if (o != c || q % 2 == 1) { cout << ":(" << endl; } else { for (i = 0, j = 0; i < s1; i++) { if (ch[i] == '?') { if (j < q / 2) { ch[i] = '('; } else { ch[i] = ')'; } j++; } } for (i = 0; i < s1; i++) { B[i + 1] = B[i]; if (ch[i] == '(') { B[i + 1]++; } else { B[i + 1]--; } } for (i = 0; i < s1 - 1; i++) { if (B[i + 1] < 1) { cout << ":(" << endl; return 0; } } if (B[i + 1] != 0) { cout << ":(" << endl; } else { cout << ch << endl; } } return 0; }
### Prompt Please formulate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int fmin(int a, int b) { if (a < b) { return a; } return b; } int main() { int s1, s2 = 0, i, j, o = 0, c = 0, q = 0, temp = 0, flag = 0; cin >> s1; int B[s1 + 1]; char ch[s1 + 1], ch2[s1 + 1]; cin >> ch; B[0] = 0; for (i = 0; i < s1; i++) { if (ch[i] == '(') { o++; } else if (ch[i] == ')') { c++; } else { q++; } } if (o < c) { i = 0; while (o < c && i < s1) { if (ch[i] == '?') { ch[i] = '('; o++; q--; } i++; } } else if (o > c) { i = s1 - 1; while (o > c && i >= 0) { if (ch[i] == '?') { ch[i] = ')'; c++; q--; } i--; } } if (o != c || q % 2 == 1) { cout << ":(" << endl; } else { for (i = 0, j = 0; i < s1; i++) { if (ch[i] == '?') { if (j < q / 2) { ch[i] = '('; } else { ch[i] = ')'; } j++; } } for (i = 0; i < s1; i++) { B[i + 1] = B[i]; if (ch[i] == '(') { B[i + 1]++; } else { B[i + 1]--; } } for (i = 0; i < s1 - 1; i++) { if (B[i + 1] < 1) { cout << ":(" << endl; return 0; } } if (B[i + 1] != 0) { cout << ":(" << endl; } else { cout << ch << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int N; string S; int main() { cin >> N; cin >> S; if (N % 2 == 1) { cout << ":(\n"; return 0; } int cnt = 0; for (auto it : S) if (it == '(') cnt++; int L = N / 2 - cnt; for (int i = 0; i < N; i++) if (S[i] == '?') { if (L != 0) S[i] = '(', L--; else S[i] = ')'; } int t = 0; for (int i = 0; i < N; i++) { if (S[i] == '(') t++; if (S[i] == ')') t--; if (i < N - 1 && t <= 0) { cout << ":(\n"; return 0; } } cout << (t == 0 ? S + "\n" : ":(\n"); return 0; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N; string S; int main() { cin >> N; cin >> S; if (N % 2 == 1) { cout << ":(\n"; return 0; } int cnt = 0; for (auto it : S) if (it == '(') cnt++; int L = N / 2 - cnt; for (int i = 0; i < N; i++) if (S[i] == '?') { if (L != 0) S[i] = '(', L--; else S[i] = ')'; } int t = 0; for (int i = 0; i < N; i++) { if (S[i] == '(') t++; if (S[i] == ')') t--; if (i < N - 1 && t <= 0) { cout << ":(\n"; return 0; } } cout << (t == 0 ? S + "\n" : ":(\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const long long linf = 1LL << 62; const int MAX = 510000; long long dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const double pi = acos(-1); const double eps = 1e-7; template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline void print2(T1 a, T2 b) { cout << a << " " << b << endl; } template <typename T1, typename T2, typename T3> inline void print3(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << endl; } const int mod = 1e9 + 7; void solve() { long long n; string s; cin >> n >> s; if (n & 1 || s[0] == ')' || s[n - 1] == '(') { puts(":("); return; } s[0] = '('; s[n - 1] = ')'; long long cnt = 0; for (long long i = (1); i < (n - 1); i++) if (s[i] == '(') cnt++; long long now = 0; bool flag = true; cnt = n / 2 - 1 - cnt; for (long long i = (1); i < (n - 1); i++) { if (s[i] == '(') { now++; } else if (s[i] == ')') { now--; } else { if (cnt) { now++; s[i] = '('; cnt--; } else { now--; s[i] = ')'; } } if (now < 0) { flag = false; break; } } if (flag && now == 0) cout << s << endl; else puts(":("); } int main() { long long t; t = 1; while (t--) { solve(); } }
### Prompt Generate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const long long linf = 1LL << 62; const int MAX = 510000; long long dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const double pi = acos(-1); const double eps = 1e-7; template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline void print2(T1 a, T2 b) { cout << a << " " << b << endl; } template <typename T1, typename T2, typename T3> inline void print3(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << endl; } const int mod = 1e9 + 7; void solve() { long long n; string s; cin >> n >> s; if (n & 1 || s[0] == ')' || s[n - 1] == '(') { puts(":("); return; } s[0] = '('; s[n - 1] = ')'; long long cnt = 0; for (long long i = (1); i < (n - 1); i++) if (s[i] == '(') cnt++; long long now = 0; bool flag = true; cnt = n / 2 - 1 - cnt; for (long long i = (1); i < (n - 1); i++) { if (s[i] == '(') { now++; } else if (s[i] == ')') { now--; } else { if (cnt) { now++; s[i] = '('; cnt--; } else { now--; s[i] = ')'; } } if (now < 0) { flag = false; break; } } if (flag && now == 0) cout << s << endl; else puts(":("); } int main() { long long t; t = 1; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 300001; int len, prefix[3]; string word; string QQ = {":(\n"}; int main() { cin >> len >> word; if (len & 1) cout << QQ << '\n'; else { int ff = 0; for (int i = 0; i < len; ++i) { ff += (word[i] == '('); } for (int i = 0; i < len; ++i) { if (word[i] == '?') { if (ff < (len >> 1)) { word[i] = '('; ++ff; } else word[i] = ')'; } } ff = 0; for (int i = 0; i < len; ++i) { if (word[i] == '(') ++ff; else { --ff; if (ff <= 0 and i != len - 1) { cout << QQ << '\n'; return 0; } } } if (ff == 0) cout << word << '\n'; else cout << QQ << '\n'; } }
### Prompt Please create a solution in Cpp to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 300001; int len, prefix[3]; string word; string QQ = {":(\n"}; int main() { cin >> len >> word; if (len & 1) cout << QQ << '\n'; else { int ff = 0; for (int i = 0; i < len; ++i) { ff += (word[i] == '('); } for (int i = 0; i < len; ++i) { if (word[i] == '?') { if (ff < (len >> 1)) { word[i] = '('; ++ff; } else word[i] = ')'; } } ff = 0; for (int i = 0; i < len; ++i) { if (word[i] == '(') ++ff; else { --ff; if (ff <= 0 and i != len - 1) { cout << QQ << '\n'; return 0; } } } if (ff == 0) cout << word << '\n'; else cout << QQ << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const int hashP = 239017; const int N = 1e5 + 10; const int MOD = 1e9 + 7; const int MOD2 = 998244353; int a[1111][1111]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int kol = 0; for (auto now : s) { if (now == '?') kol++; } if (s.size() % 2 == 1) { cout << ":("; return 0; } if (s[0] == '?') s[0] = '(', kol--; int kol1 = 0, kol2 = 0, kol11 = 0, kol22 = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') kol11++; if (s[i] == ')') kol22++; } int k = abs(kol11 - kol22), a, b; if ((kol - k) % 2 == 1) { cout << ":("; return 0; } if (kol11 + kol < kol22 || kol22 + kol < kol11) { cout << ":("; return 0; } if (kol11 < kol22) { a = k + (kol - k) / 2; b = (kol - k) / 2; } else { a = (kol - k) / 2; b = k + (kol - k) / 2; } for (int i = 0; i < n; i++) { if (s[i] == '(') kol1++; if (s[i] == ')') kol2++; if (kol2 >= kol1 && i != n - 1) { cout << ":("; return 0; } if (s[i] == '?') { if (a > 0) s[i] = '(', kol1++, a--; else if (b > 0) s[i] = ')', kol2++, b--; } if (kol2 >= kol1 && i != n - 1) { cout << ":("; return 0; } } cout << s; return 0; }
### Prompt In Cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int hashP = 239017; const int N = 1e5 + 10; const int MOD = 1e9 + 7; const int MOD2 = 998244353; int a[1111][1111]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int kol = 0; for (auto now : s) { if (now == '?') kol++; } if (s.size() % 2 == 1) { cout << ":("; return 0; } if (s[0] == '?') s[0] = '(', kol--; int kol1 = 0, kol2 = 0, kol11 = 0, kol22 = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') kol11++; if (s[i] == ')') kol22++; } int k = abs(kol11 - kol22), a, b; if ((kol - k) % 2 == 1) { cout << ":("; return 0; } if (kol11 + kol < kol22 || kol22 + kol < kol11) { cout << ":("; return 0; } if (kol11 < kol22) { a = k + (kol - k) / 2; b = (kol - k) / 2; } else { a = (kol - k) / 2; b = k + (kol - k) / 2; } for (int i = 0; i < n; i++) { if (s[i] == '(') kol1++; if (s[i] == ')') kol2++; if (kol2 >= kol1 && i != n - 1) { cout << ":("; return 0; } if (s[i] == '?') { if (a > 0) s[i] = '(', kol1++, a--; else if (b > 0) s[i] = ')', kol2++, b--; } if (kol2 >= kol1 && i != n - 1) { cout << ":("; return 0; } } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, cnto = 0, cntc = 0, fino = 0, finc = 0; string s; cin >> n; cin >> s; stack<char> st; if (s.size() % 2 != 0) { cout << ":(" << endl; return 0; } for (long long int i = 0; i < n; i++) { if (s[i] == '(') { cnto++; } else if (s[i] == ')') { cntc++; } } long long int x = (n / 2) - cnto; for (long long int i = 0; i < n; i++) { if (x != 0 and s[i] == '?') { s[i] = '('; x--; } } for (long long int i = 0; i < n; i++) { if (s[i] == '?') { s[i] = ')'; } } for (long long int i = 0; i < n; i++) { if (s[i] == '(') { fino++; } else if (s[i] == ')') { finc++; } } if (fino == finc) { st.push(s[0]); for (int i = 1; i < n - 1; i++) { if (s[i] == '(') { st.push(s[i]); } else if (s[i] == ')') { st.pop(); } if (st.size() == 0) { cout << ":(" << endl; return 0; } } if (s[n - 1] == '(') { cout << ":(" << endl; return 0; } else if (s[n - 1] == ')') { st.pop(); if (st.size() == 0) { cout << s << endl; } else { cout << ":(" << endl; return 0; } } } else { cout << ":(" << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, cnto = 0, cntc = 0, fino = 0, finc = 0; string s; cin >> n; cin >> s; stack<char> st; if (s.size() % 2 != 0) { cout << ":(" << endl; return 0; } for (long long int i = 0; i < n; i++) { if (s[i] == '(') { cnto++; } else if (s[i] == ')') { cntc++; } } long long int x = (n / 2) - cnto; for (long long int i = 0; i < n; i++) { if (x != 0 and s[i] == '?') { s[i] = '('; x--; } } for (long long int i = 0; i < n; i++) { if (s[i] == '?') { s[i] = ')'; } } for (long long int i = 0; i < n; i++) { if (s[i] == '(') { fino++; } else if (s[i] == ')') { finc++; } } if (fino == finc) { st.push(s[0]); for (int i = 1; i < n - 1; i++) { if (s[i] == '(') { st.push(s[i]); } else if (s[i] == ')') { st.pop(); } if (st.size() == 0) { cout << ":(" << endl; return 0; } } if (s[n - 1] == '(') { cout << ":(" << endl; return 0; } else if (s[n - 1] == ')') { st.pop(); if (st.size() == 0) { cout << s << endl; } else { cout << ":(" << endl; return 0; } } } else { cout << ":(" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << endl; return 0; } int n1 = 0, n2 = 0, n3 = 0; for (int i = 0; i < n; i++) { if (s[i] == '?') { n3++; } else if (s[i] == '(') { n1++; } else { n2++; } } int cnt = 0, j = 0; int n5 = (n3 - n2 + n1) / 2, n4 = n3 - n5; if (n4 < 0 || n5 < 0) { cout << ":(" << endl; return 0; } while (j < n) { if (s[j] == '(') cnt++; else if (s[j] == ')') cnt--; else { if (n4 > 0) { s[j] = '('; n4--; cnt++; } else { s[j] = ')'; n5--; cnt--; } } if (j != n - 1 && cnt <= 0) { cout << ":(" << endl; return 0; } j++; } cout << s << endl; return 0; }
### Prompt Please create a solution in cpp to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << endl; return 0; } int n1 = 0, n2 = 0, n3 = 0; for (int i = 0; i < n; i++) { if (s[i] == '?') { n3++; } else if (s[i] == '(') { n1++; } else { n2++; } } int cnt = 0, j = 0; int n5 = (n3 - n2 + n1) / 2, n4 = n3 - n5; if (n4 < 0 || n5 < 0) { cout << ":(" << endl; return 0; } while (j < n) { if (s[j] == '(') cnt++; else if (s[j] == ')') cnt--; else { if (n4 > 0) { s[j] = '('; n4--; cnt++; } else { s[j] = ')'; n5--; cnt--; } } if (j != n - 1 && cnt <= 0) { cout << ":(" << endl; return 0; } j++; } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD1 = 1e9 + 7; const int MOD2 = 998244353; const long long INF = 2 * 1e18; const long double PI = 3.14159265358979323846; int main() { long long n, cnt = 0, t = 0; bool x = 0; string s; cin >> n >> s; if (n % 2) { cout << ":("; return 0; } for (long long i = 0; i < n; i++) { if (s[i] == '(') cnt++; else if (s[i] == ')') cnt--; else t++; } long long b = (t - cnt) / 2 + cnt, a = (t - cnt) / 2; if (a < 0 || b < 0) { cout << ":("; return 0; } for (long long i = 0; i < n; i++) { if (s[i] == '?') { if (a > 0) { a--; s[i] = '('; } else { b--; s[i] = ')'; } } } cnt = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') cnt++; else { cnt--; if (i != n - 1) { if (cnt <= 0) x = 1; } else { if (cnt < 0) x = 1; } } } if (x) cout << ":("; else cout << s; }
### Prompt Develop a solution in Cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD1 = 1e9 + 7; const int MOD2 = 998244353; const long long INF = 2 * 1e18; const long double PI = 3.14159265358979323846; int main() { long long n, cnt = 0, t = 0; bool x = 0; string s; cin >> n >> s; if (n % 2) { cout << ":("; return 0; } for (long long i = 0; i < n; i++) { if (s[i] == '(') cnt++; else if (s[i] == ')') cnt--; else t++; } long long b = (t - cnt) / 2 + cnt, a = (t - cnt) / 2; if (a < 0 || b < 0) { cout << ":("; return 0; } for (long long i = 0; i < n; i++) { if (s[i] == '?') { if (a > 0) { a--; s[i] = '('; } else { b--; s[i] = ')'; } } } cnt = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') cnt++; else { cnt--; if (i != n - 1) { if (cnt <= 0) x = 1; } else { if (cnt < 0) x = 1; } } } if (x) cout << ":("; else cout << s; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5; long long mod = 1e9 + 7; long long binpow(long long a, long long n) { long long res = 1; while (n) { if (n & 1) res = (res * a) % mod; n >>= 1; a = (a * a) % mod; } return res; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); int n; string s; cin >> n >> s; if (n % 2 == 1) { cout << ":("; return 0; } int close = 0, open = 0; if (s[0] == '?' || s[0] == '(') s[0] = '('; else { cout << ":("; return 0; } if (s[n - 1] == '?' || s[n - 1] == ')') s[n - 1] = ')'; else { cout << ":("; return 0; } bool win = 1; for (int i = 0; i < n; ++i) { if (s[i] == '(') open++; else if (s[i] == ')') close++; } open = n / 2 - open; close = n / 2 - close; for (int i = 0; i < n; ++i) { if (s[i] == '?') { if (open) { s[i] = '('; open--; } else if (close) { s[i] = ')'; close--; } } } int cnt = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') cnt++; else cnt--; if (i == n - 1 && cnt != 0) { win = 0; } if (cnt <= 0 && i != n - 1) { win = 0; break; } } if (win) cout << s; else cout << ":("; return 0; }
### Prompt Please create a solution in cpp to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2e5; long long mod = 1e9 + 7; long long binpow(long long a, long long n) { long long res = 1; while (n) { if (n & 1) res = (res * a) % mod; n >>= 1; a = (a * a) % mod; } return res; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); int n; string s; cin >> n >> s; if (n % 2 == 1) { cout << ":("; return 0; } int close = 0, open = 0; if (s[0] == '?' || s[0] == '(') s[0] = '('; else { cout << ":("; return 0; } if (s[n - 1] == '?' || s[n - 1] == ')') s[n - 1] = ')'; else { cout << ":("; return 0; } bool win = 1; for (int i = 0; i < n; ++i) { if (s[i] == '(') open++; else if (s[i] == ')') close++; } open = n / 2 - open; close = n / 2 - close; for (int i = 0; i < n; ++i) { if (s[i] == '?') { if (open) { s[i] = '('; open--; } else if (close) { s[i] = ')'; close--; } } } int cnt = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') cnt++; else cnt--; if (i == n - 1 && cnt != 0) { win = 0; } if (cnt <= 0 && i != n - 1) { win = 0; break; } } if (win) cout << s; else cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; char s[300005]; int n; int l = 0, r = 0; int addl = 0; int lmax = 0; int main() { ios::sync_with_stdio(false); scanf("%d", &n); scanf("%s", s + 1); if (n & 1) { cout << ":(" << endl; return 0; } lmax = n / 2; for (int i = 1; i <= n; ++i) if (s[i] == '(') --lmax; for (int i = 1; i <= n; ++i) { if (s[i] == '(') ++l; else if (s[i] == ')') ++r; else { if (addl < lmax) ++l, ++addl, s[i] = '('; else ++r, s[i] = ')'; } if (r > l) { cout << ":(" << endl; return 0; } if (r == l && i < n) { cout << ":(" << endl; return 0; } } if (l > r) { cout << ":(" << endl; return 0; } printf("%s\n", s + 1); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[300005]; int n; int l = 0, r = 0; int addl = 0; int lmax = 0; int main() { ios::sync_with_stdio(false); scanf("%d", &n); scanf("%s", s + 1); if (n & 1) { cout << ":(" << endl; return 0; } lmax = n / 2; for (int i = 1; i <= n; ++i) if (s[i] == '(') --lmax; for (int i = 1; i <= n; ++i) { if (s[i] == '(') ++l; else if (s[i] == ')') ++r; else { if (addl < lmax) ++l, ++addl, s[i] = '('; else ++r, s[i] = ')'; } if (r > l) { cout << ":(" << endl; return 0; } if (r == l && i < n) { cout << ":(" << endl; return 0; } } if (l > r) { cout << ":(" << endl; return 0; } printf("%s\n", s + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 300000 + 77; int n, P[N]; char S[N]; int main() { scanf("%d %s", &n, S + 1); for (int i = n; i > 0; --i) P[i] = ((S[i] != '(') + P[i + 1] - (S[i] == '(')); int t = 0; for (int i = 1; i <= n; ++i) { if (S[i] == '?') { S[i] = ')'; if (P[i + 1] >= t + 1) S[i] = '('; } t += (S[i] == '(') - (S[i] == ')'); } t = 0; for (int i = 1; i <= n; ++i) { t += (S[i] == '(') - (S[i] == ')'); if (t < 0 || (t == 0 && i < n)) return !printf(":(\n"); } if (t != 0) return !printf(":(\n"); for (int i = 1; i <= n; ++i) printf("%c", S[i]); return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 300000 + 77; int n, P[N]; char S[N]; int main() { scanf("%d %s", &n, S + 1); for (int i = n; i > 0; --i) P[i] = ((S[i] != '(') + P[i + 1] - (S[i] == '(')); int t = 0; for (int i = 1; i <= n; ++i) { if (S[i] == '?') { S[i] = ')'; if (P[i + 1] >= t + 1) S[i] = '('; } t += (S[i] == '(') - (S[i] == ')'); } t = 0; for (int i = 1; i <= n; ++i) { t += (S[i] == '(') - (S[i] == ')'); if (t < 0 || (t == 0 && i < n)) return !printf(":(\n"); } if (t != 0) return !printf(":(\n"); for (int i = 1; i <= n; ++i) printf("%c", S[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 200 * 1000 + 9; const long long inf = 1e18; const long long mod = 1e9 + 7; vector<long long> hello(100005, -1); void HakunaMatata() { long long n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << '\n'; return; } string ans = s; long long cnta = 0; long long cntb = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') cnta++; else if (s[i] == ')') cntb++; } long long rema = (n / 2) - cnta; long long remb = (n / 2) - cntb; for (long long i = 0; i < n; i++) { if (rema == 0) break; if (ans[i] == '?') { ans[i] = '('; rema--; } } for (long long i = 0; i < n; i++) { if (remb == 0) break; if (ans[i] == '?') { ans[i] = ')'; remb--; } } long long cnt = 0; for (long long i = 0; i < n; i++) { if (ans[i] == '(') cnt++; if (ans[i] == ')') cnt--; if (cnt < 0) { cout << ":(" << '\n'; return; } if (cnt == 0 and i != (n - 1)) { cout << ":(" << '\n'; return; } } if (cnt > 0) { cout << ":(" << '\n'; return; } cout << ans << '\n'; return; } signed main() { HakunaMatata(); return 0; }
### Prompt Create a solution in CPP for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 200 * 1000 + 9; const long long inf = 1e18; const long long mod = 1e9 + 7; vector<long long> hello(100005, -1); void HakunaMatata() { long long n; cin >> n; string s; cin >> s; if (n % 2) { cout << ":(" << '\n'; return; } string ans = s; long long cnta = 0; long long cntb = 0; for (long long i = 0; i < n; i++) { if (s[i] == '(') cnta++; else if (s[i] == ')') cntb++; } long long rema = (n / 2) - cnta; long long remb = (n / 2) - cntb; for (long long i = 0; i < n; i++) { if (rema == 0) break; if (ans[i] == '?') { ans[i] = '('; rema--; } } for (long long i = 0; i < n; i++) { if (remb == 0) break; if (ans[i] == '?') { ans[i] = ')'; remb--; } } long long cnt = 0; for (long long i = 0; i < n; i++) { if (ans[i] == '(') cnt++; if (ans[i] == ')') cnt--; if (cnt < 0) { cout << ":(" << '\n'; return; } if (cnt == 0 and i != (n - 1)) { cout << ":(" << '\n'; return; } } if (cnt > 0) { cout << ":(" << '\n'; return; } cout << ans << '\n'; return; } signed main() { HakunaMatata(); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } void solve() { int N; string S; cin >> N >> S; if (N % 2 != 0) { cout << ":(" << "\n"; return; } int cnt1 = 0, cnt2 = 0, cnt3 = 0; for (int i = 0; i < N; i++) { if (S[i] == '(') cnt1++; else if (S[i] == ')') cnt2++; else cnt3++; } int num_add1 = N / 2 - cnt1; int num_add2 = N / 2 - cnt2; if (num_add1 < 0 || num_add2 < 0) { cout << ":(" << "\n"; return; } if (S[0] == ')') { cout << ":(" << "\n"; return; } string ans = S; int now = 0; for (int i = 0; i < N; i++) { if (S[i] == '(') now++; else if (S[i] == ')') now--; else { if (num_add1) { num_add1--; ans[i] = '('; now++; } else { ans[i] = ')'; num_add2--; now--; } } if (now == 0 && i != N - 1) { cout << ":(" << "\n"; return; } } if (now != 0 || num_add1 != 0 || num_add2 != 0) { cout << ":(" << "\n"; return; } cout << ans << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); }
### Prompt Create a solution in CPP for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } void solve() { int N; string S; cin >> N >> S; if (N % 2 != 0) { cout << ":(" << "\n"; return; } int cnt1 = 0, cnt2 = 0, cnt3 = 0; for (int i = 0; i < N; i++) { if (S[i] == '(') cnt1++; else if (S[i] == ')') cnt2++; else cnt3++; } int num_add1 = N / 2 - cnt1; int num_add2 = N / 2 - cnt2; if (num_add1 < 0 || num_add2 < 0) { cout << ":(" << "\n"; return; } if (S[0] == ')') { cout << ":(" << "\n"; return; } string ans = S; int now = 0; for (int i = 0; i < N; i++) { if (S[i] == '(') now++; else if (S[i] == ')') now--; else { if (num_add1) { num_add1--; ans[i] = '('; now++; } else { ans[i] = ')'; num_add2--; now--; } } if (now == 0 && i != N - 1) { cout << ":(" << "\n"; return; } } if (now != 0 || num_add1 != 0 || num_add2 != 0) { cout << ":(" << "\n"; return; } cout << ans << "\n"; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); } ```
#include <bits/stdc++.h> using namespace std; int n; string s; int main() { cin >> n >> s; if (n & 1) { cout << ":("; return 0; } if (s[0] == ')') { cout << ":("; return 0; } if (s[n - 1] == '(') { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; int tmp = 0; int open = 0; for (int i = 1; i < n - 1; i++) if (s[i] == '(') open++; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') tmp++; else if (s[i] == ')') tmp--; else { if (open < n / 2 - 1) s[i] = '(', tmp++, open++; else s[i] = ')', tmp--; } if (tmp < 0) break; } if (tmp != 0) cout << ":("; else cout << s; }
### Prompt Please formulate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; string s; int main() { cin >> n >> s; if (n & 1) { cout << ":("; return 0; } if (s[0] == ')') { cout << ":("; return 0; } if (s[n - 1] == '(') { cout << ":("; return 0; } s[0] = '('; s[n - 1] = ')'; int tmp = 0; int open = 0; for (int i = 1; i < n - 1; i++) if (s[i] == '(') open++; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') tmp++; else if (s[i] == ')') tmp--; else { if (open < n / 2 - 1) s[i] = '(', tmp++, open++; else s[i] = ')', tmp--; } if (tmp < 0) break; } if (tmp != 0) cout << ":("; else cout << s; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v); template <typename T> ostream &operator<<(ostream &os, const set<T> &v); template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v); template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v); template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v); int main() { long long int N; string s; cin >> N >> s; long long int Lcur = 0, Lreq, Rcur = 0, Rreq; for (long long int i = 0; i < N; i++) { if (s[i] == ')') Rcur++; else if (s[i] == '(') Lcur++; } if (Lcur > N / 2 || Rcur > N / 2 || s[0] == ')' || s[N - 1] == '(' || N % 2) { cout << ":(" << endl; return 0; } string res; Lreq = N / 2 - Lcur; Rreq = N / 2 - Rcur; bool check = false; long long int count = 0; for (long long int i = 0; i < N; i++) { if (s[i] == '?') { if (Lreq != 0) { count++; res.push_back('('); Lreq--; } else if (Rreq != 0) { count--; res.push_back(')'); Rreq--; } } else { if (s[i] == '(') count++; else if (s[i] == ')') count--; res.push_back(s[i]); } if (count == 0 && i != N - 1) check = true; } if ((Lreq != 0 && Rreq != 0) || check) cout << ":(" << endl; else { cout << res << endl; } } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for (int i = 0; i < v.size(); ++i) { os << v[i]; if (i != v.size() - 1) os << ", "; } os << "]\n"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << "]\n"; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto it : v) os << it.first << " : " << it.second << "\n"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { os << "["; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << "]\n"; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << "("; os << v.first << ", " << v.second << ")"; return os; }
### Prompt Your task is to create a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v); template <typename T> ostream &operator<<(ostream &os, const set<T> &v); template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v); template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v); template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v); int main() { long long int N; string s; cin >> N >> s; long long int Lcur = 0, Lreq, Rcur = 0, Rreq; for (long long int i = 0; i < N; i++) { if (s[i] == ')') Rcur++; else if (s[i] == '(') Lcur++; } if (Lcur > N / 2 || Rcur > N / 2 || s[0] == ')' || s[N - 1] == '(' || N % 2) { cout << ":(" << endl; return 0; } string res; Lreq = N / 2 - Lcur; Rreq = N / 2 - Rcur; bool check = false; long long int count = 0; for (long long int i = 0; i < N; i++) { if (s[i] == '?') { if (Lreq != 0) { count++; res.push_back('('); Lreq--; } else if (Rreq != 0) { count--; res.push_back(')'); Rreq--; } } else { if (s[i] == '(') count++; else if (s[i] == ')') count--; res.push_back(s[i]); } if (count == 0 && i != N - 1) check = true; } if ((Lreq != 0 && Rreq != 0) || check) cout << ":(" << endl; else { cout << res << endl; } } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for (int i = 0; i < v.size(); ++i) { os << v[i]; if (i != v.size() - 1) os << ", "; } os << "]\n"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << "]\n"; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto it : v) os << it.first << " : " << it.second << "\n"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { os << "["; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << "]\n"; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << "("; os << v.first << ", " << v.second << ")"; return os; } ```
#include <bits/stdc++.h> using namespace std; char s[300000 + 5]; int main() { int n; scanf("%d", &n); scanf("%s", s); if (n % 2) { printf(":(\n"); return 0; } int ans = n / 2; for (int i = 0; i < n; i++) if (s[i] == '(') ans--; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (ans > 0) s[i] = '(', ans--; else s[i] = ')'; } } bool flag = true; stack<int> st; for (int i = 0; i < n && flag; i++) { if (s[i] == '(') st.push(i); else { if (st.empty()) flag = false; else if (st.top() == 0 && i != n - 1) flag = false; else st.pop(); } } if (!st.empty()) flag = false; if (flag) printf("%s\n", s); else printf(":(\n"); return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[300000 + 5]; int main() { int n; scanf("%d", &n); scanf("%s", s); if (n % 2) { printf(":(\n"); return 0; } int ans = n / 2; for (int i = 0; i < n; i++) if (s[i] == '(') ans--; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (ans > 0) s[i] = '(', ans--; else s[i] = ')'; } } bool flag = true; stack<int> st; for (int i = 0; i < n && flag; i++) { if (s[i] == '(') st.push(i); else { if (st.empty()) flag = false; else if (st.top() == 0 && i != n - 1) flag = false; else st.pop(); } } if (!st.empty()) flag = false; if (flag) printf("%s\n", s); else printf(":(\n"); return 0; } ```
#include <bits/stdc++.h> const double esp = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int inf = 1e9; using namespace std; char s[300005]; int n; void unhy() { cout << ":(" << endl; return; } int main() { cin >> n; int lf = 0, rt = 0; int qu = 0; if (n & 1) { unhy(); return 0; } for (int i = 0; i < n; i++) { cin >> s[i]; if (s[i] == '(') lf++; else if (s[i] == ')') rt++; } if (lf > n / 2 || rt > n / 2) { unhy(); return 0; } int tlf = n / 2 - lf; int trt = n / 2 - rt; for (int i = 0; i < n; i++) { if (s[i] == '?') { s[i] = '('; tlf--; } if (tlf == 0) break; } for (int i = 0; i < n; i++) { if (s[i] == '?') s[i] = ')'; } bool flag = false; int temp = 0; int st = 0; for (int i = 0; i < n; i++) { if (st == 0) { if (s[i] == '(') temp++; else temp--; st++; } else { if (temp <= 0) { flag = true; break; } if (s[i] == '(') temp++; else temp--; } } if (temp != 0 || flag) unhy(); else { cout << s << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> const double esp = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int inf = 1e9; using namespace std; char s[300005]; int n; void unhy() { cout << ":(" << endl; return; } int main() { cin >> n; int lf = 0, rt = 0; int qu = 0; if (n & 1) { unhy(); return 0; } for (int i = 0; i < n; i++) { cin >> s[i]; if (s[i] == '(') lf++; else if (s[i] == ')') rt++; } if (lf > n / 2 || rt > n / 2) { unhy(); return 0; } int tlf = n / 2 - lf; int trt = n / 2 - rt; for (int i = 0; i < n; i++) { if (s[i] == '?') { s[i] = '('; tlf--; } if (tlf == 0) break; } for (int i = 0; i < n; i++) { if (s[i] == '?') s[i] = ')'; } bool flag = false; int temp = 0; int st = 0; for (int i = 0; i < n; i++) { if (st == 0) { if (s[i] == '(') temp++; else temp--; st++; } else { if (temp <= 0) { flag = true; break; } if (s[i] == '(') temp++; else temp--; } } if (temp != 0 || flag) unhy(); else { cout << s << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; const int SZ = 1e6 + 10; const int mod = 1e9 + 7; const double PI = acos(-1); const double eps = 1e-7; long long read() { long long n = 0; char a = getchar(); bool flag = 0; while (a > '9' || a < '0') { if (a == '-') flag = 1; a = getchar(); } while (a <= '9' && a >= '0') { n = n * 10 + a - '0', a = getchar(); } if (flag) n = -n; return n; } char s[SZ], ans[SZ]; int n; bool check() { if (n & 1) return false; if (s[1] != '?' && s[1] == ')') return false; if (s[n] != '?' && s[n] == '(') return false; ans[1] = '('; ans[n] = ')'; int d = 0; for (int i = 2; i <= n - 1; i++) { if (s[i] == ')') ans[i] = ')', d--; else ans[i] = '(', d++; } for (int i = n - 1; i >= 1; i--) { if (s[i] == '?' && d > 0) { ans[i] = ')'; d -= 2; if (d == 0) break; } } d = 0; for (int i = 1; i <= n; i++) { if (ans[i] == '(') d++; else d--; if (i < n && d == 0) return false; } if (d == 0) return true; return false; } int main() { n = read(); scanf("%s", s + 1); if (check()) printf("%s\n", ans + 1); else puts(":("); }
### Prompt Your challenge is to write a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; const int SZ = 1e6 + 10; const int mod = 1e9 + 7; const double PI = acos(-1); const double eps = 1e-7; long long read() { long long n = 0; char a = getchar(); bool flag = 0; while (a > '9' || a < '0') { if (a == '-') flag = 1; a = getchar(); } while (a <= '9' && a >= '0') { n = n * 10 + a - '0', a = getchar(); } if (flag) n = -n; return n; } char s[SZ], ans[SZ]; int n; bool check() { if (n & 1) return false; if (s[1] != '?' && s[1] == ')') return false; if (s[n] != '?' && s[n] == '(') return false; ans[1] = '('; ans[n] = ')'; int d = 0; for (int i = 2; i <= n - 1; i++) { if (s[i] == ')') ans[i] = ')', d--; else ans[i] = '(', d++; } for (int i = n - 1; i >= 1; i--) { if (s[i] == '?' && d > 0) { ans[i] = ')'; d -= 2; if (d == 0) break; } } d = 0; for (int i = 1; i <= n; i++) { if (ans[i] == '(') d++; else d--; if (i < n && d == 0) return false; } if (d == 0) return true; return false; } int main() { n = read(); scanf("%s", s + 1); if (check()) printf("%s\n", ans + 1); else puts(":("); } ```
#include <bits/stdc++.h> using namespace std; bool check(string s) { stack<char> st; for (int index = 0; index < s.size(); index++) { if (index != 0 && st.empty()) return false; if (s[index] == '(') { st.push('('); } else { if (st.empty()) { return false; } else { st.pop(); } } } bool f = (st.empty()); return f; } int main() { int n; cin >> n; string s; cin >> s; int open = 0, close = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') open++; if (s[i] == ')') close++; } if (n % 2 != 0) { cout << ":("; return 0; } int a = n / 2 - open; int b = n / 2 - close; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (a) { s[i] = '('; a--; } else { s[i] = ')'; b--; } } } cout << (check(s) ? s : ":("); }
### Prompt Please formulate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool check(string s) { stack<char> st; for (int index = 0; index < s.size(); index++) { if (index != 0 && st.empty()) return false; if (s[index] == '(') { st.push('('); } else { if (st.empty()) { return false; } else { st.pop(); } } } bool f = (st.empty()); return f; } int main() { int n; cin >> n; string s; cin >> s; int open = 0, close = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') open++; if (s[i] == ')') close++; } if (n % 2 != 0) { cout << ":("; return 0; } int a = n / 2 - open; int b = n / 2 - close; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (a) { s[i] = '('; a--; } else { s[i] = ')'; b--; } } } cout << (check(s) ? s : ":("); } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; if (n % 2 == 1) { cout << ":("; return 0; } string a; cin >> a; if (a[0] == ')' || a[n - 1] == '(' || a[1] == ')' || a[n - 2] == '(') { cout << ":("; return 0; } long long int s = 0, d = 0; a[0] = '('; a[n - 1] = ')'; long long int sum = 0; for (long long int i = 0; i < n; i++) { if (a[i] == '(') { sum++; } } if (sum > n / 2) { cout << ":("; return 0; } long long int p = n / 2 - sum; sum = 1; long long int br = 0; for (long long int i = 1; i < n; i++) { if (sum <= 0) { cout << ":("; return 0; } if (a[i] == ')') { sum--; } else if (a[i] == '?' && p > 0) { a[i] = '('; sum++; p--; } else if (a[i] == '?' && p <= 0) { a[i] = ')'; sum--; } else { sum++; } } cout << a; }
### Prompt Please provide a CPP coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; if (n % 2 == 1) { cout << ":("; return 0; } string a; cin >> a; if (a[0] == ')' || a[n - 1] == '(' || a[1] == ')' || a[n - 2] == '(') { cout << ":("; return 0; } long long int s = 0, d = 0; a[0] = '('; a[n - 1] = ')'; long long int sum = 0; for (long long int i = 0; i < n; i++) { if (a[i] == '(') { sum++; } } if (sum > n / 2) { cout << ":("; return 0; } long long int p = n / 2 - sum; sum = 1; long long int br = 0; for (long long int i = 1; i < n; i++) { if (sum <= 0) { cout << ":("; return 0; } if (a[i] == ')') { sum--; } else if (a[i] == '?' && p > 0) { a[i] = '('; sum++; p--; } else if (a[i] == '?' && p <= 0) { a[i] = ')'; sum--; } else { sum++; } } cout << a; } ```
#include <bits/stdc++.h> using namespace std; bool sortbysec(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) { return (a.second < b.second); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int64_t n, a = 0, b = 0; string s; cin >> n >> s; if (n % 2) return cout << ":(", 0; for (auto c : s) a += (c == '('); a = n / 2 - a; for (auto& c : s) { if (c == '?') { if (a) c = '(', a--; else c = ')'; } } a = 0; for (int64_t i = 0; i < n - 1; i++) { a += (s[i] == '(' ? 1 : -1); if (a <= 0) return cout << ":(", 0; } a += (s.back() == '(' ? 1 : -1); if (a) return cout << ":(", 0; cout << s; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool sortbysec(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) { return (a.second < b.second); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int64_t n, a = 0, b = 0; string s; cin >> n >> s; if (n % 2) return cout << ":(", 0; for (auto c : s) a += (c == '('); a = n / 2 - a; for (auto& c : s) { if (c == '?') { if (a) c = '(', a--; else c = ')'; } } a = 0; for (int64_t i = 0; i < n - 1; i++) { a += (s[i] == '(' ? 1 : -1); if (a <= 0) return cout << ":(", 0; } a += (s.back() == '(' ? 1 : -1); if (a) return cout << ":(", 0; cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; bool valid(string s) { stack<char> st; bool valid = true; for (int i = 0; i < s.length() && valid; i++) { if (i > 0 && st.empty()) valid = false; if (s[i] == '(') st.push('('); else if (st.empty()) valid = false; else st.pop(); } if (!st.empty()) valid = false; return valid; } int main() { int n, a = 0, b = 0, s = 0, q = 0; string str; cin >> n; cin >> str; for (int i = 0; i < n; i++) { if (str[i] == '(') s++; else if (str[i] == ')') s--; else q++; } q -= abs(s); if (s < 0) a = abs(s); else b = abs(s); if (q % 2 == 1) cout << ":(" << endl; else { a += q / 2; b += q / 2; for (int i = 0; i < n; i++) { if (str[i] == '?') { if (a > 0) { str[i] = '('; a--; } else { str[i] = ')'; b--; } } } if (valid(str)) cout << str << endl; else cout << ":(" << endl; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool valid(string s) { stack<char> st; bool valid = true; for (int i = 0; i < s.length() && valid; i++) { if (i > 0 && st.empty()) valid = false; if (s[i] == '(') st.push('('); else if (st.empty()) valid = false; else st.pop(); } if (!st.empty()) valid = false; return valid; } int main() { int n, a = 0, b = 0, s = 0, q = 0; string str; cin >> n; cin >> str; for (int i = 0; i < n; i++) { if (str[i] == '(') s++; else if (str[i] == ')') s--; else q++; } q -= abs(s); if (s < 0) a = abs(s); else b = abs(s); if (q % 2 == 1) cout << ":(" << endl; else { a += q / 2; b += q / 2; for (int i = 0; i < n; i++) { if (str[i] == '?') { if (a > 0) { str[i] = '('; a--; } else { str[i] = ')'; b--; } } } if (valid(str)) cout << str << endl; else cout << ":(" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; char s[N]; int main(void) { int n; while (cin >> n) { int cntl = 0; int cntr = 0; int cntq = 0; getchar(); n = abs(n); scanf("%s", s); if (s[0] == ')' || s[n - 1] == '(') { puts(":("); continue; } if (n == 0) { puts(":("); continue; } if (n & 1) { puts(":("); continue; } for (int i = 0; i < n; i++) { if (s[i] == ')') cntr++; else if (s[i] == '(') cntl++; else cntq++; } if (abs(cntl - cntr) > cntq) { puts(":("); continue; } int needl = n / 2 - cntl; for (int i = 0; i < n; i++) { if (needl) { if (s[i] == '?') { s[i] = '('; needl--; } } else { if (s[i] == '?') s[i] = ')'; } } bool flg = false; cntl = 0; cntr = 0; for (int i = 0; i < n; i++) { if (s[i] == ')') cntr++; else cntl++; if ((i + 1) % 2 == 0) { if (cntr == cntl && i + 1 != n) { flg = true; break; } } } if (flg == false) { printf("%s\n", s); } else puts(":("); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; char s[N]; int main(void) { int n; while (cin >> n) { int cntl = 0; int cntr = 0; int cntq = 0; getchar(); n = abs(n); scanf("%s", s); if (s[0] == ')' || s[n - 1] == '(') { puts(":("); continue; } if (n == 0) { puts(":("); continue; } if (n & 1) { puts(":("); continue; } for (int i = 0; i < n; i++) { if (s[i] == ')') cntr++; else if (s[i] == '(') cntl++; else cntq++; } if (abs(cntl - cntr) > cntq) { puts(":("); continue; } int needl = n / 2 - cntl; for (int i = 0; i < n; i++) { if (needl) { if (s[i] == '?') { s[i] = '('; needl--; } } else { if (s[i] == '?') s[i] = ')'; } } bool flg = false; cntl = 0; cntr = 0; for (int i = 0; i < n; i++) { if (s[i] == ')') cntr++; else cntl++; if ((i + 1) % 2 == 0) { if (cntr == cntl && i + 1 != n) { flg = true; break; } } } if (flg == false) { printf("%s\n", s); } else puts(":("); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 100010; string s; int n; bool check() { int dif = 0; for (int i = 1; i < s.length() - 1; i++) { if (s[i] == '(') dif++; else if (s[i] == ')') dif--; if (dif < 0) return 0; } if (dif == 0) return 1; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> s; int op = 0, cl = 0; bool ok = true; if (n & 1) ok = false; else { if (s[0] == ')' || s[n - 1] == '(') ok = false; else { int op = 0, cl = 0, q = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') op++; else if (s[i] == ')') cl++; else q++; } int l = 0, r = 0; q -= abs(op - cl); if (op > cl) r = op - cl; else l = cl - op; l += q / 2; r += q / 2; for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (l > 0) s[i] = '(', l--; else s[i] = ')', r--; } } } if (ok) ok = check(); s[0] = '(', s[n - 1] = ')'; } if (ok == false) cout << ":(" << endl; else cout << s << endl; }
### Prompt Your challenge is to write a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 100010; string s; int n; bool check() { int dif = 0; for (int i = 1; i < s.length() - 1; i++) { if (s[i] == '(') dif++; else if (s[i] == ')') dif--; if (dif < 0) return 0; } if (dif == 0) return 1; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> s; int op = 0, cl = 0; bool ok = true; if (n & 1) ok = false; else { if (s[0] == ')' || s[n - 1] == '(') ok = false; else { int op = 0, cl = 0, q = 0; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') op++; else if (s[i] == ')') cl++; else q++; } int l = 0, r = 0; q -= abs(op - cl); if (op > cl) r = op - cl; else l = cl - op; l += q / 2; r += q / 2; for (int i = 1; i < n - 1; i++) { if (s[i] == '?') { if (l > 0) s[i] = '(', l--; else s[i] = ')', r--; } } } if (ok) ok = check(); s[0] = '(', s[n - 1] = ')'; } if (ok == false) cout << ":(" << endl; else cout << s << endl; } ```
#include <bits/stdc++.h> using namespace std; int b[300005] = {0}; stack<char> ch; int main() { int n; cin >> n; char a[300005], ans[300005]; cin >> a; int l = 0, r = 0; for (int i = 0; i <= n - 1; i++) if (a[i] == '(') l++; else if (a[i] == ')') r++; l = n / 2 - l; r = n / 2 - r; for (int i = 0; i <= n - 1; i++) { if (a[i] == '?') { if (l) { a[i] = '('; l--; } else { while (a[i]) { if (a[i] == '?') a[i] = ')'; i++; } break; } } } ch.push(a[0]); int i = 1; int ok = 1; while (a[i]) { if (a[i] == '(') ch.push(a[i]); else { if (ch.top() == '(') { ch.pop(); if (ch.empty() && a[i + 1]) { ok = 0; break; } } else { ok = 0; break; } } i++; } if (!ch.empty()) ok = 0; if (ok) cout << a; else cout << ":("; }
### Prompt Develop a solution in CPP to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int b[300005] = {0}; stack<char> ch; int main() { int n; cin >> n; char a[300005], ans[300005]; cin >> a; int l = 0, r = 0; for (int i = 0; i <= n - 1; i++) if (a[i] == '(') l++; else if (a[i] == ')') r++; l = n / 2 - l; r = n / 2 - r; for (int i = 0; i <= n - 1; i++) { if (a[i] == '?') { if (l) { a[i] = '('; l--; } else { while (a[i]) { if (a[i] == '?') a[i] = ')'; i++; } break; } } } ch.push(a[0]); int i = 1; int ok = 1; while (a[i]) { if (a[i] == '(') ch.push(a[i]); else { if (ch.top() == '(') { ch.pop(); if (ch.empty() && a[i + 1]) { ok = 0; break; } } else { ok = 0; break; } } i++; } if (!ch.empty()) ok = 0; if (ok) cout << a; else cout << ":("; } ```
#include <bits/stdc++.h> using namespace std; ; int main() { long long n; cin >> n; string s; cin >> s; long long open = 0, close = 0; for (long long i = 0; i < n; i++) if (s[i] == '(') open++; for (long long i = 0; i < n; i++) { if (s[i] == '?') { if (open < (n / 2)) { s[i] = '('; open++; } else { s[i] = ')'; } } } open = 0, close = 0; for (long long i = 0; i < n - 1; i++) { if (s[i] == '(') open++; else close++; long long temp = open - close; if (temp <= 0) { cout << ":("; return 0; } } if (s[n - 1] == ')') close++; else open++; if (open != close) { cout << ":("; return 0; } cout << s; return 0; }
### Prompt Develop a solution in cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; ; int main() { long long n; cin >> n; string s; cin >> s; long long open = 0, close = 0; for (long long i = 0; i < n; i++) if (s[i] == '(') open++; for (long long i = 0; i < n; i++) { if (s[i] == '?') { if (open < (n / 2)) { s[i] = '('; open++; } else { s[i] = ')'; } } } open = 0, close = 0; for (long long i = 0; i < n - 1; i++) { if (s[i] == '(') open++; else close++; long long temp = open - close; if (temp <= 0) { cout << ":("; return 0; } } if (s[n - 1] == ')') close++; else open++; if (open != close) { cout << ":("; return 0; } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, k; string s; long long ans; bool flag; void solve() { int n; cin >> n; string s; cin >> s; int r = 0; for (auto i = 0; i < n; i++) { if (s[i] == '(') r++; } int h = n / 2; if (r > h || n % 2 != 0) { cout << ":("; return; } for (auto i = 0; i < n; i++) { if (s[i] == '?') { if (r < h) { r++; s[i] = '('; } else s[i] = ')'; } } int stack = 0; for (auto i = 0; i < n - 1; i++) { if (s[i] == '(') stack++; else if (s[i] == ')') stack--; if (stack <= 0) { cout << ":("; return; } } if (s[n - 1] == ')') stack--; if (stack != 0) { cout << ":("; return; } cout << s; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, k; string s; long long ans; bool flag; void solve() { int n; cin >> n; string s; cin >> s; int r = 0; for (auto i = 0; i < n; i++) { if (s[i] == '(') r++; } int h = n / 2; if (r > h || n % 2 != 0) { cout << ":("; return; } for (auto i = 0; i < n; i++) { if (s[i] == '?') { if (r < h) { r++; s[i] = '('; } else s[i] = ')'; } } int stack = 0; for (auto i = 0; i < n - 1; i++) { if (s[i] == '(') stack++; else if (s[i] == ')') stack--; if (stack <= 0) { cout << ":("; return; } } if (s[n - 1] == ')') stack--; if (stack != 0) { cout << ":("; return; } cout << s; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, ans, ans2, z, x; string s; int main() { ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); cin >> n >> s; if (n % 2) { cout << ":(\n"; return 0; } else { for (int i = 1; i < n - 1; i++) { if (s[i] == '(') ans++; else if (s[i] == ')') ans--; if (ans < 0) { for (int j = i; j >= 1; j--) { if (s[j] == '?') { s[j] = '('; ans++; } if (!ans) break; } if (ans < 0) { cout << ":("; return 0; } } } if (ans > 0) { for (int i = n - 2; i >= 1; i--) { if (s[i] == '?') s[i] = ')', ans--; if (!ans) break; } } for (int i = 1; i < n - 1; i++) { if (z % 2 == 0 && s[i] == '?') { s[i] = '('; z++; } else if (z % 2 == 1 && s[i] == '?') { s[i] = ')'; z++; } } } if (s[0] == ')' || s[n - 1] == '(') { cout << ":(\n"; return 0; } s[0] = '('; s[n - 1] = ')'; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') { x++; } else { x--; } if (x < 0) { cout << ":(\n"; return 0; } } if (x != 0) { cout << ":(\n"; return 0; } else { cout << s << endl; } }
### Prompt Develop a solution in Cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, ans, ans2, z, x; string s; int main() { ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL); cin >> n >> s; if (n % 2) { cout << ":(\n"; return 0; } else { for (int i = 1; i < n - 1; i++) { if (s[i] == '(') ans++; else if (s[i] == ')') ans--; if (ans < 0) { for (int j = i; j >= 1; j--) { if (s[j] == '?') { s[j] = '('; ans++; } if (!ans) break; } if (ans < 0) { cout << ":("; return 0; } } } if (ans > 0) { for (int i = n - 2; i >= 1; i--) { if (s[i] == '?') s[i] = ')', ans--; if (!ans) break; } } for (int i = 1; i < n - 1; i++) { if (z % 2 == 0 && s[i] == '?') { s[i] = '('; z++; } else if (z % 2 == 1 && s[i] == '?') { s[i] = ')'; z++; } } } if (s[0] == ')' || s[n - 1] == '(') { cout << ":(\n"; return 0; } s[0] = '('; s[n - 1] = ')'; for (int i = 1; i < n - 1; i++) { if (s[i] == '(') { x++; } else { x--; } if (x < 0) { cout << ":(\n"; return 0; } } if (x != 0) { cout << ":(\n"; return 0; } else { cout << s << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; int i, j, n; cin >> n; cin >> s; j = 0; for (i = 0; i < n; i++) if (s[i] == '(') j++; if (n % 2 == 1) { cout << ":("; return 0; } j = n / 2 - j; int k = 0; for (i = 0; i < n; i++) { if (s[i] == '?') { if (j) { j--; s[i] = '('; } else s[i] = ')'; } if (s[i] == '(') k++; else k--; if (k <= 0 && i != n - 1) { cout << ":("; return 0; } } if (k != 0) { cout << ":("; return 0; } cout << s; return 0; }
### Prompt Develop a solution in cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; int i, j, n; cin >> n; cin >> s; j = 0; for (i = 0; i < n; i++) if (s[i] == '(') j++; if (n % 2 == 1) { cout << ":("; return 0; } j = n / 2 - j; int k = 0; for (i = 0; i < n; i++) { if (s[i] == '?') { if (j) { j--; s[i] = '('; } else s[i] = ')'; } if (s[i] == '(') k++; else k--; if (k <= 0 && i != n - 1) { cout << ":("; return 0; } } if (k != 0) { cout << ":("; return 0; } cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; string str; int main(int argc, char const *argv[]) { cin >> n; getline(cin, str); getline(cin, str); if (str.size() % 2 == 1) { cout << ":(" << endl; return 0; } int remOpen = n / 2; int remClose = n / 2; vector<int> sign; for (auto it = str.begin(); it != str.end(); it++) { if (*it == '(') { remOpen--; } else if (*it == ')') { remClose--; } } int diff = 0; string s; for (auto it = str.begin(); it != str.end(); it++) { if (*it == '?') { if (remOpen > 0) { s.push_back('('); remOpen--; diff += 1; if (diff <= 0 && s.size() != n) { cout << ":(" << endl; return 0; } } else if (remClose > 0) { remClose--; s.push_back(')'); diff -= 1; if (diff <= 0 && s.size() != n) { cout << ":(" << endl; return 0; } } } else { diff += (*it == '(') ? 1 : -1; if (diff <= 0 && s.size() != n - 1) { cout << ":(" << endl; return 0; } s.push_back(*it); } } if (diff != 0) { cout << ":(" << endl; return 0; } cout << s << endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; string str; int main(int argc, char const *argv[]) { cin >> n; getline(cin, str); getline(cin, str); if (str.size() % 2 == 1) { cout << ":(" << endl; return 0; } int remOpen = n / 2; int remClose = n / 2; vector<int> sign; for (auto it = str.begin(); it != str.end(); it++) { if (*it == '(') { remOpen--; } else if (*it == ')') { remClose--; } } int diff = 0; string s; for (auto it = str.begin(); it != str.end(); it++) { if (*it == '?') { if (remOpen > 0) { s.push_back('('); remOpen--; diff += 1; if (diff <= 0 && s.size() != n) { cout << ":(" << endl; return 0; } } else if (remClose > 0) { remClose--; s.push_back(')'); diff -= 1; if (diff <= 0 && s.size() != n) { cout << ":(" << endl; return 0; } } } else { diff += (*it == '(') ? 1 : -1; if (diff <= 0 && s.size() != n - 1) { cout << ":(" << endl; return 0; } s.push_back(*it); } } if (diff != 0) { cout << ":(" << endl; return 0; } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 10; const long long inf = 1e9; int SUM; int n; char s[N]; int main() { scanf("%d", &n); scanf("%s", s); bool f = 1; int cnt = 0; int l = 0; for (int i = 0; i < n; ++i) { if (s[i] == '?') continue; cnt++; if (s[i] == '(') l++; } int res = n - cnt; int key = l - (cnt - l); if ((res + key) % 2) f = 0; int x = (res - key) / 2; int y = (res + key) / 2; if (x < 0 || y < 0) f = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') { SUM++; } else if (s[i] == ')') { SUM--; } else { if (x) s[i] = '(', x--, SUM++; else s[i] = ')', y--, SUM--; } if (SUM < 0) f = 0; if (i < n - 1 && SUM == 0) f = 0; } if (SUM) f = 0; if (f == 0) puts(":("); else puts(s); return 0; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 10; const long long inf = 1e9; int SUM; int n; char s[N]; int main() { scanf("%d", &n); scanf("%s", s); bool f = 1; int cnt = 0; int l = 0; for (int i = 0; i < n; ++i) { if (s[i] == '?') continue; cnt++; if (s[i] == '(') l++; } int res = n - cnt; int key = l - (cnt - l); if ((res + key) % 2) f = 0; int x = (res - key) / 2; int y = (res + key) / 2; if (x < 0 || y < 0) f = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') { SUM++; } else if (s[i] == ')') { SUM--; } else { if (x) s[i] = '(', x--, SUM++; else s[i] = ')', y--, SUM--; } if (SUM < 0) f = 0; if (i < n - 1 && SUM == 0) f = 0; } if (SUM) f = 0; if (f == 0) puts(":("); else puts(s); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, q, sum; string s; void solve() { cin >> n >> s; for (auto i : s) { if (i == '?') q++; else sum += (i == '(') - (i == ')'); } if ((q - sum) % 2 or q - sum < 0) { cout << ":("; return; } int o = (q - sum) / 2; int c = q - o; if (c < 0) { cout << ":("; return; } for (int i = 0; i < n; i++) { if (s[i] == '?') { if (o) { s[i] = '('; o--; } else { s[i] = ')'; } } } int cur = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cur++; else cur--; if (cur < 0 or (cur == 0 and i != n - 1)) { cout << ":("; return; } } if (cur) { cout << ":("; return; } cout << s << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, q, sum; string s; void solve() { cin >> n >> s; for (auto i : s) { if (i == '?') q++; else sum += (i == '(') - (i == ')'); } if ((q - sum) % 2 or q - sum < 0) { cout << ":("; return; } int o = (q - sum) / 2; int c = q - o; if (c < 0) { cout << ":("; return; } for (int i = 0; i < n; i++) { if (s[i] == '?') { if (o) { s[i] = '('; o--; } else { s[i] = ')'; } } } int cur = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') cur++; else cur--; if (cur < 0 or (cur == 0 and i != n - 1)) { cout << ":("; return; } } if (cur) { cout << ":("; return; } cout << s << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; char s[300010]; int main() { int n; scanf("%d%s", &n, s); int c = 0, cq = 0; for (int i = 0; i < n; i++) if (s[i] == '(') c++; else if (s[i] == ')') c--; else cq++; if (c > 0) { for (int i = n - 1; i >= 0 && c; i--) if (s[i] == '?') s[i] = ')', c--, cq--; } else { for (int i = 0; i < n; i++) if (s[i] == '?' && c) s[i] = '(', c++, cq--; } cq /= 2; for (int i = 0; i < n; i++) if (s[i] == '?') if (cq) s[i] = '(', cq--; else s[i] = ')'; c = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') c++; else c--; if (c < 0 || i < n - 1 && c == 0) goto fail; } if (c) goto fail; cout << s << '\n'; return 0; fail: return puts(":("), 0; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[300010]; int main() { int n; scanf("%d%s", &n, s); int c = 0, cq = 0; for (int i = 0; i < n; i++) if (s[i] == '(') c++; else if (s[i] == ')') c--; else cq++; if (c > 0) { for (int i = n - 1; i >= 0 && c; i--) if (s[i] == '?') s[i] = ')', c--, cq--; } else { for (int i = 0; i < n; i++) if (s[i] == '?' && c) s[i] = '(', c++, cq--; } cq /= 2; for (int i = 0; i < n; i++) if (s[i] == '?') if (cq) s[i] = '(', cq--; else s[i] = ')'; c = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') c++; else c--; if (c < 0 || i < n - 1 && c == 0) goto fail; } if (c) goto fail; cout << s << '\n'; return 0; fail: return puts(":("), 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long double PI = 4 * atan((long double)1); long long int get_hash(string s) { long long int N = 1000001; long long int base[N], A = 11, MD = 1110111110111; base[0] = 1; for (long long int i = (1); i < (N); ++i) base[i] = (base[i - 1] * A) % MD; long long int hs = 0; for (long long int i = (0); i < (s.size()); ++i) { hs += (s[i] * base[i]); hs %= MD; } return hs; } long long power(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res *= a; a *= a; n /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; cin >> n; string s; cin >> s; long long int cnt1 = 0, hf = n / 2; for (long long int i = (0); i < (s.size()); ++i) { if (s[i] == '(') cnt1++; } if (s[0] == '?') cnt1++; if (s[1] == '?') cnt1++; if (n == 2) { if ((s[0] == '?' || s[0] == '(') && (s[1] == '?' || s[1] == ')')) cout << "()"; else cout << ":("; return 0; } if (s[0] == '?' || s[0] == '(') s[0] = '('; else { cout << ":("; return 0; } if (s[s.size() - 1] == '?' || s[s.size() - 1] == ')') s[s.size() - 1] = ')'; else { cout << ":("; return 0; } if (s[1] == '(' || s[1] == '?') s[1] = '('; else { cout << ":("; return 0; } if (s[s.size() - 2] == '?' || s[s.size() - 2] == ')') s[s.size() - 2] = ')'; else { cout << ":("; return 0; } stack<char> st; long long int diff = hf - cnt1, cnt2 = 0; if (diff < 0) { cout << ":("; return 0; } for (long long int i = (1); i < (s.size() - 1); ++i) { if (s[i] == '(') st.push(s[i]); else if (s[i] == ')') { if (st.size() == 0) { cout << ":("; return 0; } else st.pop(); } else if (s[i] == '?') { if (cnt2 < diff) { st.push('('); cnt2++; s[i] = '('; } else { if (st.size() != 0) { st.pop(); s[i] = ')'; } else { cout << ":("; return 0; } } } } if (st.size() == 0) for (long long int i = (0); i < (s.size()); ++i) cout << s[i]; else cout << ":("; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long double PI = 4 * atan((long double)1); long long int get_hash(string s) { long long int N = 1000001; long long int base[N], A = 11, MD = 1110111110111; base[0] = 1; for (long long int i = (1); i < (N); ++i) base[i] = (base[i - 1] * A) % MD; long long int hs = 0; for (long long int i = (0); i < (s.size()); ++i) { hs += (s[i] * base[i]); hs %= MD; } return hs; } long long power(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res *= a; a *= a; n /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; cin >> n; string s; cin >> s; long long int cnt1 = 0, hf = n / 2; for (long long int i = (0); i < (s.size()); ++i) { if (s[i] == '(') cnt1++; } if (s[0] == '?') cnt1++; if (s[1] == '?') cnt1++; if (n == 2) { if ((s[0] == '?' || s[0] == '(') && (s[1] == '?' || s[1] == ')')) cout << "()"; else cout << ":("; return 0; } if (s[0] == '?' || s[0] == '(') s[0] = '('; else { cout << ":("; return 0; } if (s[s.size() - 1] == '?' || s[s.size() - 1] == ')') s[s.size() - 1] = ')'; else { cout << ":("; return 0; } if (s[1] == '(' || s[1] == '?') s[1] = '('; else { cout << ":("; return 0; } if (s[s.size() - 2] == '?' || s[s.size() - 2] == ')') s[s.size() - 2] = ')'; else { cout << ":("; return 0; } stack<char> st; long long int diff = hf - cnt1, cnt2 = 0; if (diff < 0) { cout << ":("; return 0; } for (long long int i = (1); i < (s.size() - 1); ++i) { if (s[i] == '(') st.push(s[i]); else if (s[i] == ')') { if (st.size() == 0) { cout << ":("; return 0; } else st.pop(); } else if (s[i] == '?') { if (cnt2 < diff) { st.push('('); cnt2++; s[i] = '('; } else { if (st.size() != 0) { st.pop(); s[i] = ')'; } else { cout << ":("; return 0; } } } } if (st.size() == 0) for (long long int i = (0); i < (s.size()); ++i) cout << s[i]; else cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; void by() { cout << ":("; exit(0); } int main() { int i, j, k, n, m, h; string s; cin >> n; cin >> s; if (n & 1) by(); int op = (n / 2); int cl = (n / 2); int su = 0; for (i = 0; i < n; i++) if (s[i] == '(') op--; else if (s[i] == ')') cl--; if (cl < 0 || op < 0) by(); for (i = 0; i < n; i++) { if (s[i] == '(') su++; else if (s[i] == ')') su--; else { if (op > 0) op--, s[i] = '(', su++; else if (cl > 0) cl--, s[i] = ')', su--; else by(); } if (su < 0) by(); if (su == 0 && i != n - 1) by(); } cout << s; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; void by() { cout << ":("; exit(0); } int main() { int i, j, k, n, m, h; string s; cin >> n; cin >> s; if (n & 1) by(); int op = (n / 2); int cl = (n / 2); int su = 0; for (i = 0; i < n; i++) if (s[i] == '(') op--; else if (s[i] == ')') cl--; if (cl < 0 || op < 0) by(); for (i = 0; i < n; i++) { if (s[i] == '(') su++; else if (s[i] == ')') su--; else { if (op > 0) op--, s[i] = '(', su++; else if (cl > 0) cl--, s[i] = ')', su--; else by(); } if (su < 0) by(); if (su == 0 && i != n - 1) by(); } cout << s; } ```
#include <bits/stdc++.h> using namespace std; bool check(string &s) { int ans = 0; int n = s.size(); for (int i = 1; i < n - 1; i++) { if (s[i] == ')') { ans--; } else { ans++; } if (ans < 0) { break; } } return ans == 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; int n, cont1 = 0, cont2 = 0; cin >> n >> s; for (int i = 0; i < n; i++) { if (s[i] == '(') { cont1++; } if (s[i] == ')') { cont2++; } } cont1 = n / 2 - cont1; cont2 = n / 2 - cont2; for (int i = 0; i < n && cont1 > 0; i++) { if (s[i] == '?') { s[i] = '('; cont1--; } } for (int i = 0; i < n && cont2 > 0; i++) { if (s[i] == '?') { s[i] = ')'; cont2--; } } if (!check(s) || n & 1 || s[0] == ')' || s[n - 1] == '(') { cout << ":(\n"; } else { cout << s << '\n'; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool check(string &s) { int ans = 0; int n = s.size(); for (int i = 1; i < n - 1; i++) { if (s[i] == ')') { ans--; } else { ans++; } if (ans < 0) { break; } } return ans == 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; int n, cont1 = 0, cont2 = 0; cin >> n >> s; for (int i = 0; i < n; i++) { if (s[i] == '(') { cont1++; } if (s[i] == ')') { cont2++; } } cont1 = n / 2 - cont1; cont2 = n / 2 - cont2; for (int i = 0; i < n && cont1 > 0; i++) { if (s[i] == '?') { s[i] = '('; cont1--; } } for (int i = 0; i < n && cont2 > 0; i++) { if (s[i] == '?') { s[i] = ')'; cont2--; } } if (!check(s) || n & 1 || s[0] == ')' || s[n - 1] == '(') { cout << ":(\n"; } else { cout << s << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; string s; int l, r, n; bool check() { int zhan = 0; for (int i = 1; i <= n - 2; i++) { if (s[i] == '(') zhan++; else { zhan--; if (zhan < 0) return 0; } } if (zhan == 0) return 1; return 0; } int main() { cin >> n; cin >> s; if (n % 2 != 0) { printf(":("); return 0; } if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (s[0] != '(' || s[n - 1] != ')') { printf(":("); return 0; } if (n == 2) { cout << s << endl; return 0; } for (int i = 1; i < n - 1; i++) { if (s[i] == '(') l++; if (s[i] == ')') r++; } if (l >= (n >> 1) || r >= (n >> 1)) { printf(":("); return 0; } for (int i = 1; i <= n - 2; i++) { if (s[i] == '?') { if (l + 2 <= (n >> 1)) { l++; s[i] = '('; } else { r++; s[i] = ')'; } } } if (check()) cout << s << endl; else printf(":("); return 0; }
### Prompt Develop a solution in cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; int l, r, n; bool check() { int zhan = 0; for (int i = 1; i <= n - 2; i++) { if (s[i] == '(') zhan++; else { zhan--; if (zhan < 0) return 0; } } if (zhan == 0) return 1; return 0; } int main() { cin >> n; cin >> s; if (n % 2 != 0) { printf(":("); return 0; } if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; if (s[0] != '(' || s[n - 1] != ')') { printf(":("); return 0; } if (n == 2) { cout << s << endl; return 0; } for (int i = 1; i < n - 1; i++) { if (s[i] == '(') l++; if (s[i] == ')') r++; } if (l >= (n >> 1) || r >= (n >> 1)) { printf(":("); return 0; } for (int i = 1; i <= n - 2; i++) { if (s[i] == '?') { if (l + 2 <= (n >> 1)) { l++; s[i] = '('; } else { r++; s[i] = ')'; } } } if (check()) cout << s << endl; else printf(":("); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-8; const long long INF = 1e18 + 7; inline long long read() { long long X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { X = (X << 3) + (X << 1) + (ch ^ 48); ch = getchar(); } return w ? -X : X; } inline void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) { write(x / 10); } putchar(x % 10 + '0'); } bool check(string ss) { stack<char> a; for (long long i = 0; i < ss.length(); i++) { if (ss[i] != '(' && ss[i] != ')') { ss.erase(i, 1); i--; } } for (long long i = 0; i < ss.length(); i++) { if (ss[i] == '(') { a.push(ss[i]); } else { if (a.empty()) { return false; } else { if (a.top() == '(' && ss[i] != ')') { return false; } else { a.pop(); } } } } bool flag1 = a.empty(), flag2 = true; long long cnt = 0; for (long long i = 1; i < ss.length() - 1; i++) { if (ss[i] == '(') cnt++; else { cnt--; if (cnt < 0) flag2 = false; } } return (flag1 && flag2 && !cnt); } string s, ans; long long n, cnt1, cnt2; signed main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; cin >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; long long i, j; for (i = 1; i < n - 1; i++) { if (s[i] == '(') cnt1++; if (s[i] == ')') cnt2++; } if (n % 2 == 1 || cnt1 > n / 2 || cnt2 > n / 2 || s[0] != '(' || s[n - 1] != ')') { cout << ":("; return 0; } for (i = 1; i < n - 1; i++) { if (s[i] == '?') { if (cnt1 + 2 <= n / 2) { s[i] = '('; cnt1++; } else { s[i] = ')'; cnt2++; } } } if (check(s)) { cout << s; } else cout << ":("; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long double EPS = 1e-8; const long long INF = 1e18 + 7; inline long long read() { long long X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { X = (X << 3) + (X << 1) + (ch ^ 48); ch = getchar(); } return w ? -X : X; } inline void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) { write(x / 10); } putchar(x % 10 + '0'); } bool check(string ss) { stack<char> a; for (long long i = 0; i < ss.length(); i++) { if (ss[i] != '(' && ss[i] != ')') { ss.erase(i, 1); i--; } } for (long long i = 0; i < ss.length(); i++) { if (ss[i] == '(') { a.push(ss[i]); } else { if (a.empty()) { return false; } else { if (a.top() == '(' && ss[i] != ')') { return false; } else { a.pop(); } } } } bool flag1 = a.empty(), flag2 = true; long long cnt = 0; for (long long i = 1; i < ss.length() - 1; i++) { if (ss[i] == '(') cnt++; else { cnt--; if (cnt < 0) flag2 = false; } } return (flag1 && flag2 && !cnt); } string s, ans; long long n, cnt1, cnt2; signed main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; cin >> s; if (s[0] == '?') s[0] = '('; if (s[n - 1] == '?') s[n - 1] = ')'; long long i, j; for (i = 1; i < n - 1; i++) { if (s[i] == '(') cnt1++; if (s[i] == ')') cnt2++; } if (n % 2 == 1 || cnt1 > n / 2 || cnt2 > n / 2 || s[0] != '(' || s[n - 1] != ')') { cout << ":("; return 0; } for (i = 1; i < n - 1; i++) { if (s[i] == '?') { if (cnt1 + 2 <= n / 2) { s[i] = '('; cnt1++; } else { s[i] = ')'; cnt2++; } } } if (check(s)) { cout << s; } else cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int a = 0, b = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') { a += 1; } else if (s[i] == ')') { b += 1; } } a = n / 2 - a; b = n / 2 - b; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (a > 0) { a -= 1; s[i] = '('; } else if (b > 0) { b -= 1; s[i] = ')'; } else { return cout << ":(", 0; } } } int cur = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') { cur += 1; } else { cur -= 1; } if (cur < 0) { return cout << ":(", 0; } if (i != n - 1 && cur == 0) { return cout << ":(", 0; } } if (cur != 0) { return cout << ":(", 0; } else { cout << s; } }
### Prompt Create a solution in CPP for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; int a = 0, b = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') { a += 1; } else if (s[i] == ')') { b += 1; } } a = n / 2 - a; b = n / 2 - b; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (a > 0) { a -= 1; s[i] = '('; } else if (b > 0) { b -= 1; s[i] = ')'; } else { return cout << ":(", 0; } } } int cur = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') { cur += 1; } else { cur -= 1; } if (cur < 0) { return cout << ":(", 0; } if (i != n - 1 && cur == 0) { return cout << ":(", 0; } } if (cur != 0) { return cout << ":(", 0; } else { cout << s; } } ```
#include <bits/stdc++.h> using namespace std; void pr() { cout << ":("; exit(0); } int main() { string s; long long int i, j, k, l, p; cin >> p >> s; if (p % 2) { cout << ":(" << "\n"; } else if ((s[0] == '(' && s[p - 1] == '?') || (s[0] == '?' && s[p - 1] == '?') || (s[0] == '(' && s[p - 1] == ')') || (s[0] == '?' && s[p - 1] == ')')) { long long int su = 0, o = (p / 2), c = (p / 2); for (int i = 0; i < p; i++) { if (s[i] == '(') o--; else if (s[i] == ')') c--; } if (o < 0 || c < 0) pr(); for (i = 0; i < p; i++) { if (s[i] == '(') su++; else if (s[i] == ')') su--; else { if (o > 0) o--, su++, s[i] = '('; else if (c > 0) c--, su--, s[i] = ')'; else pr(); } if (su < 0 || (su == 0 && i != p - 1)) pr(); } cout << s << "\n"; } else { cout << ":(" << "\n"; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; void pr() { cout << ":("; exit(0); } int main() { string s; long long int i, j, k, l, p; cin >> p >> s; if (p % 2) { cout << ":(" << "\n"; } else if ((s[0] == '(' && s[p - 1] == '?') || (s[0] == '?' && s[p - 1] == '?') || (s[0] == '(' && s[p - 1] == ')') || (s[0] == '?' && s[p - 1] == ')')) { long long int su = 0, o = (p / 2), c = (p / 2); for (int i = 0; i < p; i++) { if (s[i] == '(') o--; else if (s[i] == ')') c--; } if (o < 0 || c < 0) pr(); for (i = 0; i < p; i++) { if (s[i] == '(') su++; else if (s[i] == ')') su--; else { if (o > 0) o--, su++, s[i] = '('; else if (c > 0) c--, su--, s[i] = ')'; else pr(); } if (su < 0 || (su == 0 && i != p - 1)) pr(); } cout << s << "\n"; } else { cout << ":(" << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string a; cin >> a; if (n == 0 || n == 1 || n % 2 != 0) cout << ""; int l = 0, r = 0; for (int i = 0; i < n; i++) { if (a[i] == '(') l++; if (a[i] == ')') r++; if (l > n / 2 || r > n / 2) { cout << ":(" << endl; return 0; } } l = n / 2 - l; for (int i = 0; i < n; i++) { if (a[i] == '?' && l) { a[i] = '('; l--; } else if (a[i] == '?') { a[i] = ')'; r--; } } l = 0; for (int i = 0; i < n; i++) { if (a[i] == '(') l++; if (a[i] == ')') l--; if (l < 0 || (l == 0 && i != n - 1)) { cout << ":(" << endl; return 0; } } if (l != 0) { cout << ":(" << endl; return 0; } cout << a; return 0; }
### Prompt Create a solution in CPP for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string a; cin >> a; if (n == 0 || n == 1 || n % 2 != 0) cout << ""; int l = 0, r = 0; for (int i = 0; i < n; i++) { if (a[i] == '(') l++; if (a[i] == ')') r++; if (l > n / 2 || r > n / 2) { cout << ":(" << endl; return 0; } } l = n / 2 - l; for (int i = 0; i < n; i++) { if (a[i] == '?' && l) { a[i] = '('; l--; } else if (a[i] == '?') { a[i] = ')'; r--; } } l = 0; for (int i = 0; i < n; i++) { if (a[i] == '(') l++; if (a[i] == ')') l--; if (l < 0 || (l == 0 && i != n - 1)) { cout << ":(" << endl; return 0; } } if (l != 0) { cout << ":(" << endl; return 0; } cout << a; return 0; } ```
#include <bits/stdc++.h> const int INF = 0x3f3f3f3f; const int N = 500005; char s[N], p[N]; int read() { int x = 0, v = 1; char ch = getchar(); for (; ch < '0' || ch > '9'; v = (ch == '-') ? (-1) : v, ch = getchar()) ; for (; ch <= '9' && ch >= '0'; x = x * 10 + ch - '0', ch = getchar()) ; return x * v; } int main(void) { int n; scanf("%d", &n); if (n & 1) return 0 & puts(":("); scanf("%s", s + 1); int l = 0, r = 0; for (int i = 1; i <= n; ++i) { if (s[i] == '(') l++; else if (s[i] == ')') r++; } int L = n / 2 - l, R = n / 2 - r; for (int i = 1; i <= n; ++i) { if (s[i] == '?') { if (L) L--, p[i] = '('; else R--, p[i] = ')'; } else p[i] = s[i]; } l = r = 0; for (int i = 1; i <= n; ++i) { if (p[i] == '(') l++; else r++; if (r > l || (r == l && i < n)) return 0 & puts(":("); } if (l != r) return 0 & puts(":("); for (int i = 1; i <= n; ++i) putchar(p[i]); return 0; }
### Prompt Generate a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> const int INF = 0x3f3f3f3f; const int N = 500005; char s[N], p[N]; int read() { int x = 0, v = 1; char ch = getchar(); for (; ch < '0' || ch > '9'; v = (ch == '-') ? (-1) : v, ch = getchar()) ; for (; ch <= '9' && ch >= '0'; x = x * 10 + ch - '0', ch = getchar()) ; return x * v; } int main(void) { int n; scanf("%d", &n); if (n & 1) return 0 & puts(":("); scanf("%s", s + 1); int l = 0, r = 0; for (int i = 1; i <= n; ++i) { if (s[i] == '(') l++; else if (s[i] == ')') r++; } int L = n / 2 - l, R = n / 2 - r; for (int i = 1; i <= n; ++i) { if (s[i] == '?') { if (L) L--, p[i] = '('; else R--, p[i] = ')'; } else p[i] = s[i]; } l = r = 0; for (int i = 1; i <= n; ++i) { if (p[i] == '(') l++; else r++; if (r > l || (r == l && i < n)) return 0 & puts(":("); } if (l != r) return 0 & puts(":("); for (int i = 1; i <= n; ++i) putchar(p[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; string s; int main() { int n; cin >> n >> s; if (n % 2) { cout << ":(\n"; return 0; } int l = 0, r = 0; for (int i = 0; i < (n); ++i) { if (s[i] == '(') l++; if (s[i] == ')') r++; } if (l > n / 2 || r > n / 2) { cout << ":(\n"; return 0; } for (int i = 0; i < (n); ++i) if (s[i] == '?') { if (l < n / 2) s[i] = '(', l++; else s[i] = ')', r++; } int sum = 0; for (int i = 0; i < (n); ++i) { if (s[i] == '(') sum++; else sum--; if (i != n - 1 && sum <= 0 || i == n - 1 && sum != 0) { cout << ":(\n"; return 0; } } cout << s << endl; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; int main() { int n; cin >> n >> s; if (n % 2) { cout << ":(\n"; return 0; } int l = 0, r = 0; for (int i = 0; i < (n); ++i) { if (s[i] == '(') l++; if (s[i] == ')') r++; } if (l > n / 2 || r > n / 2) { cout << ":(\n"; return 0; } for (int i = 0; i < (n); ++i) if (s[i] == '?') { if (l < n / 2) s[i] = '(', l++; else s[i] = ')', r++; } int sum = 0; for (int i = 0; i < (n); ++i) { if (s[i] == '(') sum++; else sum--; if (i != n - 1 && sum <= 0 || i == n - 1 && sum != 0) { cout << ":(\n"; return 0; } } cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n & 1) { cout << ":(" << endl; return 0; } int cnt1, cnt2; cnt1 = cnt2 = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') cnt1++; else if (s[i] == ')') cnt2++; } if (cnt1 > n / 2 || cnt2 > n / 2) { cout << ":(" << endl; return 0; } cnt1 = n / 2 - cnt1; cnt2 = n / 2 - cnt2; int flag = 0; int cnt = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') cnt++; else if (s[i] == ')') cnt--; else { if (cnt1) { s[i] = '('; cnt1--; cnt++; } else { s[i] = ')'; cnt--; } } if (cnt < 0 || (cnt == 0 && i != s.size() - 1)) { flag = 1; break; } } if (cnt != 0) { flag = 1; } if (flag) { cout << ":(" << endl; } else cout << s << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n & 1) { cout << ":(" << endl; return 0; } int cnt1, cnt2; cnt1 = cnt2 = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') cnt1++; else if (s[i] == ')') cnt2++; } if (cnt1 > n / 2 || cnt2 > n / 2) { cout << ":(" << endl; return 0; } cnt1 = n / 2 - cnt1; cnt2 = n / 2 - cnt2; int flag = 0; int cnt = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') cnt++; else if (s[i] == ')') cnt--; else { if (cnt1) { s[i] = '('; cnt1--; cnt++; } else { s[i] = ')'; cnt--; } } if (cnt < 0 || (cnt == 0 && i != s.size() - 1)) { flag = 1; break; } } if (cnt != 0) { flag = 1; } if (flag) { cout << ":(" << endl; } else cout << s << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; char s[300005]; int cnt; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } int main() { n = read(); scanf("%s", s + 1); if ((n & 1) || s[1] == ')' || s[n] == '(') { printf(":(\n"); return 0; } s[1] = '(', s[n] = ')'; for (int i = 2; i < n; i++) if (s[i] == '(') cnt++; int tp = 0; for (int i = 2; i < n; i++) { if (s[i] == '(') tp++; if (s[i] == ')') tp--; if (s[i] == '?') { if (cnt < ((n - 2) / 2)) cnt++, tp++, s[i] = '('; else tp--, s[i] = ')'; } if (tp < 0) { printf(":(\n"); return 0; } } if (tp) { printf(":(\n"); return 0; } printf("%s\n", s + 1); return 0; }
### Prompt Create a solution in Cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; char s[300005]; int cnt; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } int main() { n = read(); scanf("%s", s + 1); if ((n & 1) || s[1] == ')' || s[n] == '(') { printf(":(\n"); return 0; } s[1] = '(', s[n] = ')'; for (int i = 2; i < n; i++) if (s[i] == '(') cnt++; int tp = 0; for (int i = 2; i < n; i++) { if (s[i] == '(') tp++; if (s[i] == ')') tp--; if (s[i] == '?') { if (cnt < ((n - 2) / 2)) cnt++, tp++, s[i] = '('; else tp--, s[i] = ')'; } if (tp < 0) { printf(":(\n"); return 0; } } if (tp) { printf(":(\n"); return 0; } printf("%s\n", s + 1); return 0; } ```
#include <bits/stdc++.h> using namespace std; char z[300005], ans[300005]; int st[300005], last, Min = 1e9, first, cnt[300005], chk; int main() { int i, j, n; scanf("%d %s", &n, z); if (n % 2 != 0) { printf(":("); return 0; } if (n == 2) { if (z[0] == '?') z[0] = '('; if (z[1] == '?') z[1] = ')'; if (z[0] == '(' && z[1] == ')') printf("()"); else printf(":("); return 0; } if (z[0] == ')' || z[n - 1] == '(' || n == 1) { printf(":("); return 0; } z[0] = ans[0] = '('; z[n - 1] = ans[n - 1] = ')'; cnt[0] = 1; i = 0; for (int _i = n; i < _i; i++) { if (z[i] == '?') { first = i; break; } } for (i = 1; i < n; i++) { cnt[i] = cnt[i - 1]; if (z[i] == '(') { cnt[i]++; ans[i] = '('; } else if (z[i] == ')') { cnt[i]--; ans[i] = ')'; if (cnt[i] == 0 && i != n - 1) { printf(":("); return 0; } } else if (z[i] == '?') { cnt[i]++; ans[i] = '('; } } cnt[n] = cnt[n - 1]; for (i = n - 2; i >= 1; i--) { if (z[i] == '?') { ans[i] = ')'; cnt[n] -= 2; Min = min(Min - 2, cnt[i] - 2); } if (Min <= 0 || cnt[n] < 0) { printf(":("); return 0; } if (cnt[n] == 0) break; } if (i == 0) { printf(":("); return 0; } i = 0; for (int _i = n; i < _i; i++) { if (ans[i] == '(') chk++; else if (ans[i] == ')') { chk--; if (chk == 0 && i != n - 1) { printf(":("); return 0; } } else { printf(":("); return 0; } } i = 0; for (int _i = n; i < _i; i++) printf("%c", ans[i]); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; char z[300005], ans[300005]; int st[300005], last, Min = 1e9, first, cnt[300005], chk; int main() { int i, j, n; scanf("%d %s", &n, z); if (n % 2 != 0) { printf(":("); return 0; } if (n == 2) { if (z[0] == '?') z[0] = '('; if (z[1] == '?') z[1] = ')'; if (z[0] == '(' && z[1] == ')') printf("()"); else printf(":("); return 0; } if (z[0] == ')' || z[n - 1] == '(' || n == 1) { printf(":("); return 0; } z[0] = ans[0] = '('; z[n - 1] = ans[n - 1] = ')'; cnt[0] = 1; i = 0; for (int _i = n; i < _i; i++) { if (z[i] == '?') { first = i; break; } } for (i = 1; i < n; i++) { cnt[i] = cnt[i - 1]; if (z[i] == '(') { cnt[i]++; ans[i] = '('; } else if (z[i] == ')') { cnt[i]--; ans[i] = ')'; if (cnt[i] == 0 && i != n - 1) { printf(":("); return 0; } } else if (z[i] == '?') { cnt[i]++; ans[i] = '('; } } cnt[n] = cnt[n - 1]; for (i = n - 2; i >= 1; i--) { if (z[i] == '?') { ans[i] = ')'; cnt[n] -= 2; Min = min(Min - 2, cnt[i] - 2); } if (Min <= 0 || cnt[n] < 0) { printf(":("); return 0; } if (cnt[n] == 0) break; } if (i == 0) { printf(":("); return 0; } i = 0; for (int _i = n; i < _i; i++) { if (ans[i] == '(') chk++; else if (ans[i] == ')') { chk--; if (chk == 0 && i != n - 1) { printf(":("); return 0; } } else { printf(":("); return 0; } } i = 0; for (int _i = n; i < _i; i++) printf("%c", ans[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, otvoreni = 0, otvorio = 0; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> s; if (n & 1) { cout << ":("; return 0; } long long novi = n / 2; for (int i = 0; i < n; i++) { if (s[i] == '(') novi--; } for (int i = 0; i < n; i++) { if (s[i] == '(') { otvoreni++; } else if (s[i] == ')') { if (otvoreni <= 1 && i != n - 1) { cout << ":("; return 0; } otvoreni--; } else { if (novi) { s[i] = '('; novi--; otvoreni++; } else { if (otvoreni <= 1 && i != n - 1) { cout << ":("; return 0; } else { s[i] = ')'; otvoreni--; } } } } if (novi == 0 && otvoreni == 0) { cout << s; } else cout << ":("; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, otvoreni = 0, otvorio = 0; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; cin >> s; if (n & 1) { cout << ":("; return 0; } long long novi = n / 2; for (int i = 0; i < n; i++) { if (s[i] == '(') novi--; } for (int i = 0; i < n; i++) { if (s[i] == '(') { otvoreni++; } else if (s[i] == ')') { if (otvoreni <= 1 && i != n - 1) { cout << ":("; return 0; } otvoreni--; } else { if (novi) { s[i] = '('; novi--; otvoreni++; } else { if (otvoreni <= 1 && i != n - 1) { cout << ":("; return 0; } else { s[i] = ')'; otvoreni--; } } } } if (novi == 0 && otvoreni == 0) { cout << s; } else cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n; cin >> s; int cont1 = 0; int cont2 = 0; if ((s[0] == ')') || (s[n - 1] == '(') || (n % 2 == 1)) { cout << ":(" << endl; return 0; } string s1(s, 1, n - 2); int n1 = s1.size(); int cont3 = 0; int cont4 = 0; int h = n1 / 2; for (int i = 0; i < n1; i++) { if (s1[i] == ')') cont3++; if (s1[i] == '(') cont4++; } int j = 0; int f = h - cont4; for (int i = 0; i < n1; i++) if ((s1[i] == '?') && (f > 0)) s1[i] = '(', f--; f = h - cont3; for (int i = n1 - 1; i >= 0; i--) if ((s1[i] == '?') && (f > 0)) s1[i] = ')', f--; bool g = 0; for (int i = 0; i < n1; i++) { if (s1[i] == '?') { if (g == 0) { s1[i] = '('; g = 1; } else if (g == 1) { s1[i] = ')'; g = 0; } } } for (int i = 0; i < n1; i++) { if (s1[i] == '(') cont1++; else cont1--; if (cont1 < 0) { cout << ":(" << endl; return 0; } } if (cont1 != 0) cout << ":(" << endl; else cout << '(' << s1 << ')'; return 0; }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n; cin >> s; int cont1 = 0; int cont2 = 0; if ((s[0] == ')') || (s[n - 1] == '(') || (n % 2 == 1)) { cout << ":(" << endl; return 0; } string s1(s, 1, n - 2); int n1 = s1.size(); int cont3 = 0; int cont4 = 0; int h = n1 / 2; for (int i = 0; i < n1; i++) { if (s1[i] == ')') cont3++; if (s1[i] == '(') cont4++; } int j = 0; int f = h - cont4; for (int i = 0; i < n1; i++) if ((s1[i] == '?') && (f > 0)) s1[i] = '(', f--; f = h - cont3; for (int i = n1 - 1; i >= 0; i--) if ((s1[i] == '?') && (f > 0)) s1[i] = ')', f--; bool g = 0; for (int i = 0; i < n1; i++) { if (s1[i] == '?') { if (g == 0) { s1[i] = '('; g = 1; } else if (g == 1) { s1[i] = ')'; g = 0; } } } for (int i = 0; i < n1; i++) { if (s1[i] == '(') cont1++; else cont1--; if (cont1 < 0) { cout << ":(" << endl; return 0; } } if (cont1 != 0) cout << ":(" << endl; else cout << '(' << s1 << ')'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int len; cin >> len; if (len % 2) { printf(":("); return 0; } char s[len + 5]; scanf("%s", s); stack<char> st; if (s[0] == '?' || s[0] == '(') s[0] = '('; else { printf(":("); return 0; } if (s[len - 1] == '?' || s[len - 1] == ')') s[len - 1] = ')'; else { printf(":("); return 0; } int l = 0, r = 0, wen = 0; for (int i = 1; i < len - 1; i++) { if (s[i] == '(') l++; else if (s[i] == ')') r++; else if (s[i] == '?') wen++; } for (int i = 1; i < len - 1; i++) { if (s[i] == '(') st.push('('); else if (s[i] == ')') { if (st.empty()) { printf(":("); return 0; } else { st.pop(); } } else if (s[i] == '?') { if (l < len / 2 - 1) { s[i] = '('; st.push('('); l++; } else { s[i] = ')'; if (st.empty()) { printf(":("); return 0; } else st.pop(); } } } if (st.empty()) printf("%s", s); else printf(":("); return 0; }
### Prompt Please create a solution in cpp to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int len; cin >> len; if (len % 2) { printf(":("); return 0; } char s[len + 5]; scanf("%s", s); stack<char> st; if (s[0] == '?' || s[0] == '(') s[0] = '('; else { printf(":("); return 0; } if (s[len - 1] == '?' || s[len - 1] == ')') s[len - 1] = ')'; else { printf(":("); return 0; } int l = 0, r = 0, wen = 0; for (int i = 1; i < len - 1; i++) { if (s[i] == '(') l++; else if (s[i] == ')') r++; else if (s[i] == '?') wen++; } for (int i = 1; i < len - 1; i++) { if (s[i] == '(') st.push('('); else if (s[i] == ')') { if (st.empty()) { printf(":("); return 0; } else { st.pop(); } } else if (s[i] == '?') { if (l < len / 2 - 1) { s[i] = '('; st.push('('); l++; } else { s[i] = ')'; if (st.empty()) { printf(":("); return 0; } else st.pop(); } } } if (st.empty()) printf("%s", s); else printf(":("); return 0; } ```
#include <bits/stdc++.h> using namespace std; bool check(const string &s) { int bal = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') { bal++; } else { bal--; } if (bal < 0) return false; } return bal == 0; } int main() { int n; cin >> n; string s; cin >> s; n = s.size(); if (s.size() % 2 == 1) { cout << ":("; return 0; } if (s[0] == ')' || s[1] == ')' || s[(int)s.size() - 1] == '(' || s[(int)s.size() - 2] == '(') { cout << ":("; return 0; } s[0] = '('; s.back() = ')'; s[1] = '('; s[(int(s.size() - 2))] = ')'; int c1 = 0, c2; for (int i = 0; i < n; i++) { if (s[i] == '(') { c1++; } } c1 = n / 2 - c1; string ans = s; ans[0] = '('; ans.back() = ')'; for (int i = 1; i < (int)s.size() - 1; i++) { if (s[i] == '?') { if (c1) { ans[i] = '('; c1--; } else { ans[i] = ')'; } } } if (check(ans.substr(1, n - 2))) cout << ans; else cout << ":("; return 0; }
### Prompt In cpp, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool check(const string &s) { int bal = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') { bal++; } else { bal--; } if (bal < 0) return false; } return bal == 0; } int main() { int n; cin >> n; string s; cin >> s; n = s.size(); if (s.size() % 2 == 1) { cout << ":("; return 0; } if (s[0] == ')' || s[1] == ')' || s[(int)s.size() - 1] == '(' || s[(int)s.size() - 2] == '(') { cout << ":("; return 0; } s[0] = '('; s.back() = ')'; s[1] = '('; s[(int(s.size() - 2))] = ')'; int c1 = 0, c2; for (int i = 0; i < n; i++) { if (s[i] == '(') { c1++; } } c1 = n / 2 - c1; string ans = s; ans[0] = '('; ans.back() = ')'; for (int i = 1; i < (int)s.size() - 1; i++) { if (s[i] == '?') { if (c1) { ans[i] = '('; c1--; } else { ans[i] = ')'; } } } if (check(ans.substr(1, n - 2))) cout << ans; else cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; string s; int main() { cin >> n >> s; if (s.size() % 2 != 0) return printf(":("), 0; int t = s.size() / 2; int n1 = 0, n2 = 0; for (auto c : s) { if (c == '(') n1++; if (c == ')') n2++; } n1 = t - n1; n2 = t - n2; if (n1 < 0 or n2 < 0) return printf(":("), 0; int num = 0, cnt = 0; for (auto &c : s) { cnt++; if (c == '(') num++; if (c == ')') num--; if (c == '?') { if (n1 > 0) c = '(', num++, n1--; else c = ')', num--; } if (cnt < n) if (num <= 0) return printf(":("), 0; } cout << s; }
### Prompt Please provide a CPP coded solution to the problem described below: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; string s; int main() { cin >> n >> s; if (s.size() % 2 != 0) return printf(":("), 0; int t = s.size() / 2; int n1 = 0, n2 = 0; for (auto c : s) { if (c == '(') n1++; if (c == ')') n2++; } n1 = t - n1; n2 = t - n2; if (n1 < 0 or n2 < 0) return printf(":("), 0; int num = 0, cnt = 0; for (auto &c : s) { cnt++; if (c == '(') num++; if (c == ')') num--; if (c == '?') { if (n1 > 0) c = '(', num++, n1--; else c = ')', num--; } if (cnt < n) if (num <= 0) return printf(":("), 0; } cout << s; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 100; char str[maxn]; int main() { int len; scanf("%d", &len); scanf("%s", str + 1); len = strlen(str + 1); int cnt1 = 0, cnt2 = 0; for (int i = 1; i <= len; i++) { if (str[i] == '(') cnt1++; else if (str[i] == ')') cnt2++; } if (len & 1 || str[1] == ')' || str[len] == '(') { printf(":(\n"); return 0; } cnt1 = len / 2 - cnt1; cnt2 = len / 2 - cnt2; for (int i = 1; i <= len; i++) { if (str[i] != '?') continue; if (cnt1) str[i] = '(', cnt1--; else if (cnt2) str[i] = ')', cnt2--; } int tmp = 0; for (int i = 1; i <= len; i++) { if (str[i] == '(') tmp++; else if (str[i] == ')') tmp--; if (tmp <= 0 && i != len) { printf(":(\n"); return 0; } } if (tmp == 0) { for (int i = 1; i <= len; i++) { printf("%c", str[i]); } printf("\n"); } else printf(":(\n"); return 0; }
### Prompt Please formulate a CPP solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 100; char str[maxn]; int main() { int len; scanf("%d", &len); scanf("%s", str + 1); len = strlen(str + 1); int cnt1 = 0, cnt2 = 0; for (int i = 1; i <= len; i++) { if (str[i] == '(') cnt1++; else if (str[i] == ')') cnt2++; } if (len & 1 || str[1] == ')' || str[len] == '(') { printf(":(\n"); return 0; } cnt1 = len / 2 - cnt1; cnt2 = len / 2 - cnt2; for (int i = 1; i <= len; i++) { if (str[i] != '?') continue; if (cnt1) str[i] = '(', cnt1--; else if (cnt2) str[i] = ')', cnt2--; } int tmp = 0; for (int i = 1; i <= len; i++) { if (str[i] == '(') tmp++; else if (str[i] == ')') tmp--; if (tmp <= 0 && i != len) { printf(":(\n"); return 0; } } if (tmp == 0) { for (int i = 1; i <= len; i++) { printf("%c", str[i]); } printf("\n"); } else printf(":(\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 5; int n, f[N]; string s, res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; s = ' ' + s; long long cnt = 0; queue<int> q; for (int i = 1; i <= n; i++) { if (s[i] == '(') cnt++; else if (s[i] == ')') { cnt--; if (cnt < 0 && q.empty()) return cout << ":(", 0; if (cnt < 0) s[q.front()] = '(', cnt++, q.pop(); } else q.push(i); } for (int i = n; i >= 1; i--) { if (cnt > 0 && s[i] == '?') cnt--, s[i] = ')'; } s.erase(s.begin(), s.begin() + 1); long long xx = 0; for (char x : s) xx += (x == '?'); int x = xx; cnt = 0; int i = 0; for (char &ch : s) { i++; if (ch == '?') { if (x > xx / 2) ch = '(', x--; else ch = ')'; } if (ch == '(') cnt++; else cnt--; if (cnt < 0 || (cnt == 0 && i < n)) return cout << ":(", 0; } if (cnt != 0) cout << ":("; else cout << s; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 5; int n, f[N]; string s, res; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> s; s = ' ' + s; long long cnt = 0; queue<int> q; for (int i = 1; i <= n; i++) { if (s[i] == '(') cnt++; else if (s[i] == ')') { cnt--; if (cnt < 0 && q.empty()) return cout << ":(", 0; if (cnt < 0) s[q.front()] = '(', cnt++, q.pop(); } else q.push(i); } for (int i = n; i >= 1; i--) { if (cnt > 0 && s[i] == '?') cnt--, s[i] = ')'; } s.erase(s.begin(), s.begin() + 1); long long xx = 0; for (char x : s) xx += (x == '?'); int x = xx; cnt = 0; int i = 0; for (char &ch : s) { i++; if (ch == '?') { if (x > xx / 2) ch = '(', x--; else ch = ')'; } if (ch == '(') cnt++; else cnt--; if (cnt < 0 || (cnt == 0 && i < n)) return cout << ":(", 0; } if (cnt != 0) cout << ":("; else cout << s; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a % b == 0) return b; else return gcd(b, a % b); } int sum(long long a) { int sum = 0; while (a > 0) { sum = sum + (a % 10); a = a / 10; } return sum; } int count_digit(long long n) { int count = 0; while (n > 0) { n = n / 10; count++; } return count; } int binarySearch(int x, int y, long long z, vector<long long> &v) { int low = x; int high = y; int mid = x + (y - x) / 2; while (low <= high) { if (v[mid] == z) return mid; if (v[mid] < z) return binarySearch(mid + 1, high, z, v); if (v[mid] > z) return binarySearch(low, mid - 1, z, v); } return -1; } long long modularExponentiation(long long x, long long n, long long M) { if (n == 0) return 1; else if (n % 2 == 0) return modularExponentiation((x * x) % M, n / 2, M); else return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M; } long long binaryExponentiation(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) return binaryExponentiation(x * x, n / 2); else return x * binaryExponentiation(x * x, (n - 1) / 2); } set<long long> s; void genrate(long long n, int len, int max) { if (len > max) return; s.insert(n); genrate(n * 10 + 1, len + 1, max); genrate(n * 10 + 0, len + 1, max); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tests = 1; while (tests--) { int n; cin >> n; string s; cin >> s; if (n % 2 != 0) { cout << ":("; exit(0); } int open = 0; int close = 0; int c = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') open++; else if (s[i] == ')') close++; else c++; } int rem = n / 2; int i = 0; while (open < rem && i < s.size()) { if (s[i] == '?') { s[i] = '('; open++; } i++; } i = 0; while (i < s.size() && close < rem) { if (s[i] == '?') { s[i] = ')'; close++; } i++; } stack<char> stk; stk.push(s[0]); int f = 0; for (int i = 1; i < s.size(); i++) { if (stk.empty()) { f = 1; break; } else { if (stk.top() == '(' && s[i] == ')') { stk.pop(); } else stk.push(s[i]); } } if (f == 1) { cout << ":("; exit(0); } else if (stk.size() == 0 && f == 0) { cout << s; } else { cout << ":("; } } }
### Prompt In CPP, your task is to solve the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a % b == 0) return b; else return gcd(b, a % b); } int sum(long long a) { int sum = 0; while (a > 0) { sum = sum + (a % 10); a = a / 10; } return sum; } int count_digit(long long n) { int count = 0; while (n > 0) { n = n / 10; count++; } return count; } int binarySearch(int x, int y, long long z, vector<long long> &v) { int low = x; int high = y; int mid = x + (y - x) / 2; while (low <= high) { if (v[mid] == z) return mid; if (v[mid] < z) return binarySearch(mid + 1, high, z, v); if (v[mid] > z) return binarySearch(low, mid - 1, z, v); } return -1; } long long modularExponentiation(long long x, long long n, long long M) { if (n == 0) return 1; else if (n % 2 == 0) return modularExponentiation((x * x) % M, n / 2, M); else return (x * modularExponentiation((x * x) % M, (n - 1) / 2, M)) % M; } long long binaryExponentiation(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) return binaryExponentiation(x * x, n / 2); else return x * binaryExponentiation(x * x, (n - 1) / 2); } set<long long> s; void genrate(long long n, int len, int max) { if (len > max) return; s.insert(n); genrate(n * 10 + 1, len + 1, max); genrate(n * 10 + 0, len + 1, max); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tests = 1; while (tests--) { int n; cin >> n; string s; cin >> s; if (n % 2 != 0) { cout << ":("; exit(0); } int open = 0; int close = 0; int c = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '(') open++; else if (s[i] == ')') close++; else c++; } int rem = n / 2; int i = 0; while (open < rem && i < s.size()) { if (s[i] == '?') { s[i] = '('; open++; } i++; } i = 0; while (i < s.size() && close < rem) { if (s[i] == '?') { s[i] = ')'; close++; } i++; } stack<char> stk; stk.push(s[0]); int f = 0; for (int i = 1; i < s.size(); i++) { if (stk.empty()) { f = 1; break; } else { if (stk.top() == '(' && s[i] == ')') { stk.pop(); } else stk.push(s[i]); } } if (f == 1) { cout << ":("; exit(0); } else if (stk.size() == 0 && f == 0) { cout << s; } else { cout << ":("; } } } ```
#include <bits/stdc++.h> using namespace std; char ss[333333]; string s; int n, tt, sum(0), dem[255]; bool flag(true); int main() { cin >> n; cin >> ss; if (n % 2 == 1) flag = false; for (int i = 0; i < n; i++) { ss[i] == '(' ? ++dem[40] : ss[i] == ')' ? ++dem[41] : 0; } if (dem[40] > n / 2 || dem[41] > n / 2) flag = false; dem[40] = n / 2 - dem[40]; dem[41] = n / 2 - dem[41]; for (int i = 0; i < n; i++) { if (ss[i] == '?') { if (dem[40] > 0) { ss[i] = 40; --dem[40]; } else ss[i] = ')'; } } for (int i = 0; i < n; i++) { ss[i] == '(' ? ++sum : --sum; if (sum <= 0 && i != n - 1) flag = false; } if (flag) for (int i = 0; i < n; i++) cout << ss[i]; else cout << ":("; return 0; }
### Prompt Generate a Cpp solution to the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; char ss[333333]; string s; int n, tt, sum(0), dem[255]; bool flag(true); int main() { cin >> n; cin >> ss; if (n % 2 == 1) flag = false; for (int i = 0; i < n; i++) { ss[i] == '(' ? ++dem[40] : ss[i] == ')' ? ++dem[41] : 0; } if (dem[40] > n / 2 || dem[41] > n / 2) flag = false; dem[40] = n / 2 - dem[40]; dem[41] = n / 2 - dem[41]; for (int i = 0; i < n; i++) { if (ss[i] == '?') { if (dem[40] > 0) { ss[i] = 40; --dem[40]; } else ss[i] = ')'; } } for (int i = 0; i < n; i++) { ss[i] == '(' ? ++sum : --sum; if (sum <= 0 && i != n - 1) flag = false; } if (flag) for (int i = 0; i < n; i++) cout << ss[i]; else cout << ":("; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; string a; cin >> a; if (n & 1) { cout << ":("; return 0; } int cnt1 = 1; int cnt2 = 1; if (a[0] == ')') { cout << ":("; return 0; } a[0] = '('; if (a.back() == '(') { cout << ":("; return 0; } a[a.size() - 1] = ')'; for (int i = 1; i < a.size(); ++i) if (a[i] == '(') ++cnt1; if (cnt1 > n / 2) { cout << ":("; return 0; } for (int i = 0; i < a.size(); ++i) { if (a[i] == '?') { ++cnt1; a[i] = '('; } if (cnt1 == n / 2) break; } if (cnt1 < n / 2) { cout << ":("; return 0; } for (int i = 0; i < a.size(); ++i) if (a[i] == '?') a[i] = ')'; stack<int> s; for (int i = 0; i < a.size(); ++i) { if (a[i] == '(') s.push(a[i]); else { if (s.empty()) { cout << ":("; return 0; } else { s.pop(); if (s.empty() && i != a.size() - 1) { cout << ":("; return 0; } } } } if (!s.empty()) { cout << ":("; return 0; } for (int i = 0; i < a.size(); ++i) cout << a[i]; return 0; }
### Prompt Create a solution in cpp for the following problem: Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that |s| as the length of string s. A strict prefix s[1... l] (1≀ l< |s|) of a string s = s_1s_2... s_{|s|} is string s_1s_2... s_l. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string s containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in s independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer |s| (1≀ |s|≀ 3 β‹… 10^5), the length of the string. The second line contains a string s, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). Examples Input 6 (????? Output (()()) Input 10 (???(???(? Output :( Note It can be proved that there is no solution for the second sample, so print ":(". ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; string a; cin >> a; if (n & 1) { cout << ":("; return 0; } int cnt1 = 1; int cnt2 = 1; if (a[0] == ')') { cout << ":("; return 0; } a[0] = '('; if (a.back() == '(') { cout << ":("; return 0; } a[a.size() - 1] = ')'; for (int i = 1; i < a.size(); ++i) if (a[i] == '(') ++cnt1; if (cnt1 > n / 2) { cout << ":("; return 0; } for (int i = 0; i < a.size(); ++i) { if (a[i] == '?') { ++cnt1; a[i] = '('; } if (cnt1 == n / 2) break; } if (cnt1 < n / 2) { cout << ":("; return 0; } for (int i = 0; i < a.size(); ++i) if (a[i] == '?') a[i] = ')'; stack<int> s; for (int i = 0; i < a.size(); ++i) { if (a[i] == '(') s.push(a[i]); else { if (s.empty()) { cout << ":("; return 0; } else { s.pop(); if (s.empty() && i != a.size() - 1) { cout << ":("; return 0; } } } } if (!s.empty()) { cout << ":("; return 0; } for (int i = 0; i < a.size(); ++i) cout << a[i]; return 0; } ```