text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; char s[10005]; int a[105]; int main() { gets(s); int plus = 1, minus = 0, n; vector<string> signs; for (char *p = strtok(s, " "); p; p = strtok(NULL, " ")) { if (!strcmp(p, "+")) { plus++; signs.push_back(p); } else if (!strcmp(p, "-")) { minus++; signs.push_back(p); } else if (p[0] >= '1' && p[0] <= '9') n = stoi(p); } if (plus - minus * n <= n && plus * n - minus >= n) { printf("Possible\n"); a[1] = n; int sum = n; for (int i = 0; i < signs.size(); i++) { a[i + 2] = signs[i] == "+" ? n : 1; sum += a[i + 2] * (signs[i] == "+" ? 1 : -1); } for (int i = 1; i < signs.size() + 2; i++) { if ((i == 1 || signs[i - 2] == "+")) { a[i] = max(1, a[i] - (sum - n)); sum -= (n - a[i]); if (sum == n) break; } else { a[i] = min(n, a[i] + (sum - n)); sum -= (a[i] - 1); if (sum == n) break; } } printf("%d ", a[1]); for (int i = 2; i < signs.size() + 2; i++) { printf("%s %d ", signs[i - 2].c_str(), a[i]); } printf("= %d", n); } else printf("Impossible"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); int pos = 0; int neg = 0; int i, j; int n = (int)s.size(); int val = 0; int count = 0; for (i = 0; i < n; i++) { if (s[i] == '+') { pos++; } if (s[i] == '-') { neg++; } if (s[i] == '?') count++; if (s[i] >= '0' && s[i] <= '9') { while (s[i] >= '0' && s[i] <= '9') { val = (val * 10) + s[i] - '0'; i++; } } } stack<int> s_pos, s_neg; int pos_val = 0, neg_val = 0; ; while (pos != neg) { if (pos > neg) { int tm = 0; if (neg > 0) tm = 1; int cou = min(val, pos - neg + tm); if (neg > 0) { s_neg.push(cou); neg_val += cou; neg--; } while (cou > 0) { s_pos.push(1); pos_val++; pos--; cou--; } } else { int tm = 0; if (pos > 0) tm = 1; int cou = min(val, neg - pos + tm); if (pos > 0) { s_pos.push(cou); pos_val += cou; pos--; } while (cou > 0) { s_neg.push(1); neg_val++; neg--; cou--; } } } int val2 = val - pos_val + neg_val; if (val2 < 1 || val2 > val) { cout << "Impossible"; return 0; } cout << "Possible\n"; for (i = 0; i < n; i++) { if (s[i] == '?') { cout << val2; i++; break; } else { cout << s[i]; } } int turn = 0; for (; i < n; i++) { if (s[i] == '?') { if (turn > 0 && !s_pos.empty()) { cout << s_pos.top(); s_pos.pop(); } else if (turn < 0 && !s_neg.empty()) { cout << s_neg.top(); s_neg.pop(); } else cout << "1"; } else { cout << s[i]; } if (s[i] == '+') { turn = 1; } if (s[i] == '-') { turn = -1; } } }
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; template <typename T> void print(string name, vector<T>& v) { if (name.size()) cout << name << ": "; int s = (int)v.size(); for (int i = (int)(0); i < (int)(s); ++i) cout << v[i] << " "; cout << endl; } template <typename T> void print(vector<T>& v) { print("", v); } int main() { int test_cases = 1; ios::sync_with_stdio(false); for (int test_case = (int)(0); test_case < (int)(test_cases); ++test_case) { long long x[2] = {0, 0}; long long n; string s; getline(cin, s); int ix = 0; for (int i = 0; i < (int)s.size(); i++) { char c = s[i]; if (c == ' ') continue; if (c == '=') { string sv = s.substr(i + 2); stringstream ss; ss << sv; ss >> n; break; } if (c == '+') ix = 0; else if (c == '-') ix = 1; else if (c == '?') x[ix]++; } long long a = x[0], b = x[0] * n; long long c = n + x[1], d = n + x[1] * n; if (d < a || b < c) cout << "Impossible" << endl; else { cout << "Possible" << endl; long long u = max(a, c); vector<long long> res[2]; for (int k = (int)(0); k <= (int)(1); ++k) { res[k] = vector<long long>(x[k]); long long cnt = u; if (k == 1) cnt = u - n; int ki = 0; while (cnt) { res[k][ki]++; cnt--; ki++; ki %= x[k]; } } int ix = 0; long long cx[2] = {0, 0}; string s2; for (int i = 0; i < (int)s.size(); i++) { char c = s[i]; if (c != '?') s2.push_back(c); if (c == '+') ix = 0; else if (c == '-') ix = 1; else if (c == '?') { if (ix == 0) { stringstream ss; ss << res[0][cx[0]]; s2 += ss.str(); cx[0]++; } else { stringstream ss; ss << res[1][cx[1]]; s2 += ss.str(); cx[1]++; } } } cout << s2 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; char a[1000]; int b[1000]; int main() { gets(a); int len = strlen(a); int cnt = 1; int pos; int now = 1; b[0] = 1; for (int i = 0; i < len; i++) { if (a[i] == '+') { b[cnt++] = 1; now++; } if (a[i] == '-') { b[cnt++] = -1; now--; } if (a[i] == '=') { pos = i; break; } } int n = 0; for (int i = pos + 1; i < len; i++) if (a[i] <= '9' && a[i] >= '0') n = n * 10 + a[i] - '0'; for (int i = 0; i < cnt; i++) { while (now < n && b[i] < n && b[i] > 0) now++, b[i]++; while (now > n && b[i] > -n && b[i] < 0) now--, b[i]--; } if (now != n) { printf("Impossible\n"); return 0; } printf("Possible\n"); int j = 0; for (int i = 0; i < len; i++) { if (a[i] != '?') printf("%c", a[i]); else printf("%d", abs(b[j++])); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; v.push_back(1); while (1) { char a, b; cin >> a >> b; if (b == '+') v.push_back(1); else if (b == '-') v.push_back(-1); else break; } int n; cin >> n; int l = 0, r = 0; for (int i = 0; i < v.size(); i++) { if (v[i] == 1) { l++; r += n; } else { l -= n; r--; } } if (n >= l && n <= r) { cout << "Possible" << endl; } else { cout << "Impossible" << endl; return 0; } int N = n; vector<int> ans; for (int i = v.size() - 1; i >= 0; i--) { if (v[i] == 1) { l--; r -= n; int nN = max(l, N - n); ans.push_back(N - nN); N = nN; } else { l += n; r++; int nN = min(r, N + n); ans.push_back(nN - N); N = nN; } } reverse(ans.begin(), ans.end()); cout << ans[0] << " "; for (int i = 1; i < ans.size(); i++) { if (v[i] == 1) cout << "+ "; else cout << "- "; cout << ans[i] << " "; } cout << "= "; cout << n << endl; }
#include <bits/stdc++.h> using namespace std; const int INF = (-1u) / 2; const long long int INF2 = (-1ull) / 2; int a, b, i, d[1011000], j, k, n, m, timer = 0, l, r, x, y; int c[1011000], cnt = 0, fl = 0, a2, a3 = -1000000, ans = 0; void deb(bool a) { if (a) return; printf("PROGRAM TERMINATED\n"); exit(0); } char prevv, nw; vector<int> v; int main() { prevv = '+'; cin >> nw; x++; v.push_back(1); while (1) { cin >> prevv; if (prevv == '=') break; cin >> nw; if (prevv == '+') v.push_back(1), x++; else v.push_back(-1), y++; } cin >> n; if (!((1 * x - n * y) <= n && n <= (n * x - 1 * y))) { cout << "Impossible\n"; return 0; } ans = 0; cout << "Possible\n"; for (i = 0; i < v.size(); i++) { if (v[i] == 1) x--; else y--; for (j = 1; j <= n; j++) { if (((1 * x - n * y) <= (n - (ans + v[i] * j)) && (n - (ans + v[i] * j)) <= (n * x - 1 * y))) break; } ans += (v[i] * j); if (i == 0) cout << j << " "; else if (v[i] == 1) cout << "+ " << j << " "; else if (v[i] == -1) cout << "- " << j << " "; } cout << "= " << n << endl; return 0; }
#include <bits/stdc++.h> using namespace std; pair<int, int> solve(int positive, int negative, int number) { int commonValue = (number + negative) / positive; int mod = (number + negative) % positive; return make_pair(commonValue, mod); } int main() { ios_base::sync_with_stdio(0); int sign = 1; char c; int number; int positive = 1, negative = 0; string line; while (cin >> c) { if (c == '+') { sign = 1; positive++; } else if (c == '-') { sign = -1; negative++; } else if (c == '=') { cin >> number; } line += c; } bool impossible = true; pair<int, int> res = solve(positive, negative, number); if (res.first > 0 && res.first + (res.second ? 1 : 0) <= number) { cout << "Possible\n"; sign = 1; int commonValue = res.first; int mod = res.second; for (int i = 0; i < line.size(); ++i) { if (line[i] == '?') { if (sign == 1) { cout << (commonValue + (mod ? 1 : 0)) << " "; mod = max(0, --mod); } else cout << "1 "; } else { if (line[i] == '+') sign = 1; else if (line[i] == '-') sign = -1; cout << line[i] << " "; } } cout << number << "\n"; return 0; } if (negative == 0) { cout << "Impossible\n"; return 0; } res = solve(negative, positive, -number); if (res.first > 0 && res.first + (res.second ? 1 : 0) <= number) { cout << "Possible\n"; sign = 1; int commonValue = res.first; int mod = res.second; for (int i = 0; i < line.size(); ++i) { if (line[i] == '?') { if (sign == -1) { cout << (commonValue + (mod ? 1 : 0)) << " "; mod = max(0, --mod); } else cout << "1 "; } else { if (line[i] == '+') sign = 1; else if (line[i] == '-') sign = -1; cout << line[i] << " "; } } cout << number << "\n"; return 0; } cout << "Impossible\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } char op, s[10]; int adfasdfsa, asdfasfda, n, m, np[110], nm[110], cr[110]; int main() { op = '+'; while (1) { if (op == '+') adfasdfsa++, cr[m] = 0; else asdfasfda++, cr[m] = 1; m++; scanf("%s", s); scanf("%s", s); op = s[0]; if (op == '=') break; } scanf("%d", &n); if (adfasdfsa - asdfasfda * n > n || adfasdfsa * n - asdfasfda < n) { puts("Impossible"); return 0; } puts("Possible"); for (int i = 0; i < adfasdfsa; i++) np[i] = 1; for (int i = 0; i < asdfasfda; i++) nm[i] = n; int c = n - (adfasdfsa - asdfasfda * n); for (int i = 0; i < adfasdfsa; i++) { int delta = min(c, n - np[i]); np[i] += delta; c -= delta; } for (int i = 0; i < asdfasfda; i++) { int delta = min(c, nm[i] - 1); nm[i] -= delta; c -= delta; } int vp = 0, vm = 0; for (int i = 0; i < m; i++) { if (cr[i] == 0) printf("%d ", np[vp++]); else printf("%d ", nm[vm++]); if (i == m - 1) printf("= "); else if (cr[i + 1] == 0) printf("+ "); else printf("- "); } printf("%d\n", n); }
#include <bits/stdc++.h> using namespace std; long long int n, t; string s; static long long int st[10000002], l = 0; long long int sum[2] = {0}; int main() { getline(cin, s); if (s[0] == '-') { sum[1] = 1; st[l++] = -1; } else { sum[0] = 1; st[l++] = 1; } long long int pre = 0; for (int i = 1; s.length(); i++) { if (s[i] == '+') { st[l++] = 1; sum[0]++; } else if (s[i] == '-') { st[l++] = -1; sum[1]++; } if (s[i] >= '0' and s[i] <= '9') { while (s[i] >= '0' and s[i] <= '9') { pre *= 10; pre += (s[i] - '0'); i++; } break; } } if (sum[0] * pre - sum[1] >= pre and sum[0] - sum[1] * pre <= pre) { puts("Possible\n"); long long int ansA, ansB, am; for (int i = 1; i <= pre; i++) { long long int PRE = sum[1] * i + pre; long long int pre1 = PRE / sum[0]; long long int pre2 = PRE / sum[0] + !!(PRE % sum[0]); if (pre2 >= 1 and pre2 <= pre and pre1 >= 1 and pre1 <= pre) { ansA = pre1; ansB = i; break; } } long long int cnt = 0; for (int i = 0; i < l; i++) { if (st[i] > 0) { st[i] *= ansA; cnt += st[i]; } else st[i] *= ansB; } cnt = sum[1] * ansB + pre - sum[0] * ansA; for (int i = 0; i < l; i++) { if (st[i] > 0) { if (cnt > 0) { st[i]++; cnt--; } } } printf("%lld", st[0]); for (int i = 1; i < l; i++) { printf(" "); if (st[i] >= 0) printf("+"); else printf("-"); printf(" "); printf("%lld", abs(st[i])); } printf(" = %lld\n", pre); } else puts("Impossible"); return 0; }
#include <bits/stdc++.h> using namespace std; int n, i = 0, p = 1, m = 0; int s[107]; void read() { s[0] = 1; while (1) { char a, b; scanf(" %c %c", &a, &b); if (b == '+') s[++i] = 1, p++; if (b == '-') s[++i] = -1, m++; if (b == '=') break; } scanf("%d", &n); } void solve() { if (p - n * m <= n && n <= n * p - m) { puts("Possible"); int sum = 0; for (int j = 1; j <= i; j++) { s[j - 1] == 1 ? p-- : m--; for (int l = 1; l <= n; l++) { int v = l * s[j - 1]; if (sum + v + p - n * m <= n && n <= sum + v + n * p - m) { printf("%d %c ", s[j - 1] * v, s[j] == 1 ? '+' : '-'); sum += v; break; } } } printf("%d = %d", max(n - sum, sum - n), n); } else puts("Impossible"); } int main() { read(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int positives, negatives; string qmark, op; int goal; vector<int> positive; vector<int> negative; string stuff[101]; long long int diff; int n; void subtractpositives() { for (int i = 0; i < positive.size(); i++) { if (diff >= n - 1) { positive[i] = 1; diff -= (n - 1); } else if (diff > 0) { positive[i] -= diff; diff = 0; } } } void addnegatives() { for (int i = 0; i < negative.size(); i++) { if (diff >= n - 1) { negative[i] = n; diff -= (n - 1); } else if (diff > 0) { negative[i] += diff; diff = 0; } } } int main() { int cnter = 0; positives = 1; cin >> qmark; while (cin >> op >> qmark) { if (op == "=") break; if (op == "+") positives++; else negatives++; stuff[cnter] = op; cnter++; } goal = stoi(qmark); n = goal; if (goal > positives * goal - negatives || goal < (-negatives * goal) + positives) cout << "Impossible" << endl; else { cout << "Possible" << endl; long long int sum = n * positives - negatives; for (int i = 0; i < positives; i++) { positive.push_back(n); } for (int i = 0; i < negatives; i++) { negative.push_back(1); } diff = sum - goal; subtractpositives(); addnegatives(); int curpositive = 1; int curnegative = 0; cout << positive[0] << " "; for (int i = 0; i < cnter; i++) { if (stuff[i] == "-") { cout << "- " << negative[curnegative] << ' '; curnegative++; } if (stuff[i] == "+") { cout << "+ " << positive[curpositive] << ' '; curpositive++; } } cout << "= " << goal << '\n'; } }
#include <bits/stdc++.h> using namespace std; int main() { int cnt = 0, num1 = 0, num2 = 0; char s[5], ss[105]; while (~scanf("%s", s)) { if (s[0] == '=') break; else if (s[0] == '+') { num1++; ss[cnt++] = s[0]; } else if (s[0] == '-') { num2++; ss[cnt++] = s[0]; } } num1++; int n; scanf("%d", &n); int ans[105] = {0}, sum[105] = {0}; if (n >= num1 - num2 * n && n <= num1 * n - num2) { printf("Possible\n"); if (num1 - num2 > n) { int tot = (num1 - n) / num2; for (int i = 0; i < num1; i++) ans[i] = 1; for (int i = 0; i < num2; i++) { if (i < (num1 - n) % num2) sum[i] = tot + 1; else sum[i] = tot; } } else { int tot = (num2 + n) / num1; for (int i = 0; i < num2; i++) sum[i] = 1; for (int i = 0; i < num1; i++) { if (i < (num2 + n) % num1) ans[i] = tot + 1; else ans[i] = tot; } } printf("%d ", ans[0]); int c1 = 1, c2 = 0; for (int i = 0; i < cnt; i++) { printf("%c ", ss[i]); if (ss[i] == '+') printf("%d ", ans[c1++]); else printf("%d ", sum[c2++]); } printf("= %d\n", n); } else { printf("Impossible\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 110; int pos, neg, n; int sz; int vpos[1010]; void solve(int val) { cout << "Possible\n"; int posr = val; int negr = val - n; bool first = true; for (int i = 0; i < (sz); ++i) { if (vpos[i] == 1) { int x = min(min(n, posr - pos + 1), posr); posr -= x; if (!first) cout << " + "; else first = false; cout << " " << x; --pos; } else { int x = min(min(n, negr - neg + 1), negr); negr -= x; cout << " " << -x; --neg; } } cout << " = " << n << endl; } int main() { ios::sync_with_stdio(false); ; int sign = 1; char c; while (cin >> c && c != '=') { if (c == '?') { if (sign == 1) ++pos; else ++neg; vpos[sz++] = sign; } else { if (c == '+') sign = 1; else sign = -1; } } cin >> n; int r = n + neg; int s = n + n * neg; int p = pos; int q = n * pos; if (s >= p && s <= q) solve(s); else if (r >= p && r <= q) solve(r); else cout << "Impossible\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char c, a[105]; int x = 0, p = 1, n = 0, k; while (c != '=') { cin >> c; if (c == '+') { a[x++] = c; p++; } else if (c == '-') { a[x++] = c; n++; } } cin >> k; if (k > k * p - n || k < p - n * k) cout << "Impossible" << endl; else { cout << "Possible" << endl; int S = 0; for (int i = 0; i < x; i++) { int sign = 1; if (i > 0 && a[i - 1] == '-') sign = -1; if (sign == 1) p--; if (sign == -1) n--; for (int j = 1; j <= k; j++) { if (S + j * sign + p - n * k <= k && S + j * sign + p * k - n >= k) { cout << j << " " << a[i] << " "; S = S + j * sign; break; } } } cout << abs(k - S) << " " << "=" << " " << k << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n; vector<char> sign; int main() { int pos = 1; int neg = 0; scanf(" %*c"); sign.push_back('+'); while (true) { char c; scanf(" %c", &c); if (c == '=') break; if (c == '+') pos += 1; if (c == '-') neg += 1; sign.push_back(c); scanf(" %*c"); } scanf("%d", &n); int high = pos * n - neg * 1; int low = pos * 1 - neg * n; if (n < low || high < n) return printf("Impossible\n"), 0; printf("Possible\n"); int ext = high - n; bool first = 1; for (char c : sign) { if (!first) printf("%c ", c); if (c == '+') printf("%d ", max(1, n - ext)); else printf("%d ", min(n, 1 + ext)); ext = max(0, ext - n + 1); first = 0; } printf("= %d\n", n); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<int> v; v.push_back(+1); for (;;) { char c; scanf(" %c", &c); if (c == '=') break; if (c == '+') v.push_back(+1); else if (c == '-') v.push_back(-1); } scanf("%d", &n); int ans = 0; for (auto x : v) if (x == +1) ans += n; else ans -= 1; if (ans < n) { puts("Impossible"); return 0; } vector<int> p; for (auto x : v) { if (x == +1 and ans > n) { int mn = min(ans - n, n - 1); p.push_back(n - mn); ans -= mn; } else if (x == -1 and ans > n) { int mn = min(ans - n, n - 1); p.push_back(mn + 1); ans -= mn; } else { p.push_back(x == +1 ? n : 1); } } if (ans != n) { puts("Impossible"); return 0; } puts("Possible"); printf("%d ", p[0]); for (int i = 1; i < p.size(); i++) { printf("%c %d ", (v[i] == +1 ? '+' : '-'), p[i]); } printf("= %d\n", n); return 0; }
#include <bits/stdc++.h> using namespace std; int get(string s) { int res = 0, pw = 1; while (!s.empty()) { res += (s.back() - '0') * pw; pw *= 10; s.pop_back(); } return res; } int main() { string s; getline(cin, s); vector<char> vec; for (char c : s) { if (c == ' ') { continue; } else { vec.push_back(c); } } string sn = ""; bool flag = 0; for (char c : vec) { if (c == '=') { flag = 1; continue; } if (flag) { sn += c; } } int n = get(sn); int mn = 0, mx = 0; vector<int> small, big; for (int i = 0; i < vec.size(); i++) { if (vec[i] == '=') { break; } if (i == 0) { mn = 1, mx = n; big.push_back(n); small.push_back(1); continue; } if (vec[i] == '?') { if (vec[i - 1] == '-') { mx -= 1; mn -= n; big.push_back(1); small.push_back(n); } if (vec[i - 1] == '+') { mx += n; mn += 1; big.push_back(n); small.push_back(1); } } } if (mn <= n && n <= mx) { puts("Possible"); int id = 0; for (int i = 0; i < vec.size(); i++) { if (i == 0) { if (mn < n) { int x = min(n - small[id], n - mn); mn += x; small[id] += x; } id++; continue; } if (vec[i] == '?') { if (mn < n) { if (vec[i - 1] == '-') { int x = min(small[id] - 1, n - mn); mn += x; small[id] -= x; } if (vec[i - 1] == '+') { int x = min(n - small[id], n - mn); mn += x; small[id] += x; } } id++; } } id = 0; for (char c : vec) { if (c == '?') { cout << small[id] << " "; id++; } else { cout << c << " "; if (c == '=') { cout << n; break; } } } } else { puts("Impossible"); } }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:134217728") using namespace std; char buf[2]; vector<int> sign; int n; int main() { sign.push_back(1); scanf("%s", buf); while (1) { scanf("%s", buf); if (buf[0] == '=') break; sign.push_back(buf[0] == '+' ? 1 : -1); scanf("%s", buf); } scanf("%d", &n); int MIN = 0; int MAX = 0; vector<int> res; for (int i = 0; i < sign.size(); ++i) { if (sign[i] == 1) { MIN++; MAX += n; res.push_back(n); } else { MIN -= n; MAX--; res.push_back(1); } } if (MIN <= n && n <= MAX) { printf("Possible\n"); int need = MAX - n; for (int i = 0; i < res.size(); ++i) { if (sign[i] == 1) { int d = min(need, res[i] - 1); res[i] -= d; need -= d; } else { int d = min(need, n - res[i]); res[i] += d; need -= d; } } printf("%d", res[0]); for (int i = 1; i < res.size(); ++i) { if (sign[i] == 1) printf(" + "); else printf(" - "); printf("%d", res[i]); } printf(" = %d\n", n); } else printf("Impossible\n"); return 0; }
#include <bits/stdc++.h> using namespace std; inline int get() { char ch = getchar(); int res = 1; while (ch != '?') { if (ch == '-') res = -1; if (ch == '=') return 0; ch = getchar(); } return res; } inline int read() { char ch = '*'; int f = 1; while (!isdigit(ch = getchar())) if (ch == '-') f = -1; int num = ch - '0'; while (isdigit(ch = getchar())) num = num * 10 + ch - '0'; return num * f; } const int N = 1e6 + 10; int op[N], sum, p, ip, n; int now; int main() { while (op[++n] = get()) { if (op[n] > 0) p++; else ip++; now += op[n]; } sum = read(); if (!ip && p > sum) { puts("Impossible"); return 0; } if (!p && sum > -ip) { puts("Impossible"); return 0; } for (register int i = 1; i <= n; i++) if (op[i]) { while (now < sum && op[i] < sum && op[i] > 0) now++, op[i]++; while (now > sum && op[i] > -sum && op[i] < 0) now--, op[i]--; } if (now != sum) { puts("Impossible"); return 0; } puts("Possible"); for (register int i = 1; i <= n; i++) if (op[i] != 0) { cout << abs(op[i]) << " "; if (op[i + 1] > 0) cout << "+ "; else if (op[i + 1] < 0) cout << "- "; else cout << "= "; } cout << now << endl; }
#include <bits/stdc++.h> using namespace std; int main() { char ch = 1; vector<int> v; v.push_back(1); while (ch != '=') { scanf(" %c", &ch); if (ch == '?') continue; if (ch == '+') v.push_back(1); if (ch == '-') v.push_back(-1); } int n; scanf("%d", &n); int men = 0, mai = 0; for (int i = 0; i < v.size(); i++) { if (v[i] == 1) men++, mai += n; else men -= n, mai--; }; if (men > n || mai < n) { printf("Impossible\n"); return 0; } printf("Possible\n"); for (int i = 0; i < v.size(); i++) { if (i) { if (v[i] == 1) printf(" + "); else printf(" - "); } int dif = mai - n; int u = min(dif, n - 1); mai -= u; if (v[i] == 1) printf("%d", n - u); else printf("%d", 1 + u); } printf(" = %d\n", n); }
#include <bits/stdc++.h> using namespace std; char op[200], a[200]; int n; int toInt(string x) { int ret = 0; for (int i = 0; i < x.size(); i++) { ret = ret * 10 + (x[i] - '0'); } return ret; } int main() { int cnt = 0, cnt2 = 0; cnt++; string x, y; cin >> x; int al = 0; while (cin >> x >> y) { if (x == "=") { n = toInt(y); break; } op[al++] = x[0]; if (x == "-") cnt2++; else cnt++; } int cur = n; vector<int> positive, negative; for (int i = 0; i < cnt; i++) positive.push_back(n), cur -= n; for (int i = 0; i < cnt2; i++) negative.push_back(n), cur += n; if (cur < 0) { for (int i = 0; i < cnt && cur < 0; i++) { if (cur + (n - 1) <= 0) cur += n - 1, positive[i] = 1; else positive[i] -= -cur, cur += -cur; } } else if (cur > 0) { for (int i = 0; i < cnt2 && cur > 0; i++) { if (cur - (n - 1) >= 0) cur -= n - 1, negative[i] = 1; else negative[i] -= cur, cur -= cur; } } if (cur != 0) { cout << "Impossible\n"; return 0; } cout << "Possible\n"; int i = 1, j = 0; cout << positive[0]; for (int k = 0; k < al; k++) { cout << " " << op[k] << " "; if (op[k] == '-') cout << negative[j++]; else cout << positive[i++]; } cout << " = " << n << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; char a[105][5], b[105][5]; int p = 0, q = 0, n, num = 1; void init() { int i = 1; scanf("%s", a[0]); a[0][0] = '+'; while (scanf("%s", a[i])) { if (a[i][0] == '=') { scanf("%d", &n); return; } scanf("%s", b[i]); num++; if (a[i][0] == '+') p++; else q++; i++; } } void work() { if (p == 0 && q != 0) puts("Impossible"); else if (p == 0 && q == 0) printf("Possible\n%d = %d", n, n); else if (p + 1 > n && q == 0) puts("Impossible"); else if (p + 1 <= n && q == 0) { puts("Possible"); printf("%d ", n - p); for (int i = 2; i <= num; i++) printf("%c 1 ", a[i - 1][0]); printf("= %d\n", n); } else if (p - q * n >= n || n * p - q < 0) puts("Impossible"); else if (p > q) { int x = p - q; puts("Possible"); if (p - q * n <= 0) { printf("%d ", n); for (int i = 2; i <= num; i++) { if (a[i - 1][0] == '+') printf("%c 1 ", a[i - 1][0]); else { if (x > 0) { printf("%c %d ", a[i - 1][0], min(x + 1, n)); x -= min(x, n - 1); } else printf("%c 1 ", a[i - 1][0]); } } } else { printf("%d ", n - (p - q * n)); for (int i = 2; i <= num; i++) { if (a[i - 1][0] == '+') printf("%c 1 ", a[i - 1][0]); else { if (x > 0) { printf("%c %d ", a[i - 1][0], min(x + 1, n)); x -= min(x, n - 1); } else printf("%c 1 ", a[i - 1][0]); } } } printf("= %d\n", n); } else if (p < q) { int x = q - p; puts("Possible"); printf("%d ", n); for (int i = 2; i <= num; i++) { if (a[i - 1][0] == '-') printf("%c 1 ", a[i - 1][0]); else { if (x > 0) { printf("%c %d ", a[i - 1][0], min(x + 1, n)); x -= min(x, n - 1); } else printf("%c 1 ", a[i - 1][0]); } } printf("= %d\n", n); } else { puts("Possible"); printf("%d ", n); for (int i = 2; i <= num; i++) printf("%c 1 ", a[i - 1][0]); printf("= %d\n", n); } } int main() { init(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-6; const int mod = 1e9 + 7; const int maxn = 1010; int n; int a, b; int op[maxn]; int main() { int p = 0; a = 1; while (1) { char c; cin >> c; if (c == '+') { a++; op[++p] = 1; } else if (c == '-') { b++; op[++p] = -1; } if (c == '=') { cin >> n; break; } } if (n < a - b * n || n > a * n - b) puts("Impossible"); else { cout << "Possible" << endl; int last = n + max(b, a - n); int last1 = max(b, a - n); int lastcnt = a; int lastcnt1 = b; int ans = n; int ans1 = n; if (last - n >= lastcnt - 1) { cout << n; last -= n; } else { ans = last - (lastcnt - 1); cout << ans; last -= ans; } lastcnt--; for (int i = 1; i <= p; i++) { if (op[i] == 1) { cout << " + "; if (last - n >= lastcnt - 1) { cout << n; last -= n; } else { ans = last - (lastcnt - 1); cout << ans; last -= ans; } lastcnt--; } else { cout << " - "; if (last1 - n >= lastcnt1 - 1) { cout << n; last1 -= n; } else { ans1 = last1 - (lastcnt1 - 1); cout << ans1; last1 -= ans1; } lastcnt1--; } } cout << " = " << n << endl; } }
#include <bits/stdc++.h> using namespace std; int n, neg = 0; char s[1009]; int main() { vector<int> ans; scanf("%[^=]=%d", s, &n); ans.push_back(n); for (int i = 1; s[i]; i++) { if (s[i] == ' ') continue; if (s[i] == '-') neg = 1; else if (s[i] == '+') neg = 0; else if (s[i] == '?' && !neg) { ans[0] -= 1; ans.push_back(1); } else if (s[i] == '?') { int c = 0; ans[0] += 1; ans.push_back(-1); } } int c = 1; while (ans[0] > n && c < ans.size()) { if (ans[c] > 0 && ans[c] < n) { ans[0]--; ans[c]++; } else c++; } c = 1; while (ans[0] <= 0 && c < ans.size()) { if (ans[c] < 0 && abs(ans[c]) < n) { ans[0]++; ans[c]--; } else c++; } if (ans[0] <= 0 || ans[0] > n) { printf("Impossible\n"); return 0; } printf("Possible\n"); printf("%d", ans[0]); for (int i = 1; i < ans.size(); i++) { if (ans[i] > 0) printf(" + %d", ans[i]); else printf(" - %d", abs(ans[i])); } printf(" = %d\n", n); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int MOD = 1e9 + 7; const int N = 1e2 + 10; int n, m, a[N], ans[N]; string s; void prepare() { n = 1; a[1] = 1; while (1) { cin >> s; if (s[0] == '+') a[++n] = 1; else if (s[0] == '-') a[++n] = -1; else if (s[0] == '?') continue; else break; } cin >> m; } void solve() { int nposi = count(a + 1, a + n + 1, 1), nneg = n - nposi; int L = nposi - m * nneg, R = nposi * m - nneg; if (L > m || R < m) { printf("Impossible"); return; } printf("Possible\n"); for (auto i = (1); i <= (n); i++) ans[i] = (a[i] == 1) ? 1 : m; int rem = m - L; for (auto i = (1); i <= (n); i++) { int v = min(m - 1, rem); ans[i] += a[i] * v; rem -= v; } printf("%d ", ans[1]); for (auto i = (2); i <= (n); i++) printf("%c %d ", (a[i] == 1) ? '+' : '-', ans[i]); printf("= %d", m); } int main() { prepare(); solve(); }
#include <bits/stdc++.h> using namespace std; int a[110], b[110]; char s[10]; int n, m, cnt, num; void calc() { n = 0; int len = strlen(s); for (int i = 0; i < len; i++) n = n * 10 + s[i] - '0'; } int main() { int cnt = 1; b[1] = 0; while (scanf("%s", s) != EOF) { if (s[0] == '?') cnt++; else if (s[0] == '+') b[cnt] = 0; else if (s[0] == '-') b[cnt] = 1, num++; else if (s[0] != '=') calc(); } cnt--; m = n + num; if (num == 0 && cnt > m) { printf("Impossible\n"); return 0; } int av = m / (cnt - num), q = m % (cnt - num), t = 0, Cnt = 0, k = 0; if (av == 0) Cnt = cnt - num - q; if (Cnt && 1 + (Cnt % num == 0 ? Cnt / num : Cnt / num + 1) > n) { printf("Impossible\n"); return 0; } for (int i = 1; i <= cnt; i++) if (b[i] == 1) { if (!Cnt) a[i] = 1; else if (k < Cnt % num) a[i] = 1 + (Cnt % num == 0 ? Cnt / num : Cnt / num + 1), k++; else a[i] = 1 + Cnt / num; } else { if (t < q) a[i] = av + 1, t++; else if (av == 0) a[i] = 1; else a[i] = av; } for (int i = 1; i <= cnt; i++) if (a[i] > n) { printf("Impossible\n"); return 0; } printf("Possible\n"); printf("%d", a[1]); for (int i = 2; i <= cnt; i++) { printf(" "); if (b[i] == 0) printf("+"); else printf("-"); printf(" %d", a[i]); } printf(" = %d\n", n); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1010101; inline char get(void) { static char buf[100000], *S = buf, *T = buf; if (S == T) { T = (S = buf) + fread(buf, 1, 100000, stdin); if (S == T) return EOF; } return *S++; } template <typename T> inline void read(T &x) { static char c; x = 0; int sgn = 0; for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1; for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0'; if (sgn) x = -x; } inline char Get(void) { static char c; for (c = get(); c != '+' && c != '-' && c != '?' && c != '=' && !isdigit(c); c = get()) ; return c; } int now, n, m, cnt1, cnt2, res, x; char c; char opt[N]; int ans[N]; inline void Godie(void) { puts("Impossible"); exit(0); } int main(void) { cnt1 = 1; while (1) { c = Get(); c = Get(); if (c == '+') cnt1++; else if (c == '-') cnt2++; else if (c == '=') break; opt[++m] = c; } read(n); now = cnt1 - cnt2; opt[0] = '+'; for (int i = 0; i <= m; i++) ans[i] = 1; for (int i = 0; i <= m; i++) { if (opt[i] == '+') { while (now < n && ans[i] < n) { now++; ans[i]++; } } } for (int i = 0; i <= m; i++) { if (opt[i] == '-') { while (now > n && ans[i] < n) { now--; ans[i]++; } } } if (now != n) Godie(); puts("Possible"); printf("%d ", ans[0]); for (int i = 1; i <= m; i++) printf("%c %d ", opt[i], ans[i]); printf("= %d", n); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "{" << p.first << "," << p.second << "}"; return os; } const int N = 3e5 + 5; const int oo = 1e9 + 7; int num(string s) { stringstream ss(s); int ret; ss >> ret; return ret; } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int a = 1, b = 0, n; vector<string> v; string s, t; cin >> s; assert(s == "?"); while (1) { cin >> s >> t; if (s == "+" or s == "-") { a += s == "+"; b += s == "-"; v.push_back(s); assert(t == "?"); } else { assert(s == "="); n = num(t); break; } } if (a - n * b <= n and n * a - b >= n) { cout << "Possible\n"; int now = a - b; vector<int> plas(a, 1); vector<int> minas(b, 1); for (int &x : plas) { int jog = min(max(n - now, 0), n - x); x += jog; now += jog; } for (int &x : minas) { int jog = min(max(now - n, 0), n - x); x += jog; now -= jog; } assert(now == n); a = b = 0; cout << plas[a++]; for (string s : v) { cout << " " << s << " " << (s == "+" ? plas[a++] : minas[b++]); } cout << " = " << n << "\n"; } else cout << "Impossible\n"; }
#include <bits/stdc++.h> using namespace std; int a[int(1e5 + 5)], i, m, ans, k, l, j, q, x, n, ma, mi, v, g; string s, s1, in; vector<int> pl, mn; void pr() { int P = 1, M = 0; cout << pl[0] << " "; for (int i = 1; i < k; i++) { if (a[i] == 1) { cout << "+ " << pl[P++] << " "; } else if (a[i] == -1) { cout << "- " << mn[M++] << " "; } } cout << "= " << n << endl; } int main() { getline(cin, s); v++; a[k++] = 1; for (int i = 2; i < s.size(); i += 4) { if (s[i] == '=') { i++; i++; while (i < s.size()) n = n * 10 + s[i] - '0', i++; break; } if (s[i] == '-') a[k++] = -1, g++; else a[k++] = 1, v++; } if (n * v - g < n || v - n * g > n) { cout << "Impossible" << endl; return 0; } cout << "Possible" << endl; int N = n - v + g; if (N < 0) { N *= -1; for (int i = 0; i < g; i++) { mn.push_back(1 + min(N, n - 1)); N -= min(N, n - 1); } for (int i = 0; i < v; i++) { pl.push_back(1); } } else { for (int i = 0; i < g; i++) mn.push_back(1); for (int i = 0; i < v; i++) { pl.push_back(1 + min(N, n - 1)); N -= min(N, n - 1); } } pr(); }
#include <bits/stdc++.h> using namespace std; struct Node { long long val; Node *next; }; Node *Root = NULL; void _addNodeEnd(long long n) { if (Root == NULL) { Root = new Node(); Root->next = NULL; Root->val = n; } else { Node *curNode = Root; while (curNode != NULL) curNode = curNode->next; Node *newNode = new Node(); newNode->next = NULL; newNode->val = n; curNode->next = newNode; } } struct Point { long long x; long long y; }; bool _Asc_compare(Point lhs, Point rhs) { return lhs.x < rhs.x; } bool _Desc_compare(Point lhs, Point rhs) { return lhs.x > rhs.x; } long long _unique(long long x, long long y) { return (x + (y * 100000)); } long long gcd(long long x, long long y) { if (y == 0) return x; return gcd(y, x % y); } long long a, b, c, d, e, f, g, h; long long pls = 1, mns; string st; char ch, dh; char arr[1000]; int main() { while (true) { cin >> dh >> ch; if (ch == '=') break; if (ch == '+') pls++; else if (ch == '-') mns++; arr[b++] = ch; } cin >> a; if (pls - a * mns > a || a * pls - mns < a) { cout << "Impossible" << endl; return 0; } cout << "Possible" << endl; long long s = 0; for (long long i = 0; i < b; i++) { long long sign = 1; if (i > 0 && arr[i - 1] == '-') sign = -1; if (sign == 1) pls--; if (sign == -1) mns--; for (long long j = 1; j < a + 1; j++) { if (s + j * sign + pls - a * mns <= a && s + j * sign + a * pls - mns >= a) { cout << j << " " << arr[i] << " "; s += j * sign; break; } } } cout << abs(a - s) << " = " << a << endl; }
#include <bits/stdc++.h> using namespace std; int p = 1, n, j, a[105]; char c; int main() { a[j++] = 1; while (cin >> c && c != '=') { if (c == '-') p--, a[j++] = -1; if (c == '+') p++, a[j++] = 1; } cin >> n; for (int i = 0; i < j; i++) { if (a[i] > 0) while (p < n && a[i] < n) a[i]++, p++; else while (p > n && a[i] < 0 && a[i] > -n) a[i]--, p--; } if (p != n) { cout << "Impossible\n"; return 0; } cout << "Possible\n"; for (int i = 0; i < j; i++) cout << (i ? (a[i] < 0 ? "- " : "+ ") : "") << abs(a[i]) << " "; cout << "= " << n; }
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int a, b, n; vector<char> v; int main() { int i, j, k, l; scanf("%*s"); a++; while (1) { char s[20]; scanf("%s", s); if (s[0] == '=') { scanf("%d", &n); break; } scanf("%*s"); if (s[0] == '+') a++; else b++; v.push_back(s[0]); } if (n >= a - b * n && n <= a * n - b) { printf("Possible\n"); vector<int> A, B; A.resize(a), B.resize(b); for (auto &e : A) e = n; for (auto &e : B) e = 1; int dif = a * n - b - n; for (auto &e : A) { if (dif >= n - 1) dif -= n - 1, e = 1; else e -= dif, dif = 0; } for (auto &e : B) { if (dif >= n - 1) dif -= n - 1, e = n; else e += dif, dif = 0; } printf("%d ", A.back()); A.pop_back(); for (auto e : v) { if (e == '+') { printf("+ %d ", A.back()); A.pop_back(); } else { printf("- %d ", B.back()); B.pop_back(); } } printf("= %d", n); } else printf("Impossible"); return 0; }
#include <bits/stdc++.h> using namespace std; int n, a, b, x, y, i, j, m[105]; char t, c[105]; int main() { c[0] = '+'; j = 1; while (2 > 1) { cin >> t; if (t == '=') { break; } if (t != '?') { c[j] = t; j++; } } cin >> n; while (i < j) { m[i] = 1; if (c[i] == '+') { x += m[i]; } else { x -= m[i]; } i++; } i = 0; while (x > n) { while (c[i] != '-') { i++; } while (x > n && m[i] < n) { m[i]++; x--; } i++; if (i > 101) { cout << "Impossible"; return 0; } } i = 0; while (x < n) { while (c[i] != '+') { i++; } while (x < n && m[i] < n) { m[i]++; x++; } i++; if (i > 101) { cout << "Impossible"; return 0; } } i = 0; cout << "Possible" << endl; while (i < j) { cout << m[i] << " "; if (i != j - 1) { cout << c[i + 1] << " "; } i++; } cout << "= " << n; }
#include <bits/stdc++.h> using namespace std; int main() { char ch; int sum; int p = 1, m = 0; vector<bool> signs; signs.push_back(1); while (cin >> ch) if (ch == '+') { signs.push_back(1); p++; } else if (ch == '-') { signs.push_back(0); m++; } else if (ch == '=') { cin >> sum; break; } if (sum * p - m < sum || p - m * sum > sum) { printf("Impossible\n"); return 0; } printf("Possible\n", sum); int need_p = max(sum + m - p, 0); int need_m = max(need_p + p - m - sum, 0); for (int i = 0; i < signs.size(); ++i) { if (signs[i]) { int toPut = 1; if (need_p != 0) { toPut += need_p; if (toPut > sum) toPut = sum; need_p -= (toPut - 1); } if (i) printf(" + "); printf("%d", toPut); } else { int toPut = 1; if (need_m != 0) { toPut += need_m; if (toPut > sum) toPut = sum; need_m -= (toPut - 1); } printf(" - %d", toPut); } } printf(" = %d\n", sum); return 0; }
#include <bits/stdc++.h> using namespace std; char a[1005]; int main() { char ch; int k = 0, n, pos = 1, neg = 0; while (1) { scanf(" %c", &ch); scanf(" %c", &ch); if (ch == '=') break; if (ch == '-') neg++; if (ch == '+') pos++; a[k++] = ch; } scanf("%d", &n); if (n < (pos - n * neg) || n > (n * pos - neg)) { cout << "Impossible\n"; return 0; } cout << "Possible\n"; int sum = 0; for (int i = 0; i < k; i++) { int sign = 1; if (i > 0 && a[i - 1] == '-') sign = -1; if (sign == 1) pos--; else neg--; for (int x = 1; x <= n; x++) { if ((sum + sign * x + pos - n * neg <= n) && (sum + sign * x + n * pos - neg) >= n) { cout << x << " " << a[i] << " "; sum += x * sign; break; } } } cout << abs(sum - n) << " = " << n; }
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } vector<int> vc; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vc.push_back(0); string s; while (cin >> s) { if (s == "?") continue; if (s == "=") break; if (s == "+") vc.push_back(0); if (s == "-") vc.push_back(1); } int n; cin >> n; int pos = 0, neg = 0; for (int i = (0); i < (vc.size()); ++i) { if (vc[i] == 0) pos++; else neg++; } if (n > 100) { if (pos == 1 && neg > 0) { cout << "Impossible" << endl; return 0; } cout << "Possible" << endl; cout << n << " "; pos--; for (int i = (1); i < (vc.size()); ++i) { if (pos < neg && vc[i] == 0) { int v = neg - pos + 1; cout << "+ " << v << " "; pos = neg; } else if (vc[i] == 0) { cout << "+ " << 1 << " "; } else if (neg < pos && vc[i] == 1) { int v = pos - neg + 1; cout << "- " << v << " "; neg = pos; } else if (vc[i] == 1) { cout << "- " << 1 << " "; } } cout << "= " << n << endl; return 0; } for (int i = pos; i <= pos * n; i++) { for (int j = neg; j <= neg * n; j++) { if (i - j != n) continue; cout << "Possible" << endl; int c1 = 0, c2 = 0; for (int k = (0); k < (vc.size()); ++k) { if (vc[k] == 0) { c1++; int v = i / pos; if (c1 <= (i % pos)) v++; if (k != 0) cout << "+ "; cout << v << " "; } else { c2++; int v = j / neg; if (c2 <= (j % neg)) v++; cout << "- "; cout << v << " "; } } cout << "= " << n << endl; return 0; } } cout << "Impossible" << endl; }
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)LLONG_MAX; int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; int dx8[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0}; int dy8[9] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; long long ipow(long long base, long long exp) { long long result = 1; while (exp) { if (exp & 1) result *= base; exp >>= 1; base *= base; } return result; } string tos(long long n) { stringstream ss; string ans; ss << n; ss >> ans; return ans; } long long toll(string n) { return atoll(n.c_str()); } double dist(double x1, double y1, double x2, double y2) { return sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2))); } int doubleCompare(double x, double y) { if (fabs(x - y) <= 1e-18) return 0; if (x < y) return -1; return 1; } int main() { vector<long long> ans; char tmp; long long ret = 1; while (1) { cin >> tmp; if (tmp == '=') break; if (tmp == '-') { ret--; ans.push_back(-1); } else if (tmp == '+') { ret++; ans.push_back(-2); } else { ans.push_back(1); } } long long n; cin >> n; for (int i = ans.size() - 1; i >= 0; i -= 2) { if (ret == n) break; if (i == 0) { while (ret < n && ans[i] < n) { ret++; ans[i]++; } } else { while (ret > n && ans[i - 1] == -1 && ans[i] < n) { ret--; ans[i]++; } while (ret < n && ans[i - 1] == -2 && ans[i] < n) { ret++; ans[i]++; } } } if (ret == n) { cout << "Possible\n"; for (int i = 0; i < ans.size(); i++) { if (ans[i] == -1) cout << " - "; else if (ans[i] == -2) cout << " + "; else cout << ans[i]; } cout << " = " << n; } else { cout << "Impossible"; } }
#include <bits/stdc++.h> const int MAX = 1000009; char m[MAX]; int zheng[MAX], fu[MAX]; int main() { int i, j, l, numz = 1, numf = 0, sumz, sumf, f = 1, z = 1, n = 0, t; gets(m); l = strlen(m); for (i = 0; i < l; i++) if (m[i] == '+') numz++; else if (m[i] == '-') numf++; else if (m[i] == '=') break; for (j = i + 2; j < l; j++) n = n * 10 + m[j] - '0'; if (numz * n - numf < n || numz - n * numf > n) { printf("Impossible\n"); return 0; } sumz = numz, sumf = numf; if (sumz - sumf > n) { t = sumz - sumf - n; while (t) { if (t > n - 1) { t -= (n - 1); fu[f] = n - 1; f++; } else { fu[f] = t; break; } } } else { t = n - (sumz - sumf); while (t) { if (t > n - 1) { t -= (n - 1); zheng[z] = n - 1; z++; } else { zheng[z] = t; break; } } } z = f = 1; printf("Possible\n%d ", zheng[z++] + 1); for (j = 2; j < i; j += 4) { if (m[j] == '+') printf("+ %d ", zheng[z++] + 1); else if (m[j] == '-') printf("- %d ", fu[f++] + 1); } printf("= %d\n", n); }
#include <bits/stdc++.h> const long long llinf = 0x3f3f3f3f3f3f3f3f; const int inf = 0x3f3f3f3f; using namespace std; const int maxn(5e5 + 10); string ss; int main() { getline(cin, ss, '\n'); string s = "+"; for (int i = 0; i < ss.size(); i++) { if (isspace(ss[i])) continue; s.push_back(ss[i]); } int n; int zheng(0), fu(0); int le(0), ri(0); for (int i = 0; i < s.size(); i++) { switch (s[i]) { case '+': ++i; ++zheng; break; case '-': ++i; ++fu; break; case '=': ++i; sscanf(&s[i], "%d", &n); goto Over; } } Over:; ri += zheng * n; le += zheng; le -= fu * n; ri -= fu; bool flag = (le <= n && ri >= n); cout << (flag ? "Possible" : "Impossible") << endl; if (flag) { int base0(1), base1(-n), now = zheng + (-n) * fu, left = n - now; int cut, nextout; if (left >= n - 1) cut = n - 1; else cut = left; left -= cut; nextout = cut + base0; for (int i = 0; i < ss.size(); i++) { switch (ss[i]) { case '+': if (left >= n - 1) cut = n - 1; else cut = left; left -= cut; nextout = cut + base0; cout << ss[i]; break; case '-': if (left >= n - 1) cut = n - 1; else cut = left; left -= cut; nextout = -(cut + base1); cout << ss[i]; break; case '?': cout << nextout; break; default: cout << ss[i]; } } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; char sRead[10]; int main() { vector<int> sign{1}; while (scanf("%s", sRead), sRead[0] != '=') { if (sRead[0] == '+') sign.push_back(1); else if (sRead[0] == '-') sign.push_back(-1); } int n; scanf("%d", &n); int qCount = sign.size(); int pos = 0, neg = 0; for (int x : sign) (x == 1 ? pos : neg)++; if (n < pos - neg * n || pos * n - neg < n) { printf("Impossible\n"); return 0; } printf("Possible\n"); vector<int> pNums(pos, 1), nNums(neg, 1); if (n <= pos - neg) { int toCompensate = (pos - neg) - n; for (int &x : nNums) { int amt = min(toCompensate, n - 1); toCompensate -= amt; x += amt; } } else { int toCompensate = n - (pos - neg); for (int &x : pNums) { int amt = min(toCompensate, n - 1); toCompensate -= amt; x += amt; } } for (int i = 0; i < qCount; ++i) { if (i > 0) { printf(" %c ", sign[i] == 1 ? '+' : '-'); } if (sign[i] == 1) { printf("%d", pNums.back()); pNums.pop_back(); } else { printf("%d", nNums.back()); nNums.pop_back(); } } printf(" = %d\n", n); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000005; int n, num[2], d[MAXN], cnt, ans[2][MAXN]; int main() { char ch; int sig = 1, x = 0; while (scanf("%c", &ch) != EOF) { if (ch >= '0' && ch <= '9') x = x * 10 + ch - '0'; if (ch == '=' || ch == '+' || ch == '-') { num[sig]++; if (ch == '+') d[++cnt] = sig = 1; if (ch == '-') d[++cnt] = sig = 0; } } n = x; int tmp = n + num[0]; if (n * num[1] < tmp) { printf("Impossible\n"); return 0; } for (int i = 1; i <= num[0]; i++) ans[0][i] = 1; if (num[1] > tmp) { int j = 1; for (int i = 1; i <= num[1] - tmp; i++) { while (j <= num[0] && ans[0][j] == n) j++; if (j > num[0]) { printf("Impossible\n"); return 0; } ans[0][j]++; } tmp = num[1]; } for (int i = 1; i <= num[1]; i++) { ans[1][i] = min(n, tmp - (num[1] - i)); tmp -= ans[1][i]; } printf("Possible\n"); printf("%d ", ans[1][1]); int j1 = 1, j0 = 0; for (int i = 1; i <= cnt; i++) { if (d[i] == 1) printf("+ %d ", ans[1][++j1]); else printf("- %d ", ans[0][++j0]); } printf("= %d\n", n); }
#include <bits/stdc++.h> using namespace std; vector<char> op; int p, m; vector<int> ps, ms; int main() { p = 1; for (;;) { char ch; scanf(" %*c %c", &ch); if (ch == '=') break; op.push_back(ch); if (ch == '+') p++; else m++; } int n; scanf("%d", &n); if (p - m * n <= n && n <= p * n - m) { printf("Possible\n"); ps.resize(p, 1); ms.resize(m, 1); int remain = (p - m) - n; if (remain < 0) { for (int i = 0; i < p && remain; i++) { int adde = min(n - 1, -remain); ps[i] += adde; remain += adde; } } else { for (int i = 0; i < m && remain; i++) { int adde = min(n - 1, remain); ms[i] += adde; remain -= adde; } } printf("%d ", ps[0]); for (int p1 = 1, m1 = 0, j = 0; j < op.size(); j++) { printf("%c ", op[j]); if (op[j] == '-') printf("%d ", ms[m1++]); else printf("%d ", ps[p1++]); } printf("= %d\n", n); } else { printf("Impossible\n"); } }
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& num) { bool start = false, neg = false; char c; num = 0; while ((c = getchar()) != EOF) { if (c == '-') start = neg = true; else if (c >= '0' && c <= '9') { start = true; num = num * 10 + c - '0'; } else if (start) break; } if (neg) num = -num; } const int maxn = (int)(1e6) + 5; int bi[maxn], ai[maxn], idx; char s[10], opt[10]; int main() { int n, cnt = 0; bi[++idx] = 1; while (1) { scanf("%s%s", s, opt); if (opt[0] == '=') break; if (opt[0] == '+') bi[++idx] = 1; else bi[++idx] = -1; } scanf("%d", &n); for (int i = (1); i <= (idx); i++) if (bi[i] > 0) ++cnt; long long r = 1LL * n * cnt - (idx - cnt), l = 1LL * cnt - 1LL * n * (idx - cnt); if (n < l || n > r) { printf("Impossible\n"); return 0; } printf("Possible\n"); for (int i = (1); i <= (idx); i++) if (bi[i] > 0) ai[i] = n; else ai[i] = 1; long long now = r; for (int i = (1); i <= (idx); i++) if (bi[i] > 0) if (now > n) { int dt = min(1LL * ai[i] - 1, now - n); ai[i] -= dt; now -= dt; } for (int i = (1); i <= (idx); i++) if (bi[i] < 0) if (now > n) { int dt = min(n - 1LL * ai[i], now - n); ai[i] += dt; now -= dt; } for (int i = (1); i <= (idx); i++) { if (bi[i] > 0 && i != 1) printf("+ "); else if (bi[i] < 0) printf("- "); printf("%d ", ai[i]); } printf("= %d\n", n); return 0; }
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; int n, res, sol[109]; char s[20], sig[109]; int main() { n = 0; int def = 0; scanf("%s", s); sig[n++] = '+'; def++; while (1) { scanf("%s", s); sig[n] = s[0]; if (s[0] == '+') def++; else if (s[0] == '-') def--; else break; n++; scanf("%s", s); } scanf("%d", &res); memset((sol), (0), sizeof(sol)); for (int(i) = (0); (i) < (n); ++(i)) { if (sig[i] == '+') { if (def < res) { int d = res - def; d = min(d, res - 1); sol[i] = d; def += d; } } else { if (def > res) { int d = def - res; d = min(d, res - 1); sol[i] = d; def -= d; } } } if (def != res) puts("Impossible"); else { puts("Possible"); printf("%d", sol[0] + 1); for (int(i) = (1); (i) < (n); ++(i)) printf(" %c %d", sig[i], sol[i] + 1); printf(" = %d\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; char s[1000000]; int n = 0, a = 1, d = 0, mem; inline int check(int x, char op) { if (op == '+') { int m = n - x; if (a - d * mem > m || m > mem * a - d) return 0; return 1; } int m = n + x; if (a - d * mem > m || m > mem * a - d) return 0; return 1; } int main() { int i, j, l; char op; gets(s); l = strlen(s); for (i = 0; i < l; i++) { if (s[i] == '+') a++; else if (s[i] == '-') d++; else if (s[i] >= '0' && s[i] <= '9') n = n * 10 + s[i] - '0'; } mem = n; if (a - d * n > n || n > n * a - d) { printf("Impossible\n"); return 0; } printf("Possible\n"); op = '+'; a--; for (i = 0; i < l; i++) { if (s[i] == '?') { int x; if (check(1, op)) x = 1; else if (check(mem, op)) x = mem; else { for (j = 1; j <= mem; j++) if (check(j, op)) { x = j; break; } } if (op == '+') n -= x; else n += x; printf("%d", x); continue; } printf("%c", s[i]); if (s[i] == '+') { op = '+'; a--; } else if (s[i] == '-') { op = '-'; d--; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string x, t; getline(cin, x); string temp = ""; int index = -1; for (int i = 0; i < x.size(); ++i) { if (x[i] == '=') index = i; } index += 2; for (int i = index; i < x.size(); ++i) temp.push_back(x[i]); int n = stoi(temp.c_str()); int pos = 0; int neg = 0; int i = 0; if (x[0] == '?') { ++pos; ++i; } else { ++neg; i += 2; } for (int i = 0; i < x.size(); ++i) if (x[i] == '+') ++pos; else if (x[i] == '-') ++neg; vector<int> ne(neg, 1); vector<int> po(pos, 1); int sum = (pos * 1) - neg; int j = 0, z = 0; bool in = false; while (1) { if (sum == n) break; if (sum > n) { if (z == neg) { cout << "Impossible" << endl; return 0; } if (ne[z] == n) { ++z; continue; } in = true; ++ne[z]; sum -= 1; } else if (sum < n) { if (j == pos) { cout << "Impossible" << endl; return 0; } if (po[j] == n) { ++j; continue; } in = true; ++po[j]; sum += 1; } } cout << "Possible" << endl; j = 0, z = 0; string last = ""; for (int i = 0; i < x.size(); ++i) { if (x[i] == '-' || x[i] == '+' || i == 0) { if (i > 0) { if (x[i] == '-') { last.push_back(x[i]); stringstream ss; ss << ne[z++]; last += ss.str(); ; } else { last.push_back(x[i]); stringstream ss; ss << po[j++]; last += ss.str(); } } if (i == 0) { if (x[i] == '-') { last.push_back(x[i]); stringstream ss; ss << ne[z++]; last += ss.str(); ; } else { stringstream ss; ss << po[j++]; last += ss.str(); } } } else if (x[i] != '?') last.push_back(x[i]); } cout << last << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int tosub = 0; int pls = 1; int mins = 0; string s; char a; cin >> a; while (a != '=') { if (a == '+') { s.push_back(a); pls++; } else if (a == '-') { mins++; s.push_back(a); } cin >> a; } int tmp; cin >> tmp; int mn = pls - mins * tmp; int mx = pls * tmp - mins; if (tmp < mn || tmp > mx) { cout << "Impossible\n"; return 0; } cout << "Possible\n"; int curr = 0; for (int i = (-1); i < (int)s.size(); ++i) { if (!(~i) || s[i] == '+') { if (i >= 0) { cout << s[i] << " "; } for (int j = 1; j <= tmp; ++j) { int mn = (pls - 1) - mins * tmp; int mx = (pls - 1) * tmp - mins; if (mn <= tmp - (curr + j) && mx >= tmp - (curr + j)) { cout << j << " "; pls--; curr += j; break; } } } else { cout << s[i] << " "; for (int j = 1; j <= tmp; ++j) { int mn = pls - (mins - 1) * tmp; int mx = pls * tmp - (mins - 1); if (mn <= tmp - (curr - j) && mx >= tmp - (curr - j)) { cout << j << " "; mins--; curr -= j; break; } } } } cout << " = " << tmp << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 7; const int M = 107; const int mod = 998244353; const int inf = 1e9 + 7; const double pi = acos(-1); const int maxn = N * 2; const double PI = acos(-1); int pl = 1, mn = 0; int n; string s; vector<int> a; bool canDo(int sum, int cnt) { return sum >= cnt && sum <= (long long)cnt * n; } void solve() { a.push_back(1); while (cin >> s) { if (s[0] == '+') { pl++; a.push_back(1); } if (s[0] == '-') { mn++; a.push_back(-1); } if (s[0] >= '0' && s[0] <= '9') { n = s[0] - '0'; for (int j = (1); j < (s.size()); j++) { n = n * 10 + s[j] - '0'; } } } for (int sumPlus = 1; sumPlus <= n * 100; sumPlus++) { if (canDo(sumPlus, pl) && canDo(sumPlus - n, mn)) { int sumMinus = sumPlus - n; puts("Possible"); for (int i = 0; i < a.size(); i++) { if (a[i] == 1) { if (i > 0) cout << " + "; if (pl == 1) { cout << sumPlus; continue; } int kq = 0; if (pl - 1 <= sumPlus - n) { kq = n; } else { kq = sumPlus - pl + 1; } cout << kq; sumPlus -= kq; pl--; } else { cout << " - "; if (mn == 1) { cout << sumMinus; continue; } int kq = 0; if (mn - 1 <= sumMinus - n) { kq = n; } else { kq = sumMinus - mn + 1; } sumMinus -= kq; cout << kq; mn--; } } cout << " = " << n; return; } } puts("Impossible"); } int main() { int T = 1; for (int i = (1); i < (T + 1); i++) { solve(); } }
#include <bits/stdc++.h> using namespace std; const int oo = 1000000009; const double eps = 1e-9; const int mod = 1000000007; string s; vector<int> pans, nans, fin; int main() { int p = 1, n = 0, num = 0; fin.push_back(1); while (cin >> s) { if (s[0] == '=') break; if (s[0] == '+') ++p, fin.push_back(1); if (s[0] == '-') ++n, fin.push_back(2); } cin >> s; num = stoi(s); if (num + n > num * p || p > num + num * n) cout << "Impossible" << "\n"; else { cout << "Possible" << "\n"; if (p >= num + n) { for (int i = 0; i < p; ++i) pans.push_back(1); if (n) { int l = (p - num) / n, k = (p - num) % n; for (int i = 0; i < n; ++i) { if (k) nans.push_back(l + 1), --k; else nans.push_back(l); } } } else { for (int i = 0; i < n; ++i) nans.push_back(1); int l = (num + n) / p, k = (num + n) % p; for (int i = 0; i < p; ++i) { if (k) pans.push_back(l + 1), --k; else pans.push_back(l); } } int x = 0, y = 0; for (int i = 0; i < fin.size(); ++i) { if (i == 0) cout << pans[x], ++x; else if (fin[i] == 1) cout << " + " << pans[x], ++x; else cout << " - " << nans[y], ++y; } cout << " = " << num << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { string str; getline(cin, str); long long plus = 0, minus = 0; plus++; for (int i = 0; i < str.length(); i++) { if (str[i] == '+') plus++; if (str[i] == '-') minus++; } string number; for (int i = str.length() - 1; i >= 0; i--) { if (str[i] == ' ') { break; } else number.push_back(str[i]); } reverse(number.begin(), number.end()); long long n = stoi(number); if ((plus * n - minus) < n) { puts("Impossible"); return 0; } if ((plus - n * minus) > n) { puts("Impossible"); return 0; } puts("Possible"); int now = 1; long long req = plus * n - minus - n; vector<long long> num; int last = 1; for (int i = 0; i < str.length(); i++) { if (str[i] == '=') break; if (str[i] == '?') { if (last == 1) { num.push_back(n); } else num.push_back(-1); } else if (str[i] == '+') last = 1; else if (str[i] == '-') last = 0; } for (int i = 0; i < num.size(); i++) { if (req == 0) break; else if (req > 0) { if (num[i] == n) { long long p = min(n - 1LL, req); num[i] -= p; req -= p; } else { long long p = min((n - 1), req); num[i] += (-1 * p); req -= p; } } else if (req < 0) { if (num[i] == -1) { long long p = max(req, -(n - 1)); num[i] += p; req -= p; } else { long long p = min(-1 * req, n - 1); num[i] += p; req += p; } } } cout << num[0] << " "; for (int i = 1; i < num.size(); i++) { if (num[i] > 0) { cout << "+ " << num[i] << " "; } else cout << "- " << abs(num[i]) << " "; } cout << "= " << n << "\n"; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6; vector<int> make(int num, int n, int x) { vector<int> v; for (int i = 1; i <= n; i++) { int mx = (num - (n - i)); int now = min(mx, x); v.push_back(now); num -= now; } return v; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; getline(cin, s); int pl = 1, mn = 0; for (int i = 0; i < s.size(); i++) { char x = s[i]; if (x == '+') pl++; if (x == '-') mn++; } stringstream str(s); string num; while (str >> num) ; stringstream srt(num); int n; srt >> n; int plo = pl, phi = pl * n; int mlo = mn + n, mhi = mn * n + n; if (phi < mlo || plo > mhi) cout << "Impossible" << endl, exit(0); int pt = max(plo, mlo); vector<int> v1 = make(pt, pl, n); vector<int> v2 = make(pt - n, mn, n); cout << "Possible" << endl; int pr = 0, pt1 = 0, pt2 = 0; for (int i = 0; i < s.size(); i++) { char x = s[i]; if (x == '?') { if (!pr) cout << v1[pt1++]; else cout << v2[pt2++]; } else { if (x == '+') pr = 0; if (x == '-') pr = 1; cout << x; } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; template <class T = ll> constexpr T TEN(int n) { return (n == 0) ? 1 : 10 * TEN<T>(n - 1); } int main() { ll a = 0, b = 0; bool f = true; vector<pair<string, string>> v; while (true) { string s, t; cin >> s >> t; v.push_back(make_pair(s, t)); if (f) { a++; } else { b++; } if (t == "=") break; if (t == "+") f = true; else f = false; } ll n; cin >> n; ll x = a, y = a * n; ll z = n + b, w = n + b * n; if (z <= x and x <= w) { cout << "Possible" << endl; ll u = x - z; bool f = true; for (auto st : v) { string s, t; tie(s, t) = st; ll d = 0; if (f) { d = 1; } else { d = 1; ll o = min(n - 1, u); d += o; u -= o; } cout << d << " " << t << " "; if (t == "+") f = true; else f = false; } cout << n << endl; return 0; } if (x <= z and z <= y) { cout << "Possible" << endl; ll u = z - x; bool f = true; for (auto st : v) { string s, t; tie(s, t) = st; ll d = 0; if (f) { d = 1; ll o = min(n - 1, u); d += o; u -= o; } else { d = 1; } cout << d << " " << t << " "; if (t == "+") f = true; else f = false; } cout << n << endl; return 0; } cout << "Impossible" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char str[1100]; vector<int> flag; int main() { int n; while (gets(str)) { flag.clear(); int len = strlen(str); int num[2] = {0, 0}; int id = 0; flag.push_back(1); while (str[id] != '=') { if (str[id] == '?') ++num[flag.back()]; else if (str[id] == '+') flag.push_back(1); else if (str[id] == '-') flag.push_back(0); ++id; } id += 2; sscanf(str + id, "%d", &n); if (n >= num[1] - num[0] * n && n <= num[1] * n - num[0]) { puts("Possible"); vector<int> ans(flag.size(), 1); int tmp = num[1] - num[0]; for (int i = 0; tmp != n && i < flag.size(); ++i) { if (flag[i]) while (tmp < n && ans[i] < n) ++tmp, ++ans[i]; else while (tmp > n && ans[i] < n) --tmp, ++ans[i]; } for (int i = 0; i < (flag.size()); ++i) { if (i != 0) putchar(flag[i] ? '+' : '-'), putchar(' '); printf("%d ", ans[i]); } printf("= %d\n", n); } else puts("Impossible"); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> v; int main() { char s; long long n, p = 1; v.push_back(1); while (true) { cin >> s; if (s == '+') p++, v.push_back(1); else if (s == '-') p--, v.push_back(-1); else if (s == '=') break; } cin >> n; for (int i = 0; i < v.size(); i++) { while (p < n && v[i] > 0 && v[i] < n) v[i]++, p++; while (p > n && v[i] < 0 && v[i] > -n) v[i]--, p--; } if (p != n) return cout << "Impossible", 0; cout << "Possible" << endl; cout << v[0] << " "; for (int i = 1; i < v.size(); i++) { if (v[i] > 0) cout << '+' << " "; else cout << '-' << " "; cout << ((v[i] < 0) ? (v[i] * -1) : (v[i])) << " "; } cout << '=' << " " << n; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s = "?"; string result[201]; int n_p = 1, n_m = 0, i = 0; while (s[0] != '=') { cin >> s; result[i++] = s; if (s[0] == '+') { n_p++; } if (s[0] == '-') { n_m++; } } cin >> s; int n = stoi(s, NULL); int min = n_p - n_m * n, max = n_p * n - n_m; if (n < min || n > max) { cout << "Impossible\n"; } else { cout << "Possible\n"; int gap = n - n_p + n_m; int p = 0, p_o = 0, m = 0, m_o = 0; if (gap < 0) { gap = 0 - gap; m = gap / (n - 1); m_o = gap % (n - 1); } else if (gap > 0) { p = gap / (n - 1); p_o = gap % (n - 1); } string last = "+"; for (int k = 0; k < i; k += 2) { if (last[0] == '+') { if (p > 0) { cout << n << " "; p--; } else if (p_o) { cout << p_o + 1 << " "; p_o = 0; } else { cout << "1 "; } } if (last[0] == '-') { if (m > 0) { cout << n << " "; m--; } else if (m_o) { cout << m_o + 1 << " "; m_o = 0; } else { cout << "1 "; } } last = result[k + 1]; cout << last << " "; } cout << n << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); vector<string> a; string s; int k1 = 1, k2 = 0; int n; vector<int> p1, p2; p1.push_back(1); while (cin >> s) { a.push_back(s); if (s == "+") { k1++; p1.push_back(1); } if (s == "-") { k2++; p2.push_back(1); } if (s == "=") break; } cin >> s; a.push_back(s); n = atoi(s.c_str()); int summ = k1 - k2; for (int i = (0); i < (p1.size()); i++) { while (summ < n && p1[i] < n) { p1[i]++; summ++; } } for (int i = (0); i < (p2.size()); i++) { while (summ > n && p2[i] < n) { p2[i]++; summ--; } } if (summ != n) { cout << "Impossible" << '\n'; return 0; } cout << "Possible" << '\n'; int pp1 = 0, pp2 = 0; for (int i = (0); i < (a.size()); i++) { if (a[i] == "?") { if (!i || a[i - 1] == "+") { cout << p1[pp1] << " "; pp1++; } else { cout << p2[pp2] << " "; pp2++; } continue; } cout << a[i] << " "; } cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int a[101]; int ans[101]; int main() { int n; string s = ""; a[0] = 1; int k = 0; int ct1 = 1; int ct2 = 0; while (true) { cin >> s; cin >> s; k++; if (s == "=") break; a[k] = (s == "+") ? 1 : -1; if (a[k] == 1) ct1++; else ct2++; } cin >> n; if (ct1 * n - ct2 >= n && ct1 - n * ct2 <= n) { cout << "Possible\n"; int sum = 0; for (int i = 0; i < k; i++) { ans[i] = 1; sum += ans[i] * a[i]; } if (sum > n) { int j = 0; while (sum > n) { while (a[j] != -1 || ans[j] == n) { j++; } int k = min(sum - n, n - ans[j]); ans[j] += k; sum -= k; } } else { int j = 0; while (sum < n) { while (a[j] != 1 || ans[j] == n) { j++; } int k = min(n - sum, n - ans[j]); ans[j] += k; sum += k; } } cout << ans[0] << " "; for (int i = 1; i < k; i++) { cout << (a[i] == 1 ? "+ " : "- ") << ans[i] << " "; } cout << "= " << n; } else { cout << "Impossible\n"; } return 0; }
#include <bits/stdc++.h> const int MAXN = 100005; const long long inf = 1e18; using namespace std; void solve() { int c[] = {0, 0}; int sign = 0, n; vector<char> v; char s; cin >> s; v.push_back('+'); while (s != '=') { if (s == '?') c[sign]++; else if (s == '-') { sign = 1; v.push_back(s); } else if (s == '+') { sign = 0; v.push_back(s); } cin >> s; } cin >> n; int sum = n; int mnp = c[0] - n * c[1]; int mxp = (n * c[0]) - c[1]; if (n <= mxp and n >= mnp) cout << "Possible" << '\n'; else { cout << "Impossible"; return; } for (int j = 0; j < v.size(); j++) { s = v[j]; if (j != 0) cout << s << " "; for (int i = n; i > 0; i--) { if (s == '+') { mnp = c[0] - 1 - n * c[1]; mxp = (c[0] - 1) * n - c[1]; if (sum - i <= mxp and sum - i >= mnp) { cout << i << " "; sum -= i; c[0]--; break; } } else { mnp = c[0] - n * (c[1] - 1); mxp = (c[0]) * n - (c[1] - 1); if (sum + i <= mxp and sum + i >= mnp) { cout << i << " "; sum += i; c[1]--; break; } } } } cout << "= " << n; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> template <typename T> bool domax(T &a, T b) { return (b > a ? (a = b, true) : false); } template <typename T> bool domin(T &a, T b) { return (b < a ? (a = b, true) : false); } std::string str; int add, sub, n; int main() { add = 1; char c; scanf(" %*c %c", &c); while (c != '=') { if (c == '+') { add++; str += '+'; } else { sub++; str += '-'; } scanf(" %*c %c", &c); } scanf("%d", &n); int min = add, max = add * n; int minsub = -sub * n, maxsub = -sub; if (min + minsub <= n && n <= max + maxsub) { printf("Possible\n"); int toadd = n - minsub, tosub = minsub; if (toadd > max) { toadd = max; tosub = n - toadd; } assert(toadd + tosub == n); std::vector<int> a, s; for (int i = 0; i < add; i++) a.push_back(toadd / add); for (int i = 0; i < toadd % add; i++) a[i]++; tosub *= -1; for (int i = 0; i < sub; i++) s.push_back(tosub / sub); if (sub) for (int i = 0; i < tosub % sub; i++) s[i]++; int addi = 1, subi = 0; printf("%d ", a[0]); for (int i = 0; i < (int((str).size())); i++) { if (str[i] == '+') printf("+ %d ", a[addi++]); else printf("- %d ", s[subi++]); } printf("= %d\n", n); } else { printf("Impossible\n"); } }
#include <bits/stdc++.h> using namespace std; int a[111]; char s[11111]; int main() { int i, len, nn, pos, neg, cntp, cntn; long long n, nr, nv, pr, pv, st, ed; gets(s); len = strlen(s); n = pos = neg = nn = 0; a[nn++] = 1; for (i = 1; i < len; i++) { if (s[i] == '+') a[nn++] = 1; if (s[i] == '-') a[nn++] = -1; if (isdigit(s[i])) n = n * 10 + s[i] - '0'; } for (i = 0; i < nn; i++) { if (a[i] > 0) pos++; else neg++; } st = pos; ed = pos * n; st = max(st, neg + n); ed = min(ed, neg * n + n); if (ed >= st) { puts("Possible"); if (pos) { pv = st / pos; pr = st - pv * pos; } if (neg) { st -= n; nv = st / neg; nr = st - nv * neg; } cntp = cntn = 0; for (i = 0; i < nn; i++) { if (i) { if (a[i] > 0) printf(" + "); else printf(" - "); } if (a[i] > 0) { if (cntp < pr) { printf("%I64d", pv + 1); cntp++; } else printf("%I64d", pv); } else { if (cntn < nr) { printf("%I64d", nv + 1); cntn++; } else printf("%I64d", nv); } } printf(" = %I64d\n", n); } else puts("Impossible"); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAX1 = 3e5 + 10; long long a, b, c, d, e, cnt, x, y, flag, num[100]; char nu[100], ch; int main() { cin >> ch; nu[0] = '+'; while (ch != '=') { if (ch == '?') ++a; else { ++cnt; nu[cnt] = ch; } cin >> ch; } fill(num, num + a, 1); cin >> b; for (int i = 0; i < a; ++i) { if (nu[i] == '+') ++flag; else --flag; } if (flag > b) { flag *= -1; b *= -1; e = 1; for (int i = 0; i < a; ++i) { if (nu[i] == '+') nu[i] = '-'; else nu[i] = '+'; } } x = abs(b) - 1; cnt = b - flag; for (int i = 0; i < a; ++i) { if (nu[i] == '+') { y = min(x, cnt); num[i] += y; cnt -= y; } } if (cnt) { cout << "Impossible"; return 0; } cout << "Possible\n" << num[0] << ' '; if (e) { for (int i = 1; i < a; ++i) { if (nu[i] == '+') nu[i] = '-'; else nu[i] = '+'; } } for (int i = 1; i < a; ++i) { cout << nu[i] << ' ' << num[i] << ' '; } cout << '=' << ' ' << abs(b); return 0; }
#include <bits/stdc++.h> using namespace std; string str, tmp; int x[110]; vector<int> pos, pos2; int main() { ios_base::sync_with_stdio(0); cin.tie(0); getline(cin, str); stringstream ss(str.substr(1)); char a, b; int p = 0, sum = 1; x[++p] = 1; pos.push_back(1); while (ss >> a) { if (a == '=') break; ss >> b; if (a == '+') { x[++p] = 1; pos.push_back(p); } else { x[++p] = -1; pos2.push_back(p); } sum += x[p]; } for (int i = str.size() - 1; i >= 0 && str[i] != ' '; i--) tmp += str[i]; reverse(tmp.begin(), tmp.end()); int N = atoi(tmp.c_str()); for (int i = 0; i < (int)pos.size() && sum < N; i++) { sum -= x[pos[i]]; x[pos[i]] = min(N, N - sum); sum += x[pos[i]]; } for (int i = 0; i < (int)pos2.size() && sum > N; i++) { int diff = sum - N; sum -= x[pos2[i]]; x[pos2[i]] = max(-N, x[pos2[i]] - diff); sum += x[pos2[i]]; } if (sum != N) { cout << "Impossible\n"; } else { cout << "Possible\n"; p = 0; for (int i = 0; i < (int)str.size(); i++) { if (str[i] == '\?') { cout << abs(x[++p]); } else { cout << str[i]; } } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; char s[100005]; int main() { gets(s); int len = strlen(s), cur = 1, n = 0, sub = 0, cnt = 0; for (int i = 0; i < len; i++) { if (s[i] == '+') cur = 1; else if (s[i] == '-') cur = -1; else if (s[i] == '?') { if (cur > 0) cnt++; else sub++; } else if ('0' <= s[i] && s[i] <= '9') n = n * 10 + s[i] - '0'; } if (!cnt) return puts("Impossible"); int q = (n + sub) / cnt, r = (n + sub) % cnt; if (r && q + 1 > n) return puts("Impossible"); else if (r == 0 && q > n) return puts("Impossible"); else if (q) { puts("Possible"); cnt = 0, cur = 1; for (int i = 0; i < len; i++) { if (s[i] == '?') { if (cur > 0) { cnt++; if (cnt <= r) printf("%d", q + 1); else printf("%d", q); } else putchar('1'); } else if (s[i] == '+') cur = 1, putchar('+'); else if (s[i] == '-') cur = -1, putchar('-'); else putchar(s[i]); } } else { if (!sub) return puts("Impossible"); else { int rem = cnt - n; int q = rem / sub, r = rem % sub; if (r && q + 1 > n) return puts("Impossible"); else if (r == 0 && q > n) return puts("Impossible"); sub = 0, cur = 1; puts("Possible"); for (int i = 0; i < len; i++) { if (s[i] == '?') { if (cur > 0) putchar('1'); else { sub++; if (sub <= r) printf("%d", q + 1); else printf("%d", q); } } else if (s[i] == '+') cur = 1, putchar('+'); else if (s[i] == '-') cur = -1, putchar('-'); else putchar(s[i]); } } } }
#include <bits/stdc++.h> using namespace std; int p = 1, n, j, a[105]; char c; int main() { a[j++] = 1; while (cin >> c && c != '=') { if (c == '-') p--, a[j++] = -1; if (c == '+') p++, a[j++] = 1; } cin >> n; for (int i = 0; i < j; i++) { while (p < n && a[i] > 0 && a[i] < n) a[i]++, p++; while (p > n && a[i] < 0 && a[i] > -n) a[i]--, p--; } if (p != n) { cout << "Impossible\n"; return 0; } cout << "Possible\n"; for (int i = 0; i < j; i++) cout << (i ? (a[i] < 0 ? "- " : "+ ") : "") << abs(a[i]) << " "; cout << "= " << n << endl; }
#include <bits/stdc++.h> using namespace std; int n = 0; int x; int signo[10000]; int valor[10000]; int main() { char c = '+'; do { signo[n++] = c == '+' ? 1 : -1; cin >> c; cin >> c; } while (c != '='); cin >> x; int suma = 0; for (int i = 0; i < n; i++) { valor[i] = 1; suma += valor[i] * signo[i]; } while (suma != x) { bool cambio = false; for (int i = 0; i < n and not cambio; i++) { if (valor[i] < x and ((suma < x and signo[i] == 1) or (suma > x and signo[i] == -1))) { valor[i]++; suma += signo[i]; cambio = true; } } if (not cambio) { cout << "Impossible" << endl; exit(0); } } cout << "Possible" << endl; for (int i = 0; i < n; i++) { if (i > 0) cout << (signo[i] == 1 ? " + " : " - "); cout << valor[i]; } cout << " = " << x << endl; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target( \ "avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; long long MOD = 1e9 + 7; long long intt(string s) { long long res = 0; long long cur = 1; for (int i = s.size() - 1; i > -1; i--) { res += (s[i] - '0') * cur; cur *= 10; } return res; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20); long long lst = 0; long long kol[2] = {0, 0}; long long n = 0; vector<char> c; while (true) { string s; cin >> s; if (s.size() > 1 || ('0' <= s[0] && s[0] <= '9')) { n = intt(s); break; } else { if (s[0] == '?') { kol[lst]++; } else { if (s[0] == '=') continue; c.push_back(s[0]); if (s[0] == '-') lst = 1; else lst = 0; } } } for (int i = 1; i <= min(2000000ll, n); i++) { if ((n + kol[1] * i) / kol[0] > 0 && (n + kol[1] * i + kol[0] - 1) / kol[0] <= n) { cout << "Possible" << endl; cout << (n + kol[1] * i + kol[0] - 1) / kol[0] << " "; long long koll = 1; for (auto j : c) { if (j == '+') { if (koll >= (n + kol[1] * i) % kol[0]) { cout << j << " " << (n + kol[1] * i) / kol[0] << " "; } else { koll++; cout << j << " " << (n + kol[1] * i + kol[0] - 1) / kol[0] << " "; } } else { cout << j << " " << i << " "; } } cout << "= "; cout << n; exit(0); } } cout << "Impossible"; }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const double PI = acos(-1); const int INF = (int)1e9 + 9; const int MAXN = (int)3e5 + 7; char ch[MAXN]; int val[11111]; int n; string second; int main() { getline(cin, second); char sign = '+'; int i = 0; vector<int> pos, neg; for (i = 0; i < second.size(); i++) { if (second[i] == '=') break; if (second[i] == '+') sign = '+'; else if (second[i] == '-') sign = '-'; if (second[i] == '?') { if (sign == '+') pos.push_back(i); else neg.push_back(i); } } for (; i < second.size(); i++) if (isdigit(second[i])) n = n * 10 + second[i] - '0'; int max_val = n * (int)pos.size() - (int)neg.size(); int min_val = (int)pos.size() - n * (int)neg.size(); if (min_val <= n && n <= max_val) { cout << "Possible\n"; for (auto it : neg) val[it] = 1; int tot = -n; for (auto it : neg) { val[it] = 1; tot--; } for (auto it : pos) { val[it] = 1; tot++; } for (auto it : pos) { if (tot >= 0) break; int k = min(n - 1, -tot); val[it] += k; tot += k; } for (auto it : neg) { if (tot <= 0) break; int k = min(n - 1, tot); val[it] += k; tot -= k; } for (int i = 0; i < second.size(); i++) { bool skip = 0; for (auto it : pos) if (it == i) skip = 1; for (auto it : neg) if (it == i) skip = 1; if (!skip) cout << second[i]; else cout << val[i]; } } else { cout << "Impossible"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char str[2333]; gets(str); int len = strlen(str); int i = len - 1; int n = 0; int x = 1; while (str[i] == '\n' || str[i] == ' ' || str[i] == '\r') --i; while (str[i] != '=' && str[i] != ' ') { n += (str[i] - '0') * x; x *= 10; --i; } int a, b; a = 0; b = 0; for (i = 0; str[i] != '='; ++i) { if (str[i] == '+') a++; else if (str[i] == '-') b++; } if ((b > a * n) || (a > b * n + n - 1)) { puts("Impossible"); } else { puts("Possible"); bool f = 0; int rest; if (a >= b * n) rest = a - b * n; else if (a >= b) rest = a - b; else rest = b - a; for (int i = 0; str[i]; ++i) { if (str[i] != '?') { putchar(str[i]); if (str[i] == '+') f = 0; else if (str[i] == '-') f = 1; } else { if (i == 0) { if (a >= b * n) printf("%d", n - (a - b * n)); else printf("%d", n); } else { if (a >= b * n) { if (!f) { printf("1"); } else printf("%d", n); } else { if (a >= b) { if (!f) printf("1"); else if (rest) { printf("%d", min(rest, n - 1) + 1); rest -= min(rest, n - 1); } else printf("1"); } else { if (f) printf("1"); else if (rest) { printf("%d", min(rest, n - 1) + 1); rest -= min(rest, n - 1); } else printf("1"); } } } } } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, cnt1, cnt0, arr[110]; char c, op[110]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int i = 1; while (c != '=') { cin >> c; if (c == '+') { cnt1++; op[i++] = c; } if (c == '-') { cnt0++; op[i++] = c; } } op[0] = '+'; cnt1++; cin >> n; int x = cnt1 - cnt0; for (int j = 0; j < i; j++) { arr[j] = 1; if (op[j] == '+') { while (x < n && arr[j] < n) { arr[j]++; x++; } } else { while (x > n && arr[j] < n) { arr[j]++; x--; } } } int y; y++; if (x != n) cout << "Impossible\n"; else { cout << "Possible\n" << arr[0] << ' '; for (int j = 1; j < i; j++) cout << op[j] << ' ' << arr[j] << ' '; cout << "= " << n << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> int size(const T &x) { return x.size(); } const int INF = 2147483647; int main() { string s; int sign = 1; int add = 0, sub = 0; vector<int> parts; while (cin >> s) { if (sign == 1) add++; else sub++; parts.push_back(sign); cin >> s; if (s == "=") { break; } if (s == "+") { sign = 1; } else { sign = -1; } } int n; cin >> n; int left = n; vector<int> res; bool ok = true; for (__typeof(0) i = (0); i < (size(parts)); ++i) { int nadd = add, nsub = sub; if (parts[i] == 1) nadd--; else nsub--; bool found = false; for (int x = 1; x <= n; x++) { int nleft = parts[i] == 1 ? left - x : left + x; int mn = nadd * 1 - nsub * n, mx = nadd * n - nsub * 1; if (mn <= nleft && nleft <= mx) { res.push_back(x); left = nleft; found = true; break; } } if (!found) { ok = false; break; } add = nadd; sub = nsub; } if (!ok) { printf("Impossible\n"); return 0; } printf("Possible\n"); for (__typeof(0) i = (0); i < (size(res)); ++i) { if (i != 0) { printf("%c ", parts[i] == 1 ? '+' : '-'); } printf("%d ", res[i]); } printf("= %d\n", n); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; int addcnt = 1, recnt = 0, n, n0; getline(cin, str); int pos = 0; while ((pos = str.find_first_of("+-", pos)) != string::npos) { if (str[pos] == '+') addcnt++; else recnt++; pos++; } n0 = n = stoi(str.substr(str.find_first_of("1234567890"))); n0 = max(n + recnt, addcnt); if (n0 / addcnt + (n0 % addcnt != 0) > n || addcnt > n * (recnt + 1)) cout << "Impossible" << endl; else { cout << "Possible" << endl; if (n0 == addcnt) { int negcnt = 0; bool positive = true; for (int i = 0; i < str.length(); i++) { if (str[i] == '?') { if (positive) cout << 1; else { cout << (addcnt - n) / recnt + (negcnt++ < (addcnt - n) % recnt); } } else { if (str[i] == '+') positive = true; if (str[i] == '-') positive = false; cout << str[i]; } } } else { int poscnt = 0; bool positive = true; for (int i = 0; i < str.length(); i++) { if (str[i] == '?') { if (positive) { cout << n0 / addcnt + (poscnt++ < n0 % addcnt); } else cout << 1; } else { if (str[i] == '+') positive = true; if (str[i] == '-') positive = false; cout << str[i]; } } cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 1001000; char s[M]; int lens, n; int a[M], lena, b[M], lenb; void init() { char ch = getchar(); s[++lens] = '+'; lena++; while (ch < '0' || ch > '9') { if (ch == '-' || ch == '+') { s[++lens] = ch; if (ch == '+') lena++; else lenb++; } ch = getchar(); } while (ch <= '9' && ch >= '0') { n *= 10; n += ch - '0'; ch = getchar(); } } int main() { init(); int sum = lena * n - lenb; for (int i = 1; i <= lena; i++) a[i] = n; for (int i = 1; i <= lenb; i++) b[i] = 1; if (sum < n) { printf("Impossible"); return 0; } int j = 1; int k = 1; for (int i = n + 1; i <= sum; i++) { if (j == lena + 1) { if (b[k] < n) b[k]++; else { k++; b[k]++; } } else { if (a[j] > 1) a[j]--; else { j++; if (a[j] > 1) a[j]--; else i--; } } } sum = 0; for (int i = 1; i <= lena; i++) sum += a[i]; for (int i = 1; i <= lenb; i++) sum -= b[i]; if (sum != n) { printf("Impossible"); return 0; } printf("Possible\n"); j = 2, k = 1; printf("%d ", a[1]); for (int i = 2; i <= lens; i++) if (s[i] == '+') { printf("+ "); printf("%d ", a[j]); j++; } else { printf("- "); printf("%d ", b[k]); k++; } printf("= %d", n); return 0; }
#include <bits/stdc++.h> using namespace std; int ans[101000], mark[101000]; char s[101000]; int n, top, now, n_; int main() { do s[++n_] = getchar(); while (s[n_] != '='); scanf("%d", &n); mark[1] = 0; top = 1; for (int i = 3; i < n_; i += 4) mark[++top] = (s[i] == '-'); now = 0; for (int i = 1; i <= top; i++) ans[i] = 1; for (int i = 1; i <= top; i++) if (mark[i]) now--; else now++; while (now != n) { if (now < n) { int q = 0; for (int i = 1; i <= top; i++) if (!mark[i] && ans[i] < n) { q = i; break; } if (!q) { printf("Impossible"); return 0; } int inc = min(n - ans[q], n - now); ans[q] += inc; now += inc; } else { int q = 0; for (int i = 1; i <= top; i++) if (mark[i] && ans[i] < n) { q = i; break; } if (!q) { printf("Impossible"); return 0; } int inc = min(n - ans[q], now - n); ans[q] += inc; now -= inc; } } printf("Possible\n"); printf("%d", ans[1]); for (int i = 2; i <= top; i++) { putchar(' '); if (mark[i]) putchar('-'); else putchar('+'); putchar(' '); printf("%d", ans[i]); } printf(" = %d", n); return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1000000000000; const double eps = (double)1e-8; const int mod = (int)1000000007; const int maxn = (int)1e5 + 5; int n, o, x, kp, km, t, mn, mx; vector<int> a; string st; int dzen(string st) { int x; x = 0; for (int i = (0); i < (st.size()); i++) { x = x * 10 + (st[i] - '0'); } return x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> st; km = kp = 0; t = 1; while (st != "=") { if (st == "+") t = 1; else if (st == "-") t = 0; else { if (t) { kp++; a.push_back(1); } else { km++; a.push_back(0); } } cin >> st; } cin >> st; o = dzen(st); mn = -o * km + 1 * kp; mx = -1 * km + o * kp; if (o < mn || o > mx) { cout << "Impossible" << "\n"; return 0; } cout << "Possible" << "\n"; n = o; for (int i = (0); i < (a.size()); i++) { if (a[i]) { kp--; mn = -o * km + 1 * kp; mx = -1 * km + o * kp; x = max(1, n - mx); n -= x; if (i != 0) cout << "+ "; cout << x << " "; } else { km--; mn = -o * km + 1 * kp; mx = -1 * km + o * kp; x = max(1, mn - n); n += x; if (i != 0) cout << "- "; cout << x << " "; } } cout << "= " << o << "\n"; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 1000, M = N * (N - 1) / 2; int dsu[N * 2]; int find(int i) { return dsu[i] < 0 ? i : (dsu[i] = find(dsu[i])); } bool join(int i, int j) { i = find(i); j = find(j); if (i == j) return false; if (dsu[i] > dsu[j]) dsu[i] = j; else { if (dsu[i] == dsu[j]) dsu[i]--; dsu[j] = i; } return true; } int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); static int ii[M], jj[M], ww[M], hh[M]; for (int h = 0; h < m; h++) { int i, j, w; scanf("%d%d%d", &i, &j, &w), i--, j--; ii[h] = i; jj[h] = j; ww[h] = w; hh[h] = h; } sort(hh, hh + m, [](int a, int b) { return ww[a] > ww[b]; }); while (q-- > 0) { int l, r; scanf("%d%d", &l, &r), l--, r--; fill_n(dsu, n * 2, -1); int w = -1; for (int h = 0; h < m; h++) { int h_ = hh[h]; if (l <= h_ && h_ <= r) { int i = ii[h_]; int j = jj[h_]; int i0 = i << 1, i1 = i0 | 1; int j0 = j << 1, j1 = j0 | 1; if (join(i0, j1) && !join(i1, j0)) { w = ww[h_]; break; } } } printf("%d\n", w); } }
#include <bits/stdc++.h> using namespace std; int n, m, q, dsu[2005]; map<pair<int, int>, int> ans; struct edge { int x, y, z, u; bool operator<(const edge b) const { return z < b.z; } } e[500005]; int find_(int x) { return x == dsu[x] ? x : dsu[x] = find_(dsu[x]); } void unite_(int x, int y) { dsu[find_(x)] = dsu[find_(y)]; } signed main() { cin >> n >> m >> q; for (int i = 1; i <= m; scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z), e[i].u = i, i++) ; sort(e + 1, e + 1 + m); while (q--) { int el, er; scanf("%d%d", &el, &er); for (int i = 1; i <= n * 2; i++) dsu[i] = i; bool flag = 0; for (int i = m; i >= 1; i--) if (e[i].u >= el && e[i].u <= er) { int fx = find_(e[i].x), fy = find_(e[i].y); if (fx == fy) { printf("%d\n", e[i].z); flag = 1; break; } unite_(e[i].x, e[i].y + n), unite_(e[i].x + n, e[i].y); } if (!flag) puts("-1"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1079; class edge { public: int u, v, w; }; class dsu { vector<int> p, b, siz; int highest(int v) { if (v == p[v]) return v; int r = highest(p[v]); b[v] = b[p[v]] ^ b[v]; return p[v] = r; } public: dsu(int n) : p(vector<int>(n)), b(vector<int>(n, 0)), siz(vector<int>(n, 1)) { clear(); } void clear() { for (int i = 0; i < b.size(); i++) { b[i] = 0, siz[i] = 1, p[i] = i; } } bool parity(int v) { highest(v); return b[v]; } int merge(int u, int v) { int ua = highest(u), va = highest(v); if (ua == va) { if (parity(u) == parity(v)) return 0; return 1; } if (siz[ua] < siz[va]) swap(ua, va); b[va] = parity(u) ^ 1 ^ parity(v); siz[ua] += siz[va]; p[va] = ua; return 2; } }; dsu u(maxn); vector<vector<edge> > st; vector<edge> merge(vector<edge>& a, vector<edge>& b) { u.clear(); vector<edge> c; int ai = 0, bi = 0; while (ai < a.size() || bi < b.size()) { edge i; if (bi == b.size() || (ai < a.size() && a[ai].w > b[bi].w)) i = a[ai++]; else i = b[bi++]; int res = u.merge(i.u, i.v); if (res == 0 || res == 2) c.push_back(i); if (!res) break; } return c; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<edge> e(m); st.resize(2 * m); for (int i = 0; i < m; i++) { cin >> e[i].u >> e[i].v >> e[i].w; e[i].u--; e[i].v--; st[m + i].push_back(e[i]); } for (int i = m - 1; i; i--) st[i] = merge(st[i << 1], st[i << 1 | 1]); while (q--) { int l, r; cin >> l >> r; l--; vector<edge> v; for (l += m, r += m; l < r; l >>= 1, r >>= 1) { if (l & 1) v = merge(v, st[l++]); if (r & 1) v = merge(v, st[--r]); } u.clear(); int ans = -1; for (int i = 0; i < v.size(); i++) { if (!u.merge(v[i].u, v[i].v)) { ans = v[i].w; break; } } cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const int maxM = 1100 * 1100; const int maxN = 1100; int n, m, q; int order[maxM]; int u[maxM], v[maxM], w[maxM]; int p[maxN], s[maxN], color[maxN]; vector<int> g[maxN]; int findSet(int v) { if (p[v] == v) { return v; } return p[v] = findSet(p[v]); } bool comp(int x, int y) { return w[x] > w[y]; } void invert(int v) { color[v] = 3 - color[v]; for (int i = 0; i < g[v].size(); ++i) { invert(g[v][i]); } } void solve(int l, int r) { for (int i = 0; i < n; ++i) { g[i].clear(); p[i] = i; color[i] = 1; s[i] = 1; } for (int i = 0; i < m; ++i) { if (order[i] < l || order[i] > r) { continue; } int u = ::u[order[i]]; int v = ::v[order[i]]; int pu = findSet(u); int pv = findSet(v); if (pu == pv) { if (color[u] == color[v]) { printf("%d\n", w[order[i]]); return; } continue; } if (color[u] == color[v]) { if (s[pu] < s[pv]) { invert(pu); } else { invert(pv); } } if (rand() % 2) { p[pu] = pv; s[pv] += s[pu]; g[pv].push_back(pu); } else { p[pv] = pu; s[pu] += s[pv]; g[pu].push_back(pv); } } printf("-1\n"); } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < m; ++i) { scanf("%d%d%d", &u[i], &v[i], &w[i]); --u[i]; --v[i]; order[i] = i; } sort(order, order + m, comp); for (int i = 0; i < q; ++i) { int l, r; scanf("%d%d", &l, &r); solve(l - 1, r - 1); } return 0; }
#include <bits/stdc++.h> using namespace std; namespace std { template <class S, class T> struct hash<pair<S, T>> { size_t operator()(const pair<S, T> &p) const { return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second); } }; template <class T> struct hash<vector<T>> { size_t operator()(const vector<T> &v) const { size_t h = 0; for (auto i : v) h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1; return h; } }; } // namespace std template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "[ "; for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i == v.size() - 1 ? " ]" : ", "); return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &v) { os << "{ "; for (const auto &i : v) os << i << ", "; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const map<T, U> &v) { os << "{"; for (const auto &i : v) os << " " << i.first << ": " << i.second << ","; return os << "}"; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "(" << p.first << ", " << p.second << ")"; } void dbgprint(const string &fmt) { cerr << endl; } template <class H, class... T> void dbgprint(const string &fmt, const H &h, const T &...r) { cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " "; dbgprint(fmt.substr(fmt.find(",") + 1), r...); } const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; int n, m, q; vector<pair<pair<int, int>, pair<int, int>>> e; int p[2000]; inline int root(int x) { return x == p[x] ? x : (p[x] = root(p[x])); } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < (int)m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--; b--; e.emplace_back(make_pair(c, i), make_pair(a, b)); } sort((e).begin(), (e).end()); while (q--) { int l, r; scanf("%d%d", &l, &r); l--; int ans = -1; for (int i = 0; i < (int)2 * n; i++) p[i] = i; for (int i = (int)e.size() - 1; i >= 0; i--) if (l <= e[i].first.second && e[i].first.second < r) { int a = e[i].second.first, b = e[i].second.second; int a0 = root(a * 2), b1 = root(b * 2 + 1); if (a0 == b1) continue; p[b1] = a0; int b0 = root(b * 2), a1 = root(a * 2 + 1); p[b0] = a1; if (root(a * 2) == root(b * 2)) { ans = e[i].first.first; break; } } printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int N, M, Q; pair<pair<int, int>, pair<int, int> > E[1000005]; int par[1005]; bool type[1005]; int find_parent(int x) { if (par[x] == x) return x; int y = par[x]; par[x] = find_parent(par[x]); type[x] ^= type[y]; return par[x]; } bool merge(int a, int b) { int pa = find_parent(a), pb = find_parent(b); if (pa == pb) return type[a] != type[b]; par[pa] = pb; type[pa] = type[a] ^ type[b] ^ 1; return true; } int go(int li, int ri) { memset(type, 0, sizeof(type)); for (int i = 1; i <= N; i++) par[i] = i; for (int i = M - 1; i >= 0; i--) if (li <= E[i].first.second && E[i].first.second <= ri) if (!merge(E[i].second.first, E[i].second.second)) return E[i].first.first; return -1; } int main() { scanf("%d %d %d", &N, &M, &Q); for (int i = 0; i < M; i++) { scanf("%d %d %d", &E[i].second.first, &E[i].second.second, &E[i].first.first); E[i].first.second = i + 1; } sort(E, E + M); for (int i = 0, li, ri; i < Q; i++) { scanf("%d %d", &li, &ri); printf("%d\n", go(li, ri)); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 10050; int pa[maxn]; struct Point { int u, v, dis, id; } a[maxn * 100]; bool cmp(Point x, Point y) { return x.dis > y.dis; } int find(int x) { if (pa[x] == x) return x; return pa[x] = find(pa[x]); } void unite(int x, int y) { x = find(x), y = find(y); pa[x] = y; } int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].dis); a[i].id = i; } sort(a + 1, a + 1 + m, cmp); while (q--) { int l, r; scanf("%d%d", &l, &r); int ans = -1; for (int i = 1; i <= 2 * n; i++) pa[i] = i; for (int i = 1; i <= m; i++) { if (!(a[i].id <= r && a[i].id >= l)) continue; if (find(a[i].u) == find(a[i].v)) { ans = a[i].dis; break; } else { unite(a[i].u, a[i].v + n); unite(a[i].u + n, a[i].v); } } printf("%d\n", ans); } }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 1, x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + (s - '0'); s = getchar(); } return x * f; } const int jzm = 1000000007; int n, m, i, j, s, o, k; struct it { int u, v, w, id; } e[1000005]; bool cmp(it a, it b) { return a.w > b.w; } int fa[1005 << 1]; int findf(int x) { return x == fa[x] ? x : (fa[x] = findf(fa[x])); } void unionSet(int a, int b) { int u = findf(a), v = findf(b); if (u != v) fa[u] = v; } int main() { n = read(); m = read(); int q = read(); for (int i = 1; i <= m; i++) { e[i].u = read(); e[i].v = read(); e[i].w = read(); e[i].id = i; } sort(e + 1, e + 1 + m, cmp); while (q--) { s = read(); o = read(); for (int i = 1; i <= 2 * n; i++) { fa[i] = i; } for (int i = 1; i <= m + 1; i++) { if (i == m + 1) { printf("-1\n"); break; } if (e[i].id >= s && e[i].id <= o) { int a = e[i].u, b = e[i].v; if (findf(a) == findf(b)) { printf("%d\n", e[i].w); break; } else { unionSet(a + n, b); unionSet(a, b + n); } } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; int N, fa[MAXN], M, Q, siz[MAXN]; struct edge { int x, y, v, id; friend bool operator<(edge a, edge b) { return a.v > b.v; } } E[MAXN]; void onion(int, int); int find_(int); int read() { int x = 0, f = 1; char s = getchar(); for (; !isdigit(s); s = getchar()) if (s == '-') f = -1; for (; isdigit(s); s = getchar()) x = 10 * x + (s - '0'); return x * f; } int main() { N = read(); M = read(); Q = read(); for (int i = 1; i <= M; i++) { E[i].x = read(); E[i].y = read(); E[i].v = read(); E[i].id = i; } sort(E + 1, E + M + 1); while (Q--) { int l, r, ans = -1; l = read(); r = read(); for (int i = 1; i <= 2 * N; i++) fa[i] = i, siz[i] = 1; for (int i = 1; i <= M; i++) { if (l <= E[i].id && E[i].id <= r) { if (find_(E[i].x) == find_(E[i].y)) { ans = E[i].v; break; } else { onion(E[i].x + N, E[i].y); onion(E[i].x, E[i].y + N); } } } printf("%d\n", ans); } return 0; } void onion(int x, int y) { int fx = find_(x), fy = find_(y); if (fx != fy) { if (siz[fx] > siz[fy]) swap(fx, fy); fa[fx] = fy; siz[fy] += siz[fx]; } } int find_(int x) { return fa[x] == x ? x : fa[x] = find_(fa[x]); }
#include <bits/stdc++.h> using namespace std; long long n, m, q, fa[1000010], ans, l, r; struct lujing { long long u, v, c, id; } f[1000010]; inline long long read() { register long long w = 0, e = 1; char ch = getchar(); while (!isdigit(ch)) { e = ch == '-' ? -1 : 1; ch = getchar(); } while (isdigit(ch)) { w = (w << 3) + (w << 1) + (ch ^ 48); ch = getchar(); } return w * e; } long long find(long long x) { return (fa[x] == x) ? x : (fa[x] = find(fa[x])); } inline void add(long long x, long long y) { register long long xx = find(x), yy = find(y); if (xx != yy) fa[xx] = yy; } inline bool pd(long long x, long long y) { register long long xx = find(x), yy = find(y); if (xx == yy) return true; return false; } inline bool cmp(lujing x, lujing y) { return x.c > y.c; } int main() { n = read(); m = read(); q = read(); for (register int i = 1; i <= m; ++i) f[i].u = read(), f[i].v = read(), f[i].c = read(), f[i].id = i; sort(f + 1, f + 1 + m, cmp); while (q--) { l = read(), r = read(); ans = -1; for (register int i = 1; i <= n * 2; ++i) fa[i] = i; for (register int i = 1; i <= m; ++i) { if (f[i].id >= l && f[i].id <= r) { if (pd(f[i].u, f[i].v)) { ans = f[i].c; break; } else { add(f[i].u + n, f[i].v); add(f[i].u, f[i].v + n); } } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> const int N = 1000; const int M = N * (N - 1) / 2; int n, m, q, a[M], b[M], c[M], aa[M], bb[M], cc[M], order[M], parent[N << 1]; int find(int u) { int r = u; while (parent[r] != r) { r = parent[r]; } while (u != r) { int p = parent[u]; parent[u] = r; u = p; } return r; } bool add(int a, int b) { if (find(a << 1) == find(b << 1)) { return false; } parent[find(a << 1)] = find(b << 1 | 1); parent[find(b << 1)] = find(a << 1 | 1); return true; } bool by_c(int i, int j) { return c[i] > c[j]; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < m; ++i) { scanf("%d%d%d", a + i, b + i, c + i); a[i]--; b[i]--; order[i] = i; } std::sort(order, order + m, by_c); for (int i = 0; i < m; ++i) { aa[i] = a[order[i]]; bb[i] = b[order[i]]; cc[i] = c[order[i]]; } for (int i = 0; i < q; ++i) { int l, r; scanf("%d%d", &l, &r); l--; int result = -1; std::iota(parent, parent + (n << 1), 0); for (int j = 0; j < m && !~result; ++j) { auto&& index = order[j]; if (l <= index && index < r && !add(aa[j], bb[j])) { result = cc[j]; } } printf("%d\n", result); } }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10, inf = 2e9, mod = 1e9 + 7; int n, m, q, fa[N]; struct ss { int u, v, w, id; } s[N]; bool cmp(ss s1, ss s2) { return s1.w > s2.w; } int finds(int x) { return x == fa[x] ? x : fa[x] = finds(fa[x]); } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &s[i].u, &s[i].v, &s[i].w); s[i].id = i; } sort(s + 1, s + m + 1, cmp); while (q--) { int f = 0, l, r; scanf("%d%d", &l, &r); for (int i = 1; i <= n * 3; i++) fa[i] = i; for (int i = 1; i <= m; i++) { if (s[i].id >= l && s[i].id <= r) { int fx = finds(s[i].u), fy = finds(s[i].v); if (fx == fy) { f = 1; printf("%d\n", s[i].w); break; } else { fa[fx] = finds(s[i].v + n); fa[fy] = finds(s[i].u + n); } } } if (!f) printf("%d\n", -1); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; struct ed { int x, y, w, id; } e[maxn * maxn]; bool cmp(ed a, ed b) { return a.w > b.w; } int fa[maxn], en[maxn]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void Merge(int x, int y) { int f = find(y); fa[f] = find(x); } int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].w); e[i].id = i; } sort(e + 1, e + m + 1, cmp); while (q--) { memset(en, -1, sizeof(en)); for (int i = 1; i <= n; i++) fa[i] = i; int l, r, ans = -1; scanf("%d%d", &l, &r); for (int i = 1; i <= m; i++) if (e[i].id >= l && e[i].id <= r) { if (find(e[i].x) == find(e[i].y)) { ans = e[i].w; break; } if (en[e[i].x] != -1) Merge(en[e[i].x], e[i].y); else en[e[i].x] = e[i].y; if (en[e[i].y] != -1) Merge(en[e[i].y], e[i].x); else en[e[i].y] = e[i].x; } printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, q; int from[500000], to[500000], w[500000], ord[500000], tmp[500000]; int c[1000], col[1000]; vector<int> cm[1000]; inline bool cmp(const int &lhs, const int &rhs) { return w[lhs] > w[rhs]; } void reord(int *a) { for (int i = 0; i < (int)(m); ++i) tmp[i] = a[ord[i]]; for (int i = 0; i < (int)(m); ++i) a[i] = tmp[i]; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < (int)(m); ++i) scanf("%d%d%d", from + i, to + i, w + i), --from[i], --to[i]; for (int i = 0; i < (int)(m); ++i) ord[i] = i; sort(ord, ord + m, cmp); reord(from); reord(to); reord(w); for (int times = 0; times < (int)(q); ++times) { int l, r; scanf("%d%d", &l, &r), --l, --r; for (int i = 0; i < (int)(n); ++i) { c[i] = i; cm[i].clear(); cm[i].push_back(i); col[i] = 0; } int ans = -1; for (int i = 0; i < (int)(m); ++i) { if (ord[i] < l || ord[i] > r) continue; int s = from[i]; int t = to[i]; if (c[s] == c[t]) { if (col[s] == col[t]) { ans = w[i]; break; } } else { bool eq = col[s] == col[t]; s = c[s]; t = c[t]; if (cm[s].size() < cm[t].size()) { swap(s, t); } if (eq) { for (int x : cm[t]) { col[x] ^= 1; } } for (int x : cm[t]) { cm[s].push_back(x); c[x] = s; } } } printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } inline int get() { char c; while (isspace(c = getchar())) ; int x = c - '0'; while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + (c - '0'); return x; } const int N = 1123; int S[N], sz[N]; int c[N], ch[N]; int tmp; int find(int i, int f) { if (S[S[i]] != S[i]) S[i] = find(S[i], f); return S[i]; } vector<int> V[N]; inline void join(int a, int b) { if (sz[a] < sz[b]) swap(a, b); S[b] = a; assert(!ch[a]); sz[a] += sz[b]; for (int x : V[b]) V[a].push_back(x); } const int M = 1123456; int a[M], b[M], w[M], p[M]; int main() { srand(time(NULL)); int i, j, n, m, q, l, r; n = get(); m = get(); q = get(); for (i = 0; i < m; i++) { a[i] = get() - 1; b[i] = get() - 1; if (rand() & 1) swap(a[i], b[i]); w[i] = get(); p[i] = i; } sort(p, p + m, [](int i, int j) { return w[i] > w[j]; }); while (q--) { l = get() - 1; r = get() - 1; for (i = 0; i < n; i++) S[i] = i, sz[i] = 1, c[i] = -1, ch[i] = 0, V[i].clear(), V[i].push_back(i); for (j = 0; j < m; j++) { i = p[j]; if (i < l || i > r) continue; int a = ::a[i], b = ::b[i]; int xa = find(a, tmp), xb = find(b, tmp); if (c[a] > c[b]) swap(a, b); if (c[b] == -1) c[a] = 0, c[b] = 1, join(xa, xb); else if (c[a] == c[b]) { if (xa == xb) { printf("%d\n", w[i]); break; } if (sz[xa] < sz[xb]) swap(xa, xb); for (int id : V[xb]) c[id] = !c[id]; join(xa, xb); } else if (c[a] == -1) c[a] = !c[b], join(xa, xb); else if (xa != xb) join(xa, xb); } if (j == m) puts("-1"); } }
#include <bits/stdc++.h> using namespace std; long long int powm(long long int a, int b, int n) { long long int rm = 1; while (b) { if (b % 2) { rm = (rm * a) % n; } a = (a * a) % n; b /= 2; } return rm; } int p[int(1e3 + 10)], G[int(1e3 + 10)], sz[int(1e3 + 10)] = {0}; pair<pair<int, int>, pair<int, int> > E[int(1e3 + 10) * int(1e3 + 10)]; int vec[int(1e3 + 10)][int(1e3 + 10)], pt[int(1e3 + 10)]; int merge(int node) { if (p[node] == node) return node; p[node] = merge(p[node]); return p[node]; } int f(int val) { if (val == 1) return 2; return 1; } int main() { int n, m, q, u, v, w, l, r; scanf("%d", &n); ; scanf("%d", &m); ; scanf("%d", &q); ; for (int i = int(1); i <= (int)m; i++) { scanf("%d", &u); ; scanf("%d", &v); ; scanf("%d", &w); ; E[i] = make_pair(make_pair(w, i), make_pair(u, v)); } sort(E + 1, E + m + 1); while (q--) { int stop = 0, ans = 0; scanf("%d", &l); ; scanf("%d", &r); ; memset(G, 0, sizeof(G)); ; for (int i = int(1); i <= (int)n; i++) p[i] = i, vec[i][1] = i, pt[i] = 1; E[m + 1].first.first = E[m].first.first + 1; for (int j = m; j >= 1; j--) { if (E[j].first.second < l || E[j].first.second > r) continue; u = E[j].second.first, v = E[j].second.second; if (!G[u] && !G[v]) { G[u] = 1; G[v] = 2; p[u] = p[v] = v; vec[v][++pt[v]] = u; pt[u] = 0; } else if (!G[u] && G[v]) { merge(v); if (G[v] == 1) G[u] = 2; else G[u] = 1; p[u] = p[v]; vec[p[v]][++pt[p[v]]] = u; pt[u] = 0; } else if (G[u] && !G[v]) { merge(u); if (G[u] == 1) G[v] = 2; else G[v] = 1; p[v] = p[u]; vec[p[u]][++pt[p[u]]] = v; pt[v] = 0; } else { merge(u); merge(v); if (p[u] == p[v]) { if (G[u] == G[v]) { stop = j; break; } } else { if (pt[p[u]] > pt[p[v]]) swap(u, v); if (G[u] == G[v]) { for (int i = 1; i <= pt[p[u]]; ++i) G[vec[p[u]][i]] = f(G[vec[p[u]][i]]); } for (int i = 1; i <= pt[p[u]]; ++i) vec[p[v]][++pt[p[v]]] = vec[p[u]][i]; pt[p[u]] = 0; p[p[u]] = p[v]; } } } if (stop == 0) ans = -1; else ans = E[stop].first.first; printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long int myRand(long long int B) { return (unsigned long long)rng() % B; } struct UnionFind { vector<int> par, num; vector<bool> done; UnionFind(int n) : par(n), num(n, 1), done(n, false) { iota(par.begin(), par.end(), 0); } int find(int v) { return (par[v] == v) ? v : (par[v] = find(par[v])); } void unite(int u, int v) { u = find(u), v = find(v); if (u == v) return; if (num[u] < num[v]) swap(u, v); num[u] += num[v]; par[v] = u; done[u] = done[u] | done[v]; } bool same(int u, int v) { return find(u) == find(v); } bool ispar(int v) { return v = find(v); } int size(int v) { return num[find(v)]; } }; const int BS = 524288; int CNT, rc; char inb[BS]; inline char gchar(void) { if (CNT % BS == 0) { CNT = 0; rc = fread(inb, 1, BS, stdin); } return CNT < rc ? inb[CNT++] : 0; } inline int _readint(int *ptr) { int n = 0, c = 0; *ptr = 0; while (1) { char read = gchar(); if (read >= '0' && read <= '9') read -= '0', n *= 10, n += read; else { *ptr = n; return n == 0 ? 0 : !c; } c++; } *ptr = n; return 0; } inline int readint(void) { int ptr; while (_readint(&ptr)) ; return ptr; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m, q; cin >> n >> m >> q; vector<int> x(m), y(m), w(m); vector<pair<int, int>> v(m); for (int i = 0; i < m; i++) { cin >> x[i] >> y[i] >> w[i]; x[i]--; y[i]--; v[i] = make_pair(w[i], i); } sort(v.rbegin(), v.rend()); while (q--) { int l, r; cin >> l >> r; l--; int res = -1; UnionFind uf(2 * n); for (int i = 0; i < v.size(); i++) { if (v[i].second < l || r <= v[i].second) continue; int id = v[i].second; if (uf.same(x[id], y[id])) { res = v[i].first; break; } else { uf.unite(x[id], y[id] + n); uf.unite(x[id] + n, y[id]); } } printf("%d\n", res); } }
#include <bits/stdc++.h> using namespace std; int n, q, m, f[2005]; struct node { int x, y, z, id; } a[500000]; inline int read() { int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = (x + (x << 2) << 1) + (ch ^ 48); return x * f; } bool cmp(const node &x, const node &y) { return x.z > y.z; } int getf(int x) { if (x == f[x]) return x; return f[x] = getf(f[x]); } int main() { n = read(), m = read(), q = read(); for (int i = 1; i <= m; i++) a[i].x = read(), a[i].y = read(), a[i].z = read(), a[i].id = i; sort(a + 1, a + m + 1, cmp); while (q--) { int l = read(), r = read(), ans = -1; for (int i = 1; i <= (n << 1); i++) f[i] = i; for (int i = 1; i <= m; i++) if (a[i].id >= l && a[i].id <= r) { int x = getf(a[i].x), y = getf(a[i].y); if (x == y) { ans = a[i].z; break; } f[getf(a[i].y + n)] = x, f[y] = getf(a[i].x + n); } printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> sav[1001000 << 2]; int n, m, q, fa[1010], rl[1010]; struct data { int u, v, w; } ed[1001000]; int find(int k) { if (fa[k] == k) return k; int f = find(fa[k]); rl[k] ^= rl[fa[k]]; return fa[k] = f; } void Merge(vector<int> &rt, vector<int> ls, vector<int> rs) { int i = 0, j = 0; vector<int> tmp; rt.clear(); tmp.clear(); while (i < (int)ls.size() && j < (int)rs.size()) if (ed[ls[i]].w > ed[rs[j]].w) rt.push_back(ls[i++]); else rt.push_back(rs[j++]); while (i < (int)ls.size()) rt.push_back(ls[i++]); while (j < (int)rs.size()) rt.push_back(rs[j++]); for (int i = 1; i <= n; i++) fa[i] = i, rl[i] = 0; for (int i = 0; i <= (int)rt.size() - 1; i++) { int fx = find(ed[rt[i]].u), fy = find(ed[rt[i]].v); if (fx != fy) fa[fx] = fy, rl[fx] = rl[ed[rt[i]].u] ^ rl[ed[rt[i]].v] ^ 1, tmp.push_back(rt[i]); else if (rl[ed[rt[i]].u] == rl[ed[rt[i]].v]) { tmp.push_back(rt[i]); break; } } rt = tmp; } void Build(int rt, int l, int r) { sav[rt].clear(); if (l == r) { sav[rt].push_back(l); return; } Build(rt << 1, l, ((l + r) >> 1)); Build(rt << 1 | 1, ((l + r) >> 1) + 1, r); Merge(sav[rt], sav[rt << 1], sav[rt << 1 | 1]); } bool cmp(int a, int b) { return ed[a].w > ed[b].w; } int Query(int rt, int l, int r, int L, int R, vector<int> &tmp) { int flag = 0; if (l >= L && r <= R) { for (int i = 0; i <= (int)sav[rt].size() - 1; i++) tmp.push_back(sav[rt][i]); if (rt == 1) flag = 1; else return 2333; } if (!flag && L <= ((l + r) >> 1)) Query(rt << 1, l, ((l + r) >> 1), L, R, tmp); if (!flag && R > ((l + r) >> 1)) Query(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R, tmp); if (rt == 1) { int ans = -1; for (int i = 1; i <= n; i++) fa[i] = i, rl[i] = 0; sort(tmp.begin(), tmp.end(), cmp); for (int i = 0; i <= (int)tmp.size() - 1; i++) { int fx = find(ed[tmp[i]].u), fy = find(ed[tmp[i]].v); if (fx != fy) fa[fx] = fy, rl[fx] = rl[ed[tmp[i]].u] ^ rl[ed[tmp[i]].v] ^ 1; else if (rl[ed[tmp[i]].u] == rl[ed[tmp[i]].v]) { ans = ed[tmp[i]].w; break; } } return ans; } return 2333; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= m; i++) scanf("%d%d%d", &ed[i].u, &ed[i].v, &ed[i].w); Build(1, 1, m); while (q--) { int l, r; scanf("%d%d", &l, &r); vector<int> tmp; tmp.clear(); printf("%d\n", Query(1, 1, m, l, r, tmp)); } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 501 * 1001; vector<pair<pair<int, int>, pair<int, int> > > yal; int par[MAXN] = {}, col[MAXN] = {}; int root(int x) { if (par[x] < 0) { return x; } int p = root(par[x]); col[x] ^= col[par[x]]; return par[x] = p; } int main() { int n, q, m, l, r; scanf("%d%d%d", &n, &m, &q); int x, y, z; for (int i = 0; i < m; i++) scanf("%d%d%d", &x, &y, &z), yal.push_back({{z, i}, {x, y}}); sort(yal.rbegin(), yal.rend()); for (int i = 0; i < q; i++) { scanf("%d%d", &l, &r); for (int i = 1; i <= n; i++) par[i] = -1, col[i] = 0; bool f = 1; for (int j = 0; j < m && f; j++) { if (yal[j].first.second >= l - 1 && yal[j].first.second < r) { x = yal[j].second.first, y = yal[j].second.second; if (root(x) == root(y)) { if (col[x] == col[y]) printf("%d\n", yal[j].first.first), f = 0; } else { bool ff = 0; if (col[x] == col[y]) ff = 1; x = root(x); y = root(y); if (par[y] < par[x]) swap(x, y); par[x] += par[y]; if (ff) { col[y] = 1; } par[y] = x; } } } if (f) puts("-1"); } }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-9; const int INF = 1E9; const int MAXN = 1111; const int MAXM = 1000500; int n, q, m, a, b, c, u; pair<pair<int, int>, pair<int, int> > edges[MAXM]; int color[MAXN], setId[MAXN]; int sets[MAXN][MAXN]; int sz[MAXN]; int id; int idu, idv, f; void mergeTo(int from, int to, int x) { for (int i = 0; i < (int)(sz[from]); i++) { u = sets[from][i]; setId[u] = to; color[u] ^= x; sets[to][sz[to]++] = u; } sz[from] = 0; } bool add(int u, int v) { if (color[u] == -1 && color[v] == -1) { color[u] = 0; color[v] = 1; id = setId[u]; setId[v] = id; sets[id][sz[id]++] = v; return 1; } if (color[u] == -1) return add(v, u); if (color[v] == -1) { color[v] = color[u] ^ 1; id = setId[u]; setId[v] = setId[u]; sets[id][sz[id]++] = v; return 1; } idu = setId[u]; idv = setId[v]; if (idu == idv) return color[u] != color[v]; f = color[u] == color[v]; if (sz[idu] <= sz[idv]) { mergeTo(idu, idv, f); } else { mergeTo(idv, idu, f); } return 1; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < (int)(m); i++) { scanf("%d%d%d", &a, &b, &c); a--, b--; edges[i] = {{c, i}, {a, b}}; } sort(edges, edges + m); reverse(edges, edges + m); for (int iii = 0; iii < (int)(q); iii++) { int L, R; scanf("%d%d", &L, &R); L--, R--; for (int i = 0; i < (int)(n); i++) { color[i] = -1; setId[i] = i; sets[i][0] = i; sz[i] = 1; } int ans = -1; for (int i = 0; i < (int)(m); i++) if (L <= edges[i].first.second && edges[i].first.second <= R) { if (!add(edges[i].second.first, edges[i].second.second)) { ans = edges[i].first.first; break; } } printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("inline,Ofast", 3) #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma GCC optimize("-fpeephole2") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-fsched-spec") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-falign-jumps") #pragma GCC optimize("-falign-loops") #pragma GCC optimize("-falign-labels") #pragma GCC optimize("-fdevirtualize") #pragma GCC optimize("-fcaller-saves") #pragma GCC optimize("-fcrossjumping") #pragma GCC optimize("-fthread-jumps") #pragma GCC optimize("-funroll-loops") #pragma GCC optimize("-fwhole-program") #pragma GCC optimize("-freorder-blocks") #pragma GCC optimize("-fschedule-insns") #pragma GCC optimize("inline-functions") #pragma GCC optimize("-fschedule-insns2") #pragma GCC optimize("-fstrict-aliasing") #pragma GCC optimize("-fstrict-overflow") #pragma GCC optimize("-falign-functions") #pragma GCC optimize("-fcse-skip-blocks") #pragma GCC optimize("-fcse-follow-jumps") #pragma GCC optimize("-fsched-interblock") #pragma GCC optimize("-fpartial-inlining") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("-freorder-functions") #pragma GCC optimize("-findirect-inlining") #pragma GCC optimize("inline-small-functions") #pragma GCC optimize("-finline-small-functions") #pragma GCC optimize("-ftree-switch-conversion") #pragma GCC optimize("-foptimize-sibling-calls") #pragma GCC optimize("-fexpensive-optimizations") #pragma GCC optimize("-funsafe-loop-optimizations") #pragma GCC optimize("inline-functions-called-once") #pragma GCC optimize("-fdelete-null-pointer-checks") using namespace std; long long n, m, q, fa[1000010], ans, l, r; struct lujing { long long u, v, c, id; } f[1000010]; inline long long read() { long long w = 0, e = 1; char ch = getchar(); while (!isdigit(ch)) { e = ch == '-' ? -1 : 1; ch = getchar(); } while (isdigit(ch)) { w = (w << 3) + (w << 1) + (ch ^ 48); ch = getchar(); } return w * e; } long long find(long long x) { return (fa[x] == x) ? x : (fa[x] = find(fa[x])); } void add(long long x, long long y) { long long xx = find(x), yy = find(y); if (xx != yy) fa[xx] = yy; } bool pd(long long x, long long y) { long long xx = find(x), yy = find(y); if (xx == yy) return true; return false; } bool cmp(lujing x, lujing y) { return x.c > y.c; } int main() { n = read(); m = read(); q = read(); for (register int i = 1; i <= m; ++i) f[i].u = read(), f[i].v = read(), f[i].c = read(), f[i].id = i; sort(f + 1, f + 1 + m, cmp); while (q--) { l = read(), r = read(); ans = -1; for (register int i = 1; i <= n * 2; ++i) fa[i] = i; for (register int i = 1; i <= m; ++i) { if (f[i].id >= l && f[i].id <= r) { if (pd(f[i].u, f[i].v)) { ans = f[i].c; break; } else { add(f[i].u + n, f[i].v); add(f[i].u, f[i].v + n); } } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int par[1001], r[1001], p[1001]; int n, m, q; vector<pair<int, pair<int, int> > > segt[2 * 1000 * 999]; multiset<pair<int, pair<int, int> > > mu; void up(int node, int l, int r, int in, pair<int, pair<int, int> > v) { segt[node].push_back(v); if (l == r) return; int m = (l + r) / 2; if (in <= m) up(2 * node + 1, l, m, in, v); else up(2 * node + 2, m + 1, r, in, v); } void qu(int node, int l, int r, int s, int e) { if (l > e || r < s) return; if (l >= s && r <= e) { mu.insert(segt[node].begin(), segt[node].end()); return; } int m = (l + r) / 2; qu(2 * node + 1, l, m, s, e); qu(2 * node + 2, m + 1, r, s, e); } pair<int, int> find(int f) { if (par[par[f]] != par[f]) { int x = p[f]; pair<int, int> y = find(par[f]); p[f] = (x ^ y.second); par[f] = y.first; } return make_pair(par[f], p[f]); } int mer(int f, int s) { pair<int, int> pf = find(f), ps = find(s); if (pf.first == ps.first) { if (pf.second == ps.second) return 0; return 1; } else { if (r[pf.first] >= r[ps.first]) { par[ps.first] = pf.first; p[ps.first] = ((ps.second ^ 1) ^ (pf.second)); if (r[pf.first] == r[ps.first]) r[pf.first]++; } else { par[pf.first] = ps.first; p[pf.first] = ((pf.second ^ 1) ^ ps.second); } return 2; } } vector<pair<int, pair<int, int> > > v1; void init(int node, int l, int ri) { sort((segt[node]).begin(), (segt[node]).end()); for (int j = 0; j < n; ++j) { par[j] = j; r[j] = 1; p[j] = 0; } for (int i = segt[node].size() - 1; i >= 0; --i) { int ch = mer(segt[node][i].second.first, segt[node][i].second.second); if (ch == 0) { v1.push_back(segt[node][i]); break; } else if (ch == 2) { v1.push_back(segt[node][i]); } } reverse((v1).begin(), (v1).end()); segt[node].clear(); for (int i = 0; i < v1.size(); ++i) segt[node].push_back(v1[i]); v1.clear(); if (l == ri) return; int m = (l + ri) / 2; init(2 * node + 1, l, m); init(2 * node + 2, m + 1, ri); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> q; for (int i = 0; i < m; ++i) { int f, s, w; cin >> f >> s >> w; f--; s--; up(0, 0, m - 1, i, make_pair(w, make_pair(f, s))); } init(0, 0, m - 1); for (int i = 0; i < q; ++i) { mu.clear(); int f, s; cin >> f >> s; f--; s--; for (int j = 0; j < n; ++j) { par[j] = j; r[j] = 1; p[j] = 0; } qu(0, 0, m - 1, f, s); int v = -1; for (auto it = (--mu.end()); 1; --it) { if (mer((*it).second.first, (*it).second.second) == 0) { v = (*it).first; break; } if (it == mu.begin()) break; } cout << v << '\n'; } return (0); }
#include <bits/stdc++.h> using namespace std; int p[10000]; int sz[10000]; int color[10000]; int used[10000]; int all; vector<int> w[10000]; int n, q, m; int z, y, x; struct edg { int x, y, z, n; edg(int X, int Y, int Z, int N) { x = X; y = Y; z = Z; n = N; } }; bool cmp(edg a, edg b) { return a.z > b.z; } vector<edg> v; int findset(int x) { if (p[x] != x) p[x] = findset(p[x]); return p[x]; } void dfs(int x) { color[x] = 1 - color[x]; used[x] = all; for (int i = 0; i < w[x].size(); i++) { int to = w[x][i]; if (used[to] == all) continue; dfs(to); } } bool add(int x, int y) { int xx = findset(x); int yy = findset(y); if (xx != yy) { if (sz[xx] < sz[yy]) { swap(xx, yy); swap(x, y); } p[yy] = xx; sz[xx] += sz[yy]; if (color[x] != color[y]) { w[x].push_back(y); w[y].push_back(x); return true; } else { ++all; dfs(y); w[x].push_back(y); w[y].push_back(x); return true; } } else { w[x].push_back(y); w[y].push_back(x); if (color[x] == color[y]) return false; return true; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> q; for (int i = 1; i <= m; i++) { cin >> x >> y >> z; edg cur(x, y, z, i); v.push_back(cur); } sort(v.begin(), v.end(), cmp); for (int q1 = 1; q1 <= q; q1++) { for (int i = 1; i <= n; i++) { p[i] = i; color[i] = 1; sz[i] = 1; w[i].clear(); } int l, r; cin >> l >> r; int ans = -1; for (int i = 0; i < v.size(); i++) if (v[i].n >= l && v[i].n <= r) { x = v[i].x; y = v[i].y; if (!add(x, y)) { ans = v[i].z; break; } } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } pair<int, int> edge[500010]; int w[500010]; int n, m, Q; int id[500010]; int col[1010]; int root[1010]; int rev[500010]; inline int find_root(int u) { if (root[u] == u) return u; int first = find_root(root[u]); col[u] ^= col[root[u]]; return root[u] = first; } int main() { gn(n, m); gn(Q); for (int i = 1; i <= m; i++) { gn(edge[i].first, edge[i].second); gn(w[i]); } for (int i = 1; i <= m; i++) id[i] = i; sort(id + 1, id + m + 1, [&](int u, int v) { return w[u] > w[v]; }); while (Q--) { int L, R; gn(L, R); for (int i = 1; i <= n; i++) { col[i] = 0; root[i] = i; } int ans = -1; for (int i = 1; i <= m; i++) { if (id[i] > R || id[i] < L) continue; int u = edge[id[i]].first, v = edge[id[i]].second; if (find_root(u) == find_root(v)) { if (col[u] == col[v]) { ans = w[id[i]]; break; } continue; } col[root[u]] = col[u] ^ col[v] ^ 1; root[root[u]] = root[v]; } printf("%d\n", ans); } }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const long double PI = acosl((long double)-1.0); const long long LINF = ((1ull << 63) - 1ull); const int MOD = 1000000007; const int MAXN = 1000005; int n, m, k; int v1[MAXN], v2[MAXN]; pair<int, int> w[MAXN]; int ans[1001]; int p[1001]; int c[1001]; int l[1001]; int r[1001]; vector<vector<vector<int> > > comps; int num; void init() { scanf("%d%d%d", &n, &m, &k); comps.resize(k + 1); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &v1[i], &v2[i], &w[i].first); w[i].second = i; } for (int i = 1; i <= k; i++) { scanf("%d%d", &l[i], &r[i]); } } void init_dsu() { comps[num].resize(n + 1); for (int i = 1; i <= n; i++) { p[i] = i; comps[num][i].push_back(i); } } inline bool union_sets(int v1, int v2) { if (p[v1] == p[v2]) { return c[v1] != c[v2]; } int p1 = p[v1]; int p2 = p[v2]; if (comps[num][p1].size() < comps[num][p2].size()) { swap(p1, p2); } for (int i = 0; i < comps[num][p2].size(); i++) { p[comps[num][p2][i]] = p1; } comps[num][p1].insert(comps[num][p1].end(), comps[num][p2].begin(), comps[num][p2].end()); if (c[v1] == c[v2]) { for (int i = 0; i < comps[num][p2].size(); i++) { c[comps[num][p2][i]] ^= 1; } } return true; } void solve() { init(); sort(w + 1, w + m + 1); for (num = 1; num <= k; num++) { init_dsu(); int ans = -1; for (int j = m; j >= 1; j--) { if (l[num] <= w[j].second && w[j].second <= r[num]) { if (!union_sets(v1[w[j].second], v2[w[j].second])) { ans = w[j].first; break; } } } printf("%d\n", ans); } } void precalc() {} int main() { srand(25); precalc(); int tests = 1; for (int i = 1; i <= tests; i++) { solve(); } return 0; }