output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long M = (long long)1e9 + 7; const double pi = 2 * acos(0.0); const double esp = 1e-9; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int main() { int test = 1; cin >> test; for (int cs = 1; cs <= test; ++cs) { string s, ts, w; int x; s += '#'; cin >> ts >> x; s += ts; w += '#'; for (int i = 1; i <= (int)ts.size(); ++i) w += '1'; bool ck = true; for (int i = 1; i < (int)s.size(); ++i) { if (s[i] == '0') { if (i - x > 0) w[i - x] = '0'; if (i + x < (int)s.size()) w[i + x] = '0'; } } for (int i = 1; i < (int)w.size(); ++i) { if (i + x <= (int)s.size() - 1) { if ((s[i] == '1' && w[i + x] == '1')) { continue; } if (s[i] != '1' && w[i + x] == '1') { ck = false; break; } } if (i - x >= 1) { if (s[i] == '1' && w[i - x] == '1') { continue; } if (s[i] != '1' && w[i - x] == '1') { ck = false; break; } } if ((i + x >= (int)s.size() && i - x < 1)) { if (s[i] == '1') { ck = false; break; } } else { if (i + x < (int)s.size()) if (s[i] != '0') { ck = false; break; } if (i - x > 0) if (s[i] != '0') { ck = false; break; } } } if (!ck) { cout << -1; } else { for (int i = 1; i < (int)w.size(); ++i) cout << w[i]; } cout << '\n'; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long M = (long long)1e9 + 7; const double pi = 2 * acos(0.0); const double esp = 1e-9; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int main() { int test = 1; cin >> test; for (int cs = 1; cs <= test; ++cs) { string s, ts, w; int x; s += '#'; cin >> ts >> x; s += ts; w += '#'; for (int i = 1; i <= (int)ts.size(); ++i) w += '1'; bool ck = true; for (int i = 1; i < (int)s.size(); ++i) { if (s[i] == '0') { if (i - x > 0) w[i - x] = '0'; if (i + x < (int)s.size()) w[i + x] = '0'; } } for (int i = 1; i < (int)w.size(); ++i) { if (i + x <= (int)s.size() - 1) { if ((s[i] == '1' && w[i + x] == '1')) { continue; } if (s[i] != '1' && w[i + x] == '1') { ck = false; break; } } if (i - x >= 1) { if (s[i] == '1' && w[i - x] == '1') { continue; } if (s[i] != '1' && w[i - x] == '1') { ck = false; break; } } if ((i + x >= (int)s.size() && i - x < 1)) { if (s[i] == '1') { ck = false; break; } } else { if (i + x < (int)s.size()) if (s[i] != '0') { ck = false; break; } if (i - x > 0) if (s[i] != '0') { ck = false; break; } } } if (!ck) { cout << -1; } else { for (int i = 1; i < (int)w.size(); ++i) cout << w[i]; } cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { string s; int x; cin >> s >> x; int n = s.length(); string w; for (int i = 0; i < n; i++) w += '1'; for (int i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) w[i - x] = '0'; if (i + x < n) w[i + x] = '0'; } } string z = s; for (int i = 0; i < n; i++) { if (i - x >= 0 && w[i - x] == '1') { z[i] = '1'; } else if (i + x < n && w[i + x] == '1') { z[i] = '1'; } else { z[i] = '0'; } } if (s == z) { cout << w << endl; } else { cout << -1 << endl; } } int main() { int t; cin >> t; while (t--) { solve(); } }
### Prompt Develop a solution in CPP to the problem described below: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { string s; int x; cin >> s >> x; int n = s.length(); string w; for (int i = 0; i < n; i++) w += '1'; for (int i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) w[i - x] = '0'; if (i + x < n) w[i + x] = '0'; } } string z = s; for (int i = 0; i < n; i++) { if (i - x >= 0 && w[i - x] == '1') { z[i] = '1'; } else if (i + x < n && w[i + x] == '1') { z[i] = '1'; } else { z[i] = '0'; } } if (s == z) { cout << w << endl; } else { cout << -1 << endl; } } int main() { int t; cin >> t; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; void solve() { string S; cin >> S; int n = (int)S.size(); int x; cin >> x; vector<int> s(n, 0); for (int i = 0; i < n; i++) { s[i] = S[i] - '0'; } vector<int> res(n, 1); bool ok = true; for (int i = 0; i < n; i++) { int lidx = i - x; int ridx = i + x; if (s[i] == 0) { if (lidx >= 0) res[lidx] = 0; if (ridx < n) res[ridx] = 0; } } for (int i = 0; i < n; i++) { int lidx = i - x; int ridx = i + x; int cnt1 = 0; if (lidx >= 0 && res[lidx] == 1) cnt1++; if (ridx < n && res[ridx] == 1) cnt1++; if (s[i]) { if (cnt1 == 0) ok = false; } else { if (cnt1) ok = false; } } if (!ok) { cout << -1 << endl; return; } for (int i : res) { cout << i; } cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int T; T = 1; cin >> T; while (T--) { solve(); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { string S; cin >> S; int n = (int)S.size(); int x; cin >> x; vector<int> s(n, 0); for (int i = 0; i < n; i++) { s[i] = S[i] - '0'; } vector<int> res(n, 1); bool ok = true; for (int i = 0; i < n; i++) { int lidx = i - x; int ridx = i + x; if (s[i] == 0) { if (lidx >= 0) res[lidx] = 0; if (ridx < n) res[ridx] = 0; } } for (int i = 0; i < n; i++) { int lidx = i - x; int ridx = i + x; int cnt1 = 0; if (lidx >= 0 && res[lidx] == 1) cnt1++; if (ridx < n && res[ridx] == 1) cnt1++; if (s[i]) { if (cnt1 == 0) ok = false; } else { if (cnt1) ok = false; } } if (!ok) { cout << -1 << endl; return; } for (int i : res) { cout << i; } cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int T; T = 1; cin >> T; while (T--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAX_N = 5e5 + 20; const int MAXM = 120010; const long long MOD = 998244353; const int MAXN = 2e5 + 20; int n; int x; char s[MAXN]; char t[MAXN]; bool solve() { for (int i = 1; i <= n; i++) t[i] = '?'; for (int i = 1; i <= n; i++) { if (s[i] == '0') { if (i - x >= 1 && t[i - x] == '1') return 0; if (i + x <= n) t[i + x] = '0'; if (i - x >= 1 && t[i - x] == '?') t[i - x] = '0'; } else { if (i - x >= 1 && t[i - x] == '1') { if (i + x <= n) t[i + x] = '?'; } else { if (i - x >= 1 && t[i - x] == '?') { t[i - x] = '1'; if (i + x <= n) t[i + x] = '?'; } else if (i + x <= n) t[i + x] = '1'; else return 0; } } } return 1; } int main() { int test; scanf("%d", &test); while (test--) { scanf("%s", s + 1); scanf("%d", &x); n = strlen(s + 1); if (solve()) { for (int i = 1; i <= n; i++) { if (t[i] != '?') printf("%c", t[i]); else printf("%c", '1'); } printf("\n"); } else { printf("-1\n"); } } }
### Prompt Create a solution in cpp for the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAX_N = 5e5 + 20; const int MAXM = 120010; const long long MOD = 998244353; const int MAXN = 2e5 + 20; int n; int x; char s[MAXN]; char t[MAXN]; bool solve() { for (int i = 1; i <= n; i++) t[i] = '?'; for (int i = 1; i <= n; i++) { if (s[i] == '0') { if (i - x >= 1 && t[i - x] == '1') return 0; if (i + x <= n) t[i + x] = '0'; if (i - x >= 1 && t[i - x] == '?') t[i - x] = '0'; } else { if (i - x >= 1 && t[i - x] == '1') { if (i + x <= n) t[i + x] = '?'; } else { if (i - x >= 1 && t[i - x] == '?') { t[i - x] = '1'; if (i + x <= n) t[i + x] = '?'; } else if (i + x <= n) t[i + x] = '1'; else return 0; } } } return 1; } int main() { int test; scanf("%d", &test); while (test--) { scanf("%s", s + 1); scanf("%d", &x); n = strlen(s + 1); if (solve()) { for (int i = 1; i <= n; i++) { if (t[i] != '?') printf("%c", t[i]); else printf("%c", '1'); } printf("\n"); } else { printf("-1\n"); } } } ```
#include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; long long n = s.size(); long long x; cin >> x; string ans; for (long long i = 0; i < n; i++) { ans.push_back('1'); } for (long long i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) ans[i - x] = '0'; if (i + x < n) ans[i + x] = '0'; } } for (long long i = 0; i < n; i++) { if (s[i] == '1') { bool good = false; if (i - x >= 0 && ans[i - x] == '1') good = true; if (i + x < n && ans[i + x] == '1') good = true; if (!good) { cout << "-1\n"; return; } } } for (auto& c : ans) { if (c == '-') c = '0'; } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed; cout.precision(30); long long t = 1; cin >> t; while (t--) solve(); }
### Prompt Construct a cpp code solution to the problem outlined: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; long long n = s.size(); long long x; cin >> x; string ans; for (long long i = 0; i < n; i++) { ans.push_back('1'); } for (long long i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) ans[i - x] = '0'; if (i + x < n) ans[i + x] = '0'; } } for (long long i = 0; i < n; i++) { if (s[i] == '1') { bool good = false; if (i - x >= 0 && ans[i - x] == '1') good = true; if (i + x < n && ans[i + x] == '1') good = true; if (!good) { cout << "-1\n"; return; } } } for (auto& c : ans) { if (c == '-') c = '0'; } cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed; cout.precision(30); long long t = 1; cin >> t; while (t--) solve(); } ```
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tests; cin >> tests; while (tests--) { string s; cin >> s; long long x; cin >> x; string ans(s.size(), '?'); function<bool(long long)> valid = [&](long long ind) { return (ind >= 0 and ind < s.size()); }; bool ok = true; for (long long j = 0; j < 2; j++) for (long long i = 0; i < s.size(); i++) { if (!valid(i - x) and !valid(i + x)) { if (s[i] == '1') { ok = false; break; } } if (!valid(i - x) and valid(i + x)) { if (ans[i + x] == '?') ans[i + x] = s[i]; else if (ans[i + x] != s[i]) { ok = false; break; } } if (valid(i - x) and !valid(i + x)) { if (ans[i - x] == '?') ans[i - x] = s[i]; else if (ans[i - x] != s[i]) { ok = false; break; } } if (valid(i + x) and valid(i - x)) { if (ans[i - x] == '?' and ans[i + x] == '?') { if (s[i] == '0') { ans[i - x] = ans[i + x] = s[i]; } } if (ans[i - x] != '?' and ans[i + x] == '?') { if (s[i] == '1') { if (ans[i - x] == '0') ans[i + x] = '1'; } if (s[i] == '0') { if (ans[i - x] == '0') ans[i + x] = '0'; else if (ans[i - x] == '1') { ok = false; break; } } } if (ans[i - x] == '?' and ans[i + x] != '?') { if (s[i] == '1') { if (ans[i + x] == '0') ans[i - x] = '1'; } if (s[i] == '0') { if (ans[i + x] == '0') ans[i - x] = '0'; else if (ans[i + x] == '1') { ok = false; break; } } } } } if (!ok) { cout << -1 << '\n'; } else { for (char ch : ans) { if (ch == '?') cout << '1'; else cout << ch; } cout << '\n'; } } return 0; }
### Prompt Please formulate a cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tests; cin >> tests; while (tests--) { string s; cin >> s; long long x; cin >> x; string ans(s.size(), '?'); function<bool(long long)> valid = [&](long long ind) { return (ind >= 0 and ind < s.size()); }; bool ok = true; for (long long j = 0; j < 2; j++) for (long long i = 0; i < s.size(); i++) { if (!valid(i - x) and !valid(i + x)) { if (s[i] == '1') { ok = false; break; } } if (!valid(i - x) and valid(i + x)) { if (ans[i + x] == '?') ans[i + x] = s[i]; else if (ans[i + x] != s[i]) { ok = false; break; } } if (valid(i - x) and !valid(i + x)) { if (ans[i - x] == '?') ans[i - x] = s[i]; else if (ans[i - x] != s[i]) { ok = false; break; } } if (valid(i + x) and valid(i - x)) { if (ans[i - x] == '?' and ans[i + x] == '?') { if (s[i] == '0') { ans[i - x] = ans[i + x] = s[i]; } } if (ans[i - x] != '?' and ans[i + x] == '?') { if (s[i] == '1') { if (ans[i - x] == '0') ans[i + x] = '1'; } if (s[i] == '0') { if (ans[i - x] == '0') ans[i + x] = '0'; else if (ans[i - x] == '1') { ok = false; break; } } } if (ans[i - x] == '?' and ans[i + x] != '?') { if (s[i] == '1') { if (ans[i + x] == '0') ans[i - x] = '1'; } if (s[i] == '0') { if (ans[i + x] == '0') ans[i - x] = '0'; else if (ans[i + x] == '1') { ok = false; break; } } } } } if (!ok) { cout << -1 << '\n'; } else { for (char ch : ans) { if (ch == '?') cout << '1'; else cout << ch; } cout << '\n'; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int x; cin >> x; for (int j = 0; j < x; j++) { string s; cin >> s; int si = s.size(); int n, ans = 0; cin >> n; vector<char> w(si, '1'); for (int i = 0; i < si; i++) { if (i >= n && s[i] == '0') { w[i - n] = '0'; } if (i + n <= si - 1 && s[i] == '0') { w[i + n] = '0'; } } for (int i = 0; i < si; i++) { if (s[i] == '1') { if (i >= n and w[i - n] == '1') continue; if ((i + n) < si and w[i + n] == '1') continue; else { ans = -1; } } } if (ans == -1) { cout << -1 << endl; continue; } else { for (auto it : w) { cout << it; } cout << endl; } } }
### Prompt Generate a cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int x; cin >> x; for (int j = 0; j < x; j++) { string s; cin >> s; int si = s.size(); int n, ans = 0; cin >> n; vector<char> w(si, '1'); for (int i = 0; i < si; i++) { if (i >= n && s[i] == '0') { w[i - n] = '0'; } if (i + n <= si - 1 && s[i] == '0') { w[i + n] = '0'; } } for (int i = 0; i < si; i++) { if (s[i] == '1') { if (i >= n and w[i - n] == '1') continue; if ((i + n) < si and w[i + n] == '1') continue; else { ans = -1; } } } if (ans == -1) { cout << -1 << endl; continue; } else { for (auto it : w) { cout << it; } cout << endl; } } } ```
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } int inf = 0x3f3f3f3f; long long infl = 0x3f3f3f3f3f3f3f3fLL; long double infd = 1.0 / 0.0; const long long MOD = 1e9 + 7; const long double pi = 2 * acos(0.0); int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt = 1; cin >> tt; while (tt--) { string s; cin >> s; int x; cin >> x; int n = (int)s.size(); string tmp = string(n, '1'); for (int i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) tmp[i - x] = '0'; if (i + x < n) tmp[i + x] = '0'; } } string s1 = string(n, '0'); string w = tmp; for (int i = 0; i < n; i++) { if (i + x < n && w[i + x] == '1') s1[i] = '1'; if (i - x >= 0 && w[i - x] == '1') s1[i] = '1'; } if (s1 != s) { cout << -1 << '\n'; continue; } cout << tmp << '\n'; } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } int inf = 0x3f3f3f3f; long long infl = 0x3f3f3f3f3f3f3f3fLL; long double infd = 1.0 / 0.0; const long long MOD = 1e9 + 7; const long double pi = 2 * acos(0.0); int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt = 1; cin >> tt; while (tt--) { string s; cin >> s; int x; cin >> x; int n = (int)s.size(); string tmp = string(n, '1'); for (int i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) tmp[i - x] = '0'; if (i + x < n) tmp[i + x] = '0'; } } string s1 = string(n, '0'); string w = tmp; for (int i = 0; i < n; i++) { if (i + x < n && w[i + x] == '1') s1[i] = '1'; if (i - x >= 0 && w[i - x] == '1') s1[i] = '1'; } if (s1 != s) { cout << -1 << '\n'; continue; } cout << tmp << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; string check(const string &s, long long int x) { string z; for (long long int i = 0; i < s.length(); i++) { bool flag1 = false, flag2 = false; if (i - x >= 0 && s[i - x] == '1') { flag1 = true; } if (i + x <= s.length() - 1 && s[i + x] == '1') { flag2 = true; } if (flag1 || flag2) { z += '1'; } else { z += '0'; } } return z; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { string s; long long int x; cin >> s >> x; char arr[s.length()]; for (long long int i = 0; i < s.length(); i++) { arr[i] = '@'; } bool flag = false; for (long long int i = 0; i < s.length(); i++) { if (s[i] == '0') { if (i + x < s.length()) { if (arr[i + x] == '@' || arr[i + x] == '0') { arr[i + x] = '0'; } else { flag = true; break; } } if (i - x >= 0) { if (arr[i - x] == '@' || arr[i - x] == '0') { arr[i - x] = '0'; } else { flag = true; break; } } } } for (long long int i = s.length() - 1; i >= 0; i--) { if (s[i] == '0') { if (i + x < s.length()) { if (arr[i + x] == '@' || arr[i + x] == '0') { arr[i + x] = '0'; } else { flag = true; break; } } if (i - x >= 0) { if (arr[i - x] == '@' || arr[i - x] == '0') { arr[i - x] = '0'; } else { flag = true; break; } } } } if (flag) { cout << -1; continue; } for (long long int i = 0; i < s.length(); i++) { if (arr[i] == '@') { arr[i] = '1'; } } string z; for (long long int i = 0; i < s.length(); i++) { z += arr[i]; } if (check(z, x) == s) { cout << z << '\n'; } else { cout << -1 << '\n'; } } return 0; }
### Prompt Create a solution in Cpp for the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; string check(const string &s, long long int x) { string z; for (long long int i = 0; i < s.length(); i++) { bool flag1 = false, flag2 = false; if (i - x >= 0 && s[i - x] == '1') { flag1 = true; } if (i + x <= s.length() - 1 && s[i + x] == '1') { flag2 = true; } if (flag1 || flag2) { z += '1'; } else { z += '0'; } } return z; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { string s; long long int x; cin >> s >> x; char arr[s.length()]; for (long long int i = 0; i < s.length(); i++) { arr[i] = '@'; } bool flag = false; for (long long int i = 0; i < s.length(); i++) { if (s[i] == '0') { if (i + x < s.length()) { if (arr[i + x] == '@' || arr[i + x] == '0') { arr[i + x] = '0'; } else { flag = true; break; } } if (i - x >= 0) { if (arr[i - x] == '@' || arr[i - x] == '0') { arr[i - x] = '0'; } else { flag = true; break; } } } } for (long long int i = s.length() - 1; i >= 0; i--) { if (s[i] == '0') { if (i + x < s.length()) { if (arr[i + x] == '@' || arr[i + x] == '0') { arr[i + x] = '0'; } else { flag = true; break; } } if (i - x >= 0) { if (arr[i - x] == '@' || arr[i - x] == '0') { arr[i - x] = '0'; } else { flag = true; break; } } } } if (flag) { cout << -1; continue; } for (long long int i = 0; i < s.length(); i++) { if (arr[i] == '@') { arr[i] = '1'; } } string z; for (long long int i = 0; i < s.length(); i++) { z += arr[i]; } if (check(z, x) == s) { cout << z << '\n'; } else { cout << -1 << '\n'; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, mk = 0; string s, temp, second = ""; cin >> s; temp = s; n = s.size(); long long int x; cin >> x; for (long long int i = 0; i < n; i++) s[i] = '1'; for (long long int i = 0; i < n; i++) { if (temp[i] == '1') continue; if (i - x >= 0) s[i - x] = '0'; if (i + x < n) s[i + x] = '0'; } for (long long int i = 0; i < n; i++) { char mx = '0'; if (i - x >= 0 && s[i - x] == '1') mx = '1'; if (i + x < n && s[i + x] == '1') mx = '1'; second += mx; if (temp[i] != second[i]) { mk = 1; break; } } if (mk) cout << "-1" << endl; else cout << s << endl; } }
### Prompt Generate a Cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, mk = 0; string s, temp, second = ""; cin >> s; temp = s; n = s.size(); long long int x; cin >> x; for (long long int i = 0; i < n; i++) s[i] = '1'; for (long long int i = 0; i < n; i++) { if (temp[i] == '1') continue; if (i - x >= 0) s[i - x] = '0'; if (i + x < n) s[i + x] = '0'; } for (long long int i = 0; i < n; i++) { char mx = '0'; if (i - x >= 0 && s[i - x] == '1') mx = '1'; if (i + x < n && s[i + x] == '1') mx = '1'; second += mx; if (temp[i] != second[i]) { mk = 1; break; } } if (mk) cout << "-1" << endl; else cout << s << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int x; cin >> x; int a[s.length()]; for (int i = 0; i < s.length(); i++) { a[i] = 1; } for (int i = 0; i < s.length(); i++) { if (s[i] == '0') { if (i - x >= 0) { a[i - x] = 0; } if (i + x < s.length()) { a[i + x] = 0; } } } int tag = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == '1') { int tag1 = 0; if (i - x >= 0) { if (a[i - x] == 1) { a[i - x] = 1; tag1++; } } if (i + x < s.length()) { if (a[i + x] == 1) { tag1++; } } if (tag1 == 0) { tag = 1; break; } } } if (tag == 1) { cout << "-1" << endl; } else { for (int i = 0; i < s.length(); i++) { cout << a[i]; } cout << endl; } } }
### Prompt Your challenge is to write a cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int x; cin >> x; int a[s.length()]; for (int i = 0; i < s.length(); i++) { a[i] = 1; } for (int i = 0; i < s.length(); i++) { if (s[i] == '0') { if (i - x >= 0) { a[i - x] = 0; } if (i + x < s.length()) { a[i + x] = 0; } } } int tag = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == '1') { int tag1 = 0; if (i - x >= 0) { if (a[i - x] == 1) { a[i - x] = 1; tag1++; } } if (i + x < s.length()) { if (a[i + x] == 1) { tag1++; } } if (tag1 == 0) { tag = 1; break; } } } if (tag == 1) { cout << "-1" << endl; } else { for (int i = 0; i < s.length(); i++) { cout << a[i]; } cout << endl; } } } ```
#include <bits/stdc++.h> using namespace std; int MOD = 1e9 + 7; long long int power(long long int a, long long int b) { long long int res = 1; a = a % MOD; while (b > 0) { if (b & 1) { res = (res * a) % MOD; b--; } a = (a * a) % MOD; b >>= 1; } return res; } long long int fermat_inv(long long int y) { return power(y, MOD - 2); } long long int gcd(long long int a, long long int b) { return (b == 0) ? a : gcd(b, a % b); } long long int min(long long int a, long long int b) { return (a > b) ? b : a; } long long int max(long long int a, long long int b) { return (a > b) ? a : b; } bool prime[1000001]; vector<int> primes; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p < 1000001; p++) if (prime[p]) primes.push_back(p); } long long int fact[1001]; void factorial(int n) { fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i, fact[i] %= MOD; } long long int ncr(long long int n, long long int r) { if (n < r) return -1; else { long long int x = fact[r] * fact[n - r] % MOD; return fact[n] * fermat_inv(x) % MOD; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while (t--) { string s; int x; cin >> s >> x; int n = s.size(); string ans = ""; for (int i = 0; i < n; i++) ans += '1'; for (int i = 0; i < n; i++) { if (s[i] == '1') continue; if (i + x < n) ans[i + x] = '0'; if (i - x >= 0) ans[i - x] = '0'; } bool b = true; for (int i = 0; i < n; i++) { if (s[i] == '0') continue; bool c = false; if (i + x < n && ans[i + x] == '1') c = true; if (i - x >= 0 && ans[i - x] == '1') c = true; if (!c) b = false; } if (b) cout << ans << '\n'; else cout << "-1\n"; } }
### Prompt Your challenge is to write a CPP solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int MOD = 1e9 + 7; long long int power(long long int a, long long int b) { long long int res = 1; a = a % MOD; while (b > 0) { if (b & 1) { res = (res * a) % MOD; b--; } a = (a * a) % MOD; b >>= 1; } return res; } long long int fermat_inv(long long int y) { return power(y, MOD - 2); } long long int gcd(long long int a, long long int b) { return (b == 0) ? a : gcd(b, a % b); } long long int min(long long int a, long long int b) { return (a > b) ? b : a; } long long int max(long long int a, long long int b) { return (a > b) ? a : b; } bool prime[1000001]; vector<int> primes; void SieveOfEratosthenes(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p < 1000001; p++) if (prime[p]) primes.push_back(p); } long long int fact[1001]; void factorial(int n) { fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i, fact[i] %= MOD; } long long int ncr(long long int n, long long int r) { if (n < r) return -1; else { long long int x = fact[r] * fact[n - r] % MOD; return fact[n] * fermat_inv(x) % MOD; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while (t--) { string s; int x; cin >> s >> x; int n = s.size(); string ans = ""; for (int i = 0; i < n; i++) ans += '1'; for (int i = 0; i < n; i++) { if (s[i] == '1') continue; if (i + x < n) ans[i + x] = '0'; if (i - x >= 0) ans[i - x] = '0'; } bool b = true; for (int i = 0; i < n; i++) { if (s[i] == '0') continue; bool c = false; if (i + x < n && ans[i + x] == '1') c = true; if (i - x >= 0 && ans[i - x] == '1') c = true; if (!c) b = false; } if (b) cout << ans << '\n'; else cout << "-1\n"; } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { string s; cin >> s; string r(s.size(), '1'); long long n; cin >> n; for (long long i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i - n >= 0) { r[i - n] = '0'; } if (i + n < s.size()) { r[i + n] = '0'; } } } bool can = true; for (long long i = 0; i < s.size(); i++) { char c = '0'; if (i - n >= 0 && r[i - n] == '1') { c = '1'; } else if (i + n < s.size() && r[i + n] == '1') { c = '1'; } if (c != s[i]) { can = false; break; } } if (can) cout << r << endl; else cout << "-1" << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { string s; cin >> s; string r(s.size(), '1'); long long n; cin >> n; for (long long i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i - n >= 0) { r[i - n] = '0'; } if (i + n < s.size()) { r[i + n] = '0'; } } } bool can = true; for (long long i = 0; i < s.size(); i++) { char c = '0'; if (i - n >= 0 && r[i - n] == '1') { c = '1'; } else if (i + n < s.size() && r[i + n] == '1') { c = '1'; } if (c != s[i]) { can = false; break; } } if (can) cout << r << endl; else cout << "-1" << endl; } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long int MAXN = 1e5 + 5; const long long int mod = 1e9 + 7; long long int it = 0, ans = 0; template <typename temp> void sp(temp x) { cout << x << " "; } template <typename temp> void np(temp x) { cout << x << "\n"; } template <typename Arg1, typename... Args> void sp(Arg1 arg1, Args... args) { cout << arg1 << " "; sp(args...); } template <typename Arg1, typename... Args> void np(Arg1 arg1, Args... args) { cout << arg1 << "\n"; np(args...); } template <typename temp> void spv(temp T, long long int n) { for (int i = 0; i < n; i++) sp(T[i]); } template <typename temp> void npv(temp T, long long int n) { for (int i = 0; i < n; i++) np(T[i]); } template <typename T> T modpow(T base, T exp, T modulus) { base %= modulus; T result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result; } template <typename T> T modinv(T base, T exp) { return modpow(base, exp - 2, exp); } long long int spf[MAXN]; void sieve() { spf[1] = 1; for (auto i = 2; i < MAXN; i++) spf[i] = i; for (auto i = 4; i < MAXN; i += 2) spf[i] = 2; for (auto i = 3; i * i < MAXN; i++) { if (spf[i] == i) { for (auto j = i * i; j < MAXN; j += i) if (spf[j] == j) spf[j] = i; } } return; } vector<long long int> getFactorization(long long int x) { vector<long long int> res; while (x != 1) { res.push_back(spf[x]); x = x / spf[x]; } return res; } void check() { string s; long long int k; cin >> s >> k; long long int n = s.size(); string w(n, '1'); for (int i = 0; i < n; i++) { if (s[i] == '0') { if (i + k < n) w[i + k] = '0'; if (i - k >= 0) w[i - k] = '0'; } } for (int i = 0; i < n; i++) { bool ok = false; ok |= (i + k < n && w[i + k] == '1'); ok |= (i - k >= 0 && w[i - k] == '1'); if (s[i] != ok + '0') { np(-1); return; } } np(w); return; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); ; long long int t = 1; cin >> t; while (t--) check(); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long int MAXN = 1e5 + 5; const long long int mod = 1e9 + 7; long long int it = 0, ans = 0; template <typename temp> void sp(temp x) { cout << x << " "; } template <typename temp> void np(temp x) { cout << x << "\n"; } template <typename Arg1, typename... Args> void sp(Arg1 arg1, Args... args) { cout << arg1 << " "; sp(args...); } template <typename Arg1, typename... Args> void np(Arg1 arg1, Args... args) { cout << arg1 << "\n"; np(args...); } template <typename temp> void spv(temp T, long long int n) { for (int i = 0; i < n; i++) sp(T[i]); } template <typename temp> void npv(temp T, long long int n) { for (int i = 0; i < n; i++) np(T[i]); } template <typename T> T modpow(T base, T exp, T modulus) { base %= modulus; T result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result; } template <typename T> T modinv(T base, T exp) { return modpow(base, exp - 2, exp); } long long int spf[MAXN]; void sieve() { spf[1] = 1; for (auto i = 2; i < MAXN; i++) spf[i] = i; for (auto i = 4; i < MAXN; i += 2) spf[i] = 2; for (auto i = 3; i * i < MAXN; i++) { if (spf[i] == i) { for (auto j = i * i; j < MAXN; j += i) if (spf[j] == j) spf[j] = i; } } return; } vector<long long int> getFactorization(long long int x) { vector<long long int> res; while (x != 1) { res.push_back(spf[x]); x = x / spf[x]; } return res; } void check() { string s; long long int k; cin >> s >> k; long long int n = s.size(); string w(n, '1'); for (int i = 0; i < n; i++) { if (s[i] == '0') { if (i + k < n) w[i + k] = '0'; if (i - k >= 0) w[i - k] = '0'; } } for (int i = 0; i < n; i++) { bool ok = false; ok |= (i + k < n && w[i + k] == '1'); ok |= (i - k >= 0 && w[i - k] == '1'); if (s[i] != ok + '0') { np(-1); return; } } np(w); return; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); ; long long int t = 1; cin >> t; while (t--) check(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long bin(long long a, long long b, long long mod) { if (b == 0) return 1; if (b % 2 == 0) return bin((a * a) % mod, b / 2, mod) % mod; return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod; } void solve() { string s; cin >> s; int x; cin >> x; string s1(s.size(), '1'); for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i - x >= 0) { s1[i - x] = '0'; } if (i + x < s.size()) { s1[i + x] = '0'; } } } string ans(s.size(), '0'); for (int i = 0; i < s.size(); i++) { if (i - x >= 0) { if (s1[i - x] == '1') { ans[i] = '1'; } } if (i + x < s.size()) { if (s1[i + x] == '1') { ans[i] = '1'; } } } if (ans == s) { cout << s1 << "\n"; } else { cout << -1 << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; cin >> t; while (t--) { solve(); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long bin(long long a, long long b, long long mod) { if (b == 0) return 1; if (b % 2 == 0) return bin((a * a) % mod, b / 2, mod) % mod; return ((a % mod) * (bin((a * a) % mod, b / 2, mod) % mod)) % mod; } void solve() { string s; cin >> s; int x; cin >> x; string s1(s.size(), '1'); for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i - x >= 0) { s1[i - x] = '0'; } if (i + x < s.size()) { s1[i + x] = '0'; } } } string ans(s.size(), '0'); for (int i = 0; i < s.size(); i++) { if (i - x >= 0) { if (s1[i - x] == '1') { ans[i] = '1'; } } if (i + x < s.size()) { if (s1[i + x] == '1') { ans[i] = '1'; } } } if (ans == s) { cout << s1 << "\n"; } else { cout << -1 << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; cin >> t; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int x; cin >> x; string ans = ""; for (int i = 0; i < s.size(); i++) ans += '1'; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i + x < s.size()) ans[i + x] = '0'; if (i - x >= 0) ans[i - x] = '0'; } } int fl = 1; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { if ((i + x < s.size() && (ans[i + x] == '1')) || (i - x >= 0 && (ans[i - x] == '1'))) { } else fl = 0; } } if (fl == 1) cout << ans << endl; else cout << -1 << endl; } }
### Prompt Generate a Cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int x; cin >> x; string ans = ""; for (int i = 0; i < s.size(); i++) ans += '1'; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i + x < s.size()) ans[i + x] = '0'; if (i - x >= 0) ans[i - x] = '0'; } } int fl = 1; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { if ((i + x < s.size() && (ans[i + x] == '1')) || (i - x >= 0 && (ans[i - x] == '1'))) { } else fl = 0; } } if (fl == 1) cout << ans << endl; else cout << -1 << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; long long int x; cin >> x; long long int n = s.size(); char w[n]; for (int i = 0; i < n; i++) w[i] = '1'; bool flag = true, vool = true; for (int i = 0; i < n; i++) { if (s[i] == '0' && i >= x) w[i - x] = '0'; if (s[i] == '0' && i + x < n) w[i + x] = '0'; } for (int i = 0; i < n; i++) { if (i >= x && w[i - x] == '1' && s[i] == '1') flag = true; else if (s[i] == '1') flag = false; if (i + x < n && w[i + x] == '1' && s[i] == '1') vool = true; else if (s[i] == '1') vool = false; if (vool == false && flag == false) { break; } } if (vool == false && flag == false) cout << "-1" << endl; else { for (int i = 0; i < n; i++) cout << w[i]; } cout << "\n"; } }
### Prompt Generate a CPP solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; long long int x; cin >> x; long long int n = s.size(); char w[n]; for (int i = 0; i < n; i++) w[i] = '1'; bool flag = true, vool = true; for (int i = 0; i < n; i++) { if (s[i] == '0' && i >= x) w[i - x] = '0'; if (s[i] == '0' && i + x < n) w[i + x] = '0'; } for (int i = 0; i < n; i++) { if (i >= x && w[i - x] == '1' && s[i] == '1') flag = true; else if (s[i] == '1') flag = false; if (i + x < n && w[i + x] == '1' && s[i] == '1') vool = true; else if (s[i] == '1') vool = false; if (vool == false && flag == false) { break; } } if (vool == false && flag == false) cout << "-1" << endl; else { for (int i = 0; i < n; i++) cout << w[i]; } cout << "\n"; } } ```
#include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; long long x; cin >> x; long long n = s.length(); char ans[n]; memset(ans, '1', sizeof(ans)); for (long long i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) ans[i - x] = '0'; if (i + x < n) ans[i + x] = '0'; } } bool f = 1; for (long long i = 0; i < n; i++) { if (s[i] == '1') { if (i - x >= 0 && ans[i - x] == '1') continue; if (i + x < n && ans[i + x] == '1') continue; f = 0; } else { if (i - x >= 0 && ans[i - x] == '1') f = 0; if (i + x < n && ans[i + x] == '1') f = 0; } } if (f) { for (long long i = 0; i < n; i++) cout << ans[i]; } else cout << -1; cout << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; long long x; cin >> x; long long n = s.length(); char ans[n]; memset(ans, '1', sizeof(ans)); for (long long i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) ans[i - x] = '0'; if (i + x < n) ans[i + x] = '0'; } } bool f = 1; for (long long i = 0; i < n; i++) { if (s[i] == '1') { if (i - x >= 0 && ans[i - x] == '1') continue; if (i + x < n && ans[i + x] == '1') continue; f = 0; } else { if (i - x >= 0 && ans[i - x] == '1') f = 0; if (i + x < n && ans[i + x] == '1') f = 0; } } if (f) { for (long long i = 0; i < n; i++) cout << ans[i]; } else cout << -1; cout << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; cin >> t; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int arr[378999]; long long int arr2[234597]; long long int ab[10]; vector<long long int> adj[22222]; map<long long int, long long int> mp, mp2; map<long long int, long long int>::iterator it; vector<long long int> vec; string s; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t, i, loss, j, diff; cin >> t; while (t--) { long long int n, x, y, k, maxi = INT_MIN, mini = INT_MAX, two = 0, one1 = 0, one2 = 0, zero = 0, p; long long int cs, cw, w; mp.clear(); mp2.clear(); string s, ans = ""; cin >> s; cin >> x; long long int len = s.length(); for (i = 0; i < len; i++) { ans += '1'; } long long int f = 0; for (i = 0; i < len; i++) { if (s[i] == '1') { continue; } else if (s[i] == '0') { if (i - x >= 0) { ans[i - x] = '0'; } if (i + x < len) { ans[i + x] = '0'; } } } for (i = 0; i < len; i++) { if (s[i] == '1') { if ((i - x >= 0 && ans[i - x] == '1') || (i + x < len && ans[i + x] == '1')) continue; else { f = 1; break; } } } if (f == 1) cout << "-1\n"; else cout << ans << "\n"; } return 0; }
### Prompt Create a solution in cpp for the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int arr[378999]; long long int arr2[234597]; long long int ab[10]; vector<long long int> adj[22222]; map<long long int, long long int> mp, mp2; map<long long int, long long int>::iterator it; vector<long long int> vec; string s; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t, i, loss, j, diff; cin >> t; while (t--) { long long int n, x, y, k, maxi = INT_MIN, mini = INT_MAX, two = 0, one1 = 0, one2 = 0, zero = 0, p; long long int cs, cw, w; mp.clear(); mp2.clear(); string s, ans = ""; cin >> s; cin >> x; long long int len = s.length(); for (i = 0; i < len; i++) { ans += '1'; } long long int f = 0; for (i = 0; i < len; i++) { if (s[i] == '1') { continue; } else if (s[i] == '0') { if (i - x >= 0) { ans[i - x] = '0'; } if (i + x < len) { ans[i + x] = '0'; } } } for (i = 0; i < len; i++) { if (s[i] == '1') { if ((i - x >= 0 && ans[i - x] == '1') || (i + x < len && ans[i + x] == '1')) continue; else { f = 1; break; } } } if (f == 1) cout << "-1\n"; else cout << ans << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } long long int gcd(long long int a, long long int b) { if (a < b) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } long long int isPrime(long long int n) { long long int p = (long long int)sqrt(n); for (int i = (2); i <= (p); i += (1)) if (n % i == 0) return 0; return 1; } long long int pow(long long int b, long long int e) { if (e == 0) return 1; else if (e % 2 == 0) { long long int a = pow(b, e / 2); return a * a; } else { long long int a = pow(b, e / 2); return b * a * a; } } long long int powm(long long int x, long long int y, long long int m = 1000000007) { x = x % m; long long int res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } long long int modInverse(long long int a, long long int m) { return powm(a, m - 2, m); } void solve() { string s; cin >> s; long long int x, n = s.length(); cin >> x; vector<char> w(n, '1'); for (int i = (0); i <= (n - 1); i += (1)) { if (i >= x and s[i] == '0') w[i - x] = '0'; if (i + x <= n - 1 and s[i] == '0') w[i + x] = '0'; } string c; for (int i = (0); i <= (n - 1); i += (1)) { if ((i < x or (w[i - x] == '0')) and (i + x >= n or (w[i + x] == '0'))) c += '0'; else c += '1'; } if (c == s) { for (int i = (0); i <= (n - 1); i += (1)) cout << w[i]; cout << '\n'; } else cout << -1 << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int T; cin >> T; while (T--) { solve(); } return 0; }
### Prompt In Cpp, your task is to solve the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } long long int gcd(long long int a, long long int b) { if (a < b) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } long long int isPrime(long long int n) { long long int p = (long long int)sqrt(n); for (int i = (2); i <= (p); i += (1)) if (n % i == 0) return 0; return 1; } long long int pow(long long int b, long long int e) { if (e == 0) return 1; else if (e % 2 == 0) { long long int a = pow(b, e / 2); return a * a; } else { long long int a = pow(b, e / 2); return b * a * a; } } long long int powm(long long int x, long long int y, long long int m = 1000000007) { x = x % m; long long int res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } long long int modInverse(long long int a, long long int m) { return powm(a, m - 2, m); } void solve() { string s; cin >> s; long long int x, n = s.length(); cin >> x; vector<char> w(n, '1'); for (int i = (0); i <= (n - 1); i += (1)) { if (i >= x and s[i] == '0') w[i - x] = '0'; if (i + x <= n - 1 and s[i] == '0') w[i + x] = '0'; } string c; for (int i = (0); i <= (n - 1); i += (1)) { if ((i < x or (w[i - x] == '0')) and (i + x >= n or (w[i + x] == '0'))) c += '0'; else c += '1'; } if (c == s) { for (int i = (0); i <= (n - 1); i += (1)) cout << w[i]; cout << '\n'; } else cout << -1 << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int T; cin >> T; while (T--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const double PI = 3.141592653589793238463; const long long int MOD = 1000000007; list<long long> ls; vector<long long> v, v1, v2, vv; set<long long> s; map<long long, long long> mp; stack<char> st; stack<long long> sti; inline long long fp(long long bs, long long pw) { long long res = 1; while (pw > 0) { if (pw % 2 == 1) { res = (res * bs) % MOD; } bs = (bs * bs) % MOD; pw = pw / 2; } return res; } long long pp(long long bs, unsigned long long pw) { long long ans = 1; while (pw > 0) { if (pw & 1) ans = ans * bs; pw = pw >> 1; bs = bs * bs; } return ans; } bool ff22(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.second < b.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t, n, m, k, h, p, i, j, a, b, c, x, V, ind, sz, ans = 0, ans2, mx, sum = 0, ct = 0, ti, q, temp, t2, mn, ptr, ct1, ct2, cc, ln, diff; double kh = 4; bool fl; string s, sub, sorted, anss; cin >> t; while (t--) { ct = 0; cin >> s; n = s.length(); sub = ""; for (i = 0; i < n; i++) sub.push_back('1'); cin >> x; for (i = 0; i < n; i++) { long long ms = i - x; long long pl = i + x; if (s[i] == '0') { if (ms >= 0) { sub[ms] = '0'; } if (pl < n) { sub[pl] = '0'; } } } for (i = 0; i < n; i++) { long long ms = i - x; long long pl = i + x; if (s[i] == '1') { if ((ms >= 0 && sub[ms] == '1') || (pl < n && sub[pl] == '1')) continue; else { ct++; break; } } else { if ((ms >= 0 && sub[ms] == '1') || (pl < n && sub[pl] == '1')) { ct++; break; } } } if (ct > 0) cout << -1 << endl; else cout << sub << endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double PI = 3.141592653589793238463; const long long int MOD = 1000000007; list<long long> ls; vector<long long> v, v1, v2, vv; set<long long> s; map<long long, long long> mp; stack<char> st; stack<long long> sti; inline long long fp(long long bs, long long pw) { long long res = 1; while (pw > 0) { if (pw % 2 == 1) { res = (res * bs) % MOD; } bs = (bs * bs) % MOD; pw = pw / 2; } return res; } long long pp(long long bs, unsigned long long pw) { long long ans = 1; while (pw > 0) { if (pw & 1) ans = ans * bs; pw = pw >> 1; bs = bs * bs; } return ans; } bool ff22(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.second < b.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t, n, m, k, h, p, i, j, a, b, c, x, V, ind, sz, ans = 0, ans2, mx, sum = 0, ct = 0, ti, q, temp, t2, mn, ptr, ct1, ct2, cc, ln, diff; double kh = 4; bool fl; string s, sub, sorted, anss; cin >> t; while (t--) { ct = 0; cin >> s; n = s.length(); sub = ""; for (i = 0; i < n; i++) sub.push_back('1'); cin >> x; for (i = 0; i < n; i++) { long long ms = i - x; long long pl = i + x; if (s[i] == '0') { if (ms >= 0) { sub[ms] = '0'; } if (pl < n) { sub[pl] = '0'; } } } for (i = 0; i < n; i++) { long long ms = i - x; long long pl = i + x; if (s[i] == '1') { if ((ms >= 0 && sub[ms] == '1') || (pl < n && sub[pl] == '1')) continue; else { ct++; break; } } else { if ((ms >= 0 && sub[ms] == '1') || (pl < n && sub[pl] == '1')) { ct++; break; } } } if (ct > 0) cout << -1 << endl; else cout << sub << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { string a; cin >> a; int n = a.size(); int x; cin >> x; string b; b.assign(n, '.'); for (int i = 0; i < n; i++) { if (a[i] != '0') { continue; } if (i - x >= 0) { b[i - x] = '0'; } if (i + x < n) { b[i + x] = '0'; } } for (int i = 0; i < n; i++) { if (a[i] == '0') { continue; } if (i - x >= 0 && (b[i - x] == '1' || b[i - x] == '.')) { b[i - x] = '1'; continue; } if (i + x < n && b[i + x] != '0') { b[i + x] = '1'; continue; } cout << -1 << "\n"; return; } for (int i = 0; i < n; i++) { if (b[i] == '.') { b[i] = '1'; } } cout << b << "\n"; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { solve(); } }
### Prompt Create a solution in Cpp for the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve() { string a; cin >> a; int n = a.size(); int x; cin >> x; string b; b.assign(n, '.'); for (int i = 0; i < n; i++) { if (a[i] != '0') { continue; } if (i - x >= 0) { b[i - x] = '0'; } if (i + x < n) { b[i + x] = '0'; } } for (int i = 0; i < n; i++) { if (a[i] == '0') { continue; } if (i - x >= 0 && (b[i - x] == '1' || b[i - x] == '.')) { b[i - x] = '1'; continue; } if (i + x < n && b[i + x] != '0') { b[i + x] = '1'; continue; } cout << -1 << "\n"; return; } for (int i = 0; i < n; i++) { if (b[i] == '.') { b[i] = '1'; } } cout << b << "\n"; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; vector<string> alpha{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; vector<string> ALPHA{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; int GCD(int a, int b) { if (a == 0) return b; if (b == 0) return a; return GCD(b, a % b); } int LCM(int a, int b) { return (a * b) / GCD(a, b); } int search(vector<int> a, int b) { int m = a.size() / 2; vector<int> h; int i = -1; if (b == a[m]) return m; if (a.size() == 1 && b != a[m]) return -1; if (b < a[m]) { h = {a.begin(), a.begin() + m}; i = 0; } else { h = {a.begin() + m, a.end()}; i = m; } int s = search(h, b); return s < 0 ? -1 : i + s; } int searchU(vector<int> a, int b) { for (int i = 0; i < a.size(); i++) { if (a[i] == b) return i; } return -1; } int MIN(vector<int> a) { int best = pow(2, 31) - 1; for (auto b : a) { if (b < best) best = b; } return best; } int min_i(vector<int> a) { int best = pow(2, 31) - 1; int index; for (int i = 0; i < a.size(); i++) { if (a[i] < best) { best = a[i]; index = i; } } return index; } float min(vector<float> a) { float best = pow(2, 31) - 1; for (auto b : a) { if (b < best) best = b; } return best; } int MAX(vector<int> a) { int best = -1 * (pow(2, 31) - 1); for (auto b : a) { if (b > best) best = b; } return best; } int max_i(vector<int> a) { int best = -1 * (pow(2, 31) - 1); int index = -1; for (int i = 0; i < a.size(); i++) { if (a[i] > best) { best = a[i]; index = i; } } return index; } void printVector(vector<int> v) { for (auto i : v) { cout << i << " "; } cout << '\n'; } bool in(int val, vector<int> v) { for (auto i : v) { if (i == val) { return true; } } return false; } bool in(string val, vector<string> v) { for (auto i : v) { if (i == val) { return true; } } return false; } int sum(vector<int> v) { int sum = 0; for (auto i : v) { sum += i; } return sum; } float sq(int a) { return (float)a * a; } bool sorted(vector<int> a) { for (int i = 1; i < a.size(); i++) { if (a[i] < a[i - 1]) { return false; } } return true; } vector<int> dijkstras(vector<vector<pair<int, int>>> g, int s, int e) { vector<int> dist(g.size(), pow(2, 31) - 1); dist[s] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> next; next.push({0, s}); vector<bool> visited(g.size(), false); vector<int> prev(g.size(), -1); while (!next.empty()) { visited[next.top().second] = true; for (auto i : g[next.top().second]) { if (!visited[i.first]) { if (dist[next.top().second] + i.second < dist[i.first]) { dist[i.first] = dist[next.top().second] + i.second; prev[i.first] = next.top().second; } next.push({dist[i.first], i.first}); } } next.pop(); } vector<int> path = {e}; while (true) { if (prev[path.back()] == -1) break; path.push_back(prev[path.back()]); } reverse(path.begin(), path.end()); return path; } int main() { int cases = 1; cin >> cases; for (int _case = 0; _case < cases; _case++) { string s; cin >> s; int w; cin >> w; vector<int> a(s.size(), 1); for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i - w >= 0) a[i - w] = 0; if (i + w < s.size()) a[i + w] = 0; } } bool d = true; for (int i = 0; i < a.size(); i++) { if (s[i] == '1') { int y = 0; if (i - w >= 0) { if (a[i - w] == 1) y++; } if (i + w < a.size()) { if (a[i + w] == 1) y++; } if (y == 0) { d = false; break; } } } if (!d) { cout << -1 << '\n'; continue; } for (auto i : a) { cout << i; } cout << '\n'; } }
### Prompt In CPP, your task is to solve the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace std::chrono; vector<string> alpha{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; vector<string> ALPHA{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; int GCD(int a, int b) { if (a == 0) return b; if (b == 0) return a; return GCD(b, a % b); } int LCM(int a, int b) { return (a * b) / GCD(a, b); } int search(vector<int> a, int b) { int m = a.size() / 2; vector<int> h; int i = -1; if (b == a[m]) return m; if (a.size() == 1 && b != a[m]) return -1; if (b < a[m]) { h = {a.begin(), a.begin() + m}; i = 0; } else { h = {a.begin() + m, a.end()}; i = m; } int s = search(h, b); return s < 0 ? -1 : i + s; } int searchU(vector<int> a, int b) { for (int i = 0; i < a.size(); i++) { if (a[i] == b) return i; } return -1; } int MIN(vector<int> a) { int best = pow(2, 31) - 1; for (auto b : a) { if (b < best) best = b; } return best; } int min_i(vector<int> a) { int best = pow(2, 31) - 1; int index; for (int i = 0; i < a.size(); i++) { if (a[i] < best) { best = a[i]; index = i; } } return index; } float min(vector<float> a) { float best = pow(2, 31) - 1; for (auto b : a) { if (b < best) best = b; } return best; } int MAX(vector<int> a) { int best = -1 * (pow(2, 31) - 1); for (auto b : a) { if (b > best) best = b; } return best; } int max_i(vector<int> a) { int best = -1 * (pow(2, 31) - 1); int index = -1; for (int i = 0; i < a.size(); i++) { if (a[i] > best) { best = a[i]; index = i; } } return index; } void printVector(vector<int> v) { for (auto i : v) { cout << i << " "; } cout << '\n'; } bool in(int val, vector<int> v) { for (auto i : v) { if (i == val) { return true; } } return false; } bool in(string val, vector<string> v) { for (auto i : v) { if (i == val) { return true; } } return false; } int sum(vector<int> v) { int sum = 0; for (auto i : v) { sum += i; } return sum; } float sq(int a) { return (float)a * a; } bool sorted(vector<int> a) { for (int i = 1; i < a.size(); i++) { if (a[i] < a[i - 1]) { return false; } } return true; } vector<int> dijkstras(vector<vector<pair<int, int>>> g, int s, int e) { vector<int> dist(g.size(), pow(2, 31) - 1); dist[s] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> next; next.push({0, s}); vector<bool> visited(g.size(), false); vector<int> prev(g.size(), -1); while (!next.empty()) { visited[next.top().second] = true; for (auto i : g[next.top().second]) { if (!visited[i.first]) { if (dist[next.top().second] + i.second < dist[i.first]) { dist[i.first] = dist[next.top().second] + i.second; prev[i.first] = next.top().second; } next.push({dist[i.first], i.first}); } } next.pop(); } vector<int> path = {e}; while (true) { if (prev[path.back()] == -1) break; path.push_back(prev[path.back()]); } reverse(path.begin(), path.end()); return path; } int main() { int cases = 1; cin >> cases; for (int _case = 0; _case < cases; _case++) { string s; cin >> s; int w; cin >> w; vector<int> a(s.size(), 1); for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i - w >= 0) a[i - w] = 0; if (i + w < s.size()) a[i + w] = 0; } } bool d = true; for (int i = 0; i < a.size(); i++) { if (s[i] == '1') { int y = 0; if (i - w >= 0) { if (a[i - w] == 1) y++; } if (i + w < a.size()) { if (a[i + w] == 1) y++; } if (y == 0) { d = false; break; } } } if (!d) { cout << -1 << '\n'; continue; } for (auto i : a) { cout << i; } cout << '\n'; } } ```
#include <bits/stdc++.h> int N, x; char s[100001], w[100001]; void solve() { bool f = false; for (long int i = 0; i < strlen(s); i++) { if (s[i] == '0') { if (i >= x) w[i - x] = '0'; if (i + x < strlen(s)) w[i + x] = '0'; } } for (long int i = 0; i < strlen(s); i++) { bool status = false; if (i >= x && w[i - x] == '1') status = true; if (i + x < strlen(s) && w[i + x] == '1') status = true; if (s[i] == '1' && !status) { printf("-1"); return; } } for (long int i = 0; i < strlen(s); i++) printf("%c", w[i]); } int main() { int T; scanf("%d", &T); for (int i = 0; i < T; i++) { scanf("%s", s); if (strlen(s) > 100000) { printf("-1\n"); continue; } scanf("%d", &x); memset(w, '1', strlen(s) * sizeof(char)); solve(); printf("\n"); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> int N, x; char s[100001], w[100001]; void solve() { bool f = false; for (long int i = 0; i < strlen(s); i++) { if (s[i] == '0') { if (i >= x) w[i - x] = '0'; if (i + x < strlen(s)) w[i + x] = '0'; } } for (long int i = 0; i < strlen(s); i++) { bool status = false; if (i >= x && w[i - x] == '1') status = true; if (i + x < strlen(s) && w[i + x] == '1') status = true; if (s[i] == '1' && !status) { printf("-1"); return; } } for (long int i = 0; i < strlen(s); i++) printf("%c", w[i]); } int main() { int T; scanf("%d", &T); for (int i = 0; i < T; i++) { scanf("%s", s); if (strlen(s) > 100000) { printf("-1\n"); continue; } scanf("%d", &x); memset(w, '1', strlen(s) * sizeof(char)); solve(); printf("\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } void run_case() { string S; int X; cin >> S >> X; int N = int(S.size()); string W(N, '1'); for (int i = 0; i < N; i++) if (S[i] == '0') { if (i - X >= 0) W[i - X] = '0'; if (i + X < N) W[i + X] = '0'; } for (int i = 0; i < N; i++) { bool one = false; one = one || (i - X >= 0 && W[i - X] == '1'); one = one || (i + X < N && W[i + X] == '1'); if (S[i] != one + '0') { cout << -1 << '\n'; return; } } cout << W << '\n'; } int main() { int t; cin >> t; while (t--) { run_case(); } }
### Prompt Generate a cpp solution to the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } void run_case() { string S; int X; cin >> S >> X; int N = int(S.size()); string W(N, '1'); for (int i = 0; i < N; i++) if (S[i] == '0') { if (i - X >= 0) W[i - X] = '0'; if (i + X < N) W[i + X] = '0'; } for (int i = 0; i < N; i++) { bool one = false; one = one || (i - X >= 0 && W[i - X] == '1'); one = one || (i + X < N && W[i + X] == '1'); if (S[i] != one + '0') { cout << -1 << '\n'; return; } } cout << W << '\n'; } int main() { int t; cin >> t; while (t--) { run_case(); } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { string second; int x, n; cin >> second >> x; n = second.size(); int ans[100010]; for (int i = 0; i < n; i++) ans[i] = 1; for (int i = 0; i < n; i++) { if (second[i] == '0') { if (i - x >= 0) ans[i - x] = 0; if (i + x < n) ans[i + x] = 0; } } int deu = 1; for (int i = 0; i < n; i++) { if (second[i] == '1') { if ((i - x < 0 || ans[i - x] == 0) && (i + x >= n || ans[i + x] == 0)) { deu = 0; break; } } } if (!deu) cout << -1 << endl; else { for (int i = 0; i < n; i++) cout << ans[i]; cout << endl; } } }
### Prompt In CPP, your task is to solve the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { string second; int x, n; cin >> second >> x; n = second.size(); int ans[100010]; for (int i = 0; i < n; i++) ans[i] = 1; for (int i = 0; i < n; i++) { if (second[i] == '0') { if (i - x >= 0) ans[i - x] = 0; if (i + x < n) ans[i + x] = 0; } } int deu = 1; for (int i = 0; i < n; i++) { if (second[i] == '1') { if ((i - x < 0 || ans[i - x] == 0) && (i + x >= n || ans[i + x] == 0)) { deu = 0; break; } } } if (!deu) cout << -1 << endl; else { for (int i = 0; i < n; i++) cout << ans[i]; cout << endl; } } } ```
#include <bits/stdc++.h> using namespace std; int T, x; char str[100005], ans[100005]; char usd[100005]; int main() { scanf("%d", &T); while (T--) { scanf("%s", str + 1); scanf("%d", &x); int n = strlen(str + 1); for (int i = 1; i <= n; ++i) { if (str[i] == '0') { if (i + x <= n) ans[i + x] = '0'; if (i - x >= 1) ans[i - x] = '0'; } } for (int i = 1; i <= n; ++i) if (!ans[i]) ans[i] = '1'; for (int i = 1; i <= n; ++i) { if (i - x >= 1 && ans[i - x] == '1') usd[i] = '1'; else if (i + x <= n && ans[i + x] == '1') usd[i] = '1'; else usd[i] = '0'; } bool flag = true; for (int i = 1; i <= n; ++i) if (usd[i] != str[i]) { flag = false; break; } if (!flag) printf("-1\n"); else printf("%s\n", ans + 1); for (int i = 1; i <= n; ++i) ans[i] = 0; } return 0; }
### Prompt Create a solution in cpp for the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int T, x; char str[100005], ans[100005]; char usd[100005]; int main() { scanf("%d", &T); while (T--) { scanf("%s", str + 1); scanf("%d", &x); int n = strlen(str + 1); for (int i = 1; i <= n; ++i) { if (str[i] == '0') { if (i + x <= n) ans[i + x] = '0'; if (i - x >= 1) ans[i - x] = '0'; } } for (int i = 1; i <= n; ++i) if (!ans[i]) ans[i] = '1'; for (int i = 1; i <= n; ++i) { if (i - x >= 1 && ans[i - x] == '1') usd[i] = '1'; else if (i + x <= n && ans[i + x] == '1') usd[i] = '1'; else usd[i] = '0'; } bool flag = true; for (int i = 1; i <= n; ++i) if (usd[i] != str[i]) { flag = false; break; } if (!flag) printf("-1\n"); else printf("%s\n", ans + 1); for (int i = 1; i <= n; ++i) ans[i] = 0; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s, w; int x, i, n; cin >> s; cin >> x; n = s.length(); for (i = 0; i < n; i++) w += '2'; for (i = 0; i < n; i++) { if (s[i] == '0') { if (i >= x) w[i - x] = '0'; if (i + x < n) w[i + x] = '0'; } } int notpos = 0; for (i = 0; i < n; i++) { if (s[i] == '1') { if ((i - x < 0 || w[i - x] == '0') && (i + x >= n || w[i + x] == '0')) notpos = 1; } } if (notpos) cout << -1 << endl; else { for (int i = 0; i < n; i++) { if (w[i] == '2') w[i] = '1'; } cout << w << endl; } } }
### Prompt Develop a solution in Cpp to the problem described below: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s, w; int x, i, n; cin >> s; cin >> x; n = s.length(); for (i = 0; i < n; i++) w += '2'; for (i = 0; i < n; i++) { if (s[i] == '0') { if (i >= x) w[i - x] = '0'; if (i + x < n) w[i + x] = '0'; } } int notpos = 0; for (i = 0; i < n; i++) { if (s[i] == '1') { if ((i - x < 0 || w[i - x] == '0') && (i + x >= n || w[i + x] == '0')) notpos = 1; } } if (notpos) cout << -1 << endl; else { for (int i = 0; i < n; i++) { if (w[i] == '2') w[i] = '1'; } cout << w << endl; } } } ```
#include <bits/stdc++.h> const long long mod = 1000000007; using namespace std; void yn(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } int main() { int test; cin >> test; while (test--) { string s; cin >> s; long long x; cin >> x; string w; long long n = s.length(); bool f = false; for (int i = 0; i < n; i++) { w += '2'; } for (int i = 0; i < n; i++) { if (s[i] == '1') { if (i - x >= 0 && (w[i - x] == '2' || w[i - x] == '1')) { w[i - x] = '1'; } else if (i + x < n && (w[i + x] == '2' || w[i + x] == '1')) { w[i + x] = '1'; } else { f = true; } } else if (s[i] == '0') { if (i - x >= 0) { if (w[i - x] == '1') { f = true; } else { w[i - x] = '0'; } } if (i + x < n) { if (w[i + x] == '1') { f = true; } else { w[i + x] = '0'; } } } } for (int i = 0; i < n; i++) { if (w[i] == '2') w[i] = '1'; } if (f) cout << -1 << endl; else cout << w << endl; } }
### Prompt Construct a cpp code solution to the problem outlined: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> const long long mod = 1000000007; using namespace std; void yn(bool a) { if (a) cout << "YES" << endl; else cout << "NO" << endl; } int main() { int test; cin >> test; while (test--) { string s; cin >> s; long long x; cin >> x; string w; long long n = s.length(); bool f = false; for (int i = 0; i < n; i++) { w += '2'; } for (int i = 0; i < n; i++) { if (s[i] == '1') { if (i - x >= 0 && (w[i - x] == '2' || w[i - x] == '1')) { w[i - x] = '1'; } else if (i + x < n && (w[i + x] == '2' || w[i + x] == '1')) { w[i + x] = '1'; } else { f = true; } } else if (s[i] == '0') { if (i - x >= 0) { if (w[i - x] == '1') { f = true; } else { w[i - x] = '0'; } } if (i + x < n) { if (w[i + x] == '1') { f = true; } else { w[i + x] = '0'; } } } } for (int i = 0; i < n; i++) { if (w[i] == '2') w[i] = '1'; } if (f) cout << -1 << endl; else cout << w << endl; } } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; void __print(int x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int first = 0; cerr << '{'; for (auto &i : x) cerr << (first++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { string second; cin >> second; long long int x, done = 1, n = second.size(); cin >> x; vector<long long int> ans(n, 1); for (long long int i = 0; i < n; i++) { if (second[i] == '0') { if (i - x >= 0) { ans[i - x] = 0; } if (i + x <= n - 1) { ans[i + x] = 0; } } } for (long long int i = 0; i < n; i++) { if (second[i] != '0') { long long int fp = 0, sp = 0; if (i - x >= 0) fp = ans[i - x]; if (i + x <= n - 1) sp = ans[i + x]; if (fp + sp == 0) { done = 0; break; } } } if (done) { for (long long int i = 0; i < n; i++) cout << ans[i]; cout << "\n"; } else cout << -1 << "\n"; } return 0; }
### Prompt In CPP, your task is to solve the following problem: Consider the following process. You have a binary string (a string where each character is either 0 or 1) w of length n and an integer x. You build a new binary string s consisting of n characters. The i-th character of s is chosen as follows: * if the character w_{i-x} exists and is equal to 1, then s_i is 1 (formally, if i > x and w_{i-x} = 1, then s_i = 1); * if the character w_{i+x} exists and is equal to 1, then s_i is 1 (formally, if i + x ≀ n and w_{i+x} = 1, then s_i = 1); * if both of the aforementioned conditions are false, then s_i is 0. You are given the integer x and the resulting string s. Reconstruct the original string w. Input The first line contains one integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of two lines. The first line contains the resulting string s (2 ≀ |s| ≀ 10^5, each character of s is either 0 or 1). The second line contains one integer x (1 ≀ x ≀ |s| - 1). The total length of all strings s in the input does not exceed 10^5. Output For each test case, print the answer on a separate line as follows: * if no string w can produce the string s at the end of the process, print -1; * otherwise, print the binary string w consisting of |s| characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 Output 111011 10 -1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1000000007; void __print(int x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int first = 0; cerr << '{'; for (auto &i : x) cerr << (first++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { string second; cin >> second; long long int x, done = 1, n = second.size(); cin >> x; vector<long long int> ans(n, 1); for (long long int i = 0; i < n; i++) { if (second[i] == '0') { if (i - x >= 0) { ans[i - x] = 0; } if (i + x <= n - 1) { ans[i + x] = 0; } } } for (long long int i = 0; i < n; i++) { if (second[i] != '0') { long long int fp = 0, sp = 0; if (i - x >= 0) fp = ans[i - x]; if (i + x <= n - 1) sp = ans[i + x]; if (fp + sp == 0) { done = 0; break; } } } if (done) { for (long long int i = 0; i < n; i++) cout << ans[i]; cout << "\n"; } else cout << -1 << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int N, K, n, i, j, k, l; int a[20001][2], ls[10001], sz[10001], len; int ans[100001][2], tot; int num[10001], d[10001], Tot; bool bz[10001]; void New(int x, int y) { ++len; a[len][0] = y; a[len][1] = ls[x]; ls[x] = len; } void Add(int x, int y) { ++tot; ans[tot][0] = x, ans[tot][1] = y; } int dfss(int Fa, int t) { int i, sz = 1; for (i = ls[t]; i; i = a[i][1]) if (a[i][0] != Fa && !bz[a[i][0]]) sz += dfss(t, a[i][0]); return sz; } void dfs(int Fa, int t, int Num) { int i; sz[t] = 1; num[t] = Num; for (i = ls[t]; i; i = a[i][1]) if (a[i][0] != Fa && !bz[a[i][0]]) { dfs(t, a[i][0], Num); sz[t] += sz[a[i][0]]; } if (sz[t] >= N) d[++Tot] = t, sz[t] = 0; } void Dfs(int st, int Fa, int t) { int i; if (!bz[t]) Add(st, t); for (i = ls[t]; i; i = a[i][1]) if (a[i][0] != Fa && !bz[a[i][0]]) Dfs(st, t, a[i][0]); } void work(int t, int Num, int L) { int i, j, k, l, R, sz; sz = dfss(0, t); N = floor(sqrt(sz)); dfs(0, t, Num); R = Tot; for (i = L; i <= R - 1; i++) for (j = i + 1; j <= R; j++) Add(d[i], d[j]); for (i = L; i <= R; i++) bz[d[i]] = 1; for (i = L; i <= R; i++) Dfs(d[i], 0, d[i]); for (i = L; i <= R; i++) { for (j = ls[d[i]]; j; j = a[j][1]) if (!bz[a[j][0]] && num[a[j][0]] != Num + 1) work(a[j][0], Num + 1, Tot + 1); } } int main() { scanf("%d%d", &n, &K); for (i = 1; i <= n - 1; i++) scanf("%d%d", &j, &k), New(j, k), New(k, j); work(1, 1, 1); printf("%d\n", tot); assert(tot <= n * 10); for (i = 1; i <= tot; i++) printf("%d %d\n", ans[i][0], ans[i][1]); fclose(stdin); fclose(stdout); return 0; }
### Prompt Develop a solution in CPP to the problem described below: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int N, K, n, i, j, k, l; int a[20001][2], ls[10001], sz[10001], len; int ans[100001][2], tot; int num[10001], d[10001], Tot; bool bz[10001]; void New(int x, int y) { ++len; a[len][0] = y; a[len][1] = ls[x]; ls[x] = len; } void Add(int x, int y) { ++tot; ans[tot][0] = x, ans[tot][1] = y; } int dfss(int Fa, int t) { int i, sz = 1; for (i = ls[t]; i; i = a[i][1]) if (a[i][0] != Fa && !bz[a[i][0]]) sz += dfss(t, a[i][0]); return sz; } void dfs(int Fa, int t, int Num) { int i; sz[t] = 1; num[t] = Num; for (i = ls[t]; i; i = a[i][1]) if (a[i][0] != Fa && !bz[a[i][0]]) { dfs(t, a[i][0], Num); sz[t] += sz[a[i][0]]; } if (sz[t] >= N) d[++Tot] = t, sz[t] = 0; } void Dfs(int st, int Fa, int t) { int i; if (!bz[t]) Add(st, t); for (i = ls[t]; i; i = a[i][1]) if (a[i][0] != Fa && !bz[a[i][0]]) Dfs(st, t, a[i][0]); } void work(int t, int Num, int L) { int i, j, k, l, R, sz; sz = dfss(0, t); N = floor(sqrt(sz)); dfs(0, t, Num); R = Tot; for (i = L; i <= R - 1; i++) for (j = i + 1; j <= R; j++) Add(d[i], d[j]); for (i = L; i <= R; i++) bz[d[i]] = 1; for (i = L; i <= R; i++) Dfs(d[i], 0, d[i]); for (i = L; i <= R; i++) { for (j = ls[d[i]]; j; j = a[j][1]) if (!bz[a[j][0]] && num[a[j][0]] != Num + 1) work(a[j][0], Num + 1, Tot + 1); } } int main() { scanf("%d%d", &n, &K); for (i = 1; i <= n - 1; i++) scanf("%d%d", &j, &k), New(j, k), New(k, j); work(1, 1, 1); printf("%d\n", tot); assert(tot <= n * 10); for (i = 1; i <= tot; i++) printf("%d %d\n", ans[i][0], ans[i][1]); fclose(stdin); fclose(stdout); return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100005; int n, nn, a[N][2], lst[N], sz[N], len; int ans[N][2], idx, num[N], d[N], ptr; bool vis[N]; inline void add_edge(int x, int y) { a[++len][0] = y; a[len][1] = lst[x]; lst[x] = len; } void add(int x, int y) { ans[++idx][0] = x; ans[idx][1] = y; } int dfs1(int pos, int fa) { int ret = 1; for (int i = lst[pos]; i > 0; i = a[i][1]) if (a[i][0] != fa && !vis[a[i][0]]) ret += dfs1(a[i][0], pos); return ret; } void dfs2(int pos, int fa, int v) { sz[pos] = 1; num[pos] = v; for (int i = lst[pos]; i > 0; i = a[i][1]) if (a[i][0] != fa && !vis[a[i][0]]) { dfs2(a[i][0], pos, v); sz[pos] += sz[a[i][0]]; } if (sz[pos] >= nn) d[++ptr] = pos, sz[pos] = 0; } void dfs3(int pos, int fa, int st) { if (!vis[pos]) add(st, pos); for (int i = lst[pos]; i > 0; i = a[i][1]) if (a[i][0] != fa && !vis[a[i][0]]) dfs3(a[i][0], pos, st); } void solve(int pos, int v, int l) { int ss = dfs1(pos, 0); nn = sqrt(ss); dfs2(pos, 0, v); int r = ptr; for (int i = l; i < r; i++) for (int j = i + 1; j <= r; j++) add(d[i], d[j]); for (int i = l; i <= r; i++) vis[d[i]] = 1; for (int i = l; i <= r; i++) dfs3(d[i], 0, d[i]); for (int i = l; i <= r; i++) { for (int j = lst[d[i]]; j > 0; j = a[j][1]) if (!vis[a[j][0]] && num[a[j][0]] != v + 1) solve(a[j][0], v + 1, ptr + 1); } } int main() { ios::sync_with_stdio(false); int kk, t1, t2; cin >> n >> kk; for (int i = 1; i < n; i++) cin >> t1 >> t2, add_edge(t1, t2), add_edge(t2, t1); solve(1, 1, 1); cout << idx << endl; for (int i = 1; i <= idx; i++) cout << ans[i][0] << ' ' << ans[i][1] << endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100005; int n, nn, a[N][2], lst[N], sz[N], len; int ans[N][2], idx, num[N], d[N], ptr; bool vis[N]; inline void add_edge(int x, int y) { a[++len][0] = y; a[len][1] = lst[x]; lst[x] = len; } void add(int x, int y) { ans[++idx][0] = x; ans[idx][1] = y; } int dfs1(int pos, int fa) { int ret = 1; for (int i = lst[pos]; i > 0; i = a[i][1]) if (a[i][0] != fa && !vis[a[i][0]]) ret += dfs1(a[i][0], pos); return ret; } void dfs2(int pos, int fa, int v) { sz[pos] = 1; num[pos] = v; for (int i = lst[pos]; i > 0; i = a[i][1]) if (a[i][0] != fa && !vis[a[i][0]]) { dfs2(a[i][0], pos, v); sz[pos] += sz[a[i][0]]; } if (sz[pos] >= nn) d[++ptr] = pos, sz[pos] = 0; } void dfs3(int pos, int fa, int st) { if (!vis[pos]) add(st, pos); for (int i = lst[pos]; i > 0; i = a[i][1]) if (a[i][0] != fa && !vis[a[i][0]]) dfs3(a[i][0], pos, st); } void solve(int pos, int v, int l) { int ss = dfs1(pos, 0); nn = sqrt(ss); dfs2(pos, 0, v); int r = ptr; for (int i = l; i < r; i++) for (int j = i + 1; j <= r; j++) add(d[i], d[j]); for (int i = l; i <= r; i++) vis[d[i]] = 1; for (int i = l; i <= r; i++) dfs3(d[i], 0, d[i]); for (int i = l; i <= r; i++) { for (int j = lst[d[i]]; j > 0; j = a[j][1]) if (!vis[a[j][0]] && num[a[j][0]] != v + 1) solve(a[j][0], v + 1, ptr + 1); } } int main() { ios::sync_with_stdio(false); int kk, t1, t2; cin >> n >> kk; for (int i = 1; i < n; i++) cin >> t1 >> t2, add_edge(t1, t2), add_edge(t2, t1); solve(1, 1, 1); cout << idx << endl; for (int i = 1; i <= idx; i++) cout << ans[i][0] << ' ' << ans[i][1] << endl; return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops", \ "omit-frame-pointer", "inline") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native") #pragma GCC option("arch=native", "no-zero-upper") using namespace std; const long long oo = (long long)(1e9); vector<int> g[(long long)(3e5 + 5)], nodes; bool mk1[(long long)(3e5 + 5)], mk2[(long long)(3e5 + 5)]; int sons[(long long)(3e5 + 5)][2]; vector<pair<long long, long long> > res; void dfs1(int u) { mk1[u] = 1; sons[u][0] = 1; nodes.push_back(u); for (auto v : g[u]) if (!mk1[v] && !mk2[v]) { dfs1(v); sons[u][0] += sons[v][0]; sons[u][1] = max(sons[v][0], sons[u][1]); } } void dfs2(int u) { mk1[u] = 1; nodes.push_back(u); for (auto v : g[u]) if (!mk1[v] && !mk2[v]) dfs2(v); } void solve(int u) { dfs1(u); if (nodes.size() <= 2) { for (auto v : nodes) { sons[v][0] = sons[v][1] = 0; mk2[v] = 1; } nodes.clear(); return; } int cent1 = -1, cant = nodes.size() / 3; for (auto v : nodes) { if (cent1 == -1 && sons[v][0] >= cant && sons[v][1] <= cant) cent1 = v; mk1[v] = sons[v][0] = sons[v][1] = 0; } nodes.clear(); mk2[cent1] = 1; if (cent1 == u) { for (auto v : g[cent1]) { if (mk2[v]) continue; dfs2(v); for (auto y : nodes) { res.push_back(pair<long long, long long>(y, cent1)); mk1[y] = 0; } nodes.clear(); } for (auto v : g[cent1]) if (!mk2[v]) solve(v); return; } dfs1(u); int cent2 = -1; cant = nodes.size() / 2; for (auto v : nodes) { if (cent2 == -1 && sons[v][0] >= cant && sons[v][1] <= cant) cent2 = v; mk1[v] = sons[v][0] = sons[v][1] = 0; } nodes.clear(); mk2[cent2] = 1; for (auto v : g[cent1]) { if (mk2[v]) continue; dfs2(v); for (auto y : nodes) { res.push_back(pair<long long, long long>(y, cent1)); mk1[y] = 0; } nodes.clear(); } for (auto v : g[cent2]) { if (mk2[v]) continue; dfs2(v); for (auto y : nodes) { res.push_back(pair<long long, long long>(y, cent2)); mk1[y] = 0; } nodes.clear(); } res.push_back(pair<long long, long long>(cent1, cent2)); for (auto v : g[cent1]) if (!mk2[v]) solve(v); for (auto v : g[cent2]) if (!mk2[v]) solve(v); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; map<pair<long long, long long>, bool> there; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); there[pair<long long, long long>(u, v)] = there[pair<long long, long long>(v, u)] = 1; } solve(0); vector<pair<long long, long long> > tr; for (auto y : res) { if (there[y]) continue; tr.push_back(y); } cout << (tr.size()) << '\n'; for (auto y : tr) cout << y.first + 1 << ' ' << y.second + 1 << '\n'; return 0; }
### Prompt In CPP, your task is to solve the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops", \ "omit-frame-pointer", "inline") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma,tune=native") #pragma GCC option("arch=native", "no-zero-upper") using namespace std; const long long oo = (long long)(1e9); vector<int> g[(long long)(3e5 + 5)], nodes; bool mk1[(long long)(3e5 + 5)], mk2[(long long)(3e5 + 5)]; int sons[(long long)(3e5 + 5)][2]; vector<pair<long long, long long> > res; void dfs1(int u) { mk1[u] = 1; sons[u][0] = 1; nodes.push_back(u); for (auto v : g[u]) if (!mk1[v] && !mk2[v]) { dfs1(v); sons[u][0] += sons[v][0]; sons[u][1] = max(sons[v][0], sons[u][1]); } } void dfs2(int u) { mk1[u] = 1; nodes.push_back(u); for (auto v : g[u]) if (!mk1[v] && !mk2[v]) dfs2(v); } void solve(int u) { dfs1(u); if (nodes.size() <= 2) { for (auto v : nodes) { sons[v][0] = sons[v][1] = 0; mk2[v] = 1; } nodes.clear(); return; } int cent1 = -1, cant = nodes.size() / 3; for (auto v : nodes) { if (cent1 == -1 && sons[v][0] >= cant && sons[v][1] <= cant) cent1 = v; mk1[v] = sons[v][0] = sons[v][1] = 0; } nodes.clear(); mk2[cent1] = 1; if (cent1 == u) { for (auto v : g[cent1]) { if (mk2[v]) continue; dfs2(v); for (auto y : nodes) { res.push_back(pair<long long, long long>(y, cent1)); mk1[y] = 0; } nodes.clear(); } for (auto v : g[cent1]) if (!mk2[v]) solve(v); return; } dfs1(u); int cent2 = -1; cant = nodes.size() / 2; for (auto v : nodes) { if (cent2 == -1 && sons[v][0] >= cant && sons[v][1] <= cant) cent2 = v; mk1[v] = sons[v][0] = sons[v][1] = 0; } nodes.clear(); mk2[cent2] = 1; for (auto v : g[cent1]) { if (mk2[v]) continue; dfs2(v); for (auto y : nodes) { res.push_back(pair<long long, long long>(y, cent1)); mk1[y] = 0; } nodes.clear(); } for (auto v : g[cent2]) { if (mk2[v]) continue; dfs2(v); for (auto y : nodes) { res.push_back(pair<long long, long long>(y, cent2)); mk1[y] = 0; } nodes.clear(); } res.push_back(pair<long long, long long>(cent1, cent2)); for (auto v : g[cent1]) if (!mk2[v]) solve(v); for (auto v : g[cent2]) if (!mk2[v]) solve(v); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; map<pair<long long, long long>, bool> there; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); there[pair<long long, long long>(u, v)] = there[pair<long long, long long>(v, u)] = 1; } solve(0); vector<pair<long long, long long> > tr; for (auto y : res) { if (there[y]) continue; tr.push_back(y); } cout << (tr.size()) << '\n'; for (auto y : tr) cout << y.first + 1 << ' ' << y.second + 1 << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; template <typename T> using pair2 = pair<T, T>; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } const int N = 10010; int n; vector<int> g[N]; int sz[N]; bool bad[N]; vector<pii> ans; void dfsSz(int v, int par) { sz[v] = 1; for (int u : g[v]) { if (u == par || bad[u]) continue; dfsSz(u, v); sz[v] += sz[u]; } } int getCentr(int v) { dfsSz(v, -1); int S = sz[v]; while (true) { int w = v; for (int u : g[v]) { if (bad[u]) continue; if (sz[u] > sz[v]) continue; if (2 * sz[u] >= S) { w = u; } } if (w == v) break; v = w; } return v; } void addEdges(int v, int par, int to) { ans.push_back(make_pair(v, to)); for (int u : g[v]) { if (bad[u] || u == par) continue; addEdges(u, v, to); } } int solve(int v, int oldc, int oldv) { v = getCentr(v); dfsSz(v, -1); vector<pii> sons; for (int u : g[v]) { if (bad[u]) continue; sons.push_back(make_pair(sz[u], u)); } bad[v] = 1; if (oldc != -1) { addEdges(oldv, oldc, oldc); ans.push_back(make_pair(v, oldc)); } sort((sons).begin(), (sons).end()); reverse((sons).begin(), (sons).end()); for (int i = 0; i < (int)sons.size(); i++) { int u = sons[i].second; if (i == 0 && oldc == -1) { solve(u, v, u); } else { addEdges(u, v, v); solve(u, -1, -1); } } return v; } int main() { startTime = clock(); int k; scanf("%d%d", &n, &k); set<pii> hv; for (int i = 1; i < n; i++) { int v, u; scanf("%d%d", &v, &u); v--; u--; hv.insert(make_pair(v, u)); hv.insert(make_pair(u, v)); g[v].push_back(u); g[u].push_back(v); } solve(1, -1, -1); vector<pii> nans; for (pii t : ans) if (hv.count(t) == 0) nans.push_back(t); if ((int)nans.size() > 10 * n) throw; printf("%d\n", (int)nans.size()); for (pii t : nans) { printf("%d %d\n", t.first + 1, t.second + 1); } return 0; }
### Prompt Generate a CPP solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; template <typename T> using pair2 = pair<T, T>; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } const int N = 10010; int n; vector<int> g[N]; int sz[N]; bool bad[N]; vector<pii> ans; void dfsSz(int v, int par) { sz[v] = 1; for (int u : g[v]) { if (u == par || bad[u]) continue; dfsSz(u, v); sz[v] += sz[u]; } } int getCentr(int v) { dfsSz(v, -1); int S = sz[v]; while (true) { int w = v; for (int u : g[v]) { if (bad[u]) continue; if (sz[u] > sz[v]) continue; if (2 * sz[u] >= S) { w = u; } } if (w == v) break; v = w; } return v; } void addEdges(int v, int par, int to) { ans.push_back(make_pair(v, to)); for (int u : g[v]) { if (bad[u] || u == par) continue; addEdges(u, v, to); } } int solve(int v, int oldc, int oldv) { v = getCentr(v); dfsSz(v, -1); vector<pii> sons; for (int u : g[v]) { if (bad[u]) continue; sons.push_back(make_pair(sz[u], u)); } bad[v] = 1; if (oldc != -1) { addEdges(oldv, oldc, oldc); ans.push_back(make_pair(v, oldc)); } sort((sons).begin(), (sons).end()); reverse((sons).begin(), (sons).end()); for (int i = 0; i < (int)sons.size(); i++) { int u = sons[i].second; if (i == 0 && oldc == -1) { solve(u, v, u); } else { addEdges(u, v, v); solve(u, -1, -1); } } return v; } int main() { startTime = clock(); int k; scanf("%d%d", &n, &k); set<pii> hv; for (int i = 1; i < n; i++) { int v, u; scanf("%d%d", &v, &u); v--; u--; hv.insert(make_pair(v, u)); hv.insert(make_pair(u, v)); g[v].push_back(u); g[u].push_back(v); } solve(1, -1, -1); vector<pii> nans; for (pii t : ans) if (hv.count(t) == 0) nans.push_back(t); if ((int)nans.size() > 10 * n) throw; printf("%d\n", (int)nans.size()); for (pii t : nans) { printf("%d %d\n", t.first + 1, t.second + 1); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 10005; int n, fa[N], sz[N], bsz[N]; int pp, lnk[N], nxt[N * 2], to[N * 2]; bool ban[N]; set<pair<int, int> > S; vector<pair<int, int> > ans; void ae(int k1, int k2) { to[++pp] = k2, nxt[pp] = lnk[k1], lnk[k1] = pp; } void push(int x, int y) { if (x == y) return; if (!S.count(minmax(x, y))) { S.insert(minmax(x, y)); ans.push_back(minmax(x, y)); } } void jb(int k1, int k2, int k3) { push(k1, k2); for (int i = lnk[k1]; i; i = nxt[i]) if (to[i] != k3 && !ban[to[i]]) { jb(to[i], k2, k1); } } void sol(int k1) { vector<int> q; q.push_back(k1); for (int i = (0); i <= (((int)(q).size()) - 1); ++i) { int k1 = q[i]; sz[k1] = 1, bsz[k1] = 0; for (int i = lnk[k1]; i; i = nxt[i]) if (!ban[to[i]] && to[i] != fa[k1]) { fa[to[i]] = k1; q.push_back(to[i]); } } if (((int)(q).size()) <= 4) return; vector<int> cut; int B = min(((int)(q).size()), int(sqrt(((int)(q).size())) * 1.5)); for (int i = (((int)(q).size()) - 1); i >= (0); --i) { int k1 = q[i]; if (sz[k1] >= B || bsz[k1] > 1 || i == 0) { cut.push_back(k1); ban[k1] = 1; sz[k1] = 0; bsz[k1] = 1; } if (i) { sz[fa[k1]] += sz[k1]; bsz[fa[k1]] += bsz[k1]; } } for (int i = (0); i <= (((int)(cut).size()) - 1); ++i) { ban[cut[i]] = 1; for (int j = (i + 1); j <= (((int)(cut).size()) - 1); ++j) { push(cut[i], cut[j]); } } for (int i = (0); i <= (((int)(cut).size()) - 1); ++i) { jb(cut[i], cut[i], 0); } for (auto x : q) if (!ban[x]) sol(x); } int main() { scanf("%d%*d", &n); for (int i = (1); i <= (n - 1); ++i) { int k1, k2; scanf("%d%d", &k1, &k2); ae(k1, k2), ae(k2, k1); S.insert(minmax(k1, k2)); } sol(1); printf("%d\n", ((int)(ans).size())); for (int i = (0); i <= (((int)(ans).size()) - 1); ++i) printf("%d %d\n", ans[i].first, ans[i].second); return 0; }
### Prompt Generate a cpp solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 10005; int n, fa[N], sz[N], bsz[N]; int pp, lnk[N], nxt[N * 2], to[N * 2]; bool ban[N]; set<pair<int, int> > S; vector<pair<int, int> > ans; void ae(int k1, int k2) { to[++pp] = k2, nxt[pp] = lnk[k1], lnk[k1] = pp; } void push(int x, int y) { if (x == y) return; if (!S.count(minmax(x, y))) { S.insert(minmax(x, y)); ans.push_back(minmax(x, y)); } } void jb(int k1, int k2, int k3) { push(k1, k2); for (int i = lnk[k1]; i; i = nxt[i]) if (to[i] != k3 && !ban[to[i]]) { jb(to[i], k2, k1); } } void sol(int k1) { vector<int> q; q.push_back(k1); for (int i = (0); i <= (((int)(q).size()) - 1); ++i) { int k1 = q[i]; sz[k1] = 1, bsz[k1] = 0; for (int i = lnk[k1]; i; i = nxt[i]) if (!ban[to[i]] && to[i] != fa[k1]) { fa[to[i]] = k1; q.push_back(to[i]); } } if (((int)(q).size()) <= 4) return; vector<int> cut; int B = min(((int)(q).size()), int(sqrt(((int)(q).size())) * 1.5)); for (int i = (((int)(q).size()) - 1); i >= (0); --i) { int k1 = q[i]; if (sz[k1] >= B || bsz[k1] > 1 || i == 0) { cut.push_back(k1); ban[k1] = 1; sz[k1] = 0; bsz[k1] = 1; } if (i) { sz[fa[k1]] += sz[k1]; bsz[fa[k1]] += bsz[k1]; } } for (int i = (0); i <= (((int)(cut).size()) - 1); ++i) { ban[cut[i]] = 1; for (int j = (i + 1); j <= (((int)(cut).size()) - 1); ++j) { push(cut[i], cut[j]); } } for (int i = (0); i <= (((int)(cut).size()) - 1); ++i) { jb(cut[i], cut[i], 0); } for (auto x : q) if (!ban[x]) sol(x); } int main() { scanf("%d%*d", &n); for (int i = (1); i <= (n - 1); ++i) { int k1, k2; scanf("%d%d", &k1, &k2); ae(k1, k2), ae(k2, k1); S.insert(minmax(k1, k2)); } sol(1); printf("%d\n", ((int)(ans).size())); for (int i = (0); i <= (((int)(ans).size()) - 1); ++i) printf("%d %d\n", ans[i].first, ans[i].second); return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; bool flg = false; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') flg = true; for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); return flg ? -x : x; } int S; int n, k; struct Rec { int u, v; } rec[100010]; int top; bool inq[10010]; struct Edge { int to, nxt; } edge[20010]; int cnt = 1, last[10010]; inline void addedge(int x, int y) { edge[++cnt] = (Edge){y, last[x]}, last[x] = cnt; edge[++cnt] = (Edge){x, last[y]}, last[y] = cnt; } bool mrk[10010]; int Siz, Rt, siz[10010], maxs[10010]; void getsiz(int x, int f) { siz[x] = 1; for (int i = last[x], v; i; i = edge[i].nxt) if (!mrk[v = edge[i].to] && v ^ f) getsiz(v, x), siz[x] += siz[v]; } void getroot(int x, int f) { maxs[x] = 0; for (int i = last[x], v; i; i = edge[i].nxt) if (!mrk[v = edge[i].to] && v ^ f) getroot(v, x), maxs[x] = max(maxs[x], siz[v]); maxs[x] = max(maxs[x], Siz - siz[x]); if (!~Rt || maxs[x] < maxs[Rt]) Rt = x; } int stk[10010], stktop; void dfs1(int x, int f) { stk[++stktop] = x; for (int i = last[x], v; i; i = edge[i].nxt) if (!mrk[v = edge[i].to] && v ^ f) dfs1(v, x); } void solve(int x) { mrk[x] = 1; stktop = 0; dfs1(x, 0); for (int i = 2; i <= stktop; i++) { int y = stk[i]; rec[++top] = (Rec){x, y}; } for (int i = last[x], v; i; i = edge[i].nxt) if (!mrk[v = edge[i].to]) { getsiz(v, 0); Siz = siz[v], Rt = -1; getroot(v, 0); solve(Rt); } } int tot[10010]; vector<int> T[10010]; int fat[10010]; void dfs0(int x, int f) { tot[x] = 1; fat[x] = f; for (int i = last[x], v; i; i = edge[i].nxt) if ((v = edge[i].to) ^ f) { dfs0(v, x); if (!mrk[v]) tot[x] += tot[v]; } if (x != 1 && tot[x] < S) return; mrk[x] = 1; inq[x] = 1; T[x].reserve(tot[x] - 1); for (int i = last[x], v; i; i = edge[i].nxt) if ((v = edge[i].to) ^ f) { if (mrk[v]) continue; stktop = 0; dfs1(v, 0); for (int j = 1; j <= stktop; j++) { int y = stk[j]; rec[++top] = (Rec){x, y}; T[x].push_back(y); } getsiz(v, 0); Siz = siz[v], Rt = -1; getroot(v, 0); solve(Rt); } } int main() { n = read(), k = read(); for (int i = 1; i < n; i++) addedge(read(), read()); if (n <= 1000) { getsiz(1, 0); Siz = siz[1], Rt = -1; getroot(1, 0); solve(Rt); } else { S = (n + 74) / 75; dfs0(1, 0); stktop = 0; for (int i = 1; i <= n; i++) if (inq[i]) stk[++stktop] = i; for (int i = 1; i <= stktop; i++) for (int j = i + 1; j <= stktop; j++) { int x = stk[i], y = stk[j]; rec[++top] = (Rec){x, y}; } for (int i = 1; i <= stktop; i++) { int xx = stk[i], x = fat[stk[i]]; while (x && !inq[x]) x = fat[x]; if (!x) continue; for (int y : T[x]) rec[++top] = (Rec){xx, y}; } } printf("%d\n", top); for (int i = 1; i <= top; i++) printf("%d %d\n", rec[i].u, rec[i].v); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; bool flg = false; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') flg = true; for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); return flg ? -x : x; } int S; int n, k; struct Rec { int u, v; } rec[100010]; int top; bool inq[10010]; struct Edge { int to, nxt; } edge[20010]; int cnt = 1, last[10010]; inline void addedge(int x, int y) { edge[++cnt] = (Edge){y, last[x]}, last[x] = cnt; edge[++cnt] = (Edge){x, last[y]}, last[y] = cnt; } bool mrk[10010]; int Siz, Rt, siz[10010], maxs[10010]; void getsiz(int x, int f) { siz[x] = 1; for (int i = last[x], v; i; i = edge[i].nxt) if (!mrk[v = edge[i].to] && v ^ f) getsiz(v, x), siz[x] += siz[v]; } void getroot(int x, int f) { maxs[x] = 0; for (int i = last[x], v; i; i = edge[i].nxt) if (!mrk[v = edge[i].to] && v ^ f) getroot(v, x), maxs[x] = max(maxs[x], siz[v]); maxs[x] = max(maxs[x], Siz - siz[x]); if (!~Rt || maxs[x] < maxs[Rt]) Rt = x; } int stk[10010], stktop; void dfs1(int x, int f) { stk[++stktop] = x; for (int i = last[x], v; i; i = edge[i].nxt) if (!mrk[v = edge[i].to] && v ^ f) dfs1(v, x); } void solve(int x) { mrk[x] = 1; stktop = 0; dfs1(x, 0); for (int i = 2; i <= stktop; i++) { int y = stk[i]; rec[++top] = (Rec){x, y}; } for (int i = last[x], v; i; i = edge[i].nxt) if (!mrk[v = edge[i].to]) { getsiz(v, 0); Siz = siz[v], Rt = -1; getroot(v, 0); solve(Rt); } } int tot[10010]; vector<int> T[10010]; int fat[10010]; void dfs0(int x, int f) { tot[x] = 1; fat[x] = f; for (int i = last[x], v; i; i = edge[i].nxt) if ((v = edge[i].to) ^ f) { dfs0(v, x); if (!mrk[v]) tot[x] += tot[v]; } if (x != 1 && tot[x] < S) return; mrk[x] = 1; inq[x] = 1; T[x].reserve(tot[x] - 1); for (int i = last[x], v; i; i = edge[i].nxt) if ((v = edge[i].to) ^ f) { if (mrk[v]) continue; stktop = 0; dfs1(v, 0); for (int j = 1; j <= stktop; j++) { int y = stk[j]; rec[++top] = (Rec){x, y}; T[x].push_back(y); } getsiz(v, 0); Siz = siz[v], Rt = -1; getroot(v, 0); solve(Rt); } } int main() { n = read(), k = read(); for (int i = 1; i < n; i++) addedge(read(), read()); if (n <= 1000) { getsiz(1, 0); Siz = siz[1], Rt = -1; getroot(1, 0); solve(Rt); } else { S = (n + 74) / 75; dfs0(1, 0); stktop = 0; for (int i = 1; i <= n; i++) if (inq[i]) stk[++stktop] = i; for (int i = 1; i <= stktop; i++) for (int j = i + 1; j <= stktop; j++) { int x = stk[i], y = stk[j]; rec[++top] = (Rec){x, y}; } for (int i = 1; i <= stktop; i++) { int xx = stk[i], x = fat[stk[i]]; while (x && !inq[x]) x = fat[x]; if (!x) continue; for (int y : T[x]) rec[++top] = (Rec){xx, y}; } } printf("%d\n", top); for (int i = 1; i <= top; i++) printf("%d %d\n", rec[i].u, rec[i].v); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MN = 1e4 + 5; int N, K, i, x, y, sz[MN], mx[MN], ct, c[MN], dp[MN]; vector<int> adj[MN], elem[2]; vector<pair<int, int> > ans; int dfs(int n, int p) { sz[n] = 1; mx[n] = 0; for (auto v : adj[n]) { if (v == p || c[v]) continue; sz[n] += dfs(v, n); mx[n] = max(mx[n], sz[v]); } return sz[n]; } void dfs2(int n, int p, int tot) { if (2 * mx[n] <= tot && 2 * (tot - sz[n]) <= tot) ct = n; for (auto v : adj[n]) { if (v == p || c[v]) continue; dfs2(v, n, tot); } } void op(int n, int p, int r) { if (r ^ p) ans.push_back({n, r}); for (auto v : adj[n]) { if (v == p || c[v]) continue; op(v, n, r); } } void op2(int n, int p, int d) { elem[d].push_back(n); for (auto v : adj[n]) { if (v == p || c[v]) continue; op2(v, n, d ^ 1); } } void solve(int n) { dfs2(n, 0, dfs(n, 0)); int cur = ct; c[cur] = 1; dfs(cur, 0); pair<int, int> big(-1, -1); for (auto v : adj[cur]) { if (c[v]) continue; if (sz[v] > big.first) big = {sz[v], v}; } for (int i = 0; i < 2; i++) elem[i].clear(); for (auto v : adj[cur]) { if (c[v]) continue; if (v == big.second) op2(v, cur, 0); else op(v, cur, cur); } int idx = 0; if (elem[1].size() < elem[0].size()) idx = 1; for (auto v : elem[idx]) { if (v != big.second) ans.push_back({cur, v}); } for (auto v : adj[cur]) { if (c[v]) continue; solve(v); } } int main() { scanf("%d%d", &N, &K); for (i = 1; i < N; i++) { scanf("%d%d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } solve(1); assert(10 * N >= ans.size()); printf("%d\n", (int)ans.size()); for (auto v : ans) printf("%d %d\n", v.first, v.second); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MN = 1e4 + 5; int N, K, i, x, y, sz[MN], mx[MN], ct, c[MN], dp[MN]; vector<int> adj[MN], elem[2]; vector<pair<int, int> > ans; int dfs(int n, int p) { sz[n] = 1; mx[n] = 0; for (auto v : adj[n]) { if (v == p || c[v]) continue; sz[n] += dfs(v, n); mx[n] = max(mx[n], sz[v]); } return sz[n]; } void dfs2(int n, int p, int tot) { if (2 * mx[n] <= tot && 2 * (tot - sz[n]) <= tot) ct = n; for (auto v : adj[n]) { if (v == p || c[v]) continue; dfs2(v, n, tot); } } void op(int n, int p, int r) { if (r ^ p) ans.push_back({n, r}); for (auto v : adj[n]) { if (v == p || c[v]) continue; op(v, n, r); } } void op2(int n, int p, int d) { elem[d].push_back(n); for (auto v : adj[n]) { if (v == p || c[v]) continue; op2(v, n, d ^ 1); } } void solve(int n) { dfs2(n, 0, dfs(n, 0)); int cur = ct; c[cur] = 1; dfs(cur, 0); pair<int, int> big(-1, -1); for (auto v : adj[cur]) { if (c[v]) continue; if (sz[v] > big.first) big = {sz[v], v}; } for (int i = 0; i < 2; i++) elem[i].clear(); for (auto v : adj[cur]) { if (c[v]) continue; if (v == big.second) op2(v, cur, 0); else op(v, cur, cur); } int idx = 0; if (elem[1].size() < elem[0].size()) idx = 1; for (auto v : elem[idx]) { if (v != big.second) ans.push_back({cur, v}); } for (auto v : adj[cur]) { if (c[v]) continue; solve(v); } } int main() { scanf("%d%d", &N, &K); for (i = 1; i < N; i++) { scanf("%d%d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } solve(1); assert(10 * N >= ans.size()); printf("%d\n", (int)ans.size()); for (auto v : ans) printf("%d %d\n", v.first, v.second); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, k; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; vector<vector<int>> g(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } vector<int> color(n); vector<int> d(n); vector<int> sub(n); int iter = 0; function<void(int, int, int, int)> dfs_to_color = [&](int v, int p, int c, int pr) { color[v] = c; for (int i : g[v]) { if (i == p || color[i] != pr) continue; dfs_to_color(i, v, c, pr); } }; function<void(int, int, int)> dfs_on_color = [&](int v, int p, int c) { if (~p) d[v] = d[p] ^ 1; else d[v] = 0; sub[v] = 1; for (int i : g[v]) { if (color[i] != c || i == p) continue; dfs_on_color(i, v, c); sub[v] += sub[i]; } }; vector<pair<int, int>> ret; function<void(vector<int>)> solve = [&](vector<int> nodes) { int sz = nodes.size(); if (sz <= 4) return; int root = nodes[0]; int c = color[root]; dfs_on_color(root, -1, c); int pr = -1; while (true) { int nxt = -1; for (int i : g[root]) { if (color[i] != c || i == pr) continue; if (sub[i] > sz / 2) nxt = i; } if (nxt == -1) break; pr = root; root = nxt; } for (int i : g[root]) { if (color[i] != c) continue; iter++; dfs_to_color(i, root, iter, c); } map<int, vector<int>> to_color; for (int i : nodes) { if (i == root) continue; to_color[color[i]].push_back(i); } int pos = -1; int cd = 0; for (auto it : to_color) { int id = it.first; vector<int> nxt = it.second; solve(nxt); for (int i : nxt) color[i] = id; dfs_on_color(nxt[0], -1, id); if ((int)nxt.size() > cd) { cd = nxt.size(); pos = id; } } for (auto it : to_color) { int id = it.first; vector<int> vertices = it.second; vector<int> to_root; if (id != pos) to_root = vertices; else { vector<int> cnt(2); for (int i : vertices) cnt[d[i]]++; int chosen = cnt[1] < cnt[0]; for (int i : vertices) { if (d[i] == chosen) to_root.push_back(i); } } for (int i : to_root) { ret.emplace_back(i, root); } } }; vector<int> foo(n); iota(foo.begin(), foo.end(), 0); solve(foo); assert((int)ret.size() <= 10 * n); cout << ret.size() << endl; for (auto i : ret) { cout << i.first + 1 << " " << i.second + 1 << "\n"; } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, k; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; vector<vector<int>> g(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } vector<int> color(n); vector<int> d(n); vector<int> sub(n); int iter = 0; function<void(int, int, int, int)> dfs_to_color = [&](int v, int p, int c, int pr) { color[v] = c; for (int i : g[v]) { if (i == p || color[i] != pr) continue; dfs_to_color(i, v, c, pr); } }; function<void(int, int, int)> dfs_on_color = [&](int v, int p, int c) { if (~p) d[v] = d[p] ^ 1; else d[v] = 0; sub[v] = 1; for (int i : g[v]) { if (color[i] != c || i == p) continue; dfs_on_color(i, v, c); sub[v] += sub[i]; } }; vector<pair<int, int>> ret; function<void(vector<int>)> solve = [&](vector<int> nodes) { int sz = nodes.size(); if (sz <= 4) return; int root = nodes[0]; int c = color[root]; dfs_on_color(root, -1, c); int pr = -1; while (true) { int nxt = -1; for (int i : g[root]) { if (color[i] != c || i == pr) continue; if (sub[i] > sz / 2) nxt = i; } if (nxt == -1) break; pr = root; root = nxt; } for (int i : g[root]) { if (color[i] != c) continue; iter++; dfs_to_color(i, root, iter, c); } map<int, vector<int>> to_color; for (int i : nodes) { if (i == root) continue; to_color[color[i]].push_back(i); } int pos = -1; int cd = 0; for (auto it : to_color) { int id = it.first; vector<int> nxt = it.second; solve(nxt); for (int i : nxt) color[i] = id; dfs_on_color(nxt[0], -1, id); if ((int)nxt.size() > cd) { cd = nxt.size(); pos = id; } } for (auto it : to_color) { int id = it.first; vector<int> vertices = it.second; vector<int> to_root; if (id != pos) to_root = vertices; else { vector<int> cnt(2); for (int i : vertices) cnt[d[i]]++; int chosen = cnt[1] < cnt[0]; for (int i : vertices) { if (d[i] == chosen) to_root.push_back(i); } } for (int i : to_root) { ret.emplace_back(i, root); } } }; vector<int> foo(n); iota(foo.begin(), foo.end(), 0); solve(foo); assert((int)ret.size() <= 10 * n); cout << ret.size() << endl; for (auto i : ret) { cout << i.first + 1 << " " << i.second + 1 << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 500; const int M = 1e6 + 500; const int LOG = 18; const int OFF = (1 << LOG); int cen[N], siz[N], n, k; vector<int> cur, za[2], v[N]; vector<pair<int, int> > edg; void bojaj(int x, int lst, int koj) { za[koj].push_back(x); for (int y : v[x]) if (y != lst && !cen[y]) bojaj(y, x, !koj); } void dfs(int x, int lst) { siz[x] = 1; cur.push_back(x); for (int y : v[x]) { if (!cen[y] && y != lst) { dfs(y, x); siz[x] += siz[y]; } } } bool cmp(int x, int y) { return siz[x] > siz[y]; } void nadi(int root) { cur.clear(); dfs(root, root); int nw = root; int ja = (int)cur.size(); for (int x : cur) { if (2 * siz[x] >= ja && siz[x] < siz[nw]) nw = x; } root = nw; cen[root] = 1; cur.clear(); dfs(root, root); vector<int> dalje; for (int y : v[root]) if (!cen[y]) dalje.push_back(y); sort(dalje.begin(), dalje.end(), cmp); for (int i = 0; i < (int)dalje.size(); i++) { za[0].clear(); za[1].clear(); bojaj(dalje[i], dalje[i], 0); if ((int)za[0].size() > (int)za[1].size()) swap(za[0], za[1]); for (int x : za[0]) edg.push_back({root, x}); if (i) for (int x : za[1]) edg.push_back({root, x}); } for (int x : dalje) nadi(x); } int main() { scanf("%d%d", &n, &k); for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); v[x].push_back(y), v[y].push_back(x); } nadi(1); printf("%d\n", (int)edg.size()); for (pair<int, int> tmp : edg) printf("%d %d\n", tmp.first, tmp.second); }
### Prompt Please provide a cpp coded solution to the problem described below: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 500; const int M = 1e6 + 500; const int LOG = 18; const int OFF = (1 << LOG); int cen[N], siz[N], n, k; vector<int> cur, za[2], v[N]; vector<pair<int, int> > edg; void bojaj(int x, int lst, int koj) { za[koj].push_back(x); for (int y : v[x]) if (y != lst && !cen[y]) bojaj(y, x, !koj); } void dfs(int x, int lst) { siz[x] = 1; cur.push_back(x); for (int y : v[x]) { if (!cen[y] && y != lst) { dfs(y, x); siz[x] += siz[y]; } } } bool cmp(int x, int y) { return siz[x] > siz[y]; } void nadi(int root) { cur.clear(); dfs(root, root); int nw = root; int ja = (int)cur.size(); for (int x : cur) { if (2 * siz[x] >= ja && siz[x] < siz[nw]) nw = x; } root = nw; cen[root] = 1; cur.clear(); dfs(root, root); vector<int> dalje; for (int y : v[root]) if (!cen[y]) dalje.push_back(y); sort(dalje.begin(), dalje.end(), cmp); for (int i = 0; i < (int)dalje.size(); i++) { za[0].clear(); za[1].clear(); bojaj(dalje[i], dalje[i], 0); if ((int)za[0].size() > (int)za[1].size()) swap(za[0], za[1]); for (int x : za[0]) edg.push_back({root, x}); if (i) for (int x : za[1]) edg.push_back({root, x}); } for (int x : dalje) nadi(x); } int main() { scanf("%d%d", &n, &k); for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); v[x].push_back(y), v[y].push_back(x); } nadi(1); printf("%d\n", (int)edg.size()); for (pair<int, int> tmp : edg) printf("%d %d\n", tmp.first, tmp.second); } ```
#include <bits/stdc++.h> using namespace std; const int MN = 1e4 + 5; int N, K, i, x, y, sz[MN], mx[MN], ct, c[MN], dp[MN]; vector<int> adj[MN], elem[2]; vector<pair<int, int> > ans; int dfs(int n, int p) { sz[n] = 1; mx[n] = 0; for (auto v : adj[n]) { if (v == p || c[v]) continue; sz[n] += dfs(v, n); mx[n] = max(mx[n], sz[v]); } return sz[n]; } void dfs2(int n, int p, int tot) { if (2 * mx[n] <= tot && 2 * (tot - sz[n]) <= tot) ct = n; for (auto v : adj[n]) { if (v == p || c[v]) continue; dfs2(v, n, tot); } } void op(int n, int p, int r) { if (r ^ p) ans.push_back({n, r}); for (auto v : adj[n]) { if (v == p || c[v]) continue; op(v, n, r); } } void op2(int n, int p, int d) { elem[d].push_back(n); for (auto v : adj[n]) { if (v == p || c[v]) continue; op2(v, n, d ^ 1); } } void solve(int n) { dfs2(n, 0, dfs(n, 0)); int cur = ct; c[cur] = 1; dfs(cur, 0); pair<int, int> big(-1, -1); for (auto v : adj[cur]) { if (c[v]) continue; if (sz[v] > big.first) big = {sz[v], v}; } for (int i = 0; i < 2; i++) elem[i].clear(); for (auto v : adj[cur]) { if (c[v]) continue; if (v == big.second) op2(v, cur, 0); else op(v, cur, cur); } int idx = 0; if (elem[1].size() < elem[0].size()) idx = 1; for (auto v : elem[idx]) if (v != big.second) ans.push_back({cur, v}); for (auto v : adj[cur]) { if (c[v]) continue; solve(v); } } int main() { scanf("%d%d", &N, &K); for (i = 1; i < N; i++) { scanf("%d%d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } solve(1); assert(10 * N >= ans.size()); printf("%d\n", (int)ans.size()); for (auto v : ans) printf("%d %d\n", v.first, v.second); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MN = 1e4 + 5; int N, K, i, x, y, sz[MN], mx[MN], ct, c[MN], dp[MN]; vector<int> adj[MN], elem[2]; vector<pair<int, int> > ans; int dfs(int n, int p) { sz[n] = 1; mx[n] = 0; for (auto v : adj[n]) { if (v == p || c[v]) continue; sz[n] += dfs(v, n); mx[n] = max(mx[n], sz[v]); } return sz[n]; } void dfs2(int n, int p, int tot) { if (2 * mx[n] <= tot && 2 * (tot - sz[n]) <= tot) ct = n; for (auto v : adj[n]) { if (v == p || c[v]) continue; dfs2(v, n, tot); } } void op(int n, int p, int r) { if (r ^ p) ans.push_back({n, r}); for (auto v : adj[n]) { if (v == p || c[v]) continue; op(v, n, r); } } void op2(int n, int p, int d) { elem[d].push_back(n); for (auto v : adj[n]) { if (v == p || c[v]) continue; op2(v, n, d ^ 1); } } void solve(int n) { dfs2(n, 0, dfs(n, 0)); int cur = ct; c[cur] = 1; dfs(cur, 0); pair<int, int> big(-1, -1); for (auto v : adj[cur]) { if (c[v]) continue; if (sz[v] > big.first) big = {sz[v], v}; } for (int i = 0; i < 2; i++) elem[i].clear(); for (auto v : adj[cur]) { if (c[v]) continue; if (v == big.second) op2(v, cur, 0); else op(v, cur, cur); } int idx = 0; if (elem[1].size() < elem[0].size()) idx = 1; for (auto v : elem[idx]) if (v != big.second) ans.push_back({cur, v}); for (auto v : adj[cur]) { if (c[v]) continue; solve(v); } } int main() { scanf("%d%d", &N, &K); for (i = 1; i < N; i++) { scanf("%d%d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } solve(1); assert(10 * N >= ans.size()); printf("%d\n", (int)ans.size()); for (auto v : ans) printf("%d %d\n", v.first, v.second); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<vector<int>> g(n); for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; --x; --y; g[x].push_back(y); g[y].push_back(x); } vector<bool> alive(n, true); vector<int> sz(n); vector<int> pv(n); vector<pair<int, int>> ret; vector<int> all; vector<int> tin(n); vector<int> tout(n); int T = -1; function<void(int)> Dfs = [&](int v) { all.push_back(v); tin[v] = ++T; sz[v] = 1; for (int u : g[v]) { if (alive[u] && u != pv[v]) { pv[u] = v; Dfs(u); sz[v] += sz[u]; } } tout[v] = ++T; }; function<void(int)> Solve = [&](int v) { all.clear(); pv[v] = -1; Dfs(v); int total = sz[v]; while (true) { bool found = false; for (int u : g[v]) { if (alive[u] && pv[u] == v && 2 * sz[u] >= total) { v = u; found = true; break; } } if (!found) { break; } } alive[v] = false; set<int> best; for (int u : g[v]) { if (alive[u]) { all.clear(); pv[u] = -1; Dfs(u); set<int> cur; for (int w : g[u]) { if (alive[w]) { cur.insert(w); } } if (cur.size() > best.size()) { best = cur; } } } all.clear(); pv[v] = -1; Dfs(v); for (int u : all) { if (u != v && pv[u] != v && best.find(u) == best.end()) { ret.emplace_back(u, v); } } vector<int> children; for (int u : g[v]) { if (alive[u]) { children.push_back(u); } } for (int u : children) { Solve(u); } }; Solve(0); assert((int)ret.size() <= 10 * n); cout << ret.size() << '\n'; for (auto& p : ret) { cout << p.first + 1 << " " << p.second + 1 << '\n'; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<vector<int>> g(n); for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; --x; --y; g[x].push_back(y); g[y].push_back(x); } vector<bool> alive(n, true); vector<int> sz(n); vector<int> pv(n); vector<pair<int, int>> ret; vector<int> all; vector<int> tin(n); vector<int> tout(n); int T = -1; function<void(int)> Dfs = [&](int v) { all.push_back(v); tin[v] = ++T; sz[v] = 1; for (int u : g[v]) { if (alive[u] && u != pv[v]) { pv[u] = v; Dfs(u); sz[v] += sz[u]; } } tout[v] = ++T; }; function<void(int)> Solve = [&](int v) { all.clear(); pv[v] = -1; Dfs(v); int total = sz[v]; while (true) { bool found = false; for (int u : g[v]) { if (alive[u] && pv[u] == v && 2 * sz[u] >= total) { v = u; found = true; break; } } if (!found) { break; } } alive[v] = false; set<int> best; for (int u : g[v]) { if (alive[u]) { all.clear(); pv[u] = -1; Dfs(u); set<int> cur; for (int w : g[u]) { if (alive[w]) { cur.insert(w); } } if (cur.size() > best.size()) { best = cur; } } } all.clear(); pv[v] = -1; Dfs(v); for (int u : all) { if (u != v && pv[u] != v && best.find(u) == best.end()) { ret.emplace_back(u, v); } } vector<int> children; for (int u : g[v]) { if (alive[u]) { children.push_back(u); } } for (int u : children) { Solve(u); } }; Solve(0); assert((int)ret.size() <= 10 * n); cout << ret.size() << '\n'; for (auto& p : ret) { cout << p.first + 1 << " " << p.second + 1 << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct Node { vector<int> nb; bool isCutVertex = false; int parent = -1; int cutVertexLevel; int subtreeSize; }; int n, a, b, k; map<pair<int, int>, bool> edges; Node vertex[100005]; vector<int> treesToCut; vector<pair<int, int>> extraEdges; int visited[100005] = {0}; int cut(int root, int level, int treeSize, bool isFirstInLevel) { visited[root] = level; int totalSubtreeSize = 0; for (int nb : vertex[root].nb) { if (!vertex[nb].isCutVertex && visited[nb] < level) { vertex[nb].parent = root; visited[nb] = level; totalSubtreeSize += cut(nb, level, treeSize, false); } } totalSubtreeSize += 1; if (totalSubtreeSize >= sqrt(treeSize) && treeSize > k) { vertex[root].isCutVertex = true; vertex[root].cutVertexLevel = level; for (int nb : vertex[root].nb) { if (!vertex[nb].isCutVertex && vertex[root].parent != nb) { cut(nb, level + 1, vertex[nb].subtreeSize, true); } } return 0; } else { vertex[root].subtreeSize = totalSubtreeSize; if (isFirstInLevel && treeSize > k) { cut(root, level + 1, vertex[root].subtreeSize, true); } return totalSubtreeSize; } } vector<int> notCutNeighbors; vector<int> cutNeighbors; vector<int> oneLevelLess; void dfs(int node, int level) { visited[node] = level; notCutNeighbors.push_back(node); if (vertex[node].isCutVertex && vertex[node].cutVertexLevel == level + 1) oneLevelLess.push_back(node); for (int nb : vertex[node].nb) { if (vertex[nb].isCutVertex) { if (vertex[nb].cutVertexLevel < level) continue; if (vertex[nb].cutVertexLevel == level) { cutNeighbors.push_back(nb); continue; } } if (visited[nb] > level) { visited[nb] = level; dfs(nb, level); } } } void connect() { for (int cut : cutNeighbors) { for (int notCut : notCutNeighbors) { if (!edges[make_pair(cut, notCut)]) { extraEdges.push_back(make_pair(cut, notCut)); } } } for (int node1 : oneLevelLess) { for (int node2 : oneLevelLess) { if (node1 == node2) continue; if (!edges[make_pair(node1, node2)]) { extraEdges.push_back(make_pair(node1, node2)); } } } } int main() { cin >> n >> k; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; vertex[a - 1].nb.push_back(b - 1); vertex[b - 1].nb.push_back(a - 1); edges[make_pair(a - 1, b - 1)] = true; edges[make_pair(b - 1, a - 1)] = true; } vertex[0].parent = -1; vertex[0].subtreeSize = n; cut(0, 1, n, true); for (int i = 0; i < n; ++i) { visited[i] = n + 1; } for (int level = 20; level >= 0; --level) { notCutNeighbors.clear(); cutNeighbors.clear(); oneLevelLess.clear(); for (int j = 0; j < n; ++j) { if ((!vertex[j].isCutVertex || (vertex[j].cutVertexLevel > level)) && visited[j] > level) { dfs(j, level); connect(); notCutNeighbors.clear(); cutNeighbors.clear(); oneLevelLess.clear(); } } } cout << extraEdges.size() << "\n"; for (auto edge : extraEdges) { cout << edge.first + 1 << " " << edge.second + 1 << "\n"; } }
### Prompt Please provide a Cpp coded solution to the problem described below: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Node { vector<int> nb; bool isCutVertex = false; int parent = -1; int cutVertexLevel; int subtreeSize; }; int n, a, b, k; map<pair<int, int>, bool> edges; Node vertex[100005]; vector<int> treesToCut; vector<pair<int, int>> extraEdges; int visited[100005] = {0}; int cut(int root, int level, int treeSize, bool isFirstInLevel) { visited[root] = level; int totalSubtreeSize = 0; for (int nb : vertex[root].nb) { if (!vertex[nb].isCutVertex && visited[nb] < level) { vertex[nb].parent = root; visited[nb] = level; totalSubtreeSize += cut(nb, level, treeSize, false); } } totalSubtreeSize += 1; if (totalSubtreeSize >= sqrt(treeSize) && treeSize > k) { vertex[root].isCutVertex = true; vertex[root].cutVertexLevel = level; for (int nb : vertex[root].nb) { if (!vertex[nb].isCutVertex && vertex[root].parent != nb) { cut(nb, level + 1, vertex[nb].subtreeSize, true); } } return 0; } else { vertex[root].subtreeSize = totalSubtreeSize; if (isFirstInLevel && treeSize > k) { cut(root, level + 1, vertex[root].subtreeSize, true); } return totalSubtreeSize; } } vector<int> notCutNeighbors; vector<int> cutNeighbors; vector<int> oneLevelLess; void dfs(int node, int level) { visited[node] = level; notCutNeighbors.push_back(node); if (vertex[node].isCutVertex && vertex[node].cutVertexLevel == level + 1) oneLevelLess.push_back(node); for (int nb : vertex[node].nb) { if (vertex[nb].isCutVertex) { if (vertex[nb].cutVertexLevel < level) continue; if (vertex[nb].cutVertexLevel == level) { cutNeighbors.push_back(nb); continue; } } if (visited[nb] > level) { visited[nb] = level; dfs(nb, level); } } } void connect() { for (int cut : cutNeighbors) { for (int notCut : notCutNeighbors) { if (!edges[make_pair(cut, notCut)]) { extraEdges.push_back(make_pair(cut, notCut)); } } } for (int node1 : oneLevelLess) { for (int node2 : oneLevelLess) { if (node1 == node2) continue; if (!edges[make_pair(node1, node2)]) { extraEdges.push_back(make_pair(node1, node2)); } } } } int main() { cin >> n >> k; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; vertex[a - 1].nb.push_back(b - 1); vertex[b - 1].nb.push_back(a - 1); edges[make_pair(a - 1, b - 1)] = true; edges[make_pair(b - 1, a - 1)] = true; } vertex[0].parent = -1; vertex[0].subtreeSize = n; cut(0, 1, n, true); for (int i = 0; i < n; ++i) { visited[i] = n + 1; } for (int level = 20; level >= 0; --level) { notCutNeighbors.clear(); cutNeighbors.clear(); oneLevelLess.clear(); for (int j = 0; j < n; ++j) { if ((!vertex[j].isCutVertex || (vertex[j].cutVertexLevel > level)) && visited[j] > level) { dfs(j, level); connect(); notCutNeighbors.clear(); cutNeighbors.clear(); oneLevelLess.clear(); } } } cout << extraEdges.size() << "\n"; for (auto edge : extraEdges) { cout << edge.first + 1 << " " << edge.second + 1 << "\n"; } } ```
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") using namespace std; bool dbg = 0; clock_t start_time = clock(); void bad(string mes = "NO") { cout << mes; exit(0); } void bad(int mes) { cout << mes; exit(0); } template <typename T> string bin(T x, int st = 2) { string ans = ""; while (x > 0) { ans += char('0' + x % st); x /= st; } reverse(ans.begin(), ans.end()); return ans.empty() ? "0" : ans; } mt19937_64 mt_rand(228); template <typename T1, typename T2> inline bool upmax(T1& a, T2 b) { return (a < b ? (a = b, true) : false); } template <typename T1, typename T2> inline bool upmin(T1& a, T2 b) { return (b < a ? (a = b, true) : false); } template <typename T> T input() { T ans = 0, m = 1; char c = ' '; while (!((c >= '0' && c <= '9') || c == '-')) { c = getchar(); } if (c == '-') m = -1, c = getchar(); while (c >= '0' && c <= '9') { ans = ans * 10 + (c - '0'), c = getchar(); } return ans * m; } template <typename T> T gcd(T a, T b) { while (b) { a %= b; swap(a, b); } return a; } template <typename T> void read(T& a) { a = input<T>(); } template <typename T> void read(T& a, T& b) { read(a), read(b); } template <typename T> void read(T& a, T& b, T& c) { read(a, b), read(c); } template <typename T> void read(T& a, T& b, T& c, T& d) { read(a, b), read(c, d); } const int inf = 1e9 + 20; const short short_inf = 3e4 + 20; const long double eps = 1e-12; const int maxn = (int)10004 + 3, base = 1e9 + 7; const long long llinf = 2e18 + 5; const int mod = 1e9 + 7; int binpow(int a, int n) { int res = 1; while (n) { if (n & 1) res = 1ll * res * a % base; a = 1ll * a * a % base; n >>= 1; } return res; } vector<int> g[maxn]; bool deleted[maxn]; int sizeOfTree[maxn]; void getTreeSizes(int v, int p = -1) { sizeOfTree[v] = 1; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (to != p && !deleted[to]) { getTreeSizes(to, v); sizeOfTree[v] += sizeOfTree[to]; } } } int cntLayers = 0; vector<pair<int, int>> ans; void fixingWays(int v, int p, int& l, int depth = 0, bool first = false) { if (depth > 1 + first) { ans.push_back({l, v}); } for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (to != p && !deleted[to]) { fixingWays(to, v, l, depth + 1, first); } } } void buildingDfs(int start) { getTreeSizes(start); int centroid = -1; int N = sizeOfTree[start]; int p = -1, v = start; while (centroid == -1) { pair<int, int> maxSon = make_pair(0, -1); for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!deleted[to] && to != p) { maxSon = max(maxSon, make_pair(sizeOfTree[to], to)); } } if (maxSon.first > N / 2) { p = v; v = maxSon.second; } else centroid = v; } int curLayer = cntLayers++; if (N > 4) { bool first = true; for (auto to : g[centroid]) { if (!deleted[to]) { fixingWays(to, centroid, centroid, 1, first); first = false; } } } deleted[centroid] = 1; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!deleted[to]) buildingDfs(to); } } void buildCentroid(int& n) { cntLayers = 0; for (int i = 0; i < n; i++) { deleted[i] = 0; } buildingDfs(0); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for (int i = 0; i + 1 < n; i++) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); } buildCentroid(n); cout << ans.size() << "\n"; for (auto [u, v] : ans) { cout << u + 1 << ' ' << v + 1 << "\n"; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") using namespace std; bool dbg = 0; clock_t start_time = clock(); void bad(string mes = "NO") { cout << mes; exit(0); } void bad(int mes) { cout << mes; exit(0); } template <typename T> string bin(T x, int st = 2) { string ans = ""; while (x > 0) { ans += char('0' + x % st); x /= st; } reverse(ans.begin(), ans.end()); return ans.empty() ? "0" : ans; } mt19937_64 mt_rand(228); template <typename T1, typename T2> inline bool upmax(T1& a, T2 b) { return (a < b ? (a = b, true) : false); } template <typename T1, typename T2> inline bool upmin(T1& a, T2 b) { return (b < a ? (a = b, true) : false); } template <typename T> T input() { T ans = 0, m = 1; char c = ' '; while (!((c >= '0' && c <= '9') || c == '-')) { c = getchar(); } if (c == '-') m = -1, c = getchar(); while (c >= '0' && c <= '9') { ans = ans * 10 + (c - '0'), c = getchar(); } return ans * m; } template <typename T> T gcd(T a, T b) { while (b) { a %= b; swap(a, b); } return a; } template <typename T> void read(T& a) { a = input<T>(); } template <typename T> void read(T& a, T& b) { read(a), read(b); } template <typename T> void read(T& a, T& b, T& c) { read(a, b), read(c); } template <typename T> void read(T& a, T& b, T& c, T& d) { read(a, b), read(c, d); } const int inf = 1e9 + 20; const short short_inf = 3e4 + 20; const long double eps = 1e-12; const int maxn = (int)10004 + 3, base = 1e9 + 7; const long long llinf = 2e18 + 5; const int mod = 1e9 + 7; int binpow(int a, int n) { int res = 1; while (n) { if (n & 1) res = 1ll * res * a % base; a = 1ll * a * a % base; n >>= 1; } return res; } vector<int> g[maxn]; bool deleted[maxn]; int sizeOfTree[maxn]; void getTreeSizes(int v, int p = -1) { sizeOfTree[v] = 1; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (to != p && !deleted[to]) { getTreeSizes(to, v); sizeOfTree[v] += sizeOfTree[to]; } } } int cntLayers = 0; vector<pair<int, int>> ans; void fixingWays(int v, int p, int& l, int depth = 0, bool first = false) { if (depth > 1 + first) { ans.push_back({l, v}); } for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (to != p && !deleted[to]) { fixingWays(to, v, l, depth + 1, first); } } } void buildingDfs(int start) { getTreeSizes(start); int centroid = -1; int N = sizeOfTree[start]; int p = -1, v = start; while (centroid == -1) { pair<int, int> maxSon = make_pair(0, -1); for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!deleted[to] && to != p) { maxSon = max(maxSon, make_pair(sizeOfTree[to], to)); } } if (maxSon.first > N / 2) { p = v; v = maxSon.second; } else centroid = v; } int curLayer = cntLayers++; if (N > 4) { bool first = true; for (auto to : g[centroid]) { if (!deleted[to]) { fixingWays(to, centroid, centroid, 1, first); first = false; } } } deleted[centroid] = 1; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!deleted[to]) buildingDfs(to); } } void buildCentroid(int& n) { cntLayers = 0; for (int i = 0; i < n; i++) { deleted[i] = 0; } buildingDfs(0); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for (int i = 0; i + 1 < n; i++) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); } buildCentroid(n); cout << ans.size() << "\n"; for (auto [u, v] : ans) { cout << u + 1 << ' ' << v + 1 << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; void debug(...) {} const int maxn = 10010; int n, sz[maxn], k; bool ban[maxn]; vector<int> edge[maxn]; vector<pair<int, int>> res; int getsz(int now, int lst = -1) { sz[now] = 1; for (int u : edge[now]) if (u != lst && !ban[u]) sz[now] += getsz(u, now); return sz[now]; } int getcen(int now, int allsz, int lst = -1) { for (int u : edge[now]) if (u != lst && !ban[u]) if (sz[u] * 2 > allsz) return getcen(u, allsz, now); return now; } void con(int now, int tp, int lst = -1) { static int d; if (ban[now]) return; ++d; if (d > 1) res.emplace_back(now, tp); for (int u : edge[now]) if (u != lst) con(u, tp, now); --d; } void spcon(int now, int tp, int lst = -1) { static int d; if (ban[now]) return; ++d; if (d > 2) res.emplace_back(now, tp); for (int u : edge[now]) if (u != lst) spcon(u, tp, now); --d; } void solve(int sd = 1) { static int d; if (getsz(sd) - 1 <= k) return; ++d; int cen = getcen(sd, sz[sd]); getsz(cen); ban[cen] = true; int p = 0; for (int u : edge[cen]) if (!ban[u]) { if (sz[u] > sz[p]) p = u; } for (int u : edge[cen]) if (u == p) spcon(u, cen); else con(u, cen); int csz = sz[sd]; for (int u : edge[cen]) if (!ban[u]) solve(u); --d; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> k; for (int i = 1, a, b; i < n; ++i) { cin >> a >> b; edge[a].emplace_back(b); edge[b].emplace_back(a); } solve(); 0; assert(res.size() <= n * 10); cout << res.size() << '\n'; for (auto [a, b] : res) cout << a << ' ' << b << '\n'; }
### Prompt Generate a cpp solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; void debug(...) {} const int maxn = 10010; int n, sz[maxn], k; bool ban[maxn]; vector<int> edge[maxn]; vector<pair<int, int>> res; int getsz(int now, int lst = -1) { sz[now] = 1; for (int u : edge[now]) if (u != lst && !ban[u]) sz[now] += getsz(u, now); return sz[now]; } int getcen(int now, int allsz, int lst = -1) { for (int u : edge[now]) if (u != lst && !ban[u]) if (sz[u] * 2 > allsz) return getcen(u, allsz, now); return now; } void con(int now, int tp, int lst = -1) { static int d; if (ban[now]) return; ++d; if (d > 1) res.emplace_back(now, tp); for (int u : edge[now]) if (u != lst) con(u, tp, now); --d; } void spcon(int now, int tp, int lst = -1) { static int d; if (ban[now]) return; ++d; if (d > 2) res.emplace_back(now, tp); for (int u : edge[now]) if (u != lst) spcon(u, tp, now); --d; } void solve(int sd = 1) { static int d; if (getsz(sd) - 1 <= k) return; ++d; int cen = getcen(sd, sz[sd]); getsz(cen); ban[cen] = true; int p = 0; for (int u : edge[cen]) if (!ban[u]) { if (sz[u] > sz[p]) p = u; } for (int u : edge[cen]) if (u == p) spcon(u, cen); else con(u, cen); int csz = sz[sd]; for (int u : edge[cen]) if (!ban[u]) solve(u); --d; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> k; for (int i = 1, a, b; i < n; ++i) { cin >> a >> b; edge[a].emplace_back(b); edge[b].emplace_back(a); } solve(); 0; assert(res.size() <= n * 10); cout << res.size() << '\n'; for (auto [a, b] : res) cout << a << ' ' << b << '\n'; } ```
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char* x) { cerr << '\"' << x << '\"'; } void __print(const string& x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V>& x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T& x) { int f = 0; cerr << '{'; for (auto& i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } int pct(int x) { return __builtin_popcount(x); } int bits(int x) { return 31 - __builtin_clz(x); } int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); } int fstTrue(function<bool(int)> first, int lo, int hi) { hi++; assert(lo <= hi); while (lo < hi) { int mid = (lo + hi) / 2; first(mid) ? hi = mid : lo = mid + 1; } return lo; } const int INF = 0x3f3f3f3f; const int NINF = 0xc0c0c0c0; const long long INFLL = 0x3f3f3f3f3f3f3f3f; const long long NINFLL = 0xc0c0c0c0c0c0c0c0; const long long MOD = 1e9 + 7; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } long long binpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b >>= 1; } return res; } long long modInv(long long a) { return binpow(a, MOD - 2); } bool sortcol(const vector<long long>& v1, const vector<long long>& v2) { return v1[1] < v2[1]; } bool sortpair(const pair<int, int>& p1, const pair<int, int>& p2) { return p1.first < p2.first; } mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<long long unsigned> distribution(0, 10); const int mxN = 2e5; int q = 1, n, k, a, b, sz[mxN], depth[mxN]; vector<int> adj[mxN]; vector<pair<int, int> > ans; bool took; int init_size_and_depth(int v, int p = -1) { if (p != -1) depth[v] = depth[p] + 1; sz[v] = 1; for (auto& x : adj[v]) { if (x != p) { sz[v] += init_size_and_depth(x, v); } } return sz[v]; } int find_centroid(int v, int p, int n) { for (auto& x : adj[v]) { if (x != p && sz[x] > n / 2) { return find_centroid(x, v, n); } } return v; } vector<int> remove_edges(int v) { vector<int> neighbors; for (auto& x : adj[v]) neighbors.push_back(x); for (auto& x : neighbors) { adj[v].erase(find(adj[v].begin(), adj[v].end(), x)); adj[x].erase(find(adj[x].begin(), adj[x].end(), v)); } return neighbors; } void dfs(int c, int v, int p = -1, int dist = 0) { for (auto& x : adj[v]) { if (x != p) { if (!took && dist == 1) took = 1; else if (dist) ans.push_back({c, x}); dfs(c, x, v, dist + 1); } } } void recurse(int x) { init_size_and_depth(x); if (sz[x] <= 2) return; int c = find_centroid(x, -1, sz[x]); took = 0; dfs(c, c); vector<int> neighbors = remove_edges(c); for (auto& i : neighbors) { recurse(i); } } void solve() { cin >> n >> k; for (int i = (0); i < (n - 1); i++) { cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } recurse(0); cout << ans.size() << "\n"; for (auto& i : ans) cout << i.first + 1 << " " << i.second + 1 << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); while (q--) { solve(); } }
### Prompt Generate a Cpp solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char* x) { cerr << '\"' << x << '\"'; } void __print(const string& x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V>& x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T& x) { int f = 0; cerr << '{'; for (auto& i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } int pct(int x) { return __builtin_popcount(x); } int bits(int x) { return 31 - __builtin_clz(x); } int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); } int fstTrue(function<bool(int)> first, int lo, int hi) { hi++; assert(lo <= hi); while (lo < hi) { int mid = (lo + hi) / 2; first(mid) ? hi = mid : lo = mid + 1; } return lo; } const int INF = 0x3f3f3f3f; const int NINF = 0xc0c0c0c0; const long long INFLL = 0x3f3f3f3f3f3f3f3f; const long long NINFLL = 0xc0c0c0c0c0c0c0c0; const long long MOD = 1e9 + 7; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } long long binpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b >>= 1; } return res; } long long modInv(long long a) { return binpow(a, MOD - 2); } bool sortcol(const vector<long long>& v1, const vector<long long>& v2) { return v1[1] < v2[1]; } bool sortpair(const pair<int, int>& p1, const pair<int, int>& p2) { return p1.first < p2.first; } mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<long long unsigned> distribution(0, 10); const int mxN = 2e5; int q = 1, n, k, a, b, sz[mxN], depth[mxN]; vector<int> adj[mxN]; vector<pair<int, int> > ans; bool took; int init_size_and_depth(int v, int p = -1) { if (p != -1) depth[v] = depth[p] + 1; sz[v] = 1; for (auto& x : adj[v]) { if (x != p) { sz[v] += init_size_and_depth(x, v); } } return sz[v]; } int find_centroid(int v, int p, int n) { for (auto& x : adj[v]) { if (x != p && sz[x] > n / 2) { return find_centroid(x, v, n); } } return v; } vector<int> remove_edges(int v) { vector<int> neighbors; for (auto& x : adj[v]) neighbors.push_back(x); for (auto& x : neighbors) { adj[v].erase(find(adj[v].begin(), adj[v].end(), x)); adj[x].erase(find(adj[x].begin(), adj[x].end(), v)); } return neighbors; } void dfs(int c, int v, int p = -1, int dist = 0) { for (auto& x : adj[v]) { if (x != p) { if (!took && dist == 1) took = 1; else if (dist) ans.push_back({c, x}); dfs(c, x, v, dist + 1); } } } void recurse(int x) { init_size_and_depth(x); if (sz[x] <= 2) return; int c = find_centroid(x, -1, sz[x]); took = 0; dfs(c, c); vector<int> neighbors = remove_edges(c); for (auto& i : neighbors) { recurse(i); } } void solve() { cin >> n >> k; for (int i = (0); i < (n - 1); i++) { cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); } recurse(0); cout << ans.size() << "\n"; for (auto& i : ans) cout << i.first + 1 << " " << i.second + 1 << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); while (q--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; void solve() { long long n, k; cin >> n >> k; vector<vector<long long>> g(n); for (long long i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; --x; --y; g[x].push_back(y); g[y].push_back(x); } vector<bool> vis(n, 1); vector<long long> sz(n), pv(n), all, tin(n), tout(n); vector<pair<long long, long long>> res; long long timer = -1; function<void(long long)> dfs = [&](long long s) { all.push_back(s); tin[s] = ++timer; sz[s] = 1; for (auto u : g[s]) { if (vis[u] and u != pv[s]) { pv[u] = s; dfs(u); sz[s] += sz[u]; } } tout[s] = ++timer; }; function<void(long long)> soln = [&](long long v) { all.clear(); pv[v] = -1; dfs(v); long long total = sz[v]; while (1) { bool f = 0; for (auto u : g[v]) { if (vis[u] and pv[u] == v and 2 * sz[u] >= total) { v = u; f = 1; break; } } if (!f) break; } vis[v] = 0; set<long long> best; for (auto u : g[v]) { if (vis[u]) { all.clear(); pv[u] = -1; dfs(u); set<long long> cur; for (auto w : g[u]) if (vis[w]) cur.insert(w); if (cur.size() > best.size()) best = cur; } } all.clear(); pv[v] = -1; dfs(v); for (auto u : all) if (u != v and pv[u] != v and best.find(u) == best.end()) res.push_back({u, v}); vector<long long> cc; for (auto u : g[v]) if (vis[u]) cc.push_back(u); for (auto u : cc) soln(u); }; soln(0); cout << res.size() << '\n'; for (auto x : res) cout << x.first + 1 << ' ' << x.second + 1 << '\n'; } signed main() { ios_base::sync_with_stdio(0), cin.tie(nullptr); long long qq = 1; while (qq--) solve(); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; void solve() { long long n, k; cin >> n >> k; vector<vector<long long>> g(n); for (long long i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; --x; --y; g[x].push_back(y); g[y].push_back(x); } vector<bool> vis(n, 1); vector<long long> sz(n), pv(n), all, tin(n), tout(n); vector<pair<long long, long long>> res; long long timer = -1; function<void(long long)> dfs = [&](long long s) { all.push_back(s); tin[s] = ++timer; sz[s] = 1; for (auto u : g[s]) { if (vis[u] and u != pv[s]) { pv[u] = s; dfs(u); sz[s] += sz[u]; } } tout[s] = ++timer; }; function<void(long long)> soln = [&](long long v) { all.clear(); pv[v] = -1; dfs(v); long long total = sz[v]; while (1) { bool f = 0; for (auto u : g[v]) { if (vis[u] and pv[u] == v and 2 * sz[u] >= total) { v = u; f = 1; break; } } if (!f) break; } vis[v] = 0; set<long long> best; for (auto u : g[v]) { if (vis[u]) { all.clear(); pv[u] = -1; dfs(u); set<long long> cur; for (auto w : g[u]) if (vis[w]) cur.insert(w); if (cur.size() > best.size()) best = cur; } } all.clear(); pv[v] = -1; dfs(v); for (auto u : all) if (u != v and pv[u] != v and best.find(u) == best.end()) res.push_back({u, v}); vector<long long> cc; for (auto u : g[v]) if (vis[u]) cc.push_back(u); for (auto u : cc) soln(u); }; soln(0); cout << res.size() << '\n'; for (auto x : res) cout << x.first + 1 << ' ' << x.second + 1 << '\n'; } signed main() { ios_base::sync_with_stdio(0), cin.tie(nullptr); long long qq = 1; while (qq--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void chmin(T &x, const T &y) { if (x > y) x = y; } template <typename T> void chmax(T &x, const T &y) { if (x < y) x = y; } int read() { char c; while ((c = getchar()) < '-') ; if (c == '-') { int x = (c = getchar()) - '0'; while ((c = getchar()) >= '0') x = x * 10 + c - '0'; return -x; } int x = c - '0'; while ((c = getchar()) >= '0') x = x * 10 + c - '0'; return x; } const int N = 10000 + 5; vector<int> lk[N]; bool ban[N]; vector<pair<int, int> > ans; set<pair<int, int> > ans_set; void ans_push(int x, int y) { if (x > y) swap(x, y); pair<int, int> p = {x, y}; if (ans_set.count(p)) return; ans_set.insert(p); ans.push_back(p); } int fa[N], sz[N], bsz[N]; vector<int> block[N]; void work(int rt) { vector<int> q; fa[rt] = 0; q.push_back(rt); for (int head = 0; head <= int(q.size()) - 1; ++head) { int x = q[head]; sz[x] = 1; bsz[x] = 0; for (auto y : lk[x]) if (!ban[y] && y != fa[x]) { fa[y] = x; q.push_back(y); } } int n = q.size(); if (n <= 4) return; int g = min(n, int(sqrt(n) * 1.5)); vector<int> big; for (int head = n - 1; head >= 1; --head) { int x = q[head]; if (sz[x] >= g || bsz[x] > 1) { big.push_back(x); sz[x] = 0; bsz[x] = 1; } sz[fa[x]] += sz[x]; bsz[fa[x]] += bsz[x]; } big.push_back(rt); int m = big.size(); for (int i = 0; i <= m - 1; ++i) { ban[big[i]] = 1; for (int j = i + 1; j <= m - 1; ++j) ans_push(big[i], big[j]); } for (int head = 1; head <= n - 1; ++head) { int x = q[head]; if (!ban[x]) { int y = fa[x]; while (!ban[y]) y = fa[y]; ans_push(x, y); block[y].push_back(x); } } for (int head = 1; head <= n - 1; ++head) { int x = q[head]; if (ban[x]) { int y = fa[x]; while (!ban[y]) y = fa[y]; for (auto z : block[y]) ans_push(x, z); } } for (auto x : q) if (!ban[x]) work(x); } int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n - 1; ++i) { int x = read(), y = read(); lk[x].push_back(y); lk[y].push_back(x); if (x > y) swap(x, y); ans_set.insert({x, y}); } work(1); printf("%d\n", int(ans.size())); for (auto pr : ans) printf("%d %d\n", pr.first, pr.second); }
### Prompt Generate a Cpp solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void chmin(T &x, const T &y) { if (x > y) x = y; } template <typename T> void chmax(T &x, const T &y) { if (x < y) x = y; } int read() { char c; while ((c = getchar()) < '-') ; if (c == '-') { int x = (c = getchar()) - '0'; while ((c = getchar()) >= '0') x = x * 10 + c - '0'; return -x; } int x = c - '0'; while ((c = getchar()) >= '0') x = x * 10 + c - '0'; return x; } const int N = 10000 + 5; vector<int> lk[N]; bool ban[N]; vector<pair<int, int> > ans; set<pair<int, int> > ans_set; void ans_push(int x, int y) { if (x > y) swap(x, y); pair<int, int> p = {x, y}; if (ans_set.count(p)) return; ans_set.insert(p); ans.push_back(p); } int fa[N], sz[N], bsz[N]; vector<int> block[N]; void work(int rt) { vector<int> q; fa[rt] = 0; q.push_back(rt); for (int head = 0; head <= int(q.size()) - 1; ++head) { int x = q[head]; sz[x] = 1; bsz[x] = 0; for (auto y : lk[x]) if (!ban[y] && y != fa[x]) { fa[y] = x; q.push_back(y); } } int n = q.size(); if (n <= 4) return; int g = min(n, int(sqrt(n) * 1.5)); vector<int> big; for (int head = n - 1; head >= 1; --head) { int x = q[head]; if (sz[x] >= g || bsz[x] > 1) { big.push_back(x); sz[x] = 0; bsz[x] = 1; } sz[fa[x]] += sz[x]; bsz[fa[x]] += bsz[x]; } big.push_back(rt); int m = big.size(); for (int i = 0; i <= m - 1; ++i) { ban[big[i]] = 1; for (int j = i + 1; j <= m - 1; ++j) ans_push(big[i], big[j]); } for (int head = 1; head <= n - 1; ++head) { int x = q[head]; if (!ban[x]) { int y = fa[x]; while (!ban[y]) y = fa[y]; ans_push(x, y); block[y].push_back(x); } } for (int head = 1; head <= n - 1; ++head) { int x = q[head]; if (ban[x]) { int y = fa[x]; while (!ban[y]) y = fa[y]; for (auto z : block[y]) ans_push(x, z); } } for (auto x : q) if (!ban[x]) work(x); } int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n - 1; ++i) { int x = read(), y = read(); lk[x].push_back(y); lk[y].push_back(x); if (x > y) swap(x, y); ans_set.insert({x, y}); } work(1); printf("%d\n", int(ans.size())); for (auto pr : ans) printf("%d %d\n", pr.first, pr.second); } ```
#include <bits/stdc++.h> using namespace std; const int c = 10002; int n, k, si[c]; vector<int> sz[c], akt, eler; bool v[c], jo[c]; vector<pair<int, int> > sol; void add(int a, int b) { if (a != b) { sol.push_back({a, b}); } } bool ures(int a) { return (!v[a] && !jo[a]); } void dfs2(int a) { eler.push_back(a); v[a] = true; for (int x : sz[a]) { if (ures(x)) { dfs2(x); } } } void dfs(int a, int gy) { v[a] = true; for (int x : sz[a]) { if (ures(x)) { dfs(x, gy); si[a] += si[x]; } } if (si[a] >= gy) { jo[a] = 1; akt.push_back(a); si[a] = 0; } } void solve(int a) { eler.clear(), akt.clear(); dfs2(a); int cnt = eler.size(), gy = sqrt(cnt); for (int i : eler) { v[i] = 0, si[i] = 1; } if (cnt <= 4) { for (int i : eler) { jo[i] = 1; } return; } dfs(a, gy); for (int i : eler) { v[i] = 0; } for (int i : akt) { for (int j : akt) { if (i > j) { add(i, j); } } } vector<int> eler2 = eler; eler.clear(); for (int i : akt) { dfs2(i); for (int j : eler) { add(i, j); v[j] = 0; } eler.clear(); } for (int i : eler2) { if (!jo[i]) { solve(i); } } } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; sz[a].push_back(b), sz[b].push_back(a); } solve(1); cout << sol.size() << "\n"; for (auto i : sol) { cout << i.first << " " << i.second << "\n"; } return 0; }
### Prompt Please formulate a CPP solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int c = 10002; int n, k, si[c]; vector<int> sz[c], akt, eler; bool v[c], jo[c]; vector<pair<int, int> > sol; void add(int a, int b) { if (a != b) { sol.push_back({a, b}); } } bool ures(int a) { return (!v[a] && !jo[a]); } void dfs2(int a) { eler.push_back(a); v[a] = true; for (int x : sz[a]) { if (ures(x)) { dfs2(x); } } } void dfs(int a, int gy) { v[a] = true; for (int x : sz[a]) { if (ures(x)) { dfs(x, gy); si[a] += si[x]; } } if (si[a] >= gy) { jo[a] = 1; akt.push_back(a); si[a] = 0; } } void solve(int a) { eler.clear(), akt.clear(); dfs2(a); int cnt = eler.size(), gy = sqrt(cnt); for (int i : eler) { v[i] = 0, si[i] = 1; } if (cnt <= 4) { for (int i : eler) { jo[i] = 1; } return; } dfs(a, gy); for (int i : eler) { v[i] = 0; } for (int i : akt) { for (int j : akt) { if (i > j) { add(i, j); } } } vector<int> eler2 = eler; eler.clear(); for (int i : akt) { dfs2(i); for (int j : eler) { add(i, j); v[j] = 0; } eler.clear(); } for (int i : eler2) { if (!jo[i]) { solve(i); } } } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; sz[a].push_back(b), sz[b].push_back(a); } solve(1); cout << sol.size() << "\n"; for (auto i : sol) { cout << i.first << " " << i.second << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int c = 10002; int n, k, si[c]; vector<int> sz[c], akt, eler; bool v[c], jo[c]; vector<pair<int, int> > sol; void add(int a, int b) { if (a != b) { sol.push_back({a, b}); } } bool ures(int a) { return (!v[a] && !jo[a]); } void dfs2(int a) { eler.push_back(a); v[a] = true; for (int x : sz[a]) { if (ures(x)) { dfs2(x); } } } void dfs(int a, int gy) { v[a] = true; for (int x : sz[a]) { if (ures(x)) { dfs(x, gy); si[a] += si[x]; } } if (si[a] >= gy) { jo[a] = 1; akt.push_back(a); si[a] = 0; } } void solve(int a) { eler.clear(), akt.clear(); dfs2(a); int cnt = eler.size(), gy = sqrt(cnt); for (int i : eler) { v[i] = 0, si[i] = 1; } if (cnt <= 4) { for (int i : eler) { jo[i] = 1; } return; } dfs(a, gy); for (int i : eler) { v[i] = 0; } for (int i : akt) { for (int j : akt) { if (i > j) { add(i, j); } } } vector<int> eler2 = eler; eler.clear(); for (int i : akt) { dfs2(i); for (int j : eler) { add(i, j); v[j] = 0; } eler.clear(); } for (int i : eler2) { if (!jo[i]) { solve(i); } } } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; sz[a].push_back(b), sz[b].push_back(a); } solve(1); cout << sol.size() << "\n"; for (auto i : sol) { cout << i.first << " " << i.second << "\n"; } return 0; }
### Prompt Generate a CPP solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int c = 10002; int n, k, si[c]; vector<int> sz[c], akt, eler; bool v[c], jo[c]; vector<pair<int, int> > sol; void add(int a, int b) { if (a != b) { sol.push_back({a, b}); } } bool ures(int a) { return (!v[a] && !jo[a]); } void dfs2(int a) { eler.push_back(a); v[a] = true; for (int x : sz[a]) { if (ures(x)) { dfs2(x); } } } void dfs(int a, int gy) { v[a] = true; for (int x : sz[a]) { if (ures(x)) { dfs(x, gy); si[a] += si[x]; } } if (si[a] >= gy) { jo[a] = 1; akt.push_back(a); si[a] = 0; } } void solve(int a) { eler.clear(), akt.clear(); dfs2(a); int cnt = eler.size(), gy = sqrt(cnt); for (int i : eler) { v[i] = 0, si[i] = 1; } if (cnt <= 4) { for (int i : eler) { jo[i] = 1; } return; } dfs(a, gy); for (int i : eler) { v[i] = 0; } for (int i : akt) { for (int j : akt) { if (i > j) { add(i, j); } } } vector<int> eler2 = eler; eler.clear(); for (int i : akt) { dfs2(i); for (int j : eler) { add(i, j); v[j] = 0; } eler.clear(); } for (int i : eler2) { if (!jo[i]) { solve(i); } } } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; sz[a].push_back(b), sz[b].push_back(a); } solve(1); cout << sol.size() << "\n"; for (auto i : sol) { cout << i.first << " " << i.second << "\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } struct Node { vector<int> nb; bool is_cut = false; int parent = -1; int cut; int Size; }; int n, a, b, k; map<pair<int, int>, bool> e; Node vec[100005]; vector<int> treesToCut; vector<pair<int, int> > E; int visited[100005] = {0}; int cut(int root, int level, int t_Size, bool sel) { visited[root] = level; int Tot = 0; for (int nb : vec[root].nb) if (!vec[nb].is_cut && visited[nb] < level) { vec[nb].parent = root; visited[nb] = level; Tot += cut(nb, level, t_Size, false); } Tot += 1; if (Tot >= sqrt(t_Size) && t_Size > k) { vec[root].is_cut = true; vec[root].cut = level; for (int nb : vec[root].nb) if (!vec[nb].is_cut && vec[root].parent != nb) cut(nb, level + 1, vec[nb].Size, true); return 0; } else { vec[root].Size = Tot; if (sel && t_Size > k) cut(root, level + 1, vec[root].Size, true); return Tot; } } vector<int> NCN; vector<int> CN; vector<int> tmp; void dfs(int node, int level) { visited[node] = level; NCN.push_back(node); if (vec[node].is_cut && vec[node].cut == level + 1) tmp.push_back(node); for (int nb : vec[node].nb) { if (vec[nb].is_cut) { if (vec[nb].cut < level) continue; if (vec[nb].cut == level) { CN.push_back(nb); continue; } } if (visited[nb] > level) { visited[nb] = level; dfs(nb, level); } } } void connect() { for (int cut : CN) for (int notCut : NCN) if (!e[make_pair(cut, notCut)]) E.push_back(make_pair(cut, notCut)); for (int node1 : tmp) for (int node2 : tmp) { if (node1 == node2) continue; if (!e[make_pair(node1, node2)]) E.push_back(make_pair(node1, node2)); } } int main() { cin >> n >> k; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; vec[a - 1].nb.push_back(b - 1); vec[b - 1].nb.push_back(a - 1); e[make_pair(a - 1, b - 1)] = true; e[make_pair(b - 1, a - 1)] = true; } vec[0].parent = -1; vec[0].Size = n; cut(0, 1, n, true); for (int i = 0; i < n; ++i) visited[i] = n + 1; for (int level = 20; level >= 0; --level) { NCN.clear(); CN.clear(); tmp.clear(); for (int j = 0; j < n; ++j) if ((!vec[j].is_cut || (vec[j].cut > level)) && visited[j] > level) { dfs(j, level); connect(); NCN.clear(); CN.clear(); tmp.clear(); } } cout << E.size() << "\n"; for (auto edge : E) cout << edge.first + 1 << " " << edge.second + 1 << "\n"; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than k, while adding as few shortcuts as possible. Your solution should add no more than 10 β‹… n shortcuts. Input The first line in the standard input contains an integer n (1 ≀ n ≀ 10^4), representing the number of the cities in DuΕ‘an's railway map, and an integer k (3 ≀ k ≀ n) representing the shortcutting diameter that he wants to achieve. Each of the following n - 1 lines will contain two integers u_i and v_i (1 ≀ u_i, v_i ≀ n, u_i β‰  v_i), meaning that there is a railway between cities u_i and v_i. Output The first line of the output should contain a number t representing the number of the shortcuts that were added. Each of the following t lines should contain two integers u_i and v_i, signifying that a shortcut is added between cities u_i and v_i. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } struct Node { vector<int> nb; bool is_cut = false; int parent = -1; int cut; int Size; }; int n, a, b, k; map<pair<int, int>, bool> e; Node vec[100005]; vector<int> treesToCut; vector<pair<int, int> > E; int visited[100005] = {0}; int cut(int root, int level, int t_Size, bool sel) { visited[root] = level; int Tot = 0; for (int nb : vec[root].nb) if (!vec[nb].is_cut && visited[nb] < level) { vec[nb].parent = root; visited[nb] = level; Tot += cut(nb, level, t_Size, false); } Tot += 1; if (Tot >= sqrt(t_Size) && t_Size > k) { vec[root].is_cut = true; vec[root].cut = level; for (int nb : vec[root].nb) if (!vec[nb].is_cut && vec[root].parent != nb) cut(nb, level + 1, vec[nb].Size, true); return 0; } else { vec[root].Size = Tot; if (sel && t_Size > k) cut(root, level + 1, vec[root].Size, true); return Tot; } } vector<int> NCN; vector<int> CN; vector<int> tmp; void dfs(int node, int level) { visited[node] = level; NCN.push_back(node); if (vec[node].is_cut && vec[node].cut == level + 1) tmp.push_back(node); for (int nb : vec[node].nb) { if (vec[nb].is_cut) { if (vec[nb].cut < level) continue; if (vec[nb].cut == level) { CN.push_back(nb); continue; } } if (visited[nb] > level) { visited[nb] = level; dfs(nb, level); } } } void connect() { for (int cut : CN) for (int notCut : NCN) if (!e[make_pair(cut, notCut)]) E.push_back(make_pair(cut, notCut)); for (int node1 : tmp) for (int node2 : tmp) { if (node1 == node2) continue; if (!e[make_pair(node1, node2)]) E.push_back(make_pair(node1, node2)); } } int main() { cin >> n >> k; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; vec[a - 1].nb.push_back(b - 1); vec[b - 1].nb.push_back(a - 1); e[make_pair(a - 1, b - 1)] = true; e[make_pair(b - 1, a - 1)] = true; } vec[0].parent = -1; vec[0].Size = n; cut(0, 1, n, true); for (int i = 0; i < n; ++i) visited[i] = n + 1; for (int level = 20; level >= 0; --level) { NCN.clear(); CN.clear(); tmp.clear(); for (int j = 0; j < n; ++j) if ((!vec[j].is_cut || (vec[j].cut > level)) && visited[j] > level) { dfs(j, level); connect(); NCN.clear(); CN.clear(); tmp.clear(); } } cout << E.size() << "\n"; for (auto edge : E) cout << edge.first + 1 << " " << edge.second + 1 << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1005; const int MOD = 998244353; inline int read() { int 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; } int T, n, m; int a[2005], b[2005]; int n1, n2, m1, m2; int h1[2005], h2[2005], l1[2005], l2[2005], F, BL = 0, sum; bitset<1000001> dp[1000]; void init() { BL = 0; n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); m = read(); for (int i = 1; i <= m; ++i) b[i] = read(); for (int i = 0; i <= n; ++i) dp[i].reset(); dp[0][0] = 1; sum = 0; for (int i = 1; i <= n; ++i) { sum += a[i]; dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); } if (sum % 2) { printf("No\n"); BL = 1; return; } F = sum / 2; if (!(dp[n][F])) { puts("No"); BL = 1; return; } n1 = 0, n2 = 0; for (int i = n; i >= 1; --i) { if (dp[i - 1][F]) { h2[++n2] = a[i]; } else if (dp[i - 1][F - a[i]]) { h1[++n1] = a[i]; F -= a[i]; } } for (int i = 0; i <= m; ++i) dp[i].reset(); dp[0][0] = 1; sum = 0; for (int i = 1; i <= m; ++i) { sum += b[i]; dp[i] = dp[i - 1] | (dp[i - 1] << b[i]); } if (sum % 2) { puts("No"); BL = 1; return; } F = sum / 2; if (!(dp[m][F])) { printf("No\n"); BL = 1; return; } m1 = 0, m2 = 0; for (int i = m; i >= 1; --i) { if (dp[i - 1][F]) { l2[++m2] = b[i]; } else if (dp[i - 1][F - b[i]]) { F -= b[i]; l1[++m1] = b[i]; } } if (n != m) { puts("No"); BL = 1; return; } } bool cmp(int x, int y) { return x > y; } void solve() { puts("Yes"); int ch = 0; int maxn = max(n1, n2); int maxm = max(m1, m2); if (maxm < maxn) swap(l1, h1), swap(l2, h2), swap(n1, m1), swap(n2, m2), ch = 1; if (m1 > m2) swap(l1, l2), swap(m1, m2); if (n1 < n2) swap(n1, n2), swap(h1, h2); sort(l1 + 1, l1 + m1 + 1, cmp); sort(l2 + 1, l2 + m2 + 1, cmp); sort(h1 + 1, h1 + n1 + 1); sort(h2 + 1, h2 + n2 + 1); int nowx = 0, nowy = 0, flag1 = 0, flag2 = 0; int i = 1, j = 1; int rnd = 0; while (1) { if (rnd & 1) { if (flag2 == 0) nowy += h1[j], j++; else nowy -= h2[j], j++; if (j > n1 && !flag2) j = 1, flag2 = 1; } else { if (flag1 == 0) nowx += l1[i], i++; else nowx -= l2[i], i++; if (i > m1 && !flag1) i = 1, flag1 = 1; } rnd++; if (!ch) printf("%d %d\n", nowy, nowx); else printf("%d %d\n", nowx, nowy); if (nowx == 0 && nowy == 0) return; } } signed main() { scanf("%d", &T); while (T--) { init(); if (!BL) solve(); } return 0; }
### Prompt Create a solution in Cpp for the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1005; const int MOD = 998244353; inline int read() { int 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; } int T, n, m; int a[2005], b[2005]; int n1, n2, m1, m2; int h1[2005], h2[2005], l1[2005], l2[2005], F, BL = 0, sum; bitset<1000001> dp[1000]; void init() { BL = 0; n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); m = read(); for (int i = 1; i <= m; ++i) b[i] = read(); for (int i = 0; i <= n; ++i) dp[i].reset(); dp[0][0] = 1; sum = 0; for (int i = 1; i <= n; ++i) { sum += a[i]; dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); } if (sum % 2) { printf("No\n"); BL = 1; return; } F = sum / 2; if (!(dp[n][F])) { puts("No"); BL = 1; return; } n1 = 0, n2 = 0; for (int i = n; i >= 1; --i) { if (dp[i - 1][F]) { h2[++n2] = a[i]; } else if (dp[i - 1][F - a[i]]) { h1[++n1] = a[i]; F -= a[i]; } } for (int i = 0; i <= m; ++i) dp[i].reset(); dp[0][0] = 1; sum = 0; for (int i = 1; i <= m; ++i) { sum += b[i]; dp[i] = dp[i - 1] | (dp[i - 1] << b[i]); } if (sum % 2) { puts("No"); BL = 1; return; } F = sum / 2; if (!(dp[m][F])) { printf("No\n"); BL = 1; return; } m1 = 0, m2 = 0; for (int i = m; i >= 1; --i) { if (dp[i - 1][F]) { l2[++m2] = b[i]; } else if (dp[i - 1][F - b[i]]) { F -= b[i]; l1[++m1] = b[i]; } } if (n != m) { puts("No"); BL = 1; return; } } bool cmp(int x, int y) { return x > y; } void solve() { puts("Yes"); int ch = 0; int maxn = max(n1, n2); int maxm = max(m1, m2); if (maxm < maxn) swap(l1, h1), swap(l2, h2), swap(n1, m1), swap(n2, m2), ch = 1; if (m1 > m2) swap(l1, l2), swap(m1, m2); if (n1 < n2) swap(n1, n2), swap(h1, h2); sort(l1 + 1, l1 + m1 + 1, cmp); sort(l2 + 1, l2 + m2 + 1, cmp); sort(h1 + 1, h1 + n1 + 1); sort(h2 + 1, h2 + n2 + 1); int nowx = 0, nowy = 0, flag1 = 0, flag2 = 0; int i = 1, j = 1; int rnd = 0; while (1) { if (rnd & 1) { if (flag2 == 0) nowy += h1[j], j++; else nowy -= h2[j], j++; if (j > n1 && !flag2) j = 1, flag2 = 1; } else { if (flag1 == 0) nowx += l1[i], i++; else nowx -= l2[i], i++; if (i > m1 && !flag1) i = 1, flag1 = 1; } rnd++; if (!ch) printf("%d %d\n", nowy, nowx); else printf("%d %d\n", nowx, nowy); if (nowx == 0 && nowy == 0) return; } } signed main() { scanf("%d", &T); while (T--) { init(); if (!BL) solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve(); signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) solve(); return 0; } int a[1010], b[1010]; bitset<1000010> dp1[1010]; bitset<1000010> dp2[1010]; vector<int> up, down; vector<int> lef, rig; vector<pair<int, int>> m1, m2, m3; int cr_pr(pair<int, int> a, pair<int, int> b) { return a.first * b.second - a.second * b.first; } bool cmp(pair<int, int> a, pair<int, int> b) { return cr_pr(a, b) > 0; } void solve() { up.clear(); down.clear(); lef.clear(); rig.clear(); m1.clear(); m2.clear(); m3.clear(); int h, w, gor = 0, ver = 0; cin >> h; for (int i = 0; i < h; ++i) { cin >> a[i]; gor += a[i]; dp1[i] = 0; } cin >> w; for (int i = 0; i < w; ++i) { cin >> b[i]; ver += b[i]; dp2[i] = 0; } if (h != w || ver % 2 != 0 || gor % 2 != 0) { cout << "No\n"; return; } dp1[0][0] = 1; for (int i = 0; i < h; ++i) { dp1[i + 1] = (dp1[i] | (dp1[i] << a[i])); } dp2[0][0] = 1; for (int i = 0; i < w; ++i) { dp2[i + 1] = (dp2[i] | (dp2[i] << b[i])); } if (!dp1[h][gor / 2] || !dp2[w][ver / 2]) { cout << "No\n"; return; } ver /= 2, gor /= 2; for (int i = h; i >= 1; --i) { if (gor >= a[i - 1] && dp1[i - 1][gor - a[i - 1]] && gor != 0) { rig.push_back(a[i - 1]); gor -= a[i - 1]; } else lef.push_back(a[i - 1]); } for (int i = w; i >= 1; --i) { if (ver >= b[i - 1] && dp2[i - 1][ver - b[i - 1]] && ver != 0) { up.push_back(b[i - 1]); ver -= b[i - 1]; } else down.push_back(b[i - 1]); } if ((int)rig.size() > (int)lef.size()) { rig.swap(lef); } if ((int)down.size() > (int)up.size()) { down.swap(up); } while ((int)rig.size() > 0) { m1.push_back({rig.back(), up.back()}); up.pop_back(); rig.pop_back(); } while ((int)up.size() > 0) { m2.push_back({-lef.back(), up.back()}); up.pop_back(); lef.pop_back(); } while ((int)lef.size() > 0) { m3.push_back({-lef.back(), -down.back()}); lef.pop_back(); down.pop_back(); } sort(m1.begin(), m1.end(), cmp); sort(m3.begin(), m3.end(), cmp); int x = 0, y = 0; cout << "Yes\n"; cout << "0 0\n"; for (int i = 0; i < (int)m3.size(); ++i) { x += m3[i].first; if (x != 0 || y != 0) cout << x << " " << y << '\n'; y += m3[i].second; if (x != 0 || y != 0) cout << x << " " << y << '\n'; } for (int i = 0; i < (int)m1.size(); ++i) { x += m1[i].first; if (x != 0 || y != 0) cout << x << " " << y << '\n'; y += m1[i].second; if (x != 0 || y != 0) cout << x << " " << y << '\n'; } for (int i = 0; i < (int)m2.size(); ++i) { x += m2[i].first; if (x != 0 || y != 0) cout << x << " " << y << '\n'; y += m2[i].second; if (x != 0 || y != 0) cout << x << " " << y << '\n'; } }
### Prompt Develop a solution in cpp to the problem described below: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve(); signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) solve(); return 0; } int a[1010], b[1010]; bitset<1000010> dp1[1010]; bitset<1000010> dp2[1010]; vector<int> up, down; vector<int> lef, rig; vector<pair<int, int>> m1, m2, m3; int cr_pr(pair<int, int> a, pair<int, int> b) { return a.first * b.second - a.second * b.first; } bool cmp(pair<int, int> a, pair<int, int> b) { return cr_pr(a, b) > 0; } void solve() { up.clear(); down.clear(); lef.clear(); rig.clear(); m1.clear(); m2.clear(); m3.clear(); int h, w, gor = 0, ver = 0; cin >> h; for (int i = 0; i < h; ++i) { cin >> a[i]; gor += a[i]; dp1[i] = 0; } cin >> w; for (int i = 0; i < w; ++i) { cin >> b[i]; ver += b[i]; dp2[i] = 0; } if (h != w || ver % 2 != 0 || gor % 2 != 0) { cout << "No\n"; return; } dp1[0][0] = 1; for (int i = 0; i < h; ++i) { dp1[i + 1] = (dp1[i] | (dp1[i] << a[i])); } dp2[0][0] = 1; for (int i = 0; i < w; ++i) { dp2[i + 1] = (dp2[i] | (dp2[i] << b[i])); } if (!dp1[h][gor / 2] || !dp2[w][ver / 2]) { cout << "No\n"; return; } ver /= 2, gor /= 2; for (int i = h; i >= 1; --i) { if (gor >= a[i - 1] && dp1[i - 1][gor - a[i - 1]] && gor != 0) { rig.push_back(a[i - 1]); gor -= a[i - 1]; } else lef.push_back(a[i - 1]); } for (int i = w; i >= 1; --i) { if (ver >= b[i - 1] && dp2[i - 1][ver - b[i - 1]] && ver != 0) { up.push_back(b[i - 1]); ver -= b[i - 1]; } else down.push_back(b[i - 1]); } if ((int)rig.size() > (int)lef.size()) { rig.swap(lef); } if ((int)down.size() > (int)up.size()) { down.swap(up); } while ((int)rig.size() > 0) { m1.push_back({rig.back(), up.back()}); up.pop_back(); rig.pop_back(); } while ((int)up.size() > 0) { m2.push_back({-lef.back(), up.back()}); up.pop_back(); lef.pop_back(); } while ((int)lef.size() > 0) { m3.push_back({-lef.back(), -down.back()}); lef.pop_back(); down.pop_back(); } sort(m1.begin(), m1.end(), cmp); sort(m3.begin(), m3.end(), cmp); int x = 0, y = 0; cout << "Yes\n"; cout << "0 0\n"; for (int i = 0; i < (int)m3.size(); ++i) { x += m3[i].first; if (x != 0 || y != 0) cout << x << " " << y << '\n'; y += m3[i].second; if (x != 0 || y != 0) cout << x << " " << y << '\n'; } for (int i = 0; i < (int)m1.size(); ++i) { x += m1[i].first; if (x != 0 || y != 0) cout << x << " " << y << '\n'; y += m1[i].second; if (x != 0 || y != 0) cout << x << " " << y << '\n'; } for (int i = 0; i < (int)m2.size(); ++i) { x += m2[i].first; if (x != 0 || y != 0) cout << x << " " << y << '\n'; y += m2[i].second; if (x != 0 || y != 0) cout << x << " " << y << '\n'; } } ```
#include <bits/stdc++.h> using namespace std; const int M = 1005; int read() { int x = 0, y = 1; char ch = getchar(); while (ch < '0' || ch > '9') y = (ch == '-') ? -1 : 1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * y; } bitset<M * M / 2> dp[M]; bool check(int n, int v[], int va[], int vb[], int K) { va[0] = vb[0] = 0; dp[0][0] = 1; for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | dp[i - 1] << v[i]; if (!dp[n][K]) return 0; for (int i = n; i >= 1; i--) { if (K >= v[i] && dp[i - 1][K - v[i]]) va[++va[0]] = v[i], K -= v[i]; else vb[++vb[0]] = v[i]; } sort(va + 1, va + va[0] + 1), sort(vb + 1, vb + vb[0] + 1); return 1; } int n, m, a[M], b[M], xa[M], xb[M], ya[M], yb[M], x[M], y[M]; void solve() { n = read(); a[0] = b[0] = x[0] = y[0] = 0; for (int i = 1; i <= n; i++) a[i] = read(), a[0] += a[i]; m = read(); for (int i = 1; i <= m; i++) b[i] = read(), b[0] += b[i]; if (n != m || a[0] & 1 || b[0] & 1 || !check(n, a, xa, xb, a[0] >> 1) || !check(m, b, ya, yb, b[0] >> 1)) return (void)(printf("No\n")); if (xa[0] > xb[0]) swap(xa, xb); if (ya[0] < yb[0]) swap(ya, yb); for (int i = xa[0]; i >= 1; i--) x[++x[0]] = xa[i]; for (int i = xb[0]; i >= 1; i--) x[++x[0]] = -xb[i]; for (int i = 1; i <= ya[0]; i++) y[++y[0]] = ya[i]; for (int i = 1; i <= ya[0]; i++) y[++y[0]] = -yb[i]; int nx = 0, ny = 0; printf("Yes\n"); for (int i = 1; i <= x[0]; i++) { nx += x[i]; printf("%d %d\n", nx, ny); ny += y[i]; printf("%d %d\n", nx, ny); } } signed main() { int T = read(); while (T--) solve(); }
### Prompt Generate a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 1005; int read() { int x = 0, y = 1; char ch = getchar(); while (ch < '0' || ch > '9') y = (ch == '-') ? -1 : 1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * y; } bitset<M * M / 2> dp[M]; bool check(int n, int v[], int va[], int vb[], int K) { va[0] = vb[0] = 0; dp[0][0] = 1; for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | dp[i - 1] << v[i]; if (!dp[n][K]) return 0; for (int i = n; i >= 1; i--) { if (K >= v[i] && dp[i - 1][K - v[i]]) va[++va[0]] = v[i], K -= v[i]; else vb[++vb[0]] = v[i]; } sort(va + 1, va + va[0] + 1), sort(vb + 1, vb + vb[0] + 1); return 1; } int n, m, a[M], b[M], xa[M], xb[M], ya[M], yb[M], x[M], y[M]; void solve() { n = read(); a[0] = b[0] = x[0] = y[0] = 0; for (int i = 1; i <= n; i++) a[i] = read(), a[0] += a[i]; m = read(); for (int i = 1; i <= m; i++) b[i] = read(), b[0] += b[i]; if (n != m || a[0] & 1 || b[0] & 1 || !check(n, a, xa, xb, a[0] >> 1) || !check(m, b, ya, yb, b[0] >> 1)) return (void)(printf("No\n")); if (xa[0] > xb[0]) swap(xa, xb); if (ya[0] < yb[0]) swap(ya, yb); for (int i = xa[0]; i >= 1; i--) x[++x[0]] = xa[i]; for (int i = xb[0]; i >= 1; i--) x[++x[0]] = -xb[i]; for (int i = 1; i <= ya[0]; i++) y[++y[0]] = ya[i]; for (int i = 1; i <= ya[0]; i++) y[++y[0]] = -yb[i]; int nx = 0, ny = 0; printf("Yes\n"); for (int i = 1; i <= x[0]; i++) { nx += x[i]; printf("%d %d\n", nx, ny); ny += y[i]; printf("%d %d\n", nx, ny); } } signed main() { int T = read(); while (T--) solve(); } ```
#include <bits/stdc++.h> using namespace std; const int c = 1002, k = 30002, f = k / 2; int w, n, m, dp[c][k], baj, si1, si2; vector<int> p, jobb, bal, fel, le, s1, s2; void ks() { random_shuffle(p.begin(), p.end()); int si = p.size(); for (int i = 0; i <= si; i++) { for (int j = 0; j < k; j++) dp[i][j] = 0; } dp[0][f] = 1; for (int i = 1; i <= si; i++) { int x = p[i - 1]; for (int j = 0; j < k; j++) { if (j >= x && dp[i - 1][j - x]) { dp[i][j] = 1; } if (k - 1 - x >= j && dp[i - 1][j + x]) { dp[i][j] = 1; } if (dp[i][j]) { } } } int g = f; for (int i = si - 1; i >= 0; i--) { int x = p[i]; if (x <= g && dp[i][g - x]) { s1.push_back(x); g -= x; } else { s2.push_back(x); g += x; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> w; while (w--) { p.clear(), jobb.clear(), bal.clear(), fel.clear(), le.clear(), s1.clear(), s2.clear(); baj = 0; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; p.push_back(x); } ks(); if (!dp[n][f]) baj = 1; si1 = s1.size(), si2 = s2.size(); if (si1 < si2) { for (int i = 0; i < si1; i++) jobb.push_back(s1[i]); for (int j = 0; j < si2; j++) bal.push_back(s2[j]); } else { for (int i = 0; i < si1; i++) bal.push_back(s1[i]); for (int j = 0; j < si2; j++) jobb.push_back(s2[j]); } p.clear(); s1.clear(), s2.clear(); cin >> m; for (int i = 0; i < m; i++) { int x; cin >> x; p.push_back(x); } ks(); if (!dp[m][f]) baj = 1; si1 = s1.size(), si2 = s2.size(); if (si1 < si2) { for (int i = 0; i < si1; i++) le.push_back(s1[i]); for (int j = 0; j < si2; j++) fel.push_back(s2[j]); } else { for (int i = 0; i < si1; i++) fel.push_back(s1[i]); for (int j = 0; j < si2; j++) le.push_back(s2[j]); } s1.clear(), s2.clear(); if (n != m) baj = 1; if (baj) { cout << "No\n"; } else { cout << "Yes\n"; sort(bal.rbegin(), bal.rend()), sort(jobb.rbegin(), jobb.rend()); sort(fel.begin(), fel.end()), sort(le.begin(), le.end()); int x = 0, y = 0; int fs = fel.size(), js = jobb.size(); for (int i = 0; i < n; i++) { if (i < js) x += jobb[i]; else x -= bal[i - js]; cout << x << " " << y << "\n"; if (i < fs) y += fel[i]; else y -= le[i - fs]; cout << x << " " << y << "\n"; } } } return 0; }
### Prompt Please create a solution in cpp to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int c = 1002, k = 30002, f = k / 2; int w, n, m, dp[c][k], baj, si1, si2; vector<int> p, jobb, bal, fel, le, s1, s2; void ks() { random_shuffle(p.begin(), p.end()); int si = p.size(); for (int i = 0; i <= si; i++) { for (int j = 0; j < k; j++) dp[i][j] = 0; } dp[0][f] = 1; for (int i = 1; i <= si; i++) { int x = p[i - 1]; for (int j = 0; j < k; j++) { if (j >= x && dp[i - 1][j - x]) { dp[i][j] = 1; } if (k - 1 - x >= j && dp[i - 1][j + x]) { dp[i][j] = 1; } if (dp[i][j]) { } } } int g = f; for (int i = si - 1; i >= 0; i--) { int x = p[i]; if (x <= g && dp[i][g - x]) { s1.push_back(x); g -= x; } else { s2.push_back(x); g += x; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> w; while (w--) { p.clear(), jobb.clear(), bal.clear(), fel.clear(), le.clear(), s1.clear(), s2.clear(); baj = 0; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; p.push_back(x); } ks(); if (!dp[n][f]) baj = 1; si1 = s1.size(), si2 = s2.size(); if (si1 < si2) { for (int i = 0; i < si1; i++) jobb.push_back(s1[i]); for (int j = 0; j < si2; j++) bal.push_back(s2[j]); } else { for (int i = 0; i < si1; i++) bal.push_back(s1[i]); for (int j = 0; j < si2; j++) jobb.push_back(s2[j]); } p.clear(); s1.clear(), s2.clear(); cin >> m; for (int i = 0; i < m; i++) { int x; cin >> x; p.push_back(x); } ks(); if (!dp[m][f]) baj = 1; si1 = s1.size(), si2 = s2.size(); if (si1 < si2) { for (int i = 0; i < si1; i++) le.push_back(s1[i]); for (int j = 0; j < si2; j++) fel.push_back(s2[j]); } else { for (int i = 0; i < si1; i++) fel.push_back(s1[i]); for (int j = 0; j < si2; j++) le.push_back(s2[j]); } s1.clear(), s2.clear(); if (n != m) baj = 1; if (baj) { cout << "No\n"; } else { cout << "Yes\n"; sort(bal.rbegin(), bal.rend()), sort(jobb.rbegin(), jobb.rend()); sort(fel.begin(), fel.end()), sort(le.begin(), le.end()); int x = 0, y = 0; int fs = fel.size(), js = jobb.size(); for (int i = 0; i < n; i++) { if (i < js) x += jobb[i]; else x -= bal[i - js]; cout << x << " " << y << "\n"; if (i < fs) y += fel[i]; else y -= le[i - fs]; cout << x << " " << y << "\n"; } } } return 0; } ```
#include <bits/stdc++.h> using std::max; using std::min; const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x3f3f3f3f3f3f3f3f; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> inline _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> inline void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> inline void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> inline void read(_Tp &x) { char ch(getchar()); bool f(false); while (!isdigit(ch)) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); f && (x = -x); } template <typename _Tp, typename... Args> inline void read(_Tp &t, Args &...args) { read(t); read(args...); } inline int read_str(char *s) { char ch(getchar()); while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar(); char *tar = s; *tar = ch, ch = getchar(); while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 1005; int a[N], b[N]; const int D = 50000; std::bitset<100005> dp[N]; bool solve(int *a, int n, std::vector<int> &v1, std::vector<int> &v2) { std::shuffle(a + 1, a + n + 1, rnd); for (int i = 1; i <= n; ++i) dp[i] = (dp[i - 1] << a[i]) | (dp[i - 1] >> a[i]); if (!dp[n].test(D)) return false; int cur = D; for (int i = n; i >= 1; --i) { if (cur + a[i] <= D + D && dp[i - 1].test(cur + a[i])) cur += a[i], v1.push_back(a[i]); else cur -= a[i], v2.push_back(a[i]); } return true; } void MAIN() { int n, m; read(n); for (int i = 1; i <= n; ++i) read(a[i]); read(m); for (int i = 1; i <= m; ++i) read(b[i]); if (abs(n - m) > 1) return (void)puts("No"); std::vector<int> v1, v2, v3, v4; bool flag = solve(a, n, v1, v2) & solve(b, m, v3, v4); if (!flag) return (void)puts("No"); puts("Yes"); if (((int)v1.size()) > ((int)v2.size())) std::swap(v1, v2); if (((int)v3.size()) < ((int)v4.size())) std::swap(v3, v4); std::sort(v1.begin(), v1.end(), std::greater<int>()); std::sort(v2.begin(), v2.end(), std::greater<int>()); std::sort(v3.begin(), v3.end()); std::sort(v4.begin(), v4.end()); std::vector<int> A(v1); for (auto it : v2) A.push_back(-it); std::vector<int> B(v3); for (auto it : v4) B.push_back(-it); if (((int)A.size()) >= ((int)B.size())) { int x = 0, y = 0; for (int i = 0; i < ((int)A.size()); ++i) { printf("%d %d\n", x += A[i], y); if (i < ((int)B.size())) printf("%d %d\n", x, y += B[i]); } } else { int x = 0, y = 0; for (int i = 0; i < ((int)B.size()); ++i) { printf("%d %d\n", x, y += B[i]); if (i < ((int)A.size())) printf("%d %d\n", x += A[i], y); } } } int main() { dp[0].set(D); int _; read(_); while (_--) MAIN(); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using std::max; using std::min; const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x3f3f3f3f3f3f3f3f; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> inline _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> inline void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> inline void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> inline void read(_Tp &x) { char ch(getchar()); bool f(false); while (!isdigit(ch)) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); f && (x = -x); } template <typename _Tp, typename... Args> inline void read(_Tp &t, Args &...args) { read(t); read(args...); } inline int read_str(char *s) { char ch(getchar()); while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar(); char *tar = s; *tar = ch, ch = getchar(); while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 1005; int a[N], b[N]; const int D = 50000; std::bitset<100005> dp[N]; bool solve(int *a, int n, std::vector<int> &v1, std::vector<int> &v2) { std::shuffle(a + 1, a + n + 1, rnd); for (int i = 1; i <= n; ++i) dp[i] = (dp[i - 1] << a[i]) | (dp[i - 1] >> a[i]); if (!dp[n].test(D)) return false; int cur = D; for (int i = n; i >= 1; --i) { if (cur + a[i] <= D + D && dp[i - 1].test(cur + a[i])) cur += a[i], v1.push_back(a[i]); else cur -= a[i], v2.push_back(a[i]); } return true; } void MAIN() { int n, m; read(n); for (int i = 1; i <= n; ++i) read(a[i]); read(m); for (int i = 1; i <= m; ++i) read(b[i]); if (abs(n - m) > 1) return (void)puts("No"); std::vector<int> v1, v2, v3, v4; bool flag = solve(a, n, v1, v2) & solve(b, m, v3, v4); if (!flag) return (void)puts("No"); puts("Yes"); if (((int)v1.size()) > ((int)v2.size())) std::swap(v1, v2); if (((int)v3.size()) < ((int)v4.size())) std::swap(v3, v4); std::sort(v1.begin(), v1.end(), std::greater<int>()); std::sort(v2.begin(), v2.end(), std::greater<int>()); std::sort(v3.begin(), v3.end()); std::sort(v4.begin(), v4.end()); std::vector<int> A(v1); for (auto it : v2) A.push_back(-it); std::vector<int> B(v3); for (auto it : v4) B.push_back(-it); if (((int)A.size()) >= ((int)B.size())) { int x = 0, y = 0; for (int i = 0; i < ((int)A.size()); ++i) { printf("%d %d\n", x += A[i], y); if (i < ((int)B.size())) printf("%d %d\n", x, y += B[i]); } } else { int x = 0, y = 0; for (int i = 0; i < ((int)B.size()); ++i) { printf("%d %d\n", x, y += B[i]); if (i < ((int)A.size())) printf("%d %d\n", x += A[i], y); } } } int main() { dp[0].set(D); int _; read(_); while (_--) MAIN(); return 0; } ```
#include <bits/stdc++.h> int read() { static int c, x; while ((c = getchar()) < 48) { } x = c & 15; while ((c = getchar()) >= 48) x = x * 10 + (c & 15); return x; } int a[1010], b[1010]; int px[1010 << 1], py[1010 << 1]; std::vector<int> a1, a2, b1, b2; std::bitset<1010 * 1010> dp[1010]; int main() { dp->set(0); for (int T = read(); T--;) { int sa = 0, sb = 0; a1.clear(); a2.clear(); b1.clear(); b2.clear(); const int n = read(); for (int i = 1; i <= n; ++i) { a[i] = read(); sa += a[i]; } const int m = read(); for (int i = 1; i <= m; ++i) { b[i] = read(); sb += b[i]; } if (n != m || ((sa | sb) & 1)) { puts("No"); continue; } sa >>= 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] << a[i] | dp[i - 1]; } if (!dp[n].test(sa)) { puts("No"); continue; } for (int i = n; i; --i) { if (dp[i - 1].test(sa)) { a1.push_back(a[i]); } else { a2.push_back(a[i]); sa -= a[i]; } } sb >>= 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] << b[i] | dp[i - 1]; } if (!dp[n].test(sb)) { puts("No"); continue; } for (int i = n; i; --i) { if (dp[i - 1].test(sb)) { b1.push_back(b[i]); } else { b2.push_back(b[i]); sb -= b[i]; } } bool rev = false; if (a1.size() > b1.size()) { rev = true; a1.swap(b1); a2.swap(b2); } std::sort(a1.begin(), a1.end()); std::sort(a2.begin(), a2.end()); std::sort(b1.begin(), b1.end(), std::greater<int>()); std::sort(b2.begin(), b2.end(), std::greater<int>()); for (int i1 = a1.size(), i2 = a2.size(), j1 = b1.size(), j2 = b2.size(), x = 0, y = 0, t = n, c = 0; t--;) { if (i1) { x += a1[--i1]; } else { x -= a2[--i2]; } ++c; px[c] = x; py[c] = y; if (j1) { y += b1[--j1]; } else { y -= b2[--j2]; } ++c; px[c] = x; py[c] = y; } puts("Yes"); for (int i = 1; i <= (n << 1); ++i) { rev ? printf("%d %d\n", py[i], px[i]) : printf("%d %d\n", px[i], py[i]); } } return 0; }
### Prompt Please create a solution in Cpp to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> int read() { static int c, x; while ((c = getchar()) < 48) { } x = c & 15; while ((c = getchar()) >= 48) x = x * 10 + (c & 15); return x; } int a[1010], b[1010]; int px[1010 << 1], py[1010 << 1]; std::vector<int> a1, a2, b1, b2; std::bitset<1010 * 1010> dp[1010]; int main() { dp->set(0); for (int T = read(); T--;) { int sa = 0, sb = 0; a1.clear(); a2.clear(); b1.clear(); b2.clear(); const int n = read(); for (int i = 1; i <= n; ++i) { a[i] = read(); sa += a[i]; } const int m = read(); for (int i = 1; i <= m; ++i) { b[i] = read(); sb += b[i]; } if (n != m || ((sa | sb) & 1)) { puts("No"); continue; } sa >>= 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] << a[i] | dp[i - 1]; } if (!dp[n].test(sa)) { puts("No"); continue; } for (int i = n; i; --i) { if (dp[i - 1].test(sa)) { a1.push_back(a[i]); } else { a2.push_back(a[i]); sa -= a[i]; } } sb >>= 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] << b[i] | dp[i - 1]; } if (!dp[n].test(sb)) { puts("No"); continue; } for (int i = n; i; --i) { if (dp[i - 1].test(sb)) { b1.push_back(b[i]); } else { b2.push_back(b[i]); sb -= b[i]; } } bool rev = false; if (a1.size() > b1.size()) { rev = true; a1.swap(b1); a2.swap(b2); } std::sort(a1.begin(), a1.end()); std::sort(a2.begin(), a2.end()); std::sort(b1.begin(), b1.end(), std::greater<int>()); std::sort(b2.begin(), b2.end(), std::greater<int>()); for (int i1 = a1.size(), i2 = a2.size(), j1 = b1.size(), j2 = b2.size(), x = 0, y = 0, t = n, c = 0; t--;) { if (i1) { x += a1[--i1]; } else { x -= a2[--i2]; } ++c; px[c] = x; py[c] = y; if (j1) { y += b1[--j1]; } else { y -= b2[--j2]; } ++c; px[c] = x; py[c] = y; } puts("Yes"); for (int i = 1; i <= (n << 1); ++i) { rev ? printf("%d %d\n", py[i], px[i]) : printf("%d %d\n", px[i], py[i]); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int c = 1002, k = 10002, f = k / 2; int w, n, m, dp[c][k], baj, si1, si2; vector<int> p, jobb, bal, fel, le, s1, s2; void ks() { random_shuffle(p.begin(), p.end()); int si = p.size(); for (int i = 0; i <= si; i++) { for (int j = 0; j < k; j++) dp[i][j] = 0; } dp[0][f] = 1; for (int i = 1; i <= si; i++) { int x = p[i - 1]; for (int j = 0; j < k; j++) { if (j >= x && dp[i - 1][j - x]) { dp[i][j] = 1; } if (k - 1 - x >= j && dp[i - 1][j + x]) { dp[i][j] = 1; } } } int g = f; for (int i = si - 1; i >= 0; i--) { int x = p[i]; if (x <= g && dp[i][g - x]) { s1.push_back(x); g -= x; } else { s2.push_back(x); g += x; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> w; while (w--) { p.clear(), jobb.clear(), bal.clear(), fel.clear(), le.clear(), s1.clear(), s2.clear(); baj = 0; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; p.push_back(x); } ks(); if (!dp[n][f]) baj = 1; si1 = s1.size(), si2 = s2.size(); if (si1 < si2) { for (int i = 0; i < si1; i++) jobb.push_back(s1[i]); for (int j = 0; j < si2; j++) bal.push_back(s2[j]); } else { for (int i = 0; i < si1; i++) bal.push_back(s1[i]); for (int j = 0; j < si2; j++) jobb.push_back(s2[j]); } p.clear(); s1.clear(), s2.clear(); cin >> m; for (int i = 0; i < m; i++) { int x; cin >> x; p.push_back(x); } ks(); if (!dp[m][f]) baj = 1; si1 = s1.size(), si2 = s2.size(); if (si1 < si2) { for (int i = 0; i < si1; i++) le.push_back(s1[i]); for (int j = 0; j < si2; j++) fel.push_back(s2[j]); } else { for (int i = 0; i < si1; i++) fel.push_back(s1[i]); for (int j = 0; j < si2; j++) le.push_back(s2[j]); } s1.clear(), s2.clear(); if (n != m) baj = 1; if (baj) { cout << "No\n"; } else { cout << "Yes\n"; sort(bal.rbegin(), bal.rend()), sort(jobb.rbegin(), jobb.rend()); sort(fel.begin(), fel.end()), sort(le.begin(), le.end()); int x = 0, y = 0; int fs = fel.size(), js = jobb.size(); for (int i = 0; i < n; i++) { if (i < js) x += jobb[i]; else x -= bal[i - js]; cout << x << " " << y << "\n"; if (i < fs) y += fel[i]; else y -= le[i - fs]; cout << x << " " << y << "\n"; } } } return 0; }
### Prompt Please formulate a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int c = 1002, k = 10002, f = k / 2; int w, n, m, dp[c][k], baj, si1, si2; vector<int> p, jobb, bal, fel, le, s1, s2; void ks() { random_shuffle(p.begin(), p.end()); int si = p.size(); for (int i = 0; i <= si; i++) { for (int j = 0; j < k; j++) dp[i][j] = 0; } dp[0][f] = 1; for (int i = 1; i <= si; i++) { int x = p[i - 1]; for (int j = 0; j < k; j++) { if (j >= x && dp[i - 1][j - x]) { dp[i][j] = 1; } if (k - 1 - x >= j && dp[i - 1][j + x]) { dp[i][j] = 1; } } } int g = f; for (int i = si - 1; i >= 0; i--) { int x = p[i]; if (x <= g && dp[i][g - x]) { s1.push_back(x); g -= x; } else { s2.push_back(x); g += x; } } } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> w; while (w--) { p.clear(), jobb.clear(), bal.clear(), fel.clear(), le.clear(), s1.clear(), s2.clear(); baj = 0; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; p.push_back(x); } ks(); if (!dp[n][f]) baj = 1; si1 = s1.size(), si2 = s2.size(); if (si1 < si2) { for (int i = 0; i < si1; i++) jobb.push_back(s1[i]); for (int j = 0; j < si2; j++) bal.push_back(s2[j]); } else { for (int i = 0; i < si1; i++) bal.push_back(s1[i]); for (int j = 0; j < si2; j++) jobb.push_back(s2[j]); } p.clear(); s1.clear(), s2.clear(); cin >> m; for (int i = 0; i < m; i++) { int x; cin >> x; p.push_back(x); } ks(); if (!dp[m][f]) baj = 1; si1 = s1.size(), si2 = s2.size(); if (si1 < si2) { for (int i = 0; i < si1; i++) le.push_back(s1[i]); for (int j = 0; j < si2; j++) fel.push_back(s2[j]); } else { for (int i = 0; i < si1; i++) fel.push_back(s1[i]); for (int j = 0; j < si2; j++) le.push_back(s2[j]); } s1.clear(), s2.clear(); if (n != m) baj = 1; if (baj) { cout << "No\n"; } else { cout << "Yes\n"; sort(bal.rbegin(), bal.rend()), sort(jobb.rbegin(), jobb.rend()); sort(fel.begin(), fel.end()), sort(le.begin(), le.end()); int x = 0, y = 0; int fs = fel.size(), js = jobb.size(); for (int i = 0; i < n; i++) { if (i < js) x += jobb[i]; else x -= bal[i - js]; cout << x << " " << y << "\n"; if (i < fs) y += fel[i]; else y -= le[i - fs]; cout << x << " " << y << "\n"; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N_MAX = 1e3, B = 5e5 + 1; int t, n, m, a[2][N_MAX], h[N_MAX], v[N_MAX]; bool p[2][N_MAX], ok[2]; bitset<B> b[N_MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[0][i]; cin >> m; for (int i = 0; i < m; i++) cin >> a[1][i]; if (n != m) { cout << "No\n"; continue; } for (int i = 0; i < 2; i++) { int s = 0; for (int j = 0; j < n; j++) s += a[i][j]; if (s % 2) { ok[i] = false; break; } s /= 2; b[0] = 0; b[0][0] = b[0][a[i][0]] = true; for (int j = 1; j < n; j++) { b[j] = b[j - 1]; b[j] <<= a[i][j]; b[j] |= b[j - 1]; } if (!b[n - 1][s]) { ok[i] = false; break; } ok[i] = true; for (int j = 0; j < n; j++) p[i][j] = 0; for (int j = n - 1; 0 < j; j--) { if (a[i][j] <= s && b[j - 1][s - a[i][j]]) { p[i][j] = 1; s -= a[i][j]; } } p[i][0] = (s > 0); } if (!ok[0] || !ok[1]) { cout << "No\n"; continue; } cout << "Yes\n"; int cnt[2] = {0, 0}, db = 0; for (int i = 0; i < n; i++) cnt[p[0][i]]++; bool f = (cnt[0] < cnt[1] ? 0 : 1); for (int i = 0; i < n; i++) if (p[0][i] == f) h[db++] = a[0][i]; for (int i = 0; i < n; i++) if (p[0][i] != f) h[db++] = -a[0][i]; sort(h, h + cnt[f], greater<int>()); sort(h + cnt[f], h + n); cnt[0] = cnt[1] = db = 0; for (int i = 0; i < n; i++) cnt[p[1][i]]++; f = (cnt[0] < cnt[1] ? 1 : 0); for (int i = 0; i < n; i++) if (p[1][i] == f) v[db++] = -a[1][i]; for (int i = 0; i < n; i++) if (p[1][i] != f) v[db++] = a[1][i]; sort(v, v + cnt[f], greater<int>()); sort(v + cnt[f], v + n); int x = 0, y = 0; for (int i = 0; i < n; i++) { cout << x << " " << y << "\n"; x += h[i]; cout << x << " " << y << "\n"; y += v[i]; } } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N_MAX = 1e3, B = 5e5 + 1; int t, n, m, a[2][N_MAX], h[N_MAX], v[N_MAX]; bool p[2][N_MAX], ok[2]; bitset<B> b[N_MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[0][i]; cin >> m; for (int i = 0; i < m; i++) cin >> a[1][i]; if (n != m) { cout << "No\n"; continue; } for (int i = 0; i < 2; i++) { int s = 0; for (int j = 0; j < n; j++) s += a[i][j]; if (s % 2) { ok[i] = false; break; } s /= 2; b[0] = 0; b[0][0] = b[0][a[i][0]] = true; for (int j = 1; j < n; j++) { b[j] = b[j - 1]; b[j] <<= a[i][j]; b[j] |= b[j - 1]; } if (!b[n - 1][s]) { ok[i] = false; break; } ok[i] = true; for (int j = 0; j < n; j++) p[i][j] = 0; for (int j = n - 1; 0 < j; j--) { if (a[i][j] <= s && b[j - 1][s - a[i][j]]) { p[i][j] = 1; s -= a[i][j]; } } p[i][0] = (s > 0); } if (!ok[0] || !ok[1]) { cout << "No\n"; continue; } cout << "Yes\n"; int cnt[2] = {0, 0}, db = 0; for (int i = 0; i < n; i++) cnt[p[0][i]]++; bool f = (cnt[0] < cnt[1] ? 0 : 1); for (int i = 0; i < n; i++) if (p[0][i] == f) h[db++] = a[0][i]; for (int i = 0; i < n; i++) if (p[0][i] != f) h[db++] = -a[0][i]; sort(h, h + cnt[f], greater<int>()); sort(h + cnt[f], h + n); cnt[0] = cnt[1] = db = 0; for (int i = 0; i < n; i++) cnt[p[1][i]]++; f = (cnt[0] < cnt[1] ? 1 : 0); for (int i = 0; i < n; i++) if (p[1][i] == f) v[db++] = -a[1][i]; for (int i = 0; i < n; i++) if (p[1][i] != f) v[db++] = a[1][i]; sort(v, v + cnt[f], greater<int>()); sort(v + cnt[f], v + n); int x = 0, y = 0; for (int i = 0; i < n; i++) { cout << x << " " << y << "\n"; x += h[i]; cout << x << " " << y << "\n"; y += v[i]; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int T, h, v; int l[1010], p[1010]; bitset<1010 * 1010> dpl[1010], dpp[1010]; int as[1010], af[1010], bs[1010], bf[1010]; int nowaf, nowas, nowbf, nowbs; bool dp() { dpl[0][0] = 1; dpp[0][0] = 1; int suml = 0, sump = 0; for (int i = 1; i <= h; i++) { dpl[i] = (dpl[i - 1] << l[i]) | dpl[i - 1]; suml += l[i]; dpp[i] = (dpp[i - 1] << p[i]) | dpp[i - 1]; sump += p[i]; } if ((suml & 1) || (sump & 1)) return 0; if ((!dpl[h][suml >> 1]) || (!dpp[v][sump >> 1])) return 0; suml >>= 1, sump >>= 1; for (int i = h; i >= 1; i--) { if (dpl[i - 1][suml]) af[++nowaf] = l[i]; else suml -= l[i], as[++nowas] = l[i]; } for (int i = v; i >= 1; i--) { if (dpp[i - 1][sump]) bf[++nowbf] = p[i]; else sump -= p[i], bs[++nowbs] = p[i]; } return 1; } bool cmp(int a, int b) { return a > b; } int ansa[1010], ansb[1010]; int main() { cin >> T; while (T--) { nowaf = nowas = nowbf = nowbs = 0; cin >> h; for (int i = 1; i <= h; i++) cin >> l[i]; cin >> v; for (int j = 1; j <= v; j++) cin >> p[j]; if (h != v) { cout << "No" << endl; continue; } if (!dp()) { cout << "No" << endl; continue; } cout << "Yes" << endl; if (nowaf > nowas) swap(af, as), swap(nowaf, nowas); if (nowbf < nowbs) swap(bf, bs), swap(nowbf, nowbs); sort(af + 1, af + 1 + nowaf, cmp); sort(as + 1, as + 1 + nowas, cmp); sort(bf + 1, bf + 1 + nowbf), sort(bs + 1, bs + 1 + nowbs); int nowx = 0, nowy = 0; int nowpp = 0; for (int i = 1; i <= nowaf; i++) ansa[++nowpp] = af[i]; for (int i = 1; i <= nowas; i++) ansa[++nowpp] = -as[i]; nowpp = 0; for (int i = 1; i <= nowbf; i++) ansb[++nowpp] = bf[i]; for (int i = 1; i <= nowbs; i++) ansb[++nowpp] = -bs[i]; for (int i = 1; i <= h; i++) { nowx += ansa[i]; cout << nowx << " " << nowy << endl; nowy += ansb[i]; cout << nowx << " " << nowy << endl; } } }
### Prompt Develop a solution in Cpp to the problem described below: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; int T, h, v; int l[1010], p[1010]; bitset<1010 * 1010> dpl[1010], dpp[1010]; int as[1010], af[1010], bs[1010], bf[1010]; int nowaf, nowas, nowbf, nowbs; bool dp() { dpl[0][0] = 1; dpp[0][0] = 1; int suml = 0, sump = 0; for (int i = 1; i <= h; i++) { dpl[i] = (dpl[i - 1] << l[i]) | dpl[i - 1]; suml += l[i]; dpp[i] = (dpp[i - 1] << p[i]) | dpp[i - 1]; sump += p[i]; } if ((suml & 1) || (sump & 1)) return 0; if ((!dpl[h][suml >> 1]) || (!dpp[v][sump >> 1])) return 0; suml >>= 1, sump >>= 1; for (int i = h; i >= 1; i--) { if (dpl[i - 1][suml]) af[++nowaf] = l[i]; else suml -= l[i], as[++nowas] = l[i]; } for (int i = v; i >= 1; i--) { if (dpp[i - 1][sump]) bf[++nowbf] = p[i]; else sump -= p[i], bs[++nowbs] = p[i]; } return 1; } bool cmp(int a, int b) { return a > b; } int ansa[1010], ansb[1010]; int main() { cin >> T; while (T--) { nowaf = nowas = nowbf = nowbs = 0; cin >> h; for (int i = 1; i <= h; i++) cin >> l[i]; cin >> v; for (int j = 1; j <= v; j++) cin >> p[j]; if (h != v) { cout << "No" << endl; continue; } if (!dp()) { cout << "No" << endl; continue; } cout << "Yes" << endl; if (nowaf > nowas) swap(af, as), swap(nowaf, nowas); if (nowbf < nowbs) swap(bf, bs), swap(nowbf, nowbs); sort(af + 1, af + 1 + nowaf, cmp); sort(as + 1, as + 1 + nowas, cmp); sort(bf + 1, bf + 1 + nowbf), sort(bs + 1, bs + 1 + nowbs); int nowx = 0, nowy = 0; int nowpp = 0; for (int i = 1; i <= nowaf; i++) ansa[++nowpp] = af[i]; for (int i = 1; i <= nowas; i++) ansa[++nowpp] = -as[i]; nowpp = 0; for (int i = 1; i <= nowbf; i++) ansb[++nowpp] = bf[i]; for (int i = 1; i <= nowbs; i++) ansb[++nowpp] = -bs[i]; for (int i = 1; i <= h; i++) { nowx += ansa[i]; cout << nowx << " " << nowy << endl; nowy += ansb[i]; cout << nowx << " " << nowy << endl; } } } ```
#include <bits/stdc++.h> using namespace std; int n, m; const int maxn = 1e3 + 5; bitset<500005> dp[1005]; int a[maxn], b[maxn]; vector<int> A[2], B[2]; vector<pair<int, int> > ans; int main() { int T; cin >> T; while (T--) { for (int i = (0); i < (2); ++i) A[i].clear(), B[i].clear(); scanf("%d", &n); int ok = 1; dp[0].reset(); dp[0][0] = 1; int s = 0; for (int i = (1); i < (n + 1); ++i) scanf("%d", &a[i]), s += a[i], dp[i] = (dp[i - 1] | (dp[i - 1] << a[i])); if ((s & 1) || !dp[n][s / 2]) ok = 0; s /= 2; if (ok) for (int i = n; i > 0; --i) if (dp[i - 1][s]) A[0].push_back(a[i]); else A[1].push_back(a[i]), s -= a[i]; scanf("%d", &m); s = 0; if (n != m) ok = 0; for (int i = (1); i < (m + 1); ++i) scanf("%d", &b[i]), s += b[i], dp[i] = (dp[i - 1] | (dp[i - 1] << b[i])); if (s & 1 || !dp[m][s / 2]) ok = 0; s /= 2; if (ok) for (int i = m; i > 0; --i) if (dp[i - 1][s]) B[0].push_back(b[i]); else B[1].push_back(b[i]), s -= b[i]; if (!ok) { puts("No"); continue; } for (int i = (0); i < (2); ++i) sort(A[i].begin(), A[i].end()), sort(B[i].begin(), B[i].end()); for (int i = (0); i < (2); ++i) reverse(A[i].begin(), A[i].end()); if (A[0].size() < A[1].size()) swap(A[0], A[1]); if (B[0].size() > B[1].size()) swap(B[0], B[1]); int w1 = B[0].size(), w3 = A[1].size(), w2 = A[0].size() - B[0].size(); ans.clear(); for (int i = (0); i < (w1); ++i) ans.push_back(pair<int, int>(-A[0][i], -B[0][i])); for (int i = (0); i < (w3); ++i) ans.push_back(pair<int, int>(A[1][i], B[1][i + w2])); for (int i = (w1); i < (w1 + w2); ++i) ans.push_back(pair<int, int>(-A[0][i], B[1][i - w1])); puts("Yes"); int x = 0, y = 0; for (auto p : ans) { int dx, dy; tie(dx, dy) = p; x += dx; printf("%d %d\n", x, y); y += dy; printf("%d %d\n", x, y); } } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; const int maxn = 1e3 + 5; bitset<500005> dp[1005]; int a[maxn], b[maxn]; vector<int> A[2], B[2]; vector<pair<int, int> > ans; int main() { int T; cin >> T; while (T--) { for (int i = (0); i < (2); ++i) A[i].clear(), B[i].clear(); scanf("%d", &n); int ok = 1; dp[0].reset(); dp[0][0] = 1; int s = 0; for (int i = (1); i < (n + 1); ++i) scanf("%d", &a[i]), s += a[i], dp[i] = (dp[i - 1] | (dp[i - 1] << a[i])); if ((s & 1) || !dp[n][s / 2]) ok = 0; s /= 2; if (ok) for (int i = n; i > 0; --i) if (dp[i - 1][s]) A[0].push_back(a[i]); else A[1].push_back(a[i]), s -= a[i]; scanf("%d", &m); s = 0; if (n != m) ok = 0; for (int i = (1); i < (m + 1); ++i) scanf("%d", &b[i]), s += b[i], dp[i] = (dp[i - 1] | (dp[i - 1] << b[i])); if (s & 1 || !dp[m][s / 2]) ok = 0; s /= 2; if (ok) for (int i = m; i > 0; --i) if (dp[i - 1][s]) B[0].push_back(b[i]); else B[1].push_back(b[i]), s -= b[i]; if (!ok) { puts("No"); continue; } for (int i = (0); i < (2); ++i) sort(A[i].begin(), A[i].end()), sort(B[i].begin(), B[i].end()); for (int i = (0); i < (2); ++i) reverse(A[i].begin(), A[i].end()); if (A[0].size() < A[1].size()) swap(A[0], A[1]); if (B[0].size() > B[1].size()) swap(B[0], B[1]); int w1 = B[0].size(), w3 = A[1].size(), w2 = A[0].size() - B[0].size(); ans.clear(); for (int i = (0); i < (w1); ++i) ans.push_back(pair<int, int>(-A[0][i], -B[0][i])); for (int i = (0); i < (w3); ++i) ans.push_back(pair<int, int>(A[1][i], B[1][i + w2])); for (int i = (w1); i < (w1 + w2); ++i) ans.push_back(pair<int, int>(-A[0][i], B[1][i - w1])); puts("Yes"); int x = 0, y = 0; for (auto p : ans) { int dx, dy; tie(dx, dy) = p; x += dx; printf("%d %d\n", x, y); y += dy; printf("%d %d\n", x, y); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[1001], b[1001], d[2][2][1001], D[2][1001], T, n, m, i, j, k, l, t[2][2], I, s1, s2, x, y, t2[2]; bitset<1000001> f[1001]; bool bz, Bz; void work(int *a, int I, int s) { int i, j, k, l; for (i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (f[n][s]) { for (i = n; i >= 1; i--) if (s >= a[i] && f[i - 1][s - a[i]]) s -= a[i], d[I][0][++t[I][0]] = a[i]; else d[I][1][++t[I][1]] = a[i]; } else bz = 1; } void Write() { if (Bz) printf("%d %d\n", y, x); else printf("%d %d\n", x, y); } int main() { scanf("%d", &T), f[0][0] = 1; for (; T; --T) { s1 = s2 = bz = Bz = t[0][0] = t[0][1] = t[1][0] = t[1][1] = 0; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]), s1 += a[i]; scanf("%d", &m); for (i = 1; i <= m; i++) scanf("%d", &b[i]), s2 += b[i]; sort(a + 1, a + n + 1), sort(b + 1, b + n + 1); if ((s1 & 1) || (s2 & 1) || n != m) { printf("No\n"); continue; } s1 >>= 1, s2 >>= 1; work(a, 0, s1); work(b, 1, s2); if (bz) { printf("No\n"); continue; } printf("Yes\n"); I = Bz = t[0][0] > t[1][0]; x = y = t2[0] = t2[1] = 0; for (i = 1; i <= t[I][0]; i++) D[0][++t2[0]] = d[I][0][i]; for (i = 1; i <= t[I][1]; i++) D[0][++t2[0]] = -d[I][1][i]; for (i = t[I ^ 1][0]; i >= 1; i--) D[1][++t2[1]] = d[I ^ 1][0][i]; for (i = t[I ^ 1][1]; i >= 1; i--) D[1][++t2[1]] = -d[I ^ 1][1][i]; for (i = 1; i <= n; i++) { x += D[0][i], Write(); y += D[1][i], Write(); } } fclose(stdin); fclose(stdout); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[1001], b[1001], d[2][2][1001], D[2][1001], T, n, m, i, j, k, l, t[2][2], I, s1, s2, x, y, t2[2]; bitset<1000001> f[1001]; bool bz, Bz; void work(int *a, int I, int s) { int i, j, k, l; for (i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (f[n][s]) { for (i = n; i >= 1; i--) if (s >= a[i] && f[i - 1][s - a[i]]) s -= a[i], d[I][0][++t[I][0]] = a[i]; else d[I][1][++t[I][1]] = a[i]; } else bz = 1; } void Write() { if (Bz) printf("%d %d\n", y, x); else printf("%d %d\n", x, y); } int main() { scanf("%d", &T), f[0][0] = 1; for (; T; --T) { s1 = s2 = bz = Bz = t[0][0] = t[0][1] = t[1][0] = t[1][1] = 0; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]), s1 += a[i]; scanf("%d", &m); for (i = 1; i <= m; i++) scanf("%d", &b[i]), s2 += b[i]; sort(a + 1, a + n + 1), sort(b + 1, b + n + 1); if ((s1 & 1) || (s2 & 1) || n != m) { printf("No\n"); continue; } s1 >>= 1, s2 >>= 1; work(a, 0, s1); work(b, 1, s2); if (bz) { printf("No\n"); continue; } printf("Yes\n"); I = Bz = t[0][0] > t[1][0]; x = y = t2[0] = t2[1] = 0; for (i = 1; i <= t[I][0]; i++) D[0][++t2[0]] = d[I][0][i]; for (i = 1; i <= t[I][1]; i++) D[0][++t2[0]] = -d[I][1][i]; for (i = t[I ^ 1][0]; i >= 1; i--) D[1][++t2[1]] = d[I ^ 1][0][i]; for (i = t[I ^ 1][1]; i >= 1; i--) D[1][++t2[1]] = -d[I ^ 1][1][i]; for (i = 1; i <= n; i++) { x += D[0][i], Write(); y += D[1][i], Write(); } } fclose(stdin); fclose(stdout); return 0; } ```
#include <bits/stdc++.h> using namespace std; int dp[500010]; vector<int> t, u; int go(int n, int *v, int h) { dp[0] = 1; int s = 0; for (int i = 0; i <= h; i++) { dp[i] = -1; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = s; j >= 0; j--) { if (dp[j] != -1 && j + v[i] <= h && dp[j + v[i]] == -1) { dp[j + v[i]] = j; } } s = min(s + v[i], h); } if (dp[h] == -1) return 0; t.clear(); u.clear(); while (h > 0) { t.push_back(h - dp[h]); h = dp[h]; } reverse(t.begin(), t.end()); int k = 0; for (int i = 0; i < n; i++) { if (k < t.size() && t[k] == v[i]) { k++; } else { u.push_back(v[i]); } } return 1; } int a[1010], b[1010]; void solve() { int h, v; scanf("%d", &h); int sh = 0; for (int i = 0; i < h; i++) { scanf("%d", &a[i]); sh += a[i]; } scanf("%d", &v); int sv = 0; for (int i = 0; i < v; i++) { scanf("%d", &b[i]); sv += b[i]; } if (h != v || sh % 2 || sv % 2) { puts("No"); return; } sort(a, a + h); sort(b, b + v); if (!go(h, a, sh / 2)) { puts("No"); return; } vector<int> h1 = t, h2 = u; if (!go(v, b, sv / 2)) { puts("No"); return; } vector<int> v1 = t, v2 = u; puts("Yes"); int x = 0, y = 0; if (v2.size() > h2.size()) { reverse(v1.begin(), v1.end()); reverse(v2.begin(), v2.end()); for (int i = 0; i < v2.size(); i++) { v1.push_back(-v2[i]); } for (int i = 0; i < h2.size(); i++) { h1.push_back(-h2[i]); } for (int i = 0; i < h; i++) { y += v1[i]; printf("%d %d\n", x, y); x += h1[i]; printf("%d %d\n", x, y); } } else { reverse(h1.begin(), h1.end()); reverse(h2.begin(), h2.end()); for (int i = 0; i < v2.size(); i++) { v1.push_back(-v2[i]); } for (int i = 0; i < h2.size(); i++) { h1.push_back(-h2[i]); } for (int i = 0; i < h; i++) { x += h1[i]; printf("%d %d\n", x, y); y += v1[i]; printf("%d %d\n", x, y); } } } int main() { int T; scanf("%d", &T); while (T--) { solve(); } return 0; }
### Prompt Create a solution in Cpp for the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dp[500010]; vector<int> t, u; int go(int n, int *v, int h) { dp[0] = 1; int s = 0; for (int i = 0; i <= h; i++) { dp[i] = -1; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = s; j >= 0; j--) { if (dp[j] != -1 && j + v[i] <= h && dp[j + v[i]] == -1) { dp[j + v[i]] = j; } } s = min(s + v[i], h); } if (dp[h] == -1) return 0; t.clear(); u.clear(); while (h > 0) { t.push_back(h - dp[h]); h = dp[h]; } reverse(t.begin(), t.end()); int k = 0; for (int i = 0; i < n; i++) { if (k < t.size() && t[k] == v[i]) { k++; } else { u.push_back(v[i]); } } return 1; } int a[1010], b[1010]; void solve() { int h, v; scanf("%d", &h); int sh = 0; for (int i = 0; i < h; i++) { scanf("%d", &a[i]); sh += a[i]; } scanf("%d", &v); int sv = 0; for (int i = 0; i < v; i++) { scanf("%d", &b[i]); sv += b[i]; } if (h != v || sh % 2 || sv % 2) { puts("No"); return; } sort(a, a + h); sort(b, b + v); if (!go(h, a, sh / 2)) { puts("No"); return; } vector<int> h1 = t, h2 = u; if (!go(v, b, sv / 2)) { puts("No"); return; } vector<int> v1 = t, v2 = u; puts("Yes"); int x = 0, y = 0; if (v2.size() > h2.size()) { reverse(v1.begin(), v1.end()); reverse(v2.begin(), v2.end()); for (int i = 0; i < v2.size(); i++) { v1.push_back(-v2[i]); } for (int i = 0; i < h2.size(); i++) { h1.push_back(-h2[i]); } for (int i = 0; i < h; i++) { y += v1[i]; printf("%d %d\n", x, y); x += h1[i]; printf("%d %d\n", x, y); } } else { reverse(h1.begin(), h1.end()); reverse(h2.begin(), h2.end()); for (int i = 0; i < v2.size(); i++) { v1.push_back(-v2[i]); } for (int i = 0; i < h2.size(); i++) { h1.push_back(-h2[i]); } for (int i = 0; i < h; i++) { x += h1[i]; printf("%d %d\n", x, y); y += v1[i]; printf("%d %d\n", x, y); } } } int main() { int T; scanf("%d", &T); while (T--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; const int maxa = 1000005; int n, m, a[maxn], b[maxn], x[maxn], y[maxn], suma, sumb, sza, szb; bool A[maxn], B[maxn]; bitset<maxa> f[maxn]; inline void solve() { suma = sumb = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), suma += a[i]; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]), sumb += b[i]; sort(a + 1, a + n + 1), sort(b + 1, b + m + 1); if (n != m || suma & 1 || sumb & 1) { puts("No"); return; } f[0].reset(), f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (f[n][suma / 2] == 0) { puts("No"); return; } for (int i = n, val = suma / 2; i; i--) if (val >= a[i] && f[i - 1][val - a[i]]) A[i] = 0, val -= a[i]; else A[i] = 1; f[0].reset(0), f[0][0] = 1; for (int i = 1; i <= m; i++) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (f[m][sumb / 2] == 0) { puts("No"); return; } for (int i = m, val = sumb / 2; i; i--) if (val >= b[i] && f[i - 1][val - b[i]]) B[i] = 0, val -= b[i]; else B[i] = 1; for (int i = 1; i <= n + m; i++) x[i] = y[i] = 0; x[0] = y[0] = sza = szb = 0; for (int i = n; i; i--) if (!A[i]) x[sza << 1 | 1] = -a[i], sza++; for (int i = 1; i <= n; i++) if (A[i]) x[sza << 1 | 1] = a[i], sza++; for (int i = m; i; i--) if (B[i]) szb++, y[szb << 1] = b[i]; for (int i = 1; i <= m; i++) if (!B[i]) szb++, y[szb << 1] = -b[i]; puts("Yes"); for (int i = 1; i <= n + m; i++) x[i] += x[i - 1], y[i] += y[i - 1], printf("%d %d\n", x[i], y[i]); } int main() { int T; scanf("%d", &T); while (T--) solve(); return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1005; const int maxa = 1000005; int n, m, a[maxn], b[maxn], x[maxn], y[maxn], suma, sumb, sza, szb; bool A[maxn], B[maxn]; bitset<maxa> f[maxn]; inline void solve() { suma = sumb = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), suma += a[i]; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]), sumb += b[i]; sort(a + 1, a + n + 1), sort(b + 1, b + m + 1); if (n != m || suma & 1 || sumb & 1) { puts("No"); return; } f[0].reset(), f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (f[n][suma / 2] == 0) { puts("No"); return; } for (int i = n, val = suma / 2; i; i--) if (val >= a[i] && f[i - 1][val - a[i]]) A[i] = 0, val -= a[i]; else A[i] = 1; f[0].reset(0), f[0][0] = 1; for (int i = 1; i <= m; i++) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (f[m][sumb / 2] == 0) { puts("No"); return; } for (int i = m, val = sumb / 2; i; i--) if (val >= b[i] && f[i - 1][val - b[i]]) B[i] = 0, val -= b[i]; else B[i] = 1; for (int i = 1; i <= n + m; i++) x[i] = y[i] = 0; x[0] = y[0] = sza = szb = 0; for (int i = n; i; i--) if (!A[i]) x[sza << 1 | 1] = -a[i], sza++; for (int i = 1; i <= n; i++) if (A[i]) x[sza << 1 | 1] = a[i], sza++; for (int i = m; i; i--) if (B[i]) szb++, y[szb << 1] = b[i]; for (int i = 1; i <= m; i++) if (!B[i]) szb++, y[szb << 1] = -b[i]; puts("Yes"); for (int i = 1; i <= n + m; i++) x[i] += x[i - 1], y[i] += y[i - 1], printf("%d %d\n", x[i], y[i]); } int main() { int T; scanf("%d", &T); while (T--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007LL; long long large = 2000000000000000000LL; bool partition(vector<int> a, vector<int>& b) { int c = 0; int n = (int)a.size(); for (int i = 0; i < n; i++) c += a[i]; if (c % 2) return false; int h = c / 2; vector<vector<int> > dp(n + 1, vector<int>(h + 1, 0)); dp[0][0] = 1; for (int i = 1; i <= n; i++) { int v = a[i - 1]; for (int j = 0; j <= h; j++) { dp[i][j] |= dp[i - 1][j]; if (j >= v) dp[i][j] |= dp[i - 1][j - v]; } } if (dp[n][h] == 0) return false; b.assign(n, -1); int ch = h; for (int i = n - 1; i >= 0; i--) { if (dp[i][ch]) { b[i] = 0; } else { b[i] = 1; ch -= a[i]; } } return true; } int main() { ios::sync_with_stdio(false); int test; cin >> test; while (test--) { int n, m; cin >> n; vector<int> h(n, 0); for (int i = 0; i < n; i++) cin >> h[i]; cin >> m; vector<int> p(m, 0); for (int i = 0; i < m; i++) cin >> p[i]; if (n != m) { cout << "No" << endl; continue; } vector<int> a, b; if (!(partition(h, a) && partition(p, b))) { cout << "No" << endl; continue; } cout << "Yes" << endl; vector<int> l, r, u, d; for (int i = 0; i < n; i++) { if (a[i]) l.push_back(h[i]); else r.push_back(h[i]); if (b[i]) u.push_back(p[i]); else d.push_back(p[i]); } if (l.size() > r.size()) swap(l, r); if (u.size() < d.size()) swap(u, d); sort(l.begin(), l.end()); reverse(l.begin(), l.end()); sort(u.begin(), u.end()); sort(r.begin(), r.end()); sort(d.begin(), d.end()); reverse(d.begin(), d.end()); int x = 0, y = 0; for (int i = 0; i < (int)l.size(); i++) { cout << x << " " << y << endl; x += l[i]; cout << x << " " << y << endl; y += u[i]; } for (int i = (int)l.size(); i < (int)u.size(); i++) { cout << x << " " << y << endl; int t = i - (int)l.size(); x -= r[(int)r.size() - t - 1]; cout << x << " " << y << endl; y += u[i]; } for (int i = 0; i < (int)d.size(); i++) { int off = (int)u.size() - (int)l.size(); cout << x << " " << y << endl; x -= r[(int)r.size() - off - i - 1]; cout << x << " " << y << endl; y -= d[(int)d.size() - i - 1]; } } return 0; }
### Prompt Your task is to create a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long mod = 1000000007LL; long long large = 2000000000000000000LL; bool partition(vector<int> a, vector<int>& b) { int c = 0; int n = (int)a.size(); for (int i = 0; i < n; i++) c += a[i]; if (c % 2) return false; int h = c / 2; vector<vector<int> > dp(n + 1, vector<int>(h + 1, 0)); dp[0][0] = 1; for (int i = 1; i <= n; i++) { int v = a[i - 1]; for (int j = 0; j <= h; j++) { dp[i][j] |= dp[i - 1][j]; if (j >= v) dp[i][j] |= dp[i - 1][j - v]; } } if (dp[n][h] == 0) return false; b.assign(n, -1); int ch = h; for (int i = n - 1; i >= 0; i--) { if (dp[i][ch]) { b[i] = 0; } else { b[i] = 1; ch -= a[i]; } } return true; } int main() { ios::sync_with_stdio(false); int test; cin >> test; while (test--) { int n, m; cin >> n; vector<int> h(n, 0); for (int i = 0; i < n; i++) cin >> h[i]; cin >> m; vector<int> p(m, 0); for (int i = 0; i < m; i++) cin >> p[i]; if (n != m) { cout << "No" << endl; continue; } vector<int> a, b; if (!(partition(h, a) && partition(p, b))) { cout << "No" << endl; continue; } cout << "Yes" << endl; vector<int> l, r, u, d; for (int i = 0; i < n; i++) { if (a[i]) l.push_back(h[i]); else r.push_back(h[i]); if (b[i]) u.push_back(p[i]); else d.push_back(p[i]); } if (l.size() > r.size()) swap(l, r); if (u.size() < d.size()) swap(u, d); sort(l.begin(), l.end()); reverse(l.begin(), l.end()); sort(u.begin(), u.end()); sort(r.begin(), r.end()); sort(d.begin(), d.end()); reverse(d.begin(), d.end()); int x = 0, y = 0; for (int i = 0; i < (int)l.size(); i++) { cout << x << " " << y << endl; x += l[i]; cout << x << " " << y << endl; y += u[i]; } for (int i = (int)l.size(); i < (int)u.size(); i++) { cout << x << " " << y << endl; int t = i - (int)l.size(); x -= r[(int)r.size() - t - 1]; cout << x << " " << y << endl; y += u[i]; } for (int i = 0; i < (int)d.size(); i++) { int off = (int)u.size() - (int)l.size(); cout << x << " " << y << endl; x -= r[(int)r.size() - off - i - 1]; cout << x << " " << y << endl; y -= d[(int)d.size() - i - 1]; } } return 0; } ```
#include <bits/stdc++.h> long long gi() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) f ^= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f ? x : -x; } std::mt19937 rnd(time(NULL)); template <class T> void cxk(T& a, T b) { a = a > b ? a : b; } template <class T> void cnk(T& a, T b) { a = a < b ? a : b; } int pow(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = 1ll * ret * x % 998244353; x = 1ll * x * x % 998244353; y >>= 1; } return ret; } template <class Ta, class Tb> void inc(Ta& a, Tb b) { a = a + b >= 998244353 ? a + b - 998244353 : a + b; } template <class Ta, class Tb> void dec(Ta& a, Tb b) { a = a >= b ? a - b : a + 998244353 - b; } int n, A[1010], B[1010]; std::bitset<501 * 1001> bit[501]; bool get(int* A, std::vector<int>& p, std::vector<int>& q) { for (int i = 1; i <= n; ++i) bit[i] = 0; int sum = 0; for (int i = 1; i <= n; ++i) bit[i] = bit[i - 1] | (bit[i - 1] << A[i]), sum += A[i]; if ((sum & 1) || !bit[n][sum >> 1]) return 0; int i = n, j = sum >> 1; while (i) { if (bit[i - 1][j]) p.push_back(A[i]); else assert(bit[i - 1][j - A[i]]), j -= A[i], q.push_back(A[i]); --i; } return 1; } int main() { bit[0][0] = 1; int qwq = gi(); while (qwq--) { int n = gi(); for (int i = 1; i <= n; ++i) A[i] = gi(); int m = gi(); for (int i = 1; i <= m; ++i) B[i] = gi(); ::n = n; if (n != m) { puts("No"); continue; } std::vector<int> Al, Ar, Bu, Bd; if (!get(A, Al, Ar) || !get(B, Bu, Bd)) { puts("No"); continue; } puts("Yes"); std::vector<int> AA, BB; if (Al.size() < Ar.size()) std::swap(Al, Ar); if (Bu.size() > Bd.size()) std::swap(Bu, Bd); std::sort((Al).begin(), (Al).end(), std::greater<int>()); std::sort((Ar).begin(), (Ar).end(), std::greater<int>()); std::sort((Bu).begin(), (Bu).end()); std::sort((Bd).begin(), (Bd).end()); for (int i : Ar) AA.push_back(i); for (int i : Al) AA.push_back(-i); for (int i : Bd) BB.push_back(i); for (int i : Bu) BB.push_back(-i); int x = 0, y = 0; for (int i = 0; i < n; ++i) { x += AA[i]; printf("%d %d\n", x, y); y += BB[i]; printf("%d %d\n", x, y); } } return 0; }
### Prompt Create a solution in CPP for the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> long long gi() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) f ^= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f ? x : -x; } std::mt19937 rnd(time(NULL)); template <class T> void cxk(T& a, T b) { a = a > b ? a : b; } template <class T> void cnk(T& a, T b) { a = a < b ? a : b; } int pow(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = 1ll * ret * x % 998244353; x = 1ll * x * x % 998244353; y >>= 1; } return ret; } template <class Ta, class Tb> void inc(Ta& a, Tb b) { a = a + b >= 998244353 ? a + b - 998244353 : a + b; } template <class Ta, class Tb> void dec(Ta& a, Tb b) { a = a >= b ? a - b : a + 998244353 - b; } int n, A[1010], B[1010]; std::bitset<501 * 1001> bit[501]; bool get(int* A, std::vector<int>& p, std::vector<int>& q) { for (int i = 1; i <= n; ++i) bit[i] = 0; int sum = 0; for (int i = 1; i <= n; ++i) bit[i] = bit[i - 1] | (bit[i - 1] << A[i]), sum += A[i]; if ((sum & 1) || !bit[n][sum >> 1]) return 0; int i = n, j = sum >> 1; while (i) { if (bit[i - 1][j]) p.push_back(A[i]); else assert(bit[i - 1][j - A[i]]), j -= A[i], q.push_back(A[i]); --i; } return 1; } int main() { bit[0][0] = 1; int qwq = gi(); while (qwq--) { int n = gi(); for (int i = 1; i <= n; ++i) A[i] = gi(); int m = gi(); for (int i = 1; i <= m; ++i) B[i] = gi(); ::n = n; if (n != m) { puts("No"); continue; } std::vector<int> Al, Ar, Bu, Bd; if (!get(A, Al, Ar) || !get(B, Bu, Bd)) { puts("No"); continue; } puts("Yes"); std::vector<int> AA, BB; if (Al.size() < Ar.size()) std::swap(Al, Ar); if (Bu.size() > Bd.size()) std::swap(Bu, Bd); std::sort((Al).begin(), (Al).end(), std::greater<int>()); std::sort((Ar).begin(), (Ar).end(), std::greater<int>()); std::sort((Bu).begin(), (Bu).end()); std::sort((Bd).begin(), (Bd).end()); for (int i : Ar) AA.push_back(i); for (int i : Al) AA.push_back(-i); for (int i : Bd) BB.push_back(i); for (int i : Bu) BB.push_back(-i); int x = 0, y = 0; for (int i = 0; i < n; ++i) { x += AA[i]; printf("%d %d\n", x, y); y += BB[i]; printf("%d %d\n", x, y); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 1; bitset<1000001> dp[1001]; int x[1001], y[1001], a[1001]; bool vis[1001]; int main() { int T; scanf("%d", &T); dp[0][0] = 1; while (T--) { int h; scanf("%d", &h); int sum = 0, ok = 0, cnt = 0, cnt2 = 0; for (int i = (1); i < (h + 1); ++i) scanf("%d", &a[i]), sum += a[i], vis[i] = 0; sort(a + 1, a + 1 + h, [](int x, int y) { return x > y; }); if (sum & 1) ok = 1; if (!ok) { for (int i = (1); i < (h + 1); ++i) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[h][sum >> 1]) ok = 1; else { int pos = sum >> 1; for (int i = (h + 1) - 1; i >= (1); --i) { if (!dp[i - 1][pos]) { vis[i] = 1; pos -= a[i]; } } for (int i = (1); i < (h + 1); ++i) { if (vis[i]) x[++cnt] = a[i]; } for (int i = (1); i < (h + 1); ++i) if (!vis[i]) x[++cnt] = -a[i]; } } scanf("%d", &h); sum = 0; for (int i = (1); i < (h + 1); ++i) scanf("%d", &a[i]), sum += a[i], vis[i] = 0; sort(a + 1, a + 1 + h); if (sum & 1) ok = 1; if (!ok) { for (int i = (1); i < (h + 1); ++i) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[h][sum >> 1]) ok = 1; else { int pos = sum >> 1; for (int i = (h + 1) - 1; i >= (1); --i) { if (!dp[i - 1][pos]) { vis[i] = 1; pos -= a[i]; } } for (int i = (1); i < (h + 1); ++i) { if (vis[i]) y[++cnt2] = a[i]; } for (int i = (1); i < (h + 1); ++i) if (!vis[i]) y[++cnt2] = -a[i]; } } dp[0].reset(); dp[0][0] = 1; if (ok || cnt != cnt2) { puts("No"); } else { puts("Yes"); int px = 0, py = 0, id = 1, id2 = 1; for (int t = (0); t < (cnt + cnt2); ++t) { printf("%d %d\n", px, py); if (~t & 1) px += x[id++]; else py += y[id2++]; } } } }
### Prompt Please create a solution in CPP to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 1; bitset<1000001> dp[1001]; int x[1001], y[1001], a[1001]; bool vis[1001]; int main() { int T; scanf("%d", &T); dp[0][0] = 1; while (T--) { int h; scanf("%d", &h); int sum = 0, ok = 0, cnt = 0, cnt2 = 0; for (int i = (1); i < (h + 1); ++i) scanf("%d", &a[i]), sum += a[i], vis[i] = 0; sort(a + 1, a + 1 + h, [](int x, int y) { return x > y; }); if (sum & 1) ok = 1; if (!ok) { for (int i = (1); i < (h + 1); ++i) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[h][sum >> 1]) ok = 1; else { int pos = sum >> 1; for (int i = (h + 1) - 1; i >= (1); --i) { if (!dp[i - 1][pos]) { vis[i] = 1; pos -= a[i]; } } for (int i = (1); i < (h + 1); ++i) { if (vis[i]) x[++cnt] = a[i]; } for (int i = (1); i < (h + 1); ++i) if (!vis[i]) x[++cnt] = -a[i]; } } scanf("%d", &h); sum = 0; for (int i = (1); i < (h + 1); ++i) scanf("%d", &a[i]), sum += a[i], vis[i] = 0; sort(a + 1, a + 1 + h); if (sum & 1) ok = 1; if (!ok) { for (int i = (1); i < (h + 1); ++i) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[h][sum >> 1]) ok = 1; else { int pos = sum >> 1; for (int i = (h + 1) - 1; i >= (1); --i) { if (!dp[i - 1][pos]) { vis[i] = 1; pos -= a[i]; } } for (int i = (1); i < (h + 1); ++i) { if (vis[i]) y[++cnt2] = a[i]; } for (int i = (1); i < (h + 1); ++i) if (!vis[i]) y[++cnt2] = -a[i]; } } dp[0].reset(); dp[0][0] = 1; if (ok || cnt != cnt2) { puts("No"); } else { puts("Yes"); int px = 0, py = 0, id = 1, id2 = 1; for (int t = (0); t < (cnt + cnt2); ++t) { printf("%d %d\n", px, py); if (~t & 1) px += x[id++]; else py += y[id2++]; } } } } ```
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, sa, sb; int a[1005], b[1005]; bitset<2000001> now[1005]; int main() { int T = readint(); while (T--) { sa = sb = 0; n = readint(); for (int i = 1; i <= n; i++) a[i] = readint(), sa += a[i]; m = readint(); for (int i = 1; i <= m; i++) b[i] = readint(), sb += b[i]; if (n != m || (sa & 1) || (sb & 1)) { printf("No\n"); continue; } vector<int> a1(0), a2(0), b1(0), b2(0); now[0].reset(); now[0][1000000] = 1; for (int i = 1; i <= n; i++) now[i] = (now[i - 1] << a[i]) | (now[i - 1] >> a[i]); if (now[n][1000000]) { int num = 1000000; for (int i = n; i >= 1; i--) { if (num + a[i] > 2000000) a1.push_back(a[i]), num -= a[i]; else if (num - a[i] < 0) a2.push_back(a[i]), num += a[i]; else if (now[i - 1][num + a[i]]) a2.push_back(a[i]), num += a[i]; else a1.push_back(a[i]), num -= a[i]; } } else { printf("No\n"); continue; } now[0].reset(); now[0][1000000] = 1; for (int i = 1; i <= n; i++) now[i] = (now[i - 1] << b[i]) | (now[i - 1] >> b[i]); if (now[n][1000000]) { int num = 1000000; for (int i = n; i >= 1; i--) { if (num + b[i] > 2000000) b1.push_back(b[i]), num -= b[i]; else if (num - b[i] < 0) b2.push_back(b[i]), num += b[i]; else if (now[i - 1][num + b[i]]) b2.push_back(b[i]), num += b[i]; else b1.push_back(b[i]), num -= b[i]; } } else { printf("No\n"); continue; } printf("Yes\n"); sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); sort(b1.begin(), b1.end()); sort(b2.begin(), b2.end()); reverse(a1.begin(), a1.end()); reverse(a2.begin(), a2.end()); if (b1.size() < a1.size()) { vector<int> tmp(0); for (int i = a1.size() - b1.size(); i < b2.size(); i++) tmp.push_back(b2[i]); int tb = b1.size(); for (int i = 0; i < a1.size() - tb; i++) b1.push_back(-b2[i]); b2 = tmp; sort(b1.begin(), b1.end()); } else if (b1.size() > a1.size()) { vector<int> tmp(0); for (int i = b1.size() - a1.size(); i < b1.size(); i++) tmp.push_back(b1[i]); int tb = a1.size(); for (int i = 0; i < b1.size() - tb; i++) b2.push_back(-b1[i]); b1 = tmp; sort(b2.begin(), b2.end()); } int nx = 0, ny = 0; for (int i = 0; i < a1.size(); i++) { nx += a1[i]; printf("%d %d\n", nx, ny); ny += b1[i]; printf("%d %d\n", nx, ny); } for (int i = 0; i < a2.size(); i++) { nx -= a2[i]; printf("%d %d\n", nx, ny); ny -= b2[i]; printf("%d %d\n", nx, ny); } } return 0; }
### Prompt Generate a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, sa, sb; int a[1005], b[1005]; bitset<2000001> now[1005]; int main() { int T = readint(); while (T--) { sa = sb = 0; n = readint(); for (int i = 1; i <= n; i++) a[i] = readint(), sa += a[i]; m = readint(); for (int i = 1; i <= m; i++) b[i] = readint(), sb += b[i]; if (n != m || (sa & 1) || (sb & 1)) { printf("No\n"); continue; } vector<int> a1(0), a2(0), b1(0), b2(0); now[0].reset(); now[0][1000000] = 1; for (int i = 1; i <= n; i++) now[i] = (now[i - 1] << a[i]) | (now[i - 1] >> a[i]); if (now[n][1000000]) { int num = 1000000; for (int i = n; i >= 1; i--) { if (num + a[i] > 2000000) a1.push_back(a[i]), num -= a[i]; else if (num - a[i] < 0) a2.push_back(a[i]), num += a[i]; else if (now[i - 1][num + a[i]]) a2.push_back(a[i]), num += a[i]; else a1.push_back(a[i]), num -= a[i]; } } else { printf("No\n"); continue; } now[0].reset(); now[0][1000000] = 1; for (int i = 1; i <= n; i++) now[i] = (now[i - 1] << b[i]) | (now[i - 1] >> b[i]); if (now[n][1000000]) { int num = 1000000; for (int i = n; i >= 1; i--) { if (num + b[i] > 2000000) b1.push_back(b[i]), num -= b[i]; else if (num - b[i] < 0) b2.push_back(b[i]), num += b[i]; else if (now[i - 1][num + b[i]]) b2.push_back(b[i]), num += b[i]; else b1.push_back(b[i]), num -= b[i]; } } else { printf("No\n"); continue; } printf("Yes\n"); sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); sort(b1.begin(), b1.end()); sort(b2.begin(), b2.end()); reverse(a1.begin(), a1.end()); reverse(a2.begin(), a2.end()); if (b1.size() < a1.size()) { vector<int> tmp(0); for (int i = a1.size() - b1.size(); i < b2.size(); i++) tmp.push_back(b2[i]); int tb = b1.size(); for (int i = 0; i < a1.size() - tb; i++) b1.push_back(-b2[i]); b2 = tmp; sort(b1.begin(), b1.end()); } else if (b1.size() > a1.size()) { vector<int> tmp(0); for (int i = b1.size() - a1.size(); i < b1.size(); i++) tmp.push_back(b1[i]); int tb = a1.size(); for (int i = 0; i < b1.size() - tb; i++) b2.push_back(-b1[i]); b1 = tmp; sort(b2.begin(), b2.end()); } int nx = 0, ny = 0; for (int i = 0; i < a1.size(); i++) { nx += a1[i]; printf("%d %d\n", nx, ny); ny += b1[i]; printf("%d %d\n", nx, ny); } for (int i = 0; i < a2.size(); i++) { nx -= a2[i]; printf("%d %d\n", nx, ny); ny -= b2[i]; printf("%d %d\n", nx, ny); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3, maxm = 1e6, LIM = maxm / 64; int T, n, m, a[maxn + 5], b[maxn + 5], c[maxn + 5], d[maxn + 5], oa[maxn + 5], ob[maxn + 5]; struct hmbs { int n; unsigned long long z[LIM + 5]; void reset() { memset(z, 0, (n / 64 + 1) << 3); } hmbs() { n = 0, reset(); } hmbs operator<<(int x) { hmbs r; r.n = n + x; r.reset(); for (int i = (0); i <= int(n / 64); i++) { r.z[i + x / 64] = z[i]; } x %= 64; if (x != 0) { for (int i = (r.n / 64); i >= int(0); i--) { r.z[i + 1] |= r.z[i] >> (64 - x); r.z[i] <<= x; } } return r; } hmbs operator&(hmbs o) { hmbs r; r.n = max(n, o.n); r.reset(); for (int i = (0); i <= int(r.n / 64); i++) { r.z[i] = i > n / 64 ? 0 & o.z[i] : i > o.n / 64 ? z[i] & 0 : z[i] & o.z[i]; } return r; } hmbs operator|(hmbs o) { hmbs r; r.n = max(n, o.n); r.reset(); for (int i = (0); i <= int(r.n / 64); i++) { r.z[i] = i > n / 64 ? 0 | o.z[i] : i > o.n / 64 ? z[i] | 0 : z[i] | o.z[i]; } return r; } hmbs operator^(hmbs o) { hmbs r; r.n = max(n, o.n); r.reset(); for (int i = (0); i <= int(r.n / 64); i++) { r.z[i] = i > n / 64 ? 0 ^ o.z[i] : i > o.n / 64 ? z[i] ^ 0 : z[i] ^ o.z[i]; } return r; } } B, C, D; bool work(int a[], int b[], int &n) { scanf("%d", &n); int sum = 0; for (int i = (1); i <= int(n); i++) { scanf("%d", &a[i]); sum += a[i]; } if (sum & 1) { return false; } static int obj[maxm + 5]; memset(obj, 0, (sum + 1) << 2); B.n = 0; B.reset(); B.z[0] = 1; for (int i = (1); i <= int(n); i++) { C = B | (B << a[i]); D = C ^ B; for (int j = (0); j <= int(D.n / 64); j++) { for (unsigned long long x = D.z[j]; x; x ^= x & -x) { obj[64 * j + __builtin_ctzll(x & -x)] = i; } } B = C; } if (!obj[sum / 2]) { return false; } for (int i = (1); i <= int(n); i++) { b[i] = -1; } for (int x = sum / 2; x; x -= a[obj[x]]) { b[obj[x]] = 1; } return true; } int main() { scanf("%d", &T); while (T-- > 0) { bool F = true; F &= work(a, c, n); F &= work(b, d, m); if (!F || n != m) { puts("No"); continue; } else { puts("Yes"); } int ca = 0, cb = 0; for (int i = (1); i <= int(n); i++) { ca += c[i] == 1; } for (int i = (1); i <= int(m); i++) { cb += d[i] == 1; } if (ca <= cb) { for (int i = (1); i <= int(n); i++) { oa[i] = i; } sort(oa + 1, oa + n + 1, [&](int i, int j) { return c[i] > c[j] || (c[i] == c[j] && a[i] > a[j]); }); for (int i = (1); i <= int(m); i++) { ob[i] = i; } sort(ob + 1, ob + m + 1, [&](int i, int j) { return d[i] > d[j] || (d[i] == d[j] && b[i] < b[j]); }); int x = 0, y = 0; for (int i = (1); i <= int(n); i++) { printf("%d %d\n", x, y); x += c[oa[i]] * a[oa[i]]; printf("%d %d\n", x, y); y += d[ob[i]] * b[ob[i]]; } } else { for (int i = (1); i <= int(n); i++) { oa[i] = i; } sort(oa + 1, oa + n + 1, [&](int i, int j) { return c[i] > c[j] || (c[i] == c[j] && a[i] < a[j]); }); for (int i = (1); i <= int(m); i++) { ob[i] = i; } sort(ob + 1, ob + m + 1, [&](int i, int j) { return d[i] > d[j] || (d[i] == d[j] && b[i] > b[j]); }); int x = 0, y = 0; for (int i = (1); i <= int(n); i++) { printf("%d %d\n", x, y); y += d[ob[i]] * b[ob[i]]; printf("%d %d\n", x, y); x += c[oa[i]] * a[oa[i]]; } } } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e3, maxm = 1e6, LIM = maxm / 64; int T, n, m, a[maxn + 5], b[maxn + 5], c[maxn + 5], d[maxn + 5], oa[maxn + 5], ob[maxn + 5]; struct hmbs { int n; unsigned long long z[LIM + 5]; void reset() { memset(z, 0, (n / 64 + 1) << 3); } hmbs() { n = 0, reset(); } hmbs operator<<(int x) { hmbs r; r.n = n + x; r.reset(); for (int i = (0); i <= int(n / 64); i++) { r.z[i + x / 64] = z[i]; } x %= 64; if (x != 0) { for (int i = (r.n / 64); i >= int(0); i--) { r.z[i + 1] |= r.z[i] >> (64 - x); r.z[i] <<= x; } } return r; } hmbs operator&(hmbs o) { hmbs r; r.n = max(n, o.n); r.reset(); for (int i = (0); i <= int(r.n / 64); i++) { r.z[i] = i > n / 64 ? 0 & o.z[i] : i > o.n / 64 ? z[i] & 0 : z[i] & o.z[i]; } return r; } hmbs operator|(hmbs o) { hmbs r; r.n = max(n, o.n); r.reset(); for (int i = (0); i <= int(r.n / 64); i++) { r.z[i] = i > n / 64 ? 0 | o.z[i] : i > o.n / 64 ? z[i] | 0 : z[i] | o.z[i]; } return r; } hmbs operator^(hmbs o) { hmbs r; r.n = max(n, o.n); r.reset(); for (int i = (0); i <= int(r.n / 64); i++) { r.z[i] = i > n / 64 ? 0 ^ o.z[i] : i > o.n / 64 ? z[i] ^ 0 : z[i] ^ o.z[i]; } return r; } } B, C, D; bool work(int a[], int b[], int &n) { scanf("%d", &n); int sum = 0; for (int i = (1); i <= int(n); i++) { scanf("%d", &a[i]); sum += a[i]; } if (sum & 1) { return false; } static int obj[maxm + 5]; memset(obj, 0, (sum + 1) << 2); B.n = 0; B.reset(); B.z[0] = 1; for (int i = (1); i <= int(n); i++) { C = B | (B << a[i]); D = C ^ B; for (int j = (0); j <= int(D.n / 64); j++) { for (unsigned long long x = D.z[j]; x; x ^= x & -x) { obj[64 * j + __builtin_ctzll(x & -x)] = i; } } B = C; } if (!obj[sum / 2]) { return false; } for (int i = (1); i <= int(n); i++) { b[i] = -1; } for (int x = sum / 2; x; x -= a[obj[x]]) { b[obj[x]] = 1; } return true; } int main() { scanf("%d", &T); while (T-- > 0) { bool F = true; F &= work(a, c, n); F &= work(b, d, m); if (!F || n != m) { puts("No"); continue; } else { puts("Yes"); } int ca = 0, cb = 0; for (int i = (1); i <= int(n); i++) { ca += c[i] == 1; } for (int i = (1); i <= int(m); i++) { cb += d[i] == 1; } if (ca <= cb) { for (int i = (1); i <= int(n); i++) { oa[i] = i; } sort(oa + 1, oa + n + 1, [&](int i, int j) { return c[i] > c[j] || (c[i] == c[j] && a[i] > a[j]); }); for (int i = (1); i <= int(m); i++) { ob[i] = i; } sort(ob + 1, ob + m + 1, [&](int i, int j) { return d[i] > d[j] || (d[i] == d[j] && b[i] < b[j]); }); int x = 0, y = 0; for (int i = (1); i <= int(n); i++) { printf("%d %d\n", x, y); x += c[oa[i]] * a[oa[i]]; printf("%d %d\n", x, y); y += d[ob[i]] * b[ob[i]]; } } else { for (int i = (1); i <= int(n); i++) { oa[i] = i; } sort(oa + 1, oa + n + 1, [&](int i, int j) { return c[i] > c[j] || (c[i] == c[j] && a[i] < a[j]); }); for (int i = (1); i <= int(m); i++) { ob[i] = i; } sort(ob + 1, ob + m + 1, [&](int i, int j) { return d[i] > d[j] || (d[i] == d[j] && b[i] > b[j]); }); int x = 0, y = 0; for (int i = (1); i <= int(n); i++) { printf("%d %d\n", x, y); y += d[ob[i]] * b[ob[i]]; printf("%d %d\n", x, y); x += c[oa[i]] * a[oa[i]]; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { char c; int f = 1; while (!isdigit(c = getchar())) if (c == '-') f = -1; x = (c & 15); while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); x *= f; } int t, n, m, i, j, a[1005], b[1005], sa, sb, sx[1005], sy[1005]; bitset<1000005> f[1005]; vector<int> a1, a2, b1, b2; int main() { read(t); while (t--) { sa = sb = 0; a1.clear(); a2.clear(); b1.clear(); b2.clear(); read(n); for ((i) = 1; (i) <= (n); (i)++) { read(a[i]); sa += a[i]; } read(m); for ((i) = 1; (i) <= (m); (i)++) { read(b[i]); sb += b[i]; } if (n != m || sa % 2 == 1 || sb % 2 == 1) { puts("No"); continue; } for ((i) = 0; (i) <= (n); (i)++) f[i].reset(); f[0].set(0); for ((i) = 1; (i) <= (n); (i)++) { f[i] = (f[i - 1] | (f[i - 1] << a[i])); } if (!f[n][sa / 2]) { puts("No"); continue; } int t = sa / 2; for ((i) = (n); (i) >= 1; (i)--) { if (f[i - 1][t]) { a1.push_back(a[i]); } else { a2.push_back(a[i]); t -= a[i]; } } for ((i) = 0; (i) <= (m); (i)++) f[i].reset(); f[0].set(0); for ((i) = 1; (i) <= (m); (i)++) { f[i] = (f[i - 1] | (f[i - 1] << b[i])); } if (!f[m][sb / 2]) { puts("No"); continue; } t = sb / 2; for ((i) = (m); (i) >= 1; (i)--) { if (f[i - 1][t]) { b1.push_back(b[i]); } else { b2.push_back(b[i]); t -= b[i]; } } sort(a1.begin(), a1.end(), greater<int>()); sort(a2.begin(), a2.end(), greater<int>()); sort(b1.begin(), b1.end(), greater<int>()); sort(b2.begin(), b2.end(), greater<int>()); if (a1.size() > a2.size()) swap(a1, a2); if (b1.size() > b2.size()) swap(b1, b2); if (a1.size() <= b1.size()) { sort(b1.begin(), b1.end()); sort(b2.begin(), b2.end()); } else { sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); } t = 0; for (__typeof((a1).begin()) it = (a1).begin(); it != (a1).end(); ++it) { t++; sx[t] = sx[t - 1] + *it; } for (__typeof((a2).begin()) it = (a2).begin(); it != (a2).end(); ++it) { t++; sx[t] = sx[t - 1] - *it; } t = 0; for (__typeof((b1).begin()) it = (b1).begin(); it != (b1).end(); ++it) { t++; sy[t] = sy[t - 1] + *it; } for (__typeof((b2).begin()) it = (b2).begin(); it != (b2).end(); ++it) { t++; sy[t] = sy[t - 1] - *it; } puts("Yes"); if (a1.size() <= b1.size()) { for ((i) = 1; (i) <= (n); (i)++) { printf("%d %d\n", sx[i - 1], sy[i - 1]); printf("%d %d\n", sx[i], sy[i - 1]); } } else { for ((i) = 1; (i) <= (n); (i)++) { printf("%d %d\n", sx[i - 1], sy[i - 1]); printf("%d %d\n", sx[i - 1], sy[i]); } } } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline void read(int &x) { char c; int f = 1; while (!isdigit(c = getchar())) if (c == '-') f = -1; x = (c & 15); while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); x *= f; } int t, n, m, i, j, a[1005], b[1005], sa, sb, sx[1005], sy[1005]; bitset<1000005> f[1005]; vector<int> a1, a2, b1, b2; int main() { read(t); while (t--) { sa = sb = 0; a1.clear(); a2.clear(); b1.clear(); b2.clear(); read(n); for ((i) = 1; (i) <= (n); (i)++) { read(a[i]); sa += a[i]; } read(m); for ((i) = 1; (i) <= (m); (i)++) { read(b[i]); sb += b[i]; } if (n != m || sa % 2 == 1 || sb % 2 == 1) { puts("No"); continue; } for ((i) = 0; (i) <= (n); (i)++) f[i].reset(); f[0].set(0); for ((i) = 1; (i) <= (n); (i)++) { f[i] = (f[i - 1] | (f[i - 1] << a[i])); } if (!f[n][sa / 2]) { puts("No"); continue; } int t = sa / 2; for ((i) = (n); (i) >= 1; (i)--) { if (f[i - 1][t]) { a1.push_back(a[i]); } else { a2.push_back(a[i]); t -= a[i]; } } for ((i) = 0; (i) <= (m); (i)++) f[i].reset(); f[0].set(0); for ((i) = 1; (i) <= (m); (i)++) { f[i] = (f[i - 1] | (f[i - 1] << b[i])); } if (!f[m][sb / 2]) { puts("No"); continue; } t = sb / 2; for ((i) = (m); (i) >= 1; (i)--) { if (f[i - 1][t]) { b1.push_back(b[i]); } else { b2.push_back(b[i]); t -= b[i]; } } sort(a1.begin(), a1.end(), greater<int>()); sort(a2.begin(), a2.end(), greater<int>()); sort(b1.begin(), b1.end(), greater<int>()); sort(b2.begin(), b2.end(), greater<int>()); if (a1.size() > a2.size()) swap(a1, a2); if (b1.size() > b2.size()) swap(b1, b2); if (a1.size() <= b1.size()) { sort(b1.begin(), b1.end()); sort(b2.begin(), b2.end()); } else { sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); } t = 0; for (__typeof((a1).begin()) it = (a1).begin(); it != (a1).end(); ++it) { t++; sx[t] = sx[t - 1] + *it; } for (__typeof((a2).begin()) it = (a2).begin(); it != (a2).end(); ++it) { t++; sx[t] = sx[t - 1] - *it; } t = 0; for (__typeof((b1).begin()) it = (b1).begin(); it != (b1).end(); ++it) { t++; sy[t] = sy[t - 1] + *it; } for (__typeof((b2).begin()) it = (b2).begin(); it != (b2).end(); ++it) { t++; sy[t] = sy[t - 1] - *it; } puts("Yes"); if (a1.size() <= b1.size()) { for ((i) = 1; (i) <= (n); (i)++) { printf("%d %d\n", sx[i - 1], sy[i - 1]); printf("%d %d\n", sx[i], sy[i - 1]); } } else { for ((i) = 1; (i) <= (n); (i)++) { printf("%d %d\n", sx[i - 1], sy[i - 1]); printf("%d %d\n", sx[i - 1], sy[i]); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = (int)1e3, M = (int)1e5, MID = M / 2; int h, v; int a[N], b[N]; bool dp[N][M], up[N][M]; pair<vector<int>, vector<int> > make_part(int *val, int n) { for (int i = 0; i <= n; i++) fill(dp[i], dp[i] + M, false); dp[0][MID] = true; for (int i = 0; i < n; i++) { for (int j = 0; j < M; j++) { if (!dp[i][j]) continue; if (j >= val[i]) dp[i + 1][j - val[i]] = 1, up[i + 1][j - val[i]] = false; if (j + val[i] < M) dp[i + 1][j + val[i]] = 1, up[i + 1][j + val[i]] = true; } } if (!dp[n][MID]) return {vector<int>(), vector<int>()}; vector<int> v1, v2; int cur = MID; for (int i = n; i >= 1; i--) { if (up[i][cur]) v1.push_back(val[i - 1]), cur -= val[i - 1]; else v2.push_back(val[i - 1]), cur += val[i - 1]; } return {v1, v2}; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { cin >> h; for (int i = 0; i < (h); ++i) cin >> a[i]; cin >> v; for (int i = 0; i < (v); ++i) cin >> b[i]; pair<vector<int>, vector<int> > ap = make_part(a, h); pair<vector<int>, vector<int> > bp = make_part(b, v); if (ap.first.empty() || bp.first.empty() || h != v) { cout << "No\n"; continue; } if ((int)(ap.first).size() > (int)(ap.second).size()) swap(ap.first, ap.second); if ((int)(bp.first).size() > (int)(bp.second).size()) swap(bp.first, bp.second); int swapped = (int)(ap.first).size() > (int)(bp.first).size(); if (swapped) swap(ap, bp); sort(begin(ap.first), end(ap.first)); sort(begin(ap.second), end(ap.second)); sort(begin(bp.first), end(bp.first)); sort(begin(bp.second), end(bp.second)); vector<pair<int, int> > res; int cx = 0, cy = 0; int len0 = (int)(ap.first).size(), len1 = (int)(bp.second).size(), len_mid = (int)(bp.first).size() - len0; for (int i = 0; i < len0; i++) { cx -= ap.first[len0 - 1 - i]; res.push_back({cx, cy}); cy -= bp.first[i]; res.push_back({cx, cy}); } for (int i = 0; i < len_mid; i++) { cx += ap.second[len1 + i]; res.push_back({cx, cy}); cy -= bp.first[len0 + i]; res.push_back({cx, cy}); } for (int i = 0; i < len1; i++) { cx += ap.second[len1 - 1 - i]; res.push_back({cx, cy}); cy += bp.second[i]; res.push_back({cx, cy}); } cout << "Yes\n"; for (auto &p : res) { if (swapped) swap(p.first, p.second); cout << p.first << " " << p.second << '\n'; } } }
### Prompt Create a solution in cpp for the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = (int)1e3, M = (int)1e5, MID = M / 2; int h, v; int a[N], b[N]; bool dp[N][M], up[N][M]; pair<vector<int>, vector<int> > make_part(int *val, int n) { for (int i = 0; i <= n; i++) fill(dp[i], dp[i] + M, false); dp[0][MID] = true; for (int i = 0; i < n; i++) { for (int j = 0; j < M; j++) { if (!dp[i][j]) continue; if (j >= val[i]) dp[i + 1][j - val[i]] = 1, up[i + 1][j - val[i]] = false; if (j + val[i] < M) dp[i + 1][j + val[i]] = 1, up[i + 1][j + val[i]] = true; } } if (!dp[n][MID]) return {vector<int>(), vector<int>()}; vector<int> v1, v2; int cur = MID; for (int i = n; i >= 1; i--) { if (up[i][cur]) v1.push_back(val[i - 1]), cur -= val[i - 1]; else v2.push_back(val[i - 1]), cur += val[i - 1]; } return {v1, v2}; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { cin >> h; for (int i = 0; i < (h); ++i) cin >> a[i]; cin >> v; for (int i = 0; i < (v); ++i) cin >> b[i]; pair<vector<int>, vector<int> > ap = make_part(a, h); pair<vector<int>, vector<int> > bp = make_part(b, v); if (ap.first.empty() || bp.first.empty() || h != v) { cout << "No\n"; continue; } if ((int)(ap.first).size() > (int)(ap.second).size()) swap(ap.first, ap.second); if ((int)(bp.first).size() > (int)(bp.second).size()) swap(bp.first, bp.second); int swapped = (int)(ap.first).size() > (int)(bp.first).size(); if (swapped) swap(ap, bp); sort(begin(ap.first), end(ap.first)); sort(begin(ap.second), end(ap.second)); sort(begin(bp.first), end(bp.first)); sort(begin(bp.second), end(bp.second)); vector<pair<int, int> > res; int cx = 0, cy = 0; int len0 = (int)(ap.first).size(), len1 = (int)(bp.second).size(), len_mid = (int)(bp.first).size() - len0; for (int i = 0; i < len0; i++) { cx -= ap.first[len0 - 1 - i]; res.push_back({cx, cy}); cy -= bp.first[i]; res.push_back({cx, cy}); } for (int i = 0; i < len_mid; i++) { cx += ap.second[len1 + i]; res.push_back({cx, cy}); cy -= bp.first[len0 + i]; res.push_back({cx, cy}); } for (int i = 0; i < len1; i++) { cx += ap.second[len1 - 1 - i]; res.push_back({cx, cy}); cy += bp.second[i]; res.push_back({cx, cy}); } cout << "Yes\n"; for (auto &p : res) { if (swapped) swap(p.first, p.second); cout << p.first << " " << p.second << '\n'; } } } ```
#include <bits/stdc++.h> using namespace std; const int M = 1009; int n[2], p[2], a[M]; vector<int> d[2]; bitset<500009> b[1009]; bool cmp(int l, int r) { if (l < 0 || r < 0) return l < r; return l > r; } bool get(int o) { int x = 0; scanf("%d", &n[o]); p[o] = 0; b[0] = 1; for (int i = 1; i <= n[o]; ++i) scanf("%d", &a[i]), b[i] = 0; sort(a + 1, a + n[o] + 1); d[o].clear(); for (int i = 1; i <= n[o]; ++i) { b[i] = b[i - 1] | (b[i - 1] << a[i]); x += a[i]; } if (x % 2 == 0 && b[n[o]][x / 2]) { x /= 2; for (int i = n[o]; i >= 1; --i) { if (x >= a[i] && b[i - 1][x - a[i]]) d[o].emplace_back(a[i]), x -= a[i], p[o]++; else d[o].emplace_back(-a[i]); } if (p[o] < n[o] - p[o]) for (auto& y : d[o]) y = -y; sort(d[o].begin(), d[o].end(), cmp); return 1; } return 0; } void work() { bool bol = (!get(0)) | (!get(1)) | (n[0] != n[1]); if (bol) printf("No\n"); else { reverse(d[1].begin(), d[1].end()); int x = 0, y = 0; printf("Yes\n"); for (int i = 0; i < n[0]; ++i) { x += d[0][i]; printf("%d %d\n", x, y); y += d[1][i]; printf("%d %d\n", x, y); } } } int main() { int T; scanf("%d", &T); while (T--) work(); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int M = 1009; int n[2], p[2], a[M]; vector<int> d[2]; bitset<500009> b[1009]; bool cmp(int l, int r) { if (l < 0 || r < 0) return l < r; return l > r; } bool get(int o) { int x = 0; scanf("%d", &n[o]); p[o] = 0; b[0] = 1; for (int i = 1; i <= n[o]; ++i) scanf("%d", &a[i]), b[i] = 0; sort(a + 1, a + n[o] + 1); d[o].clear(); for (int i = 1; i <= n[o]; ++i) { b[i] = b[i - 1] | (b[i - 1] << a[i]); x += a[i]; } if (x % 2 == 0 && b[n[o]][x / 2]) { x /= 2; for (int i = n[o]; i >= 1; --i) { if (x >= a[i] && b[i - 1][x - a[i]]) d[o].emplace_back(a[i]), x -= a[i], p[o]++; else d[o].emplace_back(-a[i]); } if (p[o] < n[o] - p[o]) for (auto& y : d[o]) y = -y; sort(d[o].begin(), d[o].end(), cmp); return 1; } return 0; } void work() { bool bol = (!get(0)) | (!get(1)) | (n[0] != n[1]); if (bol) printf("No\n"); else { reverse(d[1].begin(), d[1].end()); int x = 0, y = 0; printf("Yes\n"); for (int i = 0; i < n[0]; ++i) { x += d[0][i]; printf("%d %d\n", x, y); y += d[1][i]; printf("%d %d\n", x, y); } } } int main() { int T; scanf("%d", &T); while (T--) work(); return 0; } ```
#include <bits/stdc++.h> using namespace std; bool divide(vector<long long>& a) { long long n = a.size(); long long sum = 0; for (long long i = 0; i < (long long)(n); i++) { sum += a[i]; } if (sum % 2) { return false; } sum /= 2; vector<long long> p(sum + 1, -2); p[0] = -1; long long cur_sum = 0; for (long long i = 0; i < n; i++) { long long mx = min((long long)(p.size()) - a[i], cur_sum + 1); for (long long j = mx - 1; j >= 0; j--) { if (p[j] != -2 && p[j + a[i]] == -2) { p[j + a[i]] = i; } } cur_sum += a[i]; } if (p[sum] == -2) { return false; } while (sum) { a[p[sum]] *= -1; sum += a[p[sum]]; } return true; } inline long long sgn(long long a) { return a < 0 ? -1 : 1; } bool cmp1(long long a, long long b) { if (sgn(a) != sgn(b)) { return sgn(a) > sgn(b); } return abs(a) > abs(b); } bool cmp2(long long a, long long b) { if (sgn(a) != sgn(b)) { return sgn(a) > sgn(b); } return abs(a) < abs(b); } bool cmp3(long long a, long long b) { if (sgn(a) != sgn(b)) { return sgn(a) < sgn(b); } return abs(a) > abs(b); } bool cmp4(long long a, long long b) { if (sgn(a) != sgn(b)) { return sgn(a) < sgn(b); } return abs(a) < abs(b); } bool check(vector<long long>& a, vector<long long>& b) { long long n = a.size(); vector<long long> l(500100, -2e18), r(500100, -2e18); vector<long long> l2(500100, -2e18), r2(500100, -2e18); vector<long long> w(500100, -2e18 + 1e3), v(500100, -2e18 + 1e3); vector<bool> we(500100), ve(500100); long long x = 250005, y = 250005; for (long long i = 0; i < (long long)(n); i++) { if (a[i] > 0) { we[x] = true; for (long long j = 0; j < (long long)(a[i] + 1); j++) { w[x] = y; x++; } x--; we[x] = true; } else { ve[x] = true; for (long long j = 0; j < (long long)(-a[i] + 1); j++) { v[x] = y; x--; } x++; ve[x] = true; } if (l[x] == -2e18) { l[x] = y; } else { l2[x] = y; } y += b[i]; if (r[x] == -2e18) { r[x] = y; } else { r2[x] = y; } if (l[x] > r[x]) { swap(l[x], r[x]); } } bool ok = true; for (long long i = 0; i < (long long)(l.size()); i++) { if (l[i] < w[i] && w[i] < r[i]) { ok = false; } if (l[i] < v[i] && v[i] < r[i]) { ok = false; } if ((l[i] == w[i] || w[i] == r[i]) && !we[i]) { ok = false; } if ((l[i] == v[i] || v[i] == r[i]) && !ve[i]) { ok = false; } if (l2[i] < w[i] && w[i] < r2[i]) { ok = false; } if (l2[i] < v[i] && v[i] < r2[i]) { ok = false; } if ((l2[i] == w[i] || w[i] == r2[i]) && !we[i]) { ok = false; } if ((l2[i] == v[i] || v[i] == r2[i]) && !ve[i]) { ok = false; } } if (ok) { cout << "Yes" << endl; x = y = 0; for (long long i = 0; i < (long long)(n); i++) { cout << x << " " << y << endl; x += a[i]; cout << x << " " << y << endl; y += b[i]; } return true; } return false; } int32_t main() { ios_base::sync_with_stdio(0); long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < (long long)(n); i++) { cin >> a[i]; } long long tmp; cin >> tmp; vector<long long> b(tmp); for (long long i = 0; i < (long long)(tmp); i++) { cin >> b[i]; } if (n != tmp || !divide(a) || !divide(b)) { cout << "No" << endl; continue; } bool ok = false; for (long long k = 0; k < (long long)(2); k++) { for (long long i = 0; i < (long long)(4); i++) { if (i == 0) { sort(a.begin(), a.end(), cmp4); } else if (i == 1) { sort(a.begin(), a.end(), cmp1); } else if (i == 2) { sort(a.begin(), a.end(), cmp2); } else { sort(a.begin(), a.end(), cmp3); } for (long long j = 0; j < (long long)(4); j++) { if (j == 0) { sort(b.begin(), b.end(), cmp4); } else if (j == 1) { sort(b.begin(), b.end(), cmp1); } else if (j == 2) { sort(b.begin(), b.end(), cmp2); } else { sort(b.begin(), b.end(), cmp3); } if (check(a, b)) { ok = true; break; } } if (ok) { break; } } if (ok) { break; } for (long long i = 0; i < (long long)(n); i++) { a[i] *= -1; } } if (!ok) { cout << "No" << endl; continue; } } }
### Prompt Please provide a Cpp coded solution to the problem described below: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool divide(vector<long long>& a) { long long n = a.size(); long long sum = 0; for (long long i = 0; i < (long long)(n); i++) { sum += a[i]; } if (sum % 2) { return false; } sum /= 2; vector<long long> p(sum + 1, -2); p[0] = -1; long long cur_sum = 0; for (long long i = 0; i < n; i++) { long long mx = min((long long)(p.size()) - a[i], cur_sum + 1); for (long long j = mx - 1; j >= 0; j--) { if (p[j] != -2 && p[j + a[i]] == -2) { p[j + a[i]] = i; } } cur_sum += a[i]; } if (p[sum] == -2) { return false; } while (sum) { a[p[sum]] *= -1; sum += a[p[sum]]; } return true; } inline long long sgn(long long a) { return a < 0 ? -1 : 1; } bool cmp1(long long a, long long b) { if (sgn(a) != sgn(b)) { return sgn(a) > sgn(b); } return abs(a) > abs(b); } bool cmp2(long long a, long long b) { if (sgn(a) != sgn(b)) { return sgn(a) > sgn(b); } return abs(a) < abs(b); } bool cmp3(long long a, long long b) { if (sgn(a) != sgn(b)) { return sgn(a) < sgn(b); } return abs(a) > abs(b); } bool cmp4(long long a, long long b) { if (sgn(a) != sgn(b)) { return sgn(a) < sgn(b); } return abs(a) < abs(b); } bool check(vector<long long>& a, vector<long long>& b) { long long n = a.size(); vector<long long> l(500100, -2e18), r(500100, -2e18); vector<long long> l2(500100, -2e18), r2(500100, -2e18); vector<long long> w(500100, -2e18 + 1e3), v(500100, -2e18 + 1e3); vector<bool> we(500100), ve(500100); long long x = 250005, y = 250005; for (long long i = 0; i < (long long)(n); i++) { if (a[i] > 0) { we[x] = true; for (long long j = 0; j < (long long)(a[i] + 1); j++) { w[x] = y; x++; } x--; we[x] = true; } else { ve[x] = true; for (long long j = 0; j < (long long)(-a[i] + 1); j++) { v[x] = y; x--; } x++; ve[x] = true; } if (l[x] == -2e18) { l[x] = y; } else { l2[x] = y; } y += b[i]; if (r[x] == -2e18) { r[x] = y; } else { r2[x] = y; } if (l[x] > r[x]) { swap(l[x], r[x]); } } bool ok = true; for (long long i = 0; i < (long long)(l.size()); i++) { if (l[i] < w[i] && w[i] < r[i]) { ok = false; } if (l[i] < v[i] && v[i] < r[i]) { ok = false; } if ((l[i] == w[i] || w[i] == r[i]) && !we[i]) { ok = false; } if ((l[i] == v[i] || v[i] == r[i]) && !ve[i]) { ok = false; } if (l2[i] < w[i] && w[i] < r2[i]) { ok = false; } if (l2[i] < v[i] && v[i] < r2[i]) { ok = false; } if ((l2[i] == w[i] || w[i] == r2[i]) && !we[i]) { ok = false; } if ((l2[i] == v[i] || v[i] == r2[i]) && !ve[i]) { ok = false; } } if (ok) { cout << "Yes" << endl; x = y = 0; for (long long i = 0; i < (long long)(n); i++) { cout << x << " " << y << endl; x += a[i]; cout << x << " " << y << endl; y += b[i]; } return true; } return false; } int32_t main() { ios_base::sync_with_stdio(0); long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < (long long)(n); i++) { cin >> a[i]; } long long tmp; cin >> tmp; vector<long long> b(tmp); for (long long i = 0; i < (long long)(tmp); i++) { cin >> b[i]; } if (n != tmp || !divide(a) || !divide(b)) { cout << "No" << endl; continue; } bool ok = false; for (long long k = 0; k < (long long)(2); k++) { for (long long i = 0; i < (long long)(4); i++) { if (i == 0) { sort(a.begin(), a.end(), cmp4); } else if (i == 1) { sort(a.begin(), a.end(), cmp1); } else if (i == 2) { sort(a.begin(), a.end(), cmp2); } else { sort(a.begin(), a.end(), cmp3); } for (long long j = 0; j < (long long)(4); j++) { if (j == 0) { sort(b.begin(), b.end(), cmp4); } else if (j == 1) { sort(b.begin(), b.end(), cmp1); } else if (j == 2) { sort(b.begin(), b.end(), cmp2); } else { sort(b.begin(), b.end(), cmp3); } if (check(a, b)) { ok = true; break; } } if (ok) { break; } } if (ok) { break; } for (long long i = 0; i < (long long)(n); i++) { a[i] *= -1; } } if (!ok) { cout << "No" << endl; continue; } } } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const char nl = '\n'; const int MX = 100001; pair<vector<int>, vector<int> > solve(vector<int> L) { int N = (int)(L).size(); int sum = 0; for (int i = 0; i < (N); i++) sum += L[i]; if (sum % 2) { return {vector<int>(), vector<int>()}; } sum /= 2; int lst[sum + 1]; for (int i = 0; i < (sum + 1); i++) lst[i] = -1; lst[0] = 0; int sumSF = 0; for (int i = 0; i < (N); i++) { for (int j = (min(sum - L[i], sumSF) + 1) - 1; j >= 0; j--) { if (lst[j] != -1 && lst[j + L[i]] == -1) { lst[j + L[i]] = L[i]; } } sumSF += L[i]; } if (lst[sum] == -1) return {vector<int>(), vector<int>()}; multiset<int> rem; for (auto& a : L) rem.insert(a); vector<int> fir; int cur = sum; while (cur > 0) { fir.push_back(lst[cur]); rem.erase(rem.find(lst[cur])); cur -= lst[cur]; } vector<int> sec; for (auto& a : rem) sec.push_back(a); return {fir, sec}; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while (T--) { int H; cin >> H; vector<int> L(H); for (int i = 0; i < (H); i++) cin >> L[i]; int V; cin >> V; vector<int> P(V); for (int i = 0; i < (V); i++) cin >> P[i]; if (H != V) { cout << "No" << nl; continue; } pair<vector<int>, vector<int> > A = solve(L); pair<vector<int>, vector<int> > B = solve(P); if ((int)(A.first).size() == 0 || (int)(B.first).size() == 0) { cout << "No" << nl; continue; } if ((int)(A.first).size() > (int)(A.second).size()) { swap(A.first, A.second); } if ((int)(B.first).size() < (int)(B.second).size()) { swap(B.first, B.second); } sort(A.first.begin(), A.first.end()); sort(A.second.begin(), A.second.end()); reverse(A.first.begin(), A.first.end()); reverse(A.second.begin(), A.second.end()); sort(B.first.begin(), B.first.end()); sort(B.second.begin(), B.second.end()); vector<int> hor; for (int i = 0; i < ((int)(A.first).size()); i++) hor.push_back(A.first[i]); for (int i = 0; i < ((int)(A.second).size()); i++) hor.push_back(-A.second[i]); vector<int> ver; for (int i = 0; i < ((int)(B.first).size()); i++) ver.push_back(B.first[i]); for (int i = 0; i < ((int)(B.second).size()); i++) ver.push_back(-B.second[i]); int X = 0, Y = 0; cout << "Yes" << nl; for (int i = 0; i < (H + V); i++) { cout << X << " " << Y << nl; if (i % 2 == 0) { X += hor[i / 2]; } else { Y += ver[i / 2]; } } } return 0; }
### Prompt In Cpp, your task is to solve the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const char nl = '\n'; const int MX = 100001; pair<vector<int>, vector<int> > solve(vector<int> L) { int N = (int)(L).size(); int sum = 0; for (int i = 0; i < (N); i++) sum += L[i]; if (sum % 2) { return {vector<int>(), vector<int>()}; } sum /= 2; int lst[sum + 1]; for (int i = 0; i < (sum + 1); i++) lst[i] = -1; lst[0] = 0; int sumSF = 0; for (int i = 0; i < (N); i++) { for (int j = (min(sum - L[i], sumSF) + 1) - 1; j >= 0; j--) { if (lst[j] != -1 && lst[j + L[i]] == -1) { lst[j + L[i]] = L[i]; } } sumSF += L[i]; } if (lst[sum] == -1) return {vector<int>(), vector<int>()}; multiset<int> rem; for (auto& a : L) rem.insert(a); vector<int> fir; int cur = sum; while (cur > 0) { fir.push_back(lst[cur]); rem.erase(rem.find(lst[cur])); cur -= lst[cur]; } vector<int> sec; for (auto& a : rem) sec.push_back(a); return {fir, sec}; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T; cin >> T; while (T--) { int H; cin >> H; vector<int> L(H); for (int i = 0; i < (H); i++) cin >> L[i]; int V; cin >> V; vector<int> P(V); for (int i = 0; i < (V); i++) cin >> P[i]; if (H != V) { cout << "No" << nl; continue; } pair<vector<int>, vector<int> > A = solve(L); pair<vector<int>, vector<int> > B = solve(P); if ((int)(A.first).size() == 0 || (int)(B.first).size() == 0) { cout << "No" << nl; continue; } if ((int)(A.first).size() > (int)(A.second).size()) { swap(A.first, A.second); } if ((int)(B.first).size() < (int)(B.second).size()) { swap(B.first, B.second); } sort(A.first.begin(), A.first.end()); sort(A.second.begin(), A.second.end()); reverse(A.first.begin(), A.first.end()); reverse(A.second.begin(), A.second.end()); sort(B.first.begin(), B.first.end()); sort(B.second.begin(), B.second.end()); vector<int> hor; for (int i = 0; i < ((int)(A.first).size()); i++) hor.push_back(A.first[i]); for (int i = 0; i < ((int)(A.second).size()); i++) hor.push_back(-A.second[i]); vector<int> ver; for (int i = 0; i < ((int)(B.first).size()); i++) ver.push_back(B.first[i]); for (int i = 0; i < ((int)(B.second).size()); i++) ver.push_back(-B.second[i]); int X = 0, Y = 0; cout << "Yes" << nl; for (int i = 0; i < (H + V); i++) { cout << X << " " << Y << nl; if (i % 2 == 0) { X += hor[i / 2]; } else { Y += ver[i / 2]; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; static struct FastInput { static constexpr int BUF_SIZE = 1 << 20; char buf[BUF_SIZE]; size_t chars_read = 0; size_t buf_pos = 0; FILE* in = stdin; char cur = 0; inline char get_char() { if (buf_pos >= chars_read) { chars_read = fread(buf, 1, BUF_SIZE, in); buf_pos = 0; buf[0] = (chars_read == 0 ? -1 : buf[0]); } return cur = buf[buf_pos++]; } inline void tie(int) {} inline explicit operator bool() { return cur != -1; } inline static bool is_blank(char c) { return c <= ' '; } inline bool skip_blanks() { while (is_blank(cur) && cur != -1) { get_char(); } return cur != -1; } inline FastInput& operator>>(char& c) { skip_blanks(); c = cur; return *this; } inline FastInput& operator>>(string& s) { if (skip_blanks()) { s.clear(); do { s += cur; } while (!is_blank(get_char())); } return *this; } template <typename T> inline FastInput& read_integer(T& n) { n = 0; if (skip_blanks()) { int sign = +1; if (cur == '-') { sign = -1; get_char(); } do { n += n + (n << 3) + cur - '0'; } while (!is_blank(get_char())); n *= sign; } return *this; } template <typename T> inline typename enable_if<is_integral<T>::value, FastInput&>::type operator>>( T& n) { return read_integer(n); } inline FastInput& operator>>(__int128& n) { return read_integer(n); } template <typename T> inline typename enable_if<is_floating_point<T>::value, FastInput&>::type operator>>(T& n) { n = 0; if (skip_blanks()) { string s; (*this) >> s; sscanf(s.c_str(), "%lf", &n); } return *this; } } fast_input; static struct FastOutput { static constexpr int BUF_SIZE = 1 << 20; char buf[BUF_SIZE]; size_t buf_pos = 0; static constexpr int TMP_SIZE = 1 << 20; char tmp[TMP_SIZE]; FILE* out = stdout; inline void put_char(char c) { buf[buf_pos++] = c; if (buf_pos == BUF_SIZE) { fwrite(buf, 1, buf_pos, out); buf_pos = 0; } } ~FastOutput() { fwrite(buf, 1, buf_pos, out); } inline FastOutput& operator<<(char c) { put_char(c); return *this; } inline FastOutput& operator<<(const char* s) { while (*s) { put_char(*s++); } return *this; } inline FastOutput& operator<<(const string& s) { for (int i = 0; i < (int)s.size(); i++) { put_char(s[i]); } return *this; } template <typename T> inline char* integer_to_string(T n) { char* p = tmp + TMP_SIZE - 1; if (n == 0) { *--p = '0'; } else { bool is_negative = false; if (n < 0) { is_negative = true; n = -n; } while (n > 0) { *--p = (char)('0' + n % 10); n /= 10; } if (is_negative) { *--p = '-'; } } return p; } template <typename T> inline typename enable_if<is_integral<T>::value, char*>::type stringify(T n) { return integer_to_string(n); } inline char* stringify(__int128 n) { return integer_to_string(n); } template <typename T> inline typename enable_if<is_floating_point<T>::value, char*>::type stringify( T n) { sprintf(tmp, "%.17f", n); return tmp; } template <typename T> inline FastOutput& operator<<(const T& n) { auto p = stringify(n); for (; *p != 0; p++) { put_char(*p); } return *this; } } fast_output; const int M = 1000 * 1000 / 2 / 2 + 5; const int _ = 1005; bitset<M> dp[2][_]; vector<int> q[2][2]; int n[2], sum[2]; int a[2][_]; bool cmp(int u, int v) { return u > v; } bool solve() { if (n[0] != n[1]) return false; for (long long h = (0); h <= (1); ++h) { sum[h] = 0; for (long long i = (1); i <= (n[h]); ++i) sum[h] += a[h][i]; if (sum[h] & 1) return false; sum[h] >>= 1; for (long long i = (0); i <= (n[h]); ++i) dp[h][i].reset(); dp[h][0][0] = 1; for (long long i = (1); i <= (n[h]); ++i) dp[h][i] = dp[h][i - 1] | dp[h][i - 1] << a[h][i]; if (!dp[h][n[h]][sum[h]]) return false; q[h][0].clear(); q[h][1].clear(); int now = sum[h]; for (long long i = (n[h]); i >= (1); --i) { if (dp[h][i][now] && !dp[h][i - 1][now]) { q[h][0].push_back(a[h][i]); now -= a[h][i]; } else q[h][1].push_back(a[h][i]); } if (q[h][0].size() < q[h][1].size()) swap(q[h][0], q[h][1]); } fast_output << "Yes" << '\n'; int sw = 0; if (q[0][0].size() > q[1][0].size()) { sw = 1; swap(q[0][0], q[1][0]); swap(q[0][1], q[1][1]); } sort((q[0][0]).begin(), (q[0][0]).end(), cmp); sort((q[0][1]).begin(), (q[0][1]).end(), cmp); sort((q[1][0]).begin(), (q[1][0]).end(), cmp); sort((q[1][1]).begin(), (q[1][1]).end()); pair<int, int> now = make_pair(0, 0); for (long long i = (0); i <= ((int)q[0][0].size() - 1); ++i) { now.first -= q[0][0][i]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; now.second += q[1][0][i]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; } for (long long i = ((int)q[0][0].size()); i <= ((int)q[1][0].size() - 1); ++i) { now.first += q[0][1][i - (int)q[0][0].size()]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; now.second += q[1][0][i]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; } for (long long i = ((int)q[1][0].size()); i <= ((int)q[0][0].size() + (int)q[0][1].size() - 1); ++i) { now.first += q[0][1][i - (int)q[0][0].size()]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; now.second -= q[1][1][i - (int)q[1][0].size()]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; } return true; } signed main() { ios::sync_with_stdio(false); fast_input.tie(0); int start_time = clock(); int T; fast_input >> T; while (T--) { for (long long h = (0); h <= (1); ++h) { fast_input >> n[h]; for (long long i = (1); i <= (n[h]); ++i) fast_input >> a[h][i]; } if (!solve()) fast_output << "No" << '\n'; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; static struct FastInput { static constexpr int BUF_SIZE = 1 << 20; char buf[BUF_SIZE]; size_t chars_read = 0; size_t buf_pos = 0; FILE* in = stdin; char cur = 0; inline char get_char() { if (buf_pos >= chars_read) { chars_read = fread(buf, 1, BUF_SIZE, in); buf_pos = 0; buf[0] = (chars_read == 0 ? -1 : buf[0]); } return cur = buf[buf_pos++]; } inline void tie(int) {} inline explicit operator bool() { return cur != -1; } inline static bool is_blank(char c) { return c <= ' '; } inline bool skip_blanks() { while (is_blank(cur) && cur != -1) { get_char(); } return cur != -1; } inline FastInput& operator>>(char& c) { skip_blanks(); c = cur; return *this; } inline FastInput& operator>>(string& s) { if (skip_blanks()) { s.clear(); do { s += cur; } while (!is_blank(get_char())); } return *this; } template <typename T> inline FastInput& read_integer(T& n) { n = 0; if (skip_blanks()) { int sign = +1; if (cur == '-') { sign = -1; get_char(); } do { n += n + (n << 3) + cur - '0'; } while (!is_blank(get_char())); n *= sign; } return *this; } template <typename T> inline typename enable_if<is_integral<T>::value, FastInput&>::type operator>>( T& n) { return read_integer(n); } inline FastInput& operator>>(__int128& n) { return read_integer(n); } template <typename T> inline typename enable_if<is_floating_point<T>::value, FastInput&>::type operator>>(T& n) { n = 0; if (skip_blanks()) { string s; (*this) >> s; sscanf(s.c_str(), "%lf", &n); } return *this; } } fast_input; static struct FastOutput { static constexpr int BUF_SIZE = 1 << 20; char buf[BUF_SIZE]; size_t buf_pos = 0; static constexpr int TMP_SIZE = 1 << 20; char tmp[TMP_SIZE]; FILE* out = stdout; inline void put_char(char c) { buf[buf_pos++] = c; if (buf_pos == BUF_SIZE) { fwrite(buf, 1, buf_pos, out); buf_pos = 0; } } ~FastOutput() { fwrite(buf, 1, buf_pos, out); } inline FastOutput& operator<<(char c) { put_char(c); return *this; } inline FastOutput& operator<<(const char* s) { while (*s) { put_char(*s++); } return *this; } inline FastOutput& operator<<(const string& s) { for (int i = 0; i < (int)s.size(); i++) { put_char(s[i]); } return *this; } template <typename T> inline char* integer_to_string(T n) { char* p = tmp + TMP_SIZE - 1; if (n == 0) { *--p = '0'; } else { bool is_negative = false; if (n < 0) { is_negative = true; n = -n; } while (n > 0) { *--p = (char)('0' + n % 10); n /= 10; } if (is_negative) { *--p = '-'; } } return p; } template <typename T> inline typename enable_if<is_integral<T>::value, char*>::type stringify(T n) { return integer_to_string(n); } inline char* stringify(__int128 n) { return integer_to_string(n); } template <typename T> inline typename enable_if<is_floating_point<T>::value, char*>::type stringify( T n) { sprintf(tmp, "%.17f", n); return tmp; } template <typename T> inline FastOutput& operator<<(const T& n) { auto p = stringify(n); for (; *p != 0; p++) { put_char(*p); } return *this; } } fast_output; const int M = 1000 * 1000 / 2 / 2 + 5; const int _ = 1005; bitset<M> dp[2][_]; vector<int> q[2][2]; int n[2], sum[2]; int a[2][_]; bool cmp(int u, int v) { return u > v; } bool solve() { if (n[0] != n[1]) return false; for (long long h = (0); h <= (1); ++h) { sum[h] = 0; for (long long i = (1); i <= (n[h]); ++i) sum[h] += a[h][i]; if (sum[h] & 1) return false; sum[h] >>= 1; for (long long i = (0); i <= (n[h]); ++i) dp[h][i].reset(); dp[h][0][0] = 1; for (long long i = (1); i <= (n[h]); ++i) dp[h][i] = dp[h][i - 1] | dp[h][i - 1] << a[h][i]; if (!dp[h][n[h]][sum[h]]) return false; q[h][0].clear(); q[h][1].clear(); int now = sum[h]; for (long long i = (n[h]); i >= (1); --i) { if (dp[h][i][now] && !dp[h][i - 1][now]) { q[h][0].push_back(a[h][i]); now -= a[h][i]; } else q[h][1].push_back(a[h][i]); } if (q[h][0].size() < q[h][1].size()) swap(q[h][0], q[h][1]); } fast_output << "Yes" << '\n'; int sw = 0; if (q[0][0].size() > q[1][0].size()) { sw = 1; swap(q[0][0], q[1][0]); swap(q[0][1], q[1][1]); } sort((q[0][0]).begin(), (q[0][0]).end(), cmp); sort((q[0][1]).begin(), (q[0][1]).end(), cmp); sort((q[1][0]).begin(), (q[1][0]).end(), cmp); sort((q[1][1]).begin(), (q[1][1]).end()); pair<int, int> now = make_pair(0, 0); for (long long i = (0); i <= ((int)q[0][0].size() - 1); ++i) { now.first -= q[0][0][i]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; now.second += q[1][0][i]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; } for (long long i = ((int)q[0][0].size()); i <= ((int)q[1][0].size() - 1); ++i) { now.first += q[0][1][i - (int)q[0][0].size()]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; now.second += q[1][0][i]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; } for (long long i = ((int)q[1][0].size()); i <= ((int)q[0][0].size() + (int)q[0][1].size() - 1); ++i) { now.first += q[0][1][i - (int)q[0][0].size()]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; now.second -= q[1][1][i - (int)q[1][0].size()]; if (!sw) fast_output << now.first << ' ' << now.second << '\n'; else fast_output << now.second << ' ' << now.first << '\n'; } return true; } signed main() { ios::sync_with_stdio(false); fast_input.tie(0); int start_time = clock(); int T; fast_input >> T; while (T--) { for (long long h = (0); h <= (1); ++h) { fast_input >> n[h]; for (long long i = (1); i <= (n[h]); ++i) fast_input >> a[h][i]; } if (!solve()) fast_output << "No" << '\n'; } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-all-loops") #pragma GCC target("sse4.2") using namespace std; ifstream in; ofstream out; const long long kk = 1000; const long long ml = kk * kk; const long long mod = ml * kk + 7; const long long inf = ml * ml * ml + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long h, v; vector<long long> hor, ver; vector<pair<long long, long long>> ans; vector<vector<pair<long long, long long>>> vec; pair<long long, long long> now; bool viv = false; void show(pair<long long, long long> now) { ans.push_back(now); } void step(vector<vector<long long>> &hor, vector<vector<long long>> &ver, int h, int v) { vec.push_back({}); while (hor[h].size() && ver[v].size()) { long long x = hor[h].back(); long long y = ver[v].back(); hor[h].pop_back(); ver[v].pop_back(); vec.back().push_back({x, y}); } } vector<vector<long long>> beat(vector<long long> vec, bool &good) { int sum = 0; for (auto i : vec) sum += i; if (sum & 1) { good = false; return {}; } vector<vector<bool>> dp(vec.size() + 1, vector<bool>(sum / 2 + 1, false)); dp[0][0] = true; bool lgood = false; for (int i = 0; i < vec.size(); i++) { for (int j = 0; j <= min(vec[i] - 1, (long long)sum / 2); j++) dp[i + 1][j] = dp[i][j]; for (int j = vec[i]; j <= sum / 2; j++) dp[i + 1][j] = dp[i][j] | dp[i][j - vec[i]]; if (dp[i + 1].back()) lgood = true; } if (!lgood) { good = false; return {}; } vector<vector<long long>> res(2); int need = sum / 2; for (int i = vec.size(); i > 0; i--) { int pl = 0; if (dp[i][need] && !dp[i - 1][need]) { need -= vec[i - 1]; pl = 1; } res[pl].push_back(vec[i - 1]); } return res; } void solve() { cin >> h; hor.resize(h); for (auto &i : hor) cin >> i; cin >> v; ver.resize(v); for (auto &i : ver) cin >> i; sort(hor.begin(), hor.end()); sort(ver.begin(), ver.end()); vec.clear(); ans.clear(); bool good = true; auto seg_h = beat(hor, good); auto seg_v = beat(ver, good); if (!good || hor.size() != ver.size()) { cout << "NO\n"; return; } if (seg_h[0].size() > seg_h[1].size()) swap(seg_h[0], seg_h[1]); if (seg_v[0].size() < seg_v[1].size()) swap(seg_v[0], seg_v[1]); step(seg_h, seg_v, 0, 0); step(seg_h, seg_v, 0, 1); step(seg_h, seg_v, 1, 0); step(seg_h, seg_v, 1, 1); sort(vec[0].begin(), vec[0].end(), [&](const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second * b.first < b.second * a.first; }); sort(vec[3].begin(), vec[3].end(), [&](const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second * b.first < b.second * a.first; }); now = {0, 0}; for (int i = 0; i < 4; i++) { for (auto p : vec[i]) { long long x = p.first; long long y = p.second; if ((i >> 1) & 1) x = -x; if ((i >> 0) & 1) y = -y; now.first += x; show(now); now.second += y; show(now); } } cout << "YES\n"; for (auto p : ans) { cout << p.first << ' ' << p.second << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) solve(); return 0; }
### Prompt Please formulate a CPP solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3", "unroll-all-loops") #pragma GCC target("sse4.2") using namespace std; ifstream in; ofstream out; const long long kk = 1000; const long long ml = kk * kk; const long long mod = ml * kk + 7; const long long inf = ml * ml * ml + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long h, v; vector<long long> hor, ver; vector<pair<long long, long long>> ans; vector<vector<pair<long long, long long>>> vec; pair<long long, long long> now; bool viv = false; void show(pair<long long, long long> now) { ans.push_back(now); } void step(vector<vector<long long>> &hor, vector<vector<long long>> &ver, int h, int v) { vec.push_back({}); while (hor[h].size() && ver[v].size()) { long long x = hor[h].back(); long long y = ver[v].back(); hor[h].pop_back(); ver[v].pop_back(); vec.back().push_back({x, y}); } } vector<vector<long long>> beat(vector<long long> vec, bool &good) { int sum = 0; for (auto i : vec) sum += i; if (sum & 1) { good = false; return {}; } vector<vector<bool>> dp(vec.size() + 1, vector<bool>(sum / 2 + 1, false)); dp[0][0] = true; bool lgood = false; for (int i = 0; i < vec.size(); i++) { for (int j = 0; j <= min(vec[i] - 1, (long long)sum / 2); j++) dp[i + 1][j] = dp[i][j]; for (int j = vec[i]; j <= sum / 2; j++) dp[i + 1][j] = dp[i][j] | dp[i][j - vec[i]]; if (dp[i + 1].back()) lgood = true; } if (!lgood) { good = false; return {}; } vector<vector<long long>> res(2); int need = sum / 2; for (int i = vec.size(); i > 0; i--) { int pl = 0; if (dp[i][need] && !dp[i - 1][need]) { need -= vec[i - 1]; pl = 1; } res[pl].push_back(vec[i - 1]); } return res; } void solve() { cin >> h; hor.resize(h); for (auto &i : hor) cin >> i; cin >> v; ver.resize(v); for (auto &i : ver) cin >> i; sort(hor.begin(), hor.end()); sort(ver.begin(), ver.end()); vec.clear(); ans.clear(); bool good = true; auto seg_h = beat(hor, good); auto seg_v = beat(ver, good); if (!good || hor.size() != ver.size()) { cout << "NO\n"; return; } if (seg_h[0].size() > seg_h[1].size()) swap(seg_h[0], seg_h[1]); if (seg_v[0].size() < seg_v[1].size()) swap(seg_v[0], seg_v[1]); step(seg_h, seg_v, 0, 0); step(seg_h, seg_v, 0, 1); step(seg_h, seg_v, 1, 0); step(seg_h, seg_v, 1, 1); sort(vec[0].begin(), vec[0].end(), [&](const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second * b.first < b.second * a.first; }); sort(vec[3].begin(), vec[3].end(), [&](const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second * b.first < b.second * a.first; }); now = {0, 0}; for (int i = 0; i < 4; i++) { for (auto p : vec[i]) { long long x = p.first; long long y = p.second; if ((i >> 1) & 1) x = -x; if ((i >> 0) & 1) y = -y; now.first += x; show(now); now.second += y; show(now); } } cout << "YES\n"; for (auto p : ans) { cout << p.first << ' ' << p.second << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T> void ckmn(T& a, T b) { a = min(a, b); } template <typename T> void ckmx(T& a, T b) { a = max(a, b); } void rd(int& x) { scanf("%i", &x); } void rd(long long& x) { scanf("%lld", &x); } void rd(char* x) { scanf("%s", x); } void rd(double& x) { scanf("%lf", &x); } void rd(string& x) { scanf("%s", &x); } template <typename T1, typename T2> void rd(pair<T1, T2>& x) { rd(x.first); rd(x.second); } template <typename T> void rd(vector<T>& x) { for (T& i : x) rd(i); } template <typename T, typename... A> void rd(T& x, A&... args) { rd(x); rd(args...); } template <typename T> void rd() { T x; rd(x); return x; } int ri() { int x; rd(x); return x; } template <typename T> vector<T> rv(int n) { vector<T> x(n); rd(x); return x; } template <typename T> void ra(T a[], int n, int st = 1) { for (int i = 0; i < n; ++i) rd(a[st + i]); } template <typename T1, typename T2> void ra(T1 a[], T2 b[], int n, int st = 1) { for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]); } template <typename T1, typename T2, typename T3> void ra(T1 a[], T2 b[], T3 c[], int n, int st = 1) { for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]), rd(c[st + i]); } void re(vector<int> E[], int m, bool dir = 0) { for (int i = 0, u, v; i < m; ++i) { rd(u, v); E[u].push_back(v); if (!dir) E[v].push_back(u); } } template <typename T> void re(vector<pair<int, T>> E[], int m, bool dir = 0) { for (int i = 0, u, v; i < m; ++i) { T w; rd(u, v, w); E[u].push_back({v, w}); if (!dir) E[v].push_back({u, w}); } } const int N = 1005; const int M = N * N; bitset<M> dp[N]; bool Divide(vector<int> a, vector<int>& l, vector<int>& r) { int sum = 0; for (int i : a) sum += i; if (sum & 1) return 0; dp[0].reset(); dp[0][0] = 1; for (int i = 1; i <= a.size(); i++) { dp[i] = dp[i - 1] | (dp[i - 1] << a[i - 1]); } if (!dp[a.size()][sum / 2]) return 0; l.clear(); r.clear(); sum /= 2; for (int i = a.size(); i > 0; i--) { if (dp[i - 1][sum]) l.push_back(a[i - 1]); else r.push_back(a[i - 1]), sum -= a[i - 1]; } return 1; } int main() { for (int t = ri(); t--;) { int n = ri(); auto a = rv<int>(n); int m = ri(); auto b = rv<int>(m); if (n == m) { vector<int> D, U, L, R; bool ok = Divide(a, D, U) && Divide(b, L, R); if (ok) { if (U.size() < D.size()) swap(U, D); if (L.size() > R.size()) swap(L, R); sort(U.rbegin(), U.rend()); sort(D.rbegin(), D.rend()); sort(L.begin(), L.end()); sort(R.begin(), R.end()); printf("Yes\n"); pair<int, int> now = {0, 0}; int i = 0, j = 0; for (int z = 0; z < n; z++) { if (i < D.size()) now.first += D[i++]; else now.first -= U[i - D.size()], i++; printf("%i %i\n", now.first, now.second); if (j < R.size()) now.second += R[j++]; else now.second -= L[j - R.size()], j++; printf("%i %i\n", now.first, now.second); } } else printf("No\n"); } else printf("No\n"); } return 0; }
### Prompt Create a solution in cpp for the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T> void ckmn(T& a, T b) { a = min(a, b); } template <typename T> void ckmx(T& a, T b) { a = max(a, b); } void rd(int& x) { scanf("%i", &x); } void rd(long long& x) { scanf("%lld", &x); } void rd(char* x) { scanf("%s", x); } void rd(double& x) { scanf("%lf", &x); } void rd(string& x) { scanf("%s", &x); } template <typename T1, typename T2> void rd(pair<T1, T2>& x) { rd(x.first); rd(x.second); } template <typename T> void rd(vector<T>& x) { for (T& i : x) rd(i); } template <typename T, typename... A> void rd(T& x, A&... args) { rd(x); rd(args...); } template <typename T> void rd() { T x; rd(x); return x; } int ri() { int x; rd(x); return x; } template <typename T> vector<T> rv(int n) { vector<T> x(n); rd(x); return x; } template <typename T> void ra(T a[], int n, int st = 1) { for (int i = 0; i < n; ++i) rd(a[st + i]); } template <typename T1, typename T2> void ra(T1 a[], T2 b[], int n, int st = 1) { for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]); } template <typename T1, typename T2, typename T3> void ra(T1 a[], T2 b[], T3 c[], int n, int st = 1) { for (int i = 0; i < n; ++i) rd(a[st + i]), rd(b[st + i]), rd(c[st + i]); } void re(vector<int> E[], int m, bool dir = 0) { for (int i = 0, u, v; i < m; ++i) { rd(u, v); E[u].push_back(v); if (!dir) E[v].push_back(u); } } template <typename T> void re(vector<pair<int, T>> E[], int m, bool dir = 0) { for (int i = 0, u, v; i < m; ++i) { T w; rd(u, v, w); E[u].push_back({v, w}); if (!dir) E[v].push_back({u, w}); } } const int N = 1005; const int M = N * N; bitset<M> dp[N]; bool Divide(vector<int> a, vector<int>& l, vector<int>& r) { int sum = 0; for (int i : a) sum += i; if (sum & 1) return 0; dp[0].reset(); dp[0][0] = 1; for (int i = 1; i <= a.size(); i++) { dp[i] = dp[i - 1] | (dp[i - 1] << a[i - 1]); } if (!dp[a.size()][sum / 2]) return 0; l.clear(); r.clear(); sum /= 2; for (int i = a.size(); i > 0; i--) { if (dp[i - 1][sum]) l.push_back(a[i - 1]); else r.push_back(a[i - 1]), sum -= a[i - 1]; } return 1; } int main() { for (int t = ri(); t--;) { int n = ri(); auto a = rv<int>(n); int m = ri(); auto b = rv<int>(m); if (n == m) { vector<int> D, U, L, R; bool ok = Divide(a, D, U) && Divide(b, L, R); if (ok) { if (U.size() < D.size()) swap(U, D); if (L.size() > R.size()) swap(L, R); sort(U.rbegin(), U.rend()); sort(D.rbegin(), D.rend()); sort(L.begin(), L.end()); sort(R.begin(), R.end()); printf("Yes\n"); pair<int, int> now = {0, 0}; int i = 0, j = 0; for (int z = 0; z < n; z++) { if (i < D.size()) now.first += D[i++]; else now.first -= U[i - D.size()], i++; printf("%i %i\n", now.first, now.second); if (j < R.size()) now.second += R[j++]; else now.second -= L[j - R.size()], j++; printf("%i %i\n", now.first, now.second); } } else printf("No\n"); } else printf("No\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 3e6 + 4; const long long MOD = 998244353; const long long MX = (long long)1e18; int a[N]; int nax[N]; bool col[N], dp[N]; bool solve(int n, vector<int> &b1, vector<int> &b2) { memset(dp, 0, sizeof dp); memset(nax, 0, sizeof nax); memset(col, 0, sizeof col); dp[0] = true; int sum = 0; for (int i = 0; i < (n); ++i) { cin >> a[i]; sum += a[i]; } if (sum % 2) return false; for (int i = 0; i < (n); ++i) { for (int j = (sum / 2); j >= (a[i]); --j) { if (dp[j - a[i]] && !dp[j]) { dp[j] = true; nax[j] = i; } } } if (!dp[sum / 2]) return false; int tv = sum / 2; while (tv) { col[nax[tv]] = true; tv -= a[nax[tv]]; } for (int i = 0; i < (n); ++i) { if (col[i]) b1.push_back(a[i]); else b2.push_back(a[i]); } return true; } int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ios_base::sync_with_stdio(false); cin.tie(0); ; int ttt; cin >> ttt; while (ttt--) { vector<int> up, down, left, right; memset(dp, 0, sizeof dp); int n; cin >> n; dp[0] = 1; if (!solve(n, right, left)) { int m; cin >> m; for (int i = 0; i < (m); ++i) { int tv; cin >> tv; } cout << "NO" << endl; continue; } int m; cin >> m; if (n != m) { for (int i = 0; i < (m); ++i) { int tv; cin >> tv; } cout << "NO" << endl; continue; } if (!solve(m, up, down)) { cout << "NO" << endl; continue; } sort(up.begin(), up.end()); sort(down.begin(), down.end()); sort(left.begin(), left.end()); sort(right.begin(), right.end()); reverse(left.begin(), left.end()); reverse(right.begin(), right.end()); if (up.size() > down.size()) swap(up, down); if (right.size() < left.size()) swap(left, right); int x = 0, y = 0; cout << "YES" << endl; for (int i = 0; i < (n); ++i) { cout << x << " " << y << endl; if (up.empty()) { y -= down.back(); down.pop_back(); } else { y += up.back(); up.pop_back(); } cout << x << " " << y << endl; if (right.empty()) { x -= left.back(); left.pop_back(); } else { x += right.back(); right.pop_back(); } } } return 0; }
### Prompt In CPP, your task is to solve the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 3e6 + 4; const long long MOD = 998244353; const long long MX = (long long)1e18; int a[N]; int nax[N]; bool col[N], dp[N]; bool solve(int n, vector<int> &b1, vector<int> &b2) { memset(dp, 0, sizeof dp); memset(nax, 0, sizeof nax); memset(col, 0, sizeof col); dp[0] = true; int sum = 0; for (int i = 0; i < (n); ++i) { cin >> a[i]; sum += a[i]; } if (sum % 2) return false; for (int i = 0; i < (n); ++i) { for (int j = (sum / 2); j >= (a[i]); --j) { if (dp[j - a[i]] && !dp[j]) { dp[j] = true; nax[j] = i; } } } if (!dp[sum / 2]) return false; int tv = sum / 2; while (tv) { col[nax[tv]] = true; tv -= a[nax[tv]]; } for (int i = 0; i < (n); ++i) { if (col[i]) b1.push_back(a[i]); else b2.push_back(a[i]); } return true; } int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ios_base::sync_with_stdio(false); cin.tie(0); ; int ttt; cin >> ttt; while (ttt--) { vector<int> up, down, left, right; memset(dp, 0, sizeof dp); int n; cin >> n; dp[0] = 1; if (!solve(n, right, left)) { int m; cin >> m; for (int i = 0; i < (m); ++i) { int tv; cin >> tv; } cout << "NO" << endl; continue; } int m; cin >> m; if (n != m) { for (int i = 0; i < (m); ++i) { int tv; cin >> tv; } cout << "NO" << endl; continue; } if (!solve(m, up, down)) { cout << "NO" << endl; continue; } sort(up.begin(), up.end()); sort(down.begin(), down.end()); sort(left.begin(), left.end()); sort(right.begin(), right.end()); reverse(left.begin(), left.end()); reverse(right.begin(), right.end()); if (up.size() > down.size()) swap(up, down); if (right.size() < left.size()) swap(left, right); int x = 0, y = 0; cout << "YES" << endl; for (int i = 0; i < (n); ++i) { cout << x << " " << y << endl; if (up.empty()) { y -= down.back(); down.pop_back(); } else { y += up.back(); up.pop_back(); } cout << x << " " << y << endl; if (right.empty()) { x -= left.back(); left.pop_back(); } else { x += right.back(); right.pop_back(); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int T, n, m, a[maxn], b[maxn], p[4][maxn], len[4]; pair<int, int> ans[maxn]; bitset<1000010> f[1010]; inline pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { return pair<int, int>(a.first + b.first, a.second + b.second); } int main() { scanf("%d", &T); while (T--) { a[0] = b[0] = 0; memset((len), 0, sizeof(len)); scanf("%d", &n); for (int i = (1), _end_ = (n); i <= _end_; ++i) { scanf("%d", &a[i]); a[0] += a[i]; } scanf("%d", &m); for (int i = (1), _end_ = (m); i <= _end_; ++i) { scanf("%d", &b[i]); b[0] += b[i]; } if (n != m || a[0] % 2 != 0 || b[0] % 2 != 0) { puts("No"); continue; } memset((f), 0, sizeof(f)); f[0][0] = 1; for (int i = (1), _end_ = (n); i <= _end_; ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]); int sum = a[0] / 2; if (!f[n][sum]) { puts("No"); continue; } for (int i = n; i >= 1; --i) if (sum >= a[i] && f[i - 1][sum - a[i]]) { sum -= a[i]; p[0][++len[0]] = a[i]; } else p[1][++len[1]] = a[i]; memset((f), 0, sizeof(f)); f[0][0] = 1; for (int i = (1), _end_ = (n); i <= _end_; ++i) f[i] = f[i - 1] | (f[i - 1] << b[i]); sum = b[0] / 2; if (!f[n][sum]) { puts("No"); continue; } for (int i = n; i >= 1; --i) if (sum >= b[i] && f[i - 1][sum - b[i]]) { sum -= b[i]; p[2][++len[2]] = b[i]; } else p[3][++len[3]] = b[i]; for (int i = (0), _end_ = (3); i <= _end_; ++i) sort(p[i] + 1, p[i] + len[i] + 1); bool rev = false; if (len[0] > len[2]) { rev = true; swap(len[0], len[2]); swap(len[1], len[3]); swap(p[0], p[2]); swap(p[1], p[3]); } for (int i = (1), _end_ = (len[1]); i <= _end_; ++i) p[1][i] = -p[1][i]; for (int i = (1), _end_ = (len[3]); i <= _end_; ++i) p[3][i] = -p[3][i]; pair<int, int> now = pair<int, int>(0, 0); m = 0; int k = 1, t = 1; for (int i = len[0]; i >= 1; --i) { now = now + (rev ? pair<int, int>(0, p[0][i]) : pair<int, int>(p[0][i], 0)); ans[++m] = now; now = now + (rev ? pair<int, int>(p[2][k], 0) : pair<int, int>(0, p[2][k])); ans[++m] = now; ++k; } while (k <= len[2]) { now = now + (rev ? pair<int, int>(0, p[1][t]) : pair<int, int>(p[1][t], 0)); ans[++m] = now; ++t; now = now + (rev ? pair<int, int>(p[2][k], 0) : pair<int, int>(0, p[2][k])); ans[++m] = now; ++k; } t = len[1]; for (int i = (1), _end_ = (len[3]); i <= _end_; ++i) { now = now + (rev ? pair<int, int>(0, p[1][t]) : pair<int, int>(p[1][t], 0)); ans[++m] = now; --t; now = now + (rev ? pair<int, int>(p[3][i], 0) : pair<int, int>(0, p[3][i])); ans[++m] = now; } puts("Yes"); for (int i = (1), _end_ = (m); i <= _end_; ++i) printf("%d %d\n", ans[i].first, ans[i].second); } return 0; }
### Prompt Develop a solution in CPP to the problem described below: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int T, n, m, a[maxn], b[maxn], p[4][maxn], len[4]; pair<int, int> ans[maxn]; bitset<1000010> f[1010]; inline pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { return pair<int, int>(a.first + b.first, a.second + b.second); } int main() { scanf("%d", &T); while (T--) { a[0] = b[0] = 0; memset((len), 0, sizeof(len)); scanf("%d", &n); for (int i = (1), _end_ = (n); i <= _end_; ++i) { scanf("%d", &a[i]); a[0] += a[i]; } scanf("%d", &m); for (int i = (1), _end_ = (m); i <= _end_; ++i) { scanf("%d", &b[i]); b[0] += b[i]; } if (n != m || a[0] % 2 != 0 || b[0] % 2 != 0) { puts("No"); continue; } memset((f), 0, sizeof(f)); f[0][0] = 1; for (int i = (1), _end_ = (n); i <= _end_; ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]); int sum = a[0] / 2; if (!f[n][sum]) { puts("No"); continue; } for (int i = n; i >= 1; --i) if (sum >= a[i] && f[i - 1][sum - a[i]]) { sum -= a[i]; p[0][++len[0]] = a[i]; } else p[1][++len[1]] = a[i]; memset((f), 0, sizeof(f)); f[0][0] = 1; for (int i = (1), _end_ = (n); i <= _end_; ++i) f[i] = f[i - 1] | (f[i - 1] << b[i]); sum = b[0] / 2; if (!f[n][sum]) { puts("No"); continue; } for (int i = n; i >= 1; --i) if (sum >= b[i] && f[i - 1][sum - b[i]]) { sum -= b[i]; p[2][++len[2]] = b[i]; } else p[3][++len[3]] = b[i]; for (int i = (0), _end_ = (3); i <= _end_; ++i) sort(p[i] + 1, p[i] + len[i] + 1); bool rev = false; if (len[0] > len[2]) { rev = true; swap(len[0], len[2]); swap(len[1], len[3]); swap(p[0], p[2]); swap(p[1], p[3]); } for (int i = (1), _end_ = (len[1]); i <= _end_; ++i) p[1][i] = -p[1][i]; for (int i = (1), _end_ = (len[3]); i <= _end_; ++i) p[3][i] = -p[3][i]; pair<int, int> now = pair<int, int>(0, 0); m = 0; int k = 1, t = 1; for (int i = len[0]; i >= 1; --i) { now = now + (rev ? pair<int, int>(0, p[0][i]) : pair<int, int>(p[0][i], 0)); ans[++m] = now; now = now + (rev ? pair<int, int>(p[2][k], 0) : pair<int, int>(0, p[2][k])); ans[++m] = now; ++k; } while (k <= len[2]) { now = now + (rev ? pair<int, int>(0, p[1][t]) : pair<int, int>(p[1][t], 0)); ans[++m] = now; ++t; now = now + (rev ? pair<int, int>(p[2][k], 0) : pair<int, int>(0, p[2][k])); ans[++m] = now; ++k; } t = len[1]; for (int i = (1), _end_ = (len[3]); i <= _end_; ++i) { now = now + (rev ? pair<int, int>(0, p[1][t]) : pair<int, int>(p[1][t], 0)); ans[++m] = now; --t; now = now + (rev ? pair<int, int>(p[3][i], 0) : pair<int, int>(0, p[3][i])); ans[++m] = now; } puts("Yes"); for (int i = (1), _end_ = (m); i <= _end_; ++i) printf("%d %d\n", ans[i].first, ans[i].second); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1005, U = 1e6 + 5; bitset<U> b[N]; int T, n, i, j, a[N]; vector<int> v1, v2, v3, v4; bool FL; inline void input(vector<int>& v1, vector<int>& v2) { v1.clear(); v2.clear(); scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d", a + i); random_shuffle(a + 1, a + n + 1); for (i = 0; i <= n; ++i) b[i].reset(); b[0][U / 2] = 1; for (i = 1; i <= n; ++i) b[i] = (b[i - 1] << a[i]) | (b[i - 1] >> a[i]); if (!b[n][U / 2]) { FL = 0; return; } for (i = n, j = U / 2; i; --i) if (0 <= j - a[i] && j - a[i] < U && b[i - 1][j - a[i]]) j -= a[i], v1.push_back(a[i]); else j += a[i], v2.push_back(a[i]); if (v1.size() > v2.size()) swap(v1, v2); } int main() { srand(114514); for (scanf("%d", &T); T--;) { FL = 1; input(v1, v2); input(v3, v4); if (v1.size() + v2.size() != v3.size() + v4.size()) FL = 0; if (!FL) { puts("No"); continue; } sort(v1.begin(), v1.end(), greater<int>()); sort(v2.begin(), v2.end(), greater<int>()); sort(v3.begin(), v3.end()); sort(v4.begin(), v4.end()); puts("Yes"); int x = 0, y = 0; for (i = 0; i < v1.size(); ++i) { printf("%d %d\n", x += v1[i], y); printf("%d %d\n", x, y += v4[i]); } for (j = 0; i < v4.size(); ++i, ++j) { printf("%d %d\n", x -= v2[j], y); printf("%d %d\n", x, y += v4[i]); } for (i = 0; j < v2.size(); ++j, ++i) { printf("%d %d\n", x -= v2[j], y); printf("%d %d\n", x, y -= v3[i]); } } }
### Prompt Construct a cpp code solution to the problem outlined: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1005, U = 1e6 + 5; bitset<U> b[N]; int T, n, i, j, a[N]; vector<int> v1, v2, v3, v4; bool FL; inline void input(vector<int>& v1, vector<int>& v2) { v1.clear(); v2.clear(); scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d", a + i); random_shuffle(a + 1, a + n + 1); for (i = 0; i <= n; ++i) b[i].reset(); b[0][U / 2] = 1; for (i = 1; i <= n; ++i) b[i] = (b[i - 1] << a[i]) | (b[i - 1] >> a[i]); if (!b[n][U / 2]) { FL = 0; return; } for (i = n, j = U / 2; i; --i) if (0 <= j - a[i] && j - a[i] < U && b[i - 1][j - a[i]]) j -= a[i], v1.push_back(a[i]); else j += a[i], v2.push_back(a[i]); if (v1.size() > v2.size()) swap(v1, v2); } int main() { srand(114514); for (scanf("%d", &T); T--;) { FL = 1; input(v1, v2); input(v3, v4); if (v1.size() + v2.size() != v3.size() + v4.size()) FL = 0; if (!FL) { puts("No"); continue; } sort(v1.begin(), v1.end(), greater<int>()); sort(v2.begin(), v2.end(), greater<int>()); sort(v3.begin(), v3.end()); sort(v4.begin(), v4.end()); puts("Yes"); int x = 0, y = 0; for (i = 0; i < v1.size(); ++i) { printf("%d %d\n", x += v1[i], y); printf("%d %d\n", x, y += v4[i]); } for (j = 0; i < v4.size(); ++i, ++j) { printf("%d %d\n", x -= v2[j], y); printf("%d %d\n", x, y += v4[i]); } for (i = 0; j < v2.size(); ++j, ++i) { printf("%d %d\n", x -= v2[j], y); printf("%d %d\n", x, y -= v3[i]); } } } ```
#include <bits/stdc++.h> char ibuf[1 << 15], *p1, *p2; struct { inline operator int() { register char c, f = 0; while ((c = (p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 15, stdin), p1 == p2) ? EOF : *p1++)) < 48 || c > 57) if (c == '-') f = 1; register int unsigned a = c & 15; while ((c = (p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 15, stdin), p1 == p2) ? EOF : *p1++)) >= 48 && c <= 57) a = a * 10 + (c & 15); return f ? ~a + 1 : a; } } g90; const int N = 1010; int l[N], p[N]; std::bitset<N * N / 2> f[N / 2]; unsigned int h; inline int init(const int* a, int sum, std::vector<int>* ans) { for (int i = 1; i <= h; i++) f[i] = f[i - 1] | f[i - 1] << a[i]; sum >>= 1; if (!f[h][sum]) return 0; for (int i = h; i; i--) { if (sum - a[i] >= 0 && f[i - 1][sum - a[i]]) sum -= a[i], ans[1].push_back(a[i]); else ans[0].push_back(a[i]); } assert(!sum); return 1; } int ansx[N], ansy[N]; inline void solve(std::vector<int>* x, std::vector<int>* y, int* ansx, int* ansy) { std::sort(x[0].begin(), x[0].end(), std::greater<int>()); std::sort(x[1].begin(), x[1].end(), std::greater<int>()); std::sort(y[0].begin(), y[0].end(), std::less<int>()); std::sort(y[1].begin(), y[1].end(), std::less<int>()); int nowx = 0, nowy = 0, i[2] = {0, 0}, j[2] = {0, 0}; for (int hh = 1; hh <= h * 2; hh++) { if (hh & 1) { if (i[1] < x[1].size()) nowx += x[1][i[1]++]; else nowx -= x[0][i[0]++]; } else { if (j[1] < y[1].size()) nowy += y[1][j[1]++]; else nowy -= y[0][j[0]++]; } ansx[hh] = nowx, ansy[hh] = nowy; } } int main() { f[0].set(0); for (int T = g90; T--;) { h = g90; unsigned sl = 0; for (int i = 1; i <= h; i++) l[i] = g90, sl += l[i]; unsigned v = g90, sp = 0; for (int i = 1; i <= v; i++) p[i] = g90, sp += p[i]; std::vector<int> x[2], y[2]; if (h ^ v || sl & 1 || sp & 1) goto _fail; if (!init(l, sl, x) || !init(p, sp, y)) goto _fail; if (x[1].size() <= y[1].size()) solve(x, y, ansx, ansy); else solve(y, x, ansy, ansx); puts("Yes"); for (int i = 1; i <= v + h; i++) printf("%d %d\n", ansx[i], ansy[i]); continue; _fail: puts("No"); } }
### Prompt In cpp, your task is to solve the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> char ibuf[1 << 15], *p1, *p2; struct { inline operator int() { register char c, f = 0; while ((c = (p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 15, stdin), p1 == p2) ? EOF : *p1++)) < 48 || c > 57) if (c == '-') f = 1; register int unsigned a = c & 15; while ((c = (p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 15, stdin), p1 == p2) ? EOF : *p1++)) >= 48 && c <= 57) a = a * 10 + (c & 15); return f ? ~a + 1 : a; } } g90; const int N = 1010; int l[N], p[N]; std::bitset<N * N / 2> f[N / 2]; unsigned int h; inline int init(const int* a, int sum, std::vector<int>* ans) { for (int i = 1; i <= h; i++) f[i] = f[i - 1] | f[i - 1] << a[i]; sum >>= 1; if (!f[h][sum]) return 0; for (int i = h; i; i--) { if (sum - a[i] >= 0 && f[i - 1][sum - a[i]]) sum -= a[i], ans[1].push_back(a[i]); else ans[0].push_back(a[i]); } assert(!sum); return 1; } int ansx[N], ansy[N]; inline void solve(std::vector<int>* x, std::vector<int>* y, int* ansx, int* ansy) { std::sort(x[0].begin(), x[0].end(), std::greater<int>()); std::sort(x[1].begin(), x[1].end(), std::greater<int>()); std::sort(y[0].begin(), y[0].end(), std::less<int>()); std::sort(y[1].begin(), y[1].end(), std::less<int>()); int nowx = 0, nowy = 0, i[2] = {0, 0}, j[2] = {0, 0}; for (int hh = 1; hh <= h * 2; hh++) { if (hh & 1) { if (i[1] < x[1].size()) nowx += x[1][i[1]++]; else nowx -= x[0][i[0]++]; } else { if (j[1] < y[1].size()) nowy += y[1][j[1]++]; else nowy -= y[0][j[0]++]; } ansx[hh] = nowx, ansy[hh] = nowy; } } int main() { f[0].set(0); for (int T = g90; T--;) { h = g90; unsigned sl = 0; for (int i = 1; i <= h; i++) l[i] = g90, sl += l[i]; unsigned v = g90, sp = 0; for (int i = 1; i <= v; i++) p[i] = g90, sp += p[i]; std::vector<int> x[2], y[2]; if (h ^ v || sl & 1 || sp & 1) goto _fail; if (!init(l, sl, x) || !init(p, sp, y)) goto _fail; if (x[1].size() <= y[1].size()) solve(x, y, ansx, ansy); else solve(y, x, ansy, ansx); puts("Yes"); for (int i = 1; i <= v + h; i++) printf("%d %d\n", ansx[i], ansy[i]); continue; _fail: puts("No"); } } ```
#include <bits/stdc++.h> using namespace std; int T, n, m, a[1100], b[1100], c[1100], d[1100], tmp[1100]; bitset<1000005> dp[1005]; bool work(int *a, int s, int *b) { dp[0].reset(); dp[0][0] = 1; for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[n][s]) return false; int len = 0, len2 = 0; for (int i = n; i >= 1; i--) if (s >= a[i] && dp[i - 1][s - a[i]]) b[++len] = a[i], s -= a[i]; else tmp[++len2] = -a[i]; for (int i = 1; i <= len2; i++) b[++len] = tmp[i]; return true; } int main() { scanf("%d", &T); int p, q; while (T--) { p = 0; q = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p += a[i]; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]), q += b[i]; if ((n != m) || (p & 1) || (q & 1)) { puts("No"); continue; } p >>= 1; q >>= 1; sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); if (!work(a, p, c) || !work(b, q, d)) { puts("No"); continue; } puts("Yes"); reverse(d + 1, d + n + 1); p = 0; q = 0; for (int i = 1; i <= n; i++) { p += c[i]; printf("%d %d\n", p, q); q += d[i]; printf("%d %d\n", p, q); } } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; int T, n, m, a[1100], b[1100], c[1100], d[1100], tmp[1100]; bitset<1000005> dp[1005]; bool work(int *a, int s, int *b) { dp[0].reset(); dp[0][0] = 1; for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[n][s]) return false; int len = 0, len2 = 0; for (int i = n; i >= 1; i--) if (s >= a[i] && dp[i - 1][s - a[i]]) b[++len] = a[i], s -= a[i]; else tmp[++len2] = -a[i]; for (int i = 1; i <= len2; i++) b[++len] = tmp[i]; return true; } int main() { scanf("%d", &T); int p, q; while (T--) { p = 0; q = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p += a[i]; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]), q += b[i]; if ((n != m) || (p & 1) || (q & 1)) { puts("No"); continue; } p >>= 1; q >>= 1; sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); if (!work(a, p, c) || !work(b, q, d)) { puts("No"); continue; } puts("Yes"); reverse(d + 1, d + n + 1); p = 0; q = 0; for (int i = 1; i <= n; i++) { p += c[i]; printf("%d %d\n", p, q); q += d[i]; printf("%d %d\n", p, q); } } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3", "fast-math") #pragma GCC target("tune=native,avx,avx2") using namespace std; constexpr int kN = int(1E3 + 10), kW = int(25E4 + 10); short l[kN], p[kN]; bitset<kW> posh[kN >> 2], posv[kN >> 2]; bitset<kN> bs; void malay_dp(short n, unsigned char k, int s, int kkW, vector<short> &v1, vector<short> &v2, short *a) { pair<short, pair<short, int>> now; vector<vector<unsigned char>> dp[2]; vector<vector<short>> from[2]; dp[0] = vector<vector<unsigned char>>(n - k + 1, vector<unsigned char>(kkW)); dp[1] = vector<vector<unsigned char>>(n - k + 1, vector<unsigned char>(kkW)); from[0] = vector<vector<short>>(n - k + 1, vector<short>(kkW)); from[1] = vector<vector<short>>(n - k + 1, vector<short>(kkW)); short nxt; int tot = 0; for (int i = 1; i <= k; i++) tot += a[i]; for (int i = 0; i <= n - k; i++) for (int j = 0; j < kkW; j++) dp[0][i][j] = dp[1][i][j] = k + 1; dp[0][0][tot] = 0; for (int i = 0; i <= n - k; i++) { for (int j = 0; j < kkW; j++) if (dp[1][i][j] <= k) for (int p = dp[1][i][j] + 1; p <= min(dp[1][i - 1][j], k); p++) if (j >= a[p] && dp[0][i][j - a[p]] > p) { dp[0][i][j - a[p]] = p; from[0][i][j - a[p]] = i + 4000 + k; } if (i < n - k) for (int j = 0; j < kkW; j++) if (dp[0][i][j] <= k) { if (dp[0][i + 1][j] > dp[0][i][j]) { dp[0][i + 1][j] = dp[0][i][j]; from[0][i + 1][j] = i + 2000 + k; } if (j + a[i + 1 + k] < kkW && dp[1][i + 1][j + a[i + 1 + k]] > dp[0][i][j]) { dp[1][i + 1][j + a[i + 1 + k]] = dp[0][i][j]; from[1][i + 1][j + a[i + 1 + k]] = i + k; } } } now = {0, {n, s}}; bs.reset(); for (int i = 1; i <= k; i++) bs[i] = true; while (now.second.first > k) { nxt = from[now.first][now.second.first - k][now.second.second]; if (nxt >= 4000) { nxt -= 4000; bs[dp[now.first][now.second.first - k][now.second.second]] = false; now = { 1, {nxt, now.second.second + a[dp[now.first][now.second.first - k][now.second.second]]}}; } else if (nxt >= 2000) now = {0, {nxt - 2000, now.second.second}}; else { bs[now.second.first] = true; now = {0, {now.second.first - 1, now.second.second - a[now.second.first]}}; } } for (int i = 1; i <= n; i++) if (bs[i]) v1.push_back(a[i]); else v2.push_back(a[i]); return; } void solve() { int h, v, x = 0, y = 0, toth = 0, totv = 0; vector<short> hA, hB, vA, vB, H, V; scanf("%d", &h); for (int i = 1; i <= h; i++) scanf("%hd", &l[i]); scanf("%d", &v); for (int i = 1; i <= v; i++) scanf("%hd", &p[i]); if (h != v) goto No; for (int i = 1; i <= h; i++) toth += l[i]; for (int i = 1; i <= v; i++) totv += p[i]; if ((toth & 1) || (totv & 1)) goto No; posh[0][0] = posv[0][0] = true; for (int i = 1; i <= h / 2; i++) posh[i].reset(); for (int i = 1; i <= h; i++) for (int j = min(h / 2, i); j >= 1; j--) posh[j] |= posh[j - 1] << l[i]; for (int i = 1; i <= v / 2; i++) posv[i].reset(); for (int i = 1; i <= v; i++) for (int j = min(v / 2, i); j >= 1; j--) posv[j] |= posv[j - 1] << p[i]; toth >>= 1, totv >>= 1; for (int i = h / 2; i >= 1; i--) if (posh[i][toth]) { malay_dp(h, i, toth, 1000 * (i + 1) + 1, hB, hA, l); goto Findv; } goto No; Findv: for (int i = v / 2; i >= 1; i--) if (posv[i][totv]) { malay_dp(v, i, totv, 1000 * (i + 1) + 1, vA, vB, p); goto Yes; } No: printf("No\n"); return; Yes: printf("Yes\n"); sort(hA.begin(), hA.end()); sort(hB.begin(), hB.end()); sort(vA.begin(), vA.end(), greater<short>()); sort(vB.begin(), vB.end(), greater<short>()); for (short i : hA) H.push_back(i); for (short i : hB) H.push_back(i); for (short i : vA) V.push_back(i); for (short i : vB) V.push_back(i); int sgv = 1, sgh = 1; for (int i = 0; i < h; i++) { y += V[i] * sgv; printf("%d %d\n", x, y); x += H[i] * sgh; printf("%d %d\n", x, y); if (y == totv) sgv = -sgv; if (x == toth) sgh = -sgh; } return; } int main() { int t; scanf("%d", &t); while (t--) solve(); }
### Prompt Please formulate a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3", "fast-math") #pragma GCC target("tune=native,avx,avx2") using namespace std; constexpr int kN = int(1E3 + 10), kW = int(25E4 + 10); short l[kN], p[kN]; bitset<kW> posh[kN >> 2], posv[kN >> 2]; bitset<kN> bs; void malay_dp(short n, unsigned char k, int s, int kkW, vector<short> &v1, vector<short> &v2, short *a) { pair<short, pair<short, int>> now; vector<vector<unsigned char>> dp[2]; vector<vector<short>> from[2]; dp[0] = vector<vector<unsigned char>>(n - k + 1, vector<unsigned char>(kkW)); dp[1] = vector<vector<unsigned char>>(n - k + 1, vector<unsigned char>(kkW)); from[0] = vector<vector<short>>(n - k + 1, vector<short>(kkW)); from[1] = vector<vector<short>>(n - k + 1, vector<short>(kkW)); short nxt; int tot = 0; for (int i = 1; i <= k; i++) tot += a[i]; for (int i = 0; i <= n - k; i++) for (int j = 0; j < kkW; j++) dp[0][i][j] = dp[1][i][j] = k + 1; dp[0][0][tot] = 0; for (int i = 0; i <= n - k; i++) { for (int j = 0; j < kkW; j++) if (dp[1][i][j] <= k) for (int p = dp[1][i][j] + 1; p <= min(dp[1][i - 1][j], k); p++) if (j >= a[p] && dp[0][i][j - a[p]] > p) { dp[0][i][j - a[p]] = p; from[0][i][j - a[p]] = i + 4000 + k; } if (i < n - k) for (int j = 0; j < kkW; j++) if (dp[0][i][j] <= k) { if (dp[0][i + 1][j] > dp[0][i][j]) { dp[0][i + 1][j] = dp[0][i][j]; from[0][i + 1][j] = i + 2000 + k; } if (j + a[i + 1 + k] < kkW && dp[1][i + 1][j + a[i + 1 + k]] > dp[0][i][j]) { dp[1][i + 1][j + a[i + 1 + k]] = dp[0][i][j]; from[1][i + 1][j + a[i + 1 + k]] = i + k; } } } now = {0, {n, s}}; bs.reset(); for (int i = 1; i <= k; i++) bs[i] = true; while (now.second.first > k) { nxt = from[now.first][now.second.first - k][now.second.second]; if (nxt >= 4000) { nxt -= 4000; bs[dp[now.first][now.second.first - k][now.second.second]] = false; now = { 1, {nxt, now.second.second + a[dp[now.first][now.second.first - k][now.second.second]]}}; } else if (nxt >= 2000) now = {0, {nxt - 2000, now.second.second}}; else { bs[now.second.first] = true; now = {0, {now.second.first - 1, now.second.second - a[now.second.first]}}; } } for (int i = 1; i <= n; i++) if (bs[i]) v1.push_back(a[i]); else v2.push_back(a[i]); return; } void solve() { int h, v, x = 0, y = 0, toth = 0, totv = 0; vector<short> hA, hB, vA, vB, H, V; scanf("%d", &h); for (int i = 1; i <= h; i++) scanf("%hd", &l[i]); scanf("%d", &v); for (int i = 1; i <= v; i++) scanf("%hd", &p[i]); if (h != v) goto No; for (int i = 1; i <= h; i++) toth += l[i]; for (int i = 1; i <= v; i++) totv += p[i]; if ((toth & 1) || (totv & 1)) goto No; posh[0][0] = posv[0][0] = true; for (int i = 1; i <= h / 2; i++) posh[i].reset(); for (int i = 1; i <= h; i++) for (int j = min(h / 2, i); j >= 1; j--) posh[j] |= posh[j - 1] << l[i]; for (int i = 1; i <= v / 2; i++) posv[i].reset(); for (int i = 1; i <= v; i++) for (int j = min(v / 2, i); j >= 1; j--) posv[j] |= posv[j - 1] << p[i]; toth >>= 1, totv >>= 1; for (int i = h / 2; i >= 1; i--) if (posh[i][toth]) { malay_dp(h, i, toth, 1000 * (i + 1) + 1, hB, hA, l); goto Findv; } goto No; Findv: for (int i = v / 2; i >= 1; i--) if (posv[i][totv]) { malay_dp(v, i, totv, 1000 * (i + 1) + 1, vA, vB, p); goto Yes; } No: printf("No\n"); return; Yes: printf("Yes\n"); sort(hA.begin(), hA.end()); sort(hB.begin(), hB.end()); sort(vA.begin(), vA.end(), greater<short>()); sort(vB.begin(), vB.end(), greater<short>()); for (short i : hA) H.push_back(i); for (short i : hB) H.push_back(i); for (short i : vA) V.push_back(i); for (short i : vB) V.push_back(i); int sgv = 1, sgh = 1; for (int i = 0; i < h; i++) { y += V[i] * sgv; printf("%d %d\n", x, y); x += H[i] * sgh; printf("%d %d\n", x, y); if (y == totv) sgv = -sgv; if (x == toth) sgh = -sgh; } return; } int main() { int t; scanf("%d", &t); while (t--) solve(); } ```
#include <bits/stdc++.h> using namespace std; int dp[500010]; unsigned long long lst[510][7813]; void SET(int x, int y) { lst[x][y >> 6] |= 1ull << (y & 63); } int GET(int x, int y) { return lst[x][y >> 6] >> (y & 63) & 1; } void calc(vector<int> &a, vector<int> &b) { int sum = 0; dp[0] = 1; for (int i = 0; i < a.size(); i++) { sum += a[i]; for (int j = 0; j <= (sum >> 6); j++) { lst[i][j] = 0; } for (int j = sum; j >= 0; j--) { if (j >= a[i] && dp[j - a[i]]) dp[j] = 1, SET(i, j); } } for (int i = 0; i <= sum; i++) { if (i * 2 != sum) dp[i] = 0; } if (sum & 1) return; if (!dp[sum / 2]) return; dp[sum / 2] = 0; int x = (int)a.size() - 1, y = sum / 2; while (x != -1) { int val = GET(x, y); y -= val * a[x]; if (val) b.push_back(a[x]), a.erase(a.begin() + x); x--; } return; } int main() { int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d", &n); vector<int> a1, a2, b1, b2; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); a1.push_back(x); } scanf("%d", &m); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); b1.push_back(x); } if (n != m) { printf("No\n"); continue; } calc(a1, a2), calc(b1, b2); if (!a2.size() || !b2.size()) { printf("No\n"); continue; } printf("Yes\n"); if (a1.size() == b1.size()) { sort(a1.rbegin(), a1.rend()), sort(b1.begin(), b1.end()); int x = 0, y = 0; for (int i = 0; i < a1.size(); i++) { x += a1[i]; printf("%d %d\n", x, y); y += b1[i]; printf("%d %d\n", x, y); } sort(a2.rbegin(), a2.rend()), sort(b2.begin(), b2.end()); for (int i = 0; i < a2.size(); i++) { x -= a2[i]; printf("%d %d\n", x, y); y -= b2[i]; printf("%d %d\n", x, y); } continue; } vector<pair<int, int> > ans; int flag = 0; if (a1.size() > b1.size()) flag = 1, swap(a1, b1), swap(a2, b2); int x = 0, y = 0; sort(a1.rbegin(), a1.rend()), sort(b1.rbegin(), b1.rend()); for (int i = 0; i < a1.size(); i++) { x += a1[i]; ans.push_back(make_pair(x, y)); y += b1.back(), b1.pop_back(); ans.push_back(make_pair(x, y)); } sort(a2.rbegin(), a2.rend()), sort(b1.rbegin(), b1.rend()); for (int i = 0; i < b1.size(); i++) { x -= a2.back(), a2.pop_back(); ans.push_back(make_pair(x, y)); y += b1[i]; ans.push_back(make_pair(x, y)); } sort(a2.rbegin(), a2.rend()), sort(b2.begin(), b2.end()); for (int i = 0; i < a2.size(); i++) { x -= a2[i]; ans.push_back(make_pair(x, y)); y -= b2[i]; ans.push_back(make_pair(x, y)); } if (flag) { for (int i = 0; i < ans.size(); i++) { swap(ans[i].first, ans[i].second); } reverse(ans.begin(), ans.end()); } for (int i = 0; i < ans.size(); i++) { printf("%d %d\n", ans[i].first, ans[i].second); } } return 0; }
### Prompt Develop a solution in cpp to the problem described below: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dp[500010]; unsigned long long lst[510][7813]; void SET(int x, int y) { lst[x][y >> 6] |= 1ull << (y & 63); } int GET(int x, int y) { return lst[x][y >> 6] >> (y & 63) & 1; } void calc(vector<int> &a, vector<int> &b) { int sum = 0; dp[0] = 1; for (int i = 0; i < a.size(); i++) { sum += a[i]; for (int j = 0; j <= (sum >> 6); j++) { lst[i][j] = 0; } for (int j = sum; j >= 0; j--) { if (j >= a[i] && dp[j - a[i]]) dp[j] = 1, SET(i, j); } } for (int i = 0; i <= sum; i++) { if (i * 2 != sum) dp[i] = 0; } if (sum & 1) return; if (!dp[sum / 2]) return; dp[sum / 2] = 0; int x = (int)a.size() - 1, y = sum / 2; while (x != -1) { int val = GET(x, y); y -= val * a[x]; if (val) b.push_back(a[x]), a.erase(a.begin() + x); x--; } return; } int main() { int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d", &n); vector<int> a1, a2, b1, b2; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); a1.push_back(x); } scanf("%d", &m); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); b1.push_back(x); } if (n != m) { printf("No\n"); continue; } calc(a1, a2), calc(b1, b2); if (!a2.size() || !b2.size()) { printf("No\n"); continue; } printf("Yes\n"); if (a1.size() == b1.size()) { sort(a1.rbegin(), a1.rend()), sort(b1.begin(), b1.end()); int x = 0, y = 0; for (int i = 0; i < a1.size(); i++) { x += a1[i]; printf("%d %d\n", x, y); y += b1[i]; printf("%d %d\n", x, y); } sort(a2.rbegin(), a2.rend()), sort(b2.begin(), b2.end()); for (int i = 0; i < a2.size(); i++) { x -= a2[i]; printf("%d %d\n", x, y); y -= b2[i]; printf("%d %d\n", x, y); } continue; } vector<pair<int, int> > ans; int flag = 0; if (a1.size() > b1.size()) flag = 1, swap(a1, b1), swap(a2, b2); int x = 0, y = 0; sort(a1.rbegin(), a1.rend()), sort(b1.rbegin(), b1.rend()); for (int i = 0; i < a1.size(); i++) { x += a1[i]; ans.push_back(make_pair(x, y)); y += b1.back(), b1.pop_back(); ans.push_back(make_pair(x, y)); } sort(a2.rbegin(), a2.rend()), sort(b1.rbegin(), b1.rend()); for (int i = 0; i < b1.size(); i++) { x -= a2.back(), a2.pop_back(); ans.push_back(make_pair(x, y)); y += b1[i]; ans.push_back(make_pair(x, y)); } sort(a2.rbegin(), a2.rend()), sort(b2.begin(), b2.end()); for (int i = 0; i < a2.size(); i++) { x -= a2[i]; ans.push_back(make_pair(x, y)); y -= b2[i]; ans.push_back(make_pair(x, y)); } if (flag) { for (int i = 0; i < ans.size(); i++) { swap(ans[i].first, ans[i].second); } reverse(ans.begin(), ans.end()); } for (int i = 0; i < ans.size(); i++) { printf("%d %d\n", ans[i].first, ans[i].second); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int T, a[1002], b[1002], a1[1002], a2[1002], b1[1002], b2[1002], h, v; bitset<1002 * 1002> f[1002]; bool cmp1(int x, int y) { return x < y; } bool cmp2(int x, int y) { return x > y; } int main() { cin >> T; while (T--) { scanf("%d", &h); int sum1 = 0, sum2 = 0; for (int i = 1; i <= h; i++) scanf("%d", &a[i]), sum1 += a[i]; scanf("%d", &v); for (int i = 1; i <= v; i++) scanf("%d", &b[i]), sum2 += b[i]; if (sum1 % 2 == 1 || sum2 % 2 == 1 || h != v) { printf("NO\n"); continue; } sum1 /= 2, sum2 /= 2; f[0][0] = 1; for (int i = 1; i <= h; i++) { f[i] = f[i - 1] | (f[i - 1] << a[i]); } if (!f[h][sum1]) { printf("NO\n"); continue; } int lena1 = 0, lena2 = 0, lenb1 = 0, lenb2 = 0; for (int i = h; i >= 1; i--) { if (sum1 >= a[i] && f[i - 1][sum1 - a[i]]) a1[++lena1] = a[i], sum1 -= a[i]; else a2[++lena2] = a[i]; } f[0][0] = 1; for (int i = 1; i <= v; i++) { f[i] = f[i - 1] | (f[i - 1] << b[i]); } if (!f[v][sum2]) { printf("NO\n"); continue; } for (int i = v; i >= 1; i--) { if (sum2 >= b[i] && f[i - 1][sum2 - b[i]]) b1[++lenb1] = b[i], sum2 -= b[i]; else b2[++lenb2] = b[i]; } sort(a1 + 1, a1 + lena1 + 1, cmp2); sort(a2 + 1, a2 + lena2 + 1, cmp2); sort(b1 + 1, b1 + lenb1 + 1, cmp1); sort(b2 + 1, b2 + lenb2 + 1, cmp1); if (lena1 > lena2) swap(lena1, lena2), swap(a1, a2); if (lenb1 < lenb2) swap(lenb1, lenb2), swap(b1, b2); int x = 0, y = 0; printf("YES\n"); for (int i = 1; i <= h; i++) { if (i <= lena1) x += a1[i]; else x -= a2[i - lena1]; printf("%d %d\n", x, y); if (i <= lenb1) y += b1[i]; else y -= b2[i - lenb1]; printf("%d %d\n", x, y); } } }
### Prompt Your task is to create a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; int T, a[1002], b[1002], a1[1002], a2[1002], b1[1002], b2[1002], h, v; bitset<1002 * 1002> f[1002]; bool cmp1(int x, int y) { return x < y; } bool cmp2(int x, int y) { return x > y; } int main() { cin >> T; while (T--) { scanf("%d", &h); int sum1 = 0, sum2 = 0; for (int i = 1; i <= h; i++) scanf("%d", &a[i]), sum1 += a[i]; scanf("%d", &v); for (int i = 1; i <= v; i++) scanf("%d", &b[i]), sum2 += b[i]; if (sum1 % 2 == 1 || sum2 % 2 == 1 || h != v) { printf("NO\n"); continue; } sum1 /= 2, sum2 /= 2; f[0][0] = 1; for (int i = 1; i <= h; i++) { f[i] = f[i - 1] | (f[i - 1] << a[i]); } if (!f[h][sum1]) { printf("NO\n"); continue; } int lena1 = 0, lena2 = 0, lenb1 = 0, lenb2 = 0; for (int i = h; i >= 1; i--) { if (sum1 >= a[i] && f[i - 1][sum1 - a[i]]) a1[++lena1] = a[i], sum1 -= a[i]; else a2[++lena2] = a[i]; } f[0][0] = 1; for (int i = 1; i <= v; i++) { f[i] = f[i - 1] | (f[i - 1] << b[i]); } if (!f[v][sum2]) { printf("NO\n"); continue; } for (int i = v; i >= 1; i--) { if (sum2 >= b[i] && f[i - 1][sum2 - b[i]]) b1[++lenb1] = b[i], sum2 -= b[i]; else b2[++lenb2] = b[i]; } sort(a1 + 1, a1 + lena1 + 1, cmp2); sort(a2 + 1, a2 + lena2 + 1, cmp2); sort(b1 + 1, b1 + lenb1 + 1, cmp1); sort(b2 + 1, b2 + lenb2 + 1, cmp1); if (lena1 > lena2) swap(lena1, lena2), swap(a1, a2); if (lenb1 < lenb2) swap(lenb1, lenb2), swap(b1, b2); int x = 0, y = 0; printf("YES\n"); for (int i = 1; i <= h; i++) { if (i <= lena1) x += a1[i]; else x -= a2[i - lena1]; printf("%d %d\n", x, y); if (i <= lenb1) y += b1[i]; else y -= b2[i - lenb1]; printf("%d %d\n", x, y); } } } ```
#include <bits/stdc++.h> const double eps = 1e-10; int n, m, a[3005], b[3005], x[3005], y[3005], rk[3005], t1, t2, px, py; std::bitset<1000101> B[1005]; int cmp(int a, int b) { return atan2(y[a], x[a]) + eps < atan2(y[b], x[b]); } int split(int n, int* a, std::vector<int>& v1, std::vector<int>& v2) { int sum = 0; for (int i = 1; i <= n; ++i) sum += a[i]; if (sum % 2) return 0; B[0][0] = 1; for (int i = 1; i <= n; ++i) B[i] = B[i - 1] | (B[i - 1] << a[i]); if (!B[n][sum / 2]) return 0; int p = n, s = sum / 2; while (p > 0) { if (B[p - 1][s]) { v1.push_back(a[p]); } else { s -= a[p]; v2.push_back(a[p]); } p--; } return 1; } void solve() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; ++i) scanf("%d", &b[i]); if (n != m) { puts("nO"); return; } std::vector<int> v1, v2, v3, v4; int c1 = split(n, a, v1, v2); int c2 = split(m, b, v3, v4); if (c1 + c2 < 2) { puts("No"); return; } puts("YES"); t1 = t2 = 0; for (int z : v1) x[++t1] = z; for (int z : v2) x[++t1] = -z; for (int z : v3) y[++t2] = z; for (int z : v4) y[++t2] = -z; std::sort(x + 1, x + t1 + 1); std::sort(y + 1, y + t2 + 1); for (int i = 1; i <= t2; ++i) rk[i] = i; std::sort(rk + 1, rk + t2 + 1, cmp); px = py = 0; auto walk = [&](int x, int y) { px += x; py += y; printf("%d %d\n", px, py); }; int fl1 = 0, fl2 = 0; for (int i = 1; i <= t1; ++i) { int dx = x[rk[i]], dy = y[rk[i]]; if (dx > 0 && dy < 0) fl1 = 1; if (dx < 0 && dy > 0) fl2 = 1; walk(dx, 0); walk(0, dy); } } int main() { int t; scanf("%d", &t); while (t--) solve(); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> const double eps = 1e-10; int n, m, a[3005], b[3005], x[3005], y[3005], rk[3005], t1, t2, px, py; std::bitset<1000101> B[1005]; int cmp(int a, int b) { return atan2(y[a], x[a]) + eps < atan2(y[b], x[b]); } int split(int n, int* a, std::vector<int>& v1, std::vector<int>& v2) { int sum = 0; for (int i = 1; i <= n; ++i) sum += a[i]; if (sum % 2) return 0; B[0][0] = 1; for (int i = 1; i <= n; ++i) B[i] = B[i - 1] | (B[i - 1] << a[i]); if (!B[n][sum / 2]) return 0; int p = n, s = sum / 2; while (p > 0) { if (B[p - 1][s]) { v1.push_back(a[p]); } else { s -= a[p]; v2.push_back(a[p]); } p--; } return 1; } void solve() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; ++i) scanf("%d", &b[i]); if (n != m) { puts("nO"); return; } std::vector<int> v1, v2, v3, v4; int c1 = split(n, a, v1, v2); int c2 = split(m, b, v3, v4); if (c1 + c2 < 2) { puts("No"); return; } puts("YES"); t1 = t2 = 0; for (int z : v1) x[++t1] = z; for (int z : v2) x[++t1] = -z; for (int z : v3) y[++t2] = z; for (int z : v4) y[++t2] = -z; std::sort(x + 1, x + t1 + 1); std::sort(y + 1, y + t2 + 1); for (int i = 1; i <= t2; ++i) rk[i] = i; std::sort(rk + 1, rk + t2 + 1, cmp); px = py = 0; auto walk = [&](int x, int y) { px += x; py += y; printf("%d %d\n", px, py); }; int fl1 = 0, fl2 = 0; for (int i = 1; i <= t1; ++i) { int dx = x[rk[i]], dy = y[rk[i]]; if (dx > 0 && dy < 0) fl1 = 1; if (dx < 0 && dy > 0) fl2 = 1; walk(dx, 0); walk(0, dy); } } int main() { int t; scanf("%d", &t); while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const double eps = 1e-8; const double pi = acos(-1.0); const int maxn = 1e6 + 10; const long long inf = 0x3f3f3f3f; const long long oo = 8e18; const int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int a[maxn], b[maxn], n, m; vector<int> a1, a2, b1, b2; bitset<500010> f[1010]; bool split(int num, int c[], vector<int> &c1, vector<int> &c2) { int sum = 0; for (int i = 1; i <= num; i++) sum += c[i]; if (sum & 1) return 0; sum >>= 1; f[0].reset(); f[0][0] = 1; for (int i = 1; i <= num; i++) f[i] = f[i - 1] | f[i - 1] << c[i]; if (!f[num][sum]) return 0; c1.clear(); c2.clear(); for (int i = num; i >= 1; i--) { if (f[i - 1][sum]) c1.push_back(c[i]); else c2.push_back(c[i]), sum -= c[i]; } return 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int _; cin >> _; while (_--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> m; for (int i = 1; i <= m; i++) cin >> b[i]; if (n != m || !split(n, a, a1, a2) || !split(m, b, b1, b2)) { cout << "NO" << '\n'; continue; } if (a1.size() > a2.size()) swap(a1, a2); if (b1.size() < b2.size()) swap(b1, b2); int t = b1.size() - a1.size(); sort((a1).begin(), (a1).end(), greater<int>()); sort(a2.begin() + t, a2.end(), greater<int>()); sort(b1.begin(), b1.end() - t); sort((b2).begin(), (b2).end()); for (auto x : a2) a1.push_back(-x); for (auto x : b2) b1.push_back(-x); cout << "YES" << '\n'; int x = 0, y = 0; for (int i = 0; i < n; i++) { x += a1[i]; cout << x << ' ' << y << '\n'; y += b1[i]; cout << x << ' ' << y << '\n'; } } return 0; }
### Prompt Generate a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 998244353; const double eps = 1e-8; const double pi = acos(-1.0); const int maxn = 1e6 + 10; const long long inf = 0x3f3f3f3f; const long long oo = 8e18; const int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; int a[maxn], b[maxn], n, m; vector<int> a1, a2, b1, b2; bitset<500010> f[1010]; bool split(int num, int c[], vector<int> &c1, vector<int> &c2) { int sum = 0; for (int i = 1; i <= num; i++) sum += c[i]; if (sum & 1) return 0; sum >>= 1; f[0].reset(); f[0][0] = 1; for (int i = 1; i <= num; i++) f[i] = f[i - 1] | f[i - 1] << c[i]; if (!f[num][sum]) return 0; c1.clear(); c2.clear(); for (int i = num; i >= 1; i--) { if (f[i - 1][sum]) c1.push_back(c[i]); else c2.push_back(c[i]), sum -= c[i]; } return 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int _; cin >> _; while (_--) { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> m; for (int i = 1; i <= m; i++) cin >> b[i]; if (n != m || !split(n, a, a1, a2) || !split(m, b, b1, b2)) { cout << "NO" << '\n'; continue; } if (a1.size() > a2.size()) swap(a1, a2); if (b1.size() < b2.size()) swap(b1, b2); int t = b1.size() - a1.size(); sort((a1).begin(), (a1).end(), greater<int>()); sort(a2.begin() + t, a2.end(), greater<int>()); sort(b1.begin(), b1.end() - t); sort((b2).begin(), (b2).end()); for (auto x : a2) a1.push_back(-x); for (auto x : b2) b1.push_back(-x); cout << "YES" << '\n'; int x = 0, y = 0; for (int i = 0; i < n; i++) { x += a1[i]; cout << x << ' ' << y << '\n'; y += b1[i]; cout << x << ' ' << y << '\n'; } } return 0; } ```
#include <bits/stdc++.h> int read() { static int c, x; while ((c = getchar()) < 48) { } x = c & 15; while ((c = getchar()) >= 48) x = x * 10 + (c & 15); return x; } int a[1010], b[1010]; int px[1010 << 1], py[1010 << 1]; std::vector<int> a1, a2, b1, b2; std::bitset<1010 * 1010> dp[1010]; int main() { dp->set(0); for (int T = read(); T--;) { int sa = 0, sb = 0; a1.clear(); a2.clear(); b1.clear(); b2.clear(); const int n = read(); for (int i = 1; i <= n; ++i) { a[i] = read(); sa += a[i]; } const int m = read(); for (int i = 1; i <= m; ++i) { b[i] = read(); sb += b[i]; } if (n != m || ((sa | sb) & 1)) { puts("No"); continue; } sa >>= 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] << a[i] | dp[i - 1]; } if (!dp[n].test(sa)) { puts("No"); continue; } for (int i = n; i; --i) { if (dp[i - 1].test(sa)) { a1.push_back(a[i]); } else { a2.push_back(a[i]); sa -= a[i]; } } sb >>= 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] << b[i] | dp[i - 1]; } if (!dp[n].test(sb)) { puts("No"); continue; } for (int i = n; i; --i) { if (dp[i - 1].test(sb)) { b1.push_back(b[i]); } else { b2.push_back(b[i]); sb -= b[i]; } } bool rev = false; if (a1.size() > b1.size()) { rev = true; a1.swap(b1); a2.swap(b2); } std::sort(a1.begin(), a1.end()); std::sort(a2.begin(), a2.end()); std::sort(b1.begin(), b1.end(), std::greater<int>()); std::sort(b2.begin(), b2.end(), std::greater<int>()); for (int i1 = a1.size(), i2 = a2.size(), j1 = b1.size(), j2 = b2.size(), x = 0, y = 0, t = n, c = 0; t--;) { if (i1) { x += a1[--i1]; } else { x -= a2[--i2]; } ++c; px[c] = x; py[c] = y; if (j1) { y += b1[--j1]; } else { y -= b2[--j2]; } ++c; px[c] = x; py[c] = y; } puts("Yes"); for (int i = 1; i <= (n << 1); ++i) { rev ? printf("%d %d\n", py[i], px[i]) : printf("%d %d\n", px[i], py[i]); } } return 0; }
### Prompt Generate a CPP solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> int read() { static int c, x; while ((c = getchar()) < 48) { } x = c & 15; while ((c = getchar()) >= 48) x = x * 10 + (c & 15); return x; } int a[1010], b[1010]; int px[1010 << 1], py[1010 << 1]; std::vector<int> a1, a2, b1, b2; std::bitset<1010 * 1010> dp[1010]; int main() { dp->set(0); for (int T = read(); T--;) { int sa = 0, sb = 0; a1.clear(); a2.clear(); b1.clear(); b2.clear(); const int n = read(); for (int i = 1; i <= n; ++i) { a[i] = read(); sa += a[i]; } const int m = read(); for (int i = 1; i <= m; ++i) { b[i] = read(); sb += b[i]; } if (n != m || ((sa | sb) & 1)) { puts("No"); continue; } sa >>= 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] << a[i] | dp[i - 1]; } if (!dp[n].test(sa)) { puts("No"); continue; } for (int i = n; i; --i) { if (dp[i - 1].test(sa)) { a1.push_back(a[i]); } else { a2.push_back(a[i]); sa -= a[i]; } } sb >>= 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] << b[i] | dp[i - 1]; } if (!dp[n].test(sb)) { puts("No"); continue; } for (int i = n; i; --i) { if (dp[i - 1].test(sb)) { b1.push_back(b[i]); } else { b2.push_back(b[i]); sb -= b[i]; } } bool rev = false; if (a1.size() > b1.size()) { rev = true; a1.swap(b1); a2.swap(b2); } std::sort(a1.begin(), a1.end()); std::sort(a2.begin(), a2.end()); std::sort(b1.begin(), b1.end(), std::greater<int>()); std::sort(b2.begin(), b2.end(), std::greater<int>()); for (int i1 = a1.size(), i2 = a2.size(), j1 = b1.size(), j2 = b2.size(), x = 0, y = 0, t = n, c = 0; t--;) { if (i1) { x += a1[--i1]; } else { x -= a2[--i2]; } ++c; px[c] = x; py[c] = y; if (j1) { y += b1[--j1]; } else { y -= b2[--j2]; } ++c; px[c] = x; py[c] = y; } puts("Yes"); for (int i = 1; i <= (n << 1); ++i) { rev ? printf("%d %d\n", py[i], px[i]) : printf("%d %d\n", px[i], py[i]); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; inline int add(int a, int b) { return (a += b) >= mod ? a - mod : a; } inline int sub(int a, int b) { return (a -= b) < 0 ? a + mod : a; } inline int mul(int a, int b) { return (long long)a * b % mod; } inline int qpow(int a, int b) { int res = 1; for (; b; a = mul(a, a), b >>= 1) if (b & 1) res = mul(res, a); return res; } const int N = 1005, M = 5e5 + 5; int n, m, sum; int a[N], b[N]; bitset<M> epc[N]; bool ba[N], bb[N]; inline void solve() { register int i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]), ba[i] = 0; scanf("%d", &m); for (i = 1; i <= m; i++) scanf("%d", &b[i]), bb[i] = 0; if (n != m) return puts("No"), void(); for (sum = 0, i = 1; i <= n; i++) sum += a[i]; if (sum & 1) return puts("No"), void(); epc[0].reset(); epc[0][0] = 1; for (i = 1; i <= n; i++) epc[i] = epc[i - 1] | epc[i - 1] << a[i]; if (!epc[n][sum >> 1]) return puts("No"), void(); for (k = sum >> 1; k;) { for (i = 1; i <= n; i++) if (epc[i][k]) { ba[i] = 1; k -= a[i]; break; } } for (sum = 0, i = 1; i <= n; i++) sum += b[i]; if (sum & 1) return puts("No"), void(); epc[0].reset(); epc[0][0] = 1; for (i = 1; i <= n; i++) epc[i] = epc[i - 1] | epc[i - 1] << b[i]; if (!epc[n][sum >> 1]) return puts("No"), void(); for (k = sum >> 1; k;) { for (i = 1; i <= n; i++) if (epc[i][k]) { bb[i] = 1; k -= b[i]; break; } } for (i = 1; i <= n; i++) if (ba[i]) a[i] *= -1; for (i = 1; i <= m; i++) if (bb[i]) b[i] *= -1; int _ = 0; sort(a + 1, a + n + 1, greater<int>()); sort(b + 1, b + n + 1, greater<int>()); for (i = 1; i <= n; i++) if (b[i] < 0) { reverse(a + 1, a + i); reverse(b + i, b + n + 1); break; } puts("Yes"); int x = 0, y = 0; printf("%d %d\n", x, y); for (i = 1; i <= n; i++) { y += b[i]; printf("%d %d\n", x, y); x += a[i]; if (i != n) printf("%d %d\n", x, y); } return; } signed main() { int T; scanf("%d", &T); while (T--) solve(); return 0; }
### Prompt Generate a Cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 998244353; inline int add(int a, int b) { return (a += b) >= mod ? a - mod : a; } inline int sub(int a, int b) { return (a -= b) < 0 ? a + mod : a; } inline int mul(int a, int b) { return (long long)a * b % mod; } inline int qpow(int a, int b) { int res = 1; for (; b; a = mul(a, a), b >>= 1) if (b & 1) res = mul(res, a); return res; } const int N = 1005, M = 5e5 + 5; int n, m, sum; int a[N], b[N]; bitset<M> epc[N]; bool ba[N], bb[N]; inline void solve() { register int i, j, k; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]), ba[i] = 0; scanf("%d", &m); for (i = 1; i <= m; i++) scanf("%d", &b[i]), bb[i] = 0; if (n != m) return puts("No"), void(); for (sum = 0, i = 1; i <= n; i++) sum += a[i]; if (sum & 1) return puts("No"), void(); epc[0].reset(); epc[0][0] = 1; for (i = 1; i <= n; i++) epc[i] = epc[i - 1] | epc[i - 1] << a[i]; if (!epc[n][sum >> 1]) return puts("No"), void(); for (k = sum >> 1; k;) { for (i = 1; i <= n; i++) if (epc[i][k]) { ba[i] = 1; k -= a[i]; break; } } for (sum = 0, i = 1; i <= n; i++) sum += b[i]; if (sum & 1) return puts("No"), void(); epc[0].reset(); epc[0][0] = 1; for (i = 1; i <= n; i++) epc[i] = epc[i - 1] | epc[i - 1] << b[i]; if (!epc[n][sum >> 1]) return puts("No"), void(); for (k = sum >> 1; k;) { for (i = 1; i <= n; i++) if (epc[i][k]) { bb[i] = 1; k -= b[i]; break; } } for (i = 1; i <= n; i++) if (ba[i]) a[i] *= -1; for (i = 1; i <= m; i++) if (bb[i]) b[i] *= -1; int _ = 0; sort(a + 1, a + n + 1, greater<int>()); sort(b + 1, b + n + 1, greater<int>()); for (i = 1; i <= n; i++) if (b[i] < 0) { reverse(a + 1, a + i); reverse(b + i, b + n + 1); break; } puts("Yes"); int x = 0, y = 0; printf("%d %d\n", x, y); for (i = 1; i <= n; i++) { y += b[i]; printf("%d %d\n", x, y); x += a[i]; if (i != n) printf("%d %d\n", x, y); } return; } signed main() { int T; scanf("%d", &T); while (T--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void chkmax(T &first, T second) { first < second ? first = second : T(); } template <typename T> void chkmin(T &first, T second) { first > second ? first = second : T(); } template <typename T> void readint(T &first) { first = 0; int f = 1; char c; for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) first = first * 10 + c - '0'; first *= f; } int n, m, a[1005], b[1005]; bitset<250005> f[505]; int ra[1005], rb[1005]; vector<int> ca[2], cb[2]; bool work(int n, int *a, int *r) { int s = 0; for (int i = 1; i <= n; ++i) s += a[i]; if (s & 1) return 0; s >>= 1; f[0][0] = 1; for (int i = 1; i <= n; ++i) f[i] = (f[i - 1] << a[i]) | f[i - 1]; if (!f[n][s]) return 0; for (int i = n; i; --i) if (s >= a[i] && f[i - 1][s - a[i]]) s -= a[i], r[i] = 1; return 1; } void solve() { readint(n); for (int i = 1; i <= n; ++i) readint(a[i]); readint(m); for (int i = 1; i <= m; ++i) readint(b[i]); if (n != m) { printf("No\n"); return; } memset(ra, 0, sizeof(ra)); memset(rb, 0, sizeof(rb)); if (!work(n, a, ra) || !work(n, b, rb)) { printf("No\n"); return; } for (int t = 0; t <= 1; ++t) ca[t].clear(), cb[t].clear(); for (int i = 1; i <= n; ++i) ca[ra[i]].push_back(a[i]), cb[rb[i]].push_back(b[i]); for (int t = 0; t <= 1; ++t) sort(ca[t].begin(), ca[t].end()), sort(cb[t].begin(), cb[t].end()); printf("Yes\n"); if (ca[0].size() < ca[1].size()) swap(ca[0], ca[1]); if (cb[0].size() > cb[1].size()) swap(cb[0], cb[1]); int s = 0; for (auto first : cb[0]) s += first; for (auto first : cb[1]) s -= first; assert(!s); int first = 0, second = 0; for (int i = 0; i < cb[0].size(); ++i) { printf("%d %d\n", first, second += cb[0][cb[0].size() - 1 - i]); printf("%d %d\n", first += ca[0][i], second); } for (int i = cb[0].size(); i < ca[0].size(); ++i) { printf("%d %d\n", first, second -= cb[1][ca[0].size() - 1 - i]); printf("%d %d\n", first += ca[0][i], second); } for (int i = ca[0].size(); i < n; ++i) { printf("%d %d\n", first, second -= cb[1][n - 1 - i + ca[0].size() - cb[0].size()]); printf("%d %d\n", first -= ca[1][i - ca[0].size()], second); } } int main() { int T; readint(T); while (T--) solve(); return 0; }
### Prompt Develop a solution in CPP to the problem described below: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> void chkmax(T &first, T second) { first < second ? first = second : T(); } template <typename T> void chkmin(T &first, T second) { first > second ? first = second : T(); } template <typename T> void readint(T &first) { first = 0; int f = 1; char c; for (c = getchar(); !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) first = first * 10 + c - '0'; first *= f; } int n, m, a[1005], b[1005]; bitset<250005> f[505]; int ra[1005], rb[1005]; vector<int> ca[2], cb[2]; bool work(int n, int *a, int *r) { int s = 0; for (int i = 1; i <= n; ++i) s += a[i]; if (s & 1) return 0; s >>= 1; f[0][0] = 1; for (int i = 1; i <= n; ++i) f[i] = (f[i - 1] << a[i]) | f[i - 1]; if (!f[n][s]) return 0; for (int i = n; i; --i) if (s >= a[i] && f[i - 1][s - a[i]]) s -= a[i], r[i] = 1; return 1; } void solve() { readint(n); for (int i = 1; i <= n; ++i) readint(a[i]); readint(m); for (int i = 1; i <= m; ++i) readint(b[i]); if (n != m) { printf("No\n"); return; } memset(ra, 0, sizeof(ra)); memset(rb, 0, sizeof(rb)); if (!work(n, a, ra) || !work(n, b, rb)) { printf("No\n"); return; } for (int t = 0; t <= 1; ++t) ca[t].clear(), cb[t].clear(); for (int i = 1; i <= n; ++i) ca[ra[i]].push_back(a[i]), cb[rb[i]].push_back(b[i]); for (int t = 0; t <= 1; ++t) sort(ca[t].begin(), ca[t].end()), sort(cb[t].begin(), cb[t].end()); printf("Yes\n"); if (ca[0].size() < ca[1].size()) swap(ca[0], ca[1]); if (cb[0].size() > cb[1].size()) swap(cb[0], cb[1]); int s = 0; for (auto first : cb[0]) s += first; for (auto first : cb[1]) s -= first; assert(!s); int first = 0, second = 0; for (int i = 0; i < cb[0].size(); ++i) { printf("%d %d\n", first, second += cb[0][cb[0].size() - 1 - i]); printf("%d %d\n", first += ca[0][i], second); } for (int i = cb[0].size(); i < ca[0].size(); ++i) { printf("%d %d\n", first, second -= cb[1][ca[0].size() - 1 - i]); printf("%d %d\n", first += ca[0][i], second); } for (int i = ca[0].size(); i < n; ++i) { printf("%d %d\n", first, second -= cb[1][n - 1 - i + ca[0].size() - cb[0].size()]); printf("%d %d\n", first -= ca[1][i - ca[0].size()], second); } } int main() { int T; readint(T); while (T--) solve(); return 0; } ```
#include <bits/stdc++.h> struct MI { private: char bb[1 << 14]; FILE *f; char *bs, *be; char e; bool o, l; public: MI() : f(stdin), bs(0), be(0) {} inline char get() { if (o) { o = 0; return e; } if (bs == be) be = (bs = bb) + fread(bb, 1, sizeof(bb), f); if (bs == be) { l = 1; return -1; }; return *bs++; } inline void unget(char c) { o = 1; e = c; } template <class T> inline T read() { T r; *this > r; return r; } template <class T> inline MI &operator>(T &); }; template <class T> struct Q { const static bool U = T(-1) >= T(0); inline void operator()(MI &t, T &r) const { r = 0; char c; bool y = 0; if (U) for (;;) { c = t.get(); if (c == -1) goto E; if (isdigit(c)) break; } else for (;;) { c = t.get(); if (c == -1) goto E; if (c == '-') { c = t.get(); if (isdigit(c)) { y = 1; break; }; } else if (isdigit(c)) break; ; }; for (;;) { if (c == -1) goto E; if (isdigit(c)) r = r * 10 + (c ^ 48); else break; c = t.get(); } t.unget(c); E:; if (y) r = -r; } }; template <> struct Q<char> {}; template <class T> inline MI &MI::operator>(T &t) { Q<T>()(*this, t); return *this; } template <class T> std::ostream &operator<(std::ostream &out, const T &t) { return out << t; } using std::cout; MI cin; const int $n = 1005; inline bool divide(std::vector<int> &arr, std::vector<int> &A) { static std::vector<std::bitset<500001>> can; static std::vector<int> B; const int n = arr.size(); can.clear(); can.resize(n + 1); can[0].reset(); can[0].set(0); int sum = 0; std::sort(arr.begin(), arr.end()); for (int i = 1; i <= n; ++i) { can[i] = can[i - 1] | (can[i - 1] << arr[i - 1]); sum += arr[i - 1]; } if ((sum & 1) || !can[n].test(sum >>= 1)) return 0; A.clear(); B.clear(); for (int i = n - 1, cur = sum; ~i; --i) { assert(can[i + 1].test(cur)); if (!can[i].test(cur)) { cur -= arr[i]; A.push_back(arr[i]); } else B.push_back(arr[i]); } if (A.size() > B.size()) std::swap(A, B); std::transform(B.begin(), B.end(), std::back_inserter(A), std::negate<int>()); return 1; } inline void work() { std::vector<int> hs, vs, xs, ys; hs.resize((cin.read<int>())); for (int &v : hs) cin > v; vs.resize((cin.read<int>())); for (int &v : vs) cin > v; if (hs.size() != vs.size() || !divide(hs, xs) || !divide(vs, ys)) return (void)(cout < "No\n"); std::reverse(ys.begin(), ys.end()); cout < "Yes\n"; for (int i = 0, x = 0, y = 0; i < xs.size(); ++i) { cout < (x += xs[i]) < ' ' < y < ('\n'); cout < x < ' ' < (y += ys[i]) < ('\n'); } } int main() { for (int T = (cin.read<int>()); T; --T) work(); }
### Prompt Create a solution in CPP for the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> struct MI { private: char bb[1 << 14]; FILE *f; char *bs, *be; char e; bool o, l; public: MI() : f(stdin), bs(0), be(0) {} inline char get() { if (o) { o = 0; return e; } if (bs == be) be = (bs = bb) + fread(bb, 1, sizeof(bb), f); if (bs == be) { l = 1; return -1; }; return *bs++; } inline void unget(char c) { o = 1; e = c; } template <class T> inline T read() { T r; *this > r; return r; } template <class T> inline MI &operator>(T &); }; template <class T> struct Q { const static bool U = T(-1) >= T(0); inline void operator()(MI &t, T &r) const { r = 0; char c; bool y = 0; if (U) for (;;) { c = t.get(); if (c == -1) goto E; if (isdigit(c)) break; } else for (;;) { c = t.get(); if (c == -1) goto E; if (c == '-') { c = t.get(); if (isdigit(c)) { y = 1; break; }; } else if (isdigit(c)) break; ; }; for (;;) { if (c == -1) goto E; if (isdigit(c)) r = r * 10 + (c ^ 48); else break; c = t.get(); } t.unget(c); E:; if (y) r = -r; } }; template <> struct Q<char> {}; template <class T> inline MI &MI::operator>(T &t) { Q<T>()(*this, t); return *this; } template <class T> std::ostream &operator<(std::ostream &out, const T &t) { return out << t; } using std::cout; MI cin; const int $n = 1005; inline bool divide(std::vector<int> &arr, std::vector<int> &A) { static std::vector<std::bitset<500001>> can; static std::vector<int> B; const int n = arr.size(); can.clear(); can.resize(n + 1); can[0].reset(); can[0].set(0); int sum = 0; std::sort(arr.begin(), arr.end()); for (int i = 1; i <= n; ++i) { can[i] = can[i - 1] | (can[i - 1] << arr[i - 1]); sum += arr[i - 1]; } if ((sum & 1) || !can[n].test(sum >>= 1)) return 0; A.clear(); B.clear(); for (int i = n - 1, cur = sum; ~i; --i) { assert(can[i + 1].test(cur)); if (!can[i].test(cur)) { cur -= arr[i]; A.push_back(arr[i]); } else B.push_back(arr[i]); } if (A.size() > B.size()) std::swap(A, B); std::transform(B.begin(), B.end(), std::back_inserter(A), std::negate<int>()); return 1; } inline void work() { std::vector<int> hs, vs, xs, ys; hs.resize((cin.read<int>())); for (int &v : hs) cin > v; vs.resize((cin.read<int>())); for (int &v : vs) cin > v; if (hs.size() != vs.size() || !divide(hs, xs) || !divide(vs, ys)) return (void)(cout < "No\n"); std::reverse(ys.begin(), ys.end()); cout < "Yes\n"; for (int i = 0, x = 0, y = 0; i < xs.size(); ++i) { cout < (x += xs[i]) < ' ' < y < ('\n'); cout < x < ' ' < (y += ys[i]) < ('\n'); } } int main() { for (int T = (cin.read<int>()); T; --T) work(); } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7, N = 1002, C = 1e6 + 7; bitset<C> dp[N]; pair<vector<int>, vector<int> > split(vector<int> v) { int sum = accumulate((v).begin(), (v).end(), 0); if (sum & 1) return {{}, {}}; dp[0] = 0; dp[0][0] = 1; for (int i = 0; i < v.size(); i++) dp[i + 1] = dp[i] | (dp[i] << v[i]); if (dp[v.size()][sum / 2] == 0) return {{}, {}}; vector<int> x, y; int cur = sum / 2; for (int i = v.size(); i >= 1; i--) { int tt = v[i - 1]; if (cur - tt >= 0 && dp[i - 1][cur - tt]) { x.push_back(tt); cur -= tt; } else { y.push_back(tt); } } assert(accumulate((x).begin(), (x).end(), 0) == sum / 2); if (x.size() < y.size()) swap(x, y); return {x, y}; } signed main() { std::ios::sync_with_stdio(0); std::cin.tie(0); ; int t; cin >> t; while (t--) { int h; cin >> h; vector<int> a(h); for (int i = 0; i < h; i++) cin >> a[i]; int v; cin >> v; vector<int> b(v); for (int i = 0; i < v; i++) cin >> b[i]; if (h != v) { cout << "NO" << endl; continue; } vector<int> l, r, u, d; tie(l, r) = split(a); tie(u, d) = split(b); if (l.empty() || u.empty()) { cout << "NO" << endl; continue; } for (int& i : l) i *= -1; for (int& i : d) i *= -1; int asz = r.size(), bsz = u.size() - r.size(), csz = d.size(); vector<pair<int, int> > va, vb, vc; for (int i : l) r.push_back(i); for (int i : d) u.push_back(i); for (int i = 0; i < asz; i++) va.emplace_back(r[i], u[i]); for (int i = asz; i < asz + bsz; i++) vb.emplace_back(r[i], u[i]); for (int i = asz + bsz; i < asz + bsz + csz; i++) vc.emplace_back(r[i], u[i]); auto cmp = [&](pair<int, int> a, pair<int, int> b) { return (long long)a.first * b.second - (long long)a.second * b.first > 0; }; sort((va).begin(), (va).end(), cmp); sort((vc).begin(), (vc).end(), cmp); cout << "Yes" << endl; int x = 0, y = 0; for (pair<int, int> p : va) { x += p.first; cout << x << " " << y << endl; y += p.second; cout << x << " " << y << endl; } for (pair<int, int> p : vb) { x += p.first; cout << x << " " << y << endl; y += p.second; cout << x << " " << y << endl; } for (pair<int, int> p : vc) { x += p.first; cout << x << " " << y << endl; y += p.second; cout << x << " " << y << endl; } } }
### Prompt Generate a Cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7, N = 1002, C = 1e6 + 7; bitset<C> dp[N]; pair<vector<int>, vector<int> > split(vector<int> v) { int sum = accumulate((v).begin(), (v).end(), 0); if (sum & 1) return {{}, {}}; dp[0] = 0; dp[0][0] = 1; for (int i = 0; i < v.size(); i++) dp[i + 1] = dp[i] | (dp[i] << v[i]); if (dp[v.size()][sum / 2] == 0) return {{}, {}}; vector<int> x, y; int cur = sum / 2; for (int i = v.size(); i >= 1; i--) { int tt = v[i - 1]; if (cur - tt >= 0 && dp[i - 1][cur - tt]) { x.push_back(tt); cur -= tt; } else { y.push_back(tt); } } assert(accumulate((x).begin(), (x).end(), 0) == sum / 2); if (x.size() < y.size()) swap(x, y); return {x, y}; } signed main() { std::ios::sync_with_stdio(0); std::cin.tie(0); ; int t; cin >> t; while (t--) { int h; cin >> h; vector<int> a(h); for (int i = 0; i < h; i++) cin >> a[i]; int v; cin >> v; vector<int> b(v); for (int i = 0; i < v; i++) cin >> b[i]; if (h != v) { cout << "NO" << endl; continue; } vector<int> l, r, u, d; tie(l, r) = split(a); tie(u, d) = split(b); if (l.empty() || u.empty()) { cout << "NO" << endl; continue; } for (int& i : l) i *= -1; for (int& i : d) i *= -1; int asz = r.size(), bsz = u.size() - r.size(), csz = d.size(); vector<pair<int, int> > va, vb, vc; for (int i : l) r.push_back(i); for (int i : d) u.push_back(i); for (int i = 0; i < asz; i++) va.emplace_back(r[i], u[i]); for (int i = asz; i < asz + bsz; i++) vb.emplace_back(r[i], u[i]); for (int i = asz + bsz; i < asz + bsz + csz; i++) vc.emplace_back(r[i], u[i]); auto cmp = [&](pair<int, int> a, pair<int, int> b) { return (long long)a.first * b.second - (long long)a.second * b.first > 0; }; sort((va).begin(), (va).end(), cmp); sort((vc).begin(), (vc).end(), cmp); cout << "Yes" << endl; int x = 0, y = 0; for (pair<int, int> p : va) { x += p.first; cout << x << " " << y << endl; y += p.second; cout << x << " " << y << endl; } for (pair<int, int> p : vb) { x += p.first; cout << x << " " << y << endl; y += p.second; cout << x << " " << y << endl; } for (pair<int, int> p : vc) { x += p.first; cout << x << " " << y << endl; y += p.second; cout << x << " " << y << endl; } } } ```
#include <bits/stdc++.h> using namespace std; const int nmax = 1e3 + 42, MX = 1e6 + 42; int h, w; vector<int> l, p; bitset<MX> can[nmax], idle; pair<vector<int>, vector<int> > create(vector<int> cur) { int sum = 0; for (auto k : cur) sum += k; if (sum % 2) return {{}, {}}; for (int i = 1; i <= cur.size(); i++) can[i] = idle; can[0][0] = 1; for (int i = 1; i <= cur.size(); i++) { can[i] = can[i - 1] | (can[i - 1] << cur[i - 1]); } if (can[cur.size()][sum / 2] == 0) return {{}, {}}; sum = sum / 2; vector<int> ret[2] = {{}, {}}; for (int i = cur.size(); i >= 1; i--) { if (can[i - 1][sum]) ret[1].push_back(cur[i - 1]); else { ret[0].push_back(cur[i - 1]); sum = sum - cur[i - 1]; } } return {ret[0], ret[1]}; } bool cmp(int a, int b) { return a > b; } void solve() { l = {}; scanf("%i", &h); for (int i = 1; i <= h; i++) { int sz; scanf("%i", &sz); l.push_back(sz); } p = {}; scanf("%i", &w); for (int i = 1; i <= w; i++) { int sz; scanf("%i", &sz); p.push_back(sz); } if (h != w) { printf("No\n"); return; } pair<vector<int>, vector<int> > to_add_h = create(l); pair<vector<int>, vector<int> > to_add_w = create(p); if (to_add_h.first.size() == 0 || to_add_w.first.size() == 0) { printf("No\n"); return; } printf("Yes\n"); if (to_add_h.first.size() > to_add_h.second.size()) swap(to_add_h.first, to_add_h.second); if (to_add_w.first.size() < to_add_w.second.size()) swap(to_add_w.first, to_add_w.second); sort(to_add_h.first.begin(), to_add_h.first.end(), cmp); sort(to_add_h.second.begin(), to_add_h.second.end(), cmp); sort(to_add_w.first.begin(), to_add_w.first.end()); sort(to_add_w.second.begin(), to_add_w.second.end()); vector<int> h_change = {}, w_change = {}; for (auto k : to_add_h.first) h_change.push_back(k); for (auto k : to_add_h.second) h_change.push_back(-k); for (auto k : to_add_w.first) w_change.push_back(k); for (auto k : to_add_w.second) w_change.push_back(-k); vector<pair<int, int> > outp = {}; for (int i = 0; i < h; i++) { outp.push_back({h_change[i], 0}); outp.push_back({0, w_change[i]}); } int x = 0, y = 0; for (auto k : outp) { printf("%i %i\n", x, y); x = x + k.first; y = y + k.second; } } int main() { int t; scanf("%i", &t); while (t) { t--; solve(); } return 0; }
### Prompt Please create a solution in CPP to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int nmax = 1e3 + 42, MX = 1e6 + 42; int h, w; vector<int> l, p; bitset<MX> can[nmax], idle; pair<vector<int>, vector<int> > create(vector<int> cur) { int sum = 0; for (auto k : cur) sum += k; if (sum % 2) return {{}, {}}; for (int i = 1; i <= cur.size(); i++) can[i] = idle; can[0][0] = 1; for (int i = 1; i <= cur.size(); i++) { can[i] = can[i - 1] | (can[i - 1] << cur[i - 1]); } if (can[cur.size()][sum / 2] == 0) return {{}, {}}; sum = sum / 2; vector<int> ret[2] = {{}, {}}; for (int i = cur.size(); i >= 1; i--) { if (can[i - 1][sum]) ret[1].push_back(cur[i - 1]); else { ret[0].push_back(cur[i - 1]); sum = sum - cur[i - 1]; } } return {ret[0], ret[1]}; } bool cmp(int a, int b) { return a > b; } void solve() { l = {}; scanf("%i", &h); for (int i = 1; i <= h; i++) { int sz; scanf("%i", &sz); l.push_back(sz); } p = {}; scanf("%i", &w); for (int i = 1; i <= w; i++) { int sz; scanf("%i", &sz); p.push_back(sz); } if (h != w) { printf("No\n"); return; } pair<vector<int>, vector<int> > to_add_h = create(l); pair<vector<int>, vector<int> > to_add_w = create(p); if (to_add_h.first.size() == 0 || to_add_w.first.size() == 0) { printf("No\n"); return; } printf("Yes\n"); if (to_add_h.first.size() > to_add_h.second.size()) swap(to_add_h.first, to_add_h.second); if (to_add_w.first.size() < to_add_w.second.size()) swap(to_add_w.first, to_add_w.second); sort(to_add_h.first.begin(), to_add_h.first.end(), cmp); sort(to_add_h.second.begin(), to_add_h.second.end(), cmp); sort(to_add_w.first.begin(), to_add_w.first.end()); sort(to_add_w.second.begin(), to_add_w.second.end()); vector<int> h_change = {}, w_change = {}; for (auto k : to_add_h.first) h_change.push_back(k); for (auto k : to_add_h.second) h_change.push_back(-k); for (auto k : to_add_w.first) w_change.push_back(k); for (auto k : to_add_w.second) w_change.push_back(-k); vector<pair<int, int> > outp = {}; for (int i = 0; i < h; i++) { outp.push_back({h_change[i], 0}); outp.push_back({0, w_change[i]}); } int x = 0, y = 0; for (auto k : outp) { printf("%i %i\n", x, y); x = x + k.first; y = y + k.second; } } int main() { int t; scanf("%i", &t); while (t) { t--; solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1000, maxt = maxn * maxn; int te, n, a[maxn + 5], m, b[maxn + 5], f[maxt + 5], c[maxn + 5], ti, vis[maxn + 5]; bool Make(int *a, bool fl) { int C = 0; for (int i = 1; i <= n; i++) C += a[i]; if (C & 1) return false; C >>= 1; f[0] = true; for (int i = 1; i <= C; i++) f[i] = 0; for (int i = 1; i <= n; i++) for (int j = C; j >= a[i]; j--) if (!f[j] && f[j - a[i]]) f[j] = i; if (!f[C]) return false; c[0] = 0; ti++; for (int x = C; x; x -= a[f[x]]) c[++c[0]] = a[f[x]], vis[f[x]] = ti; int cnt = c[0]; for (int i = 1; i <= n; i++) if (vis[i] < ti) c[++c[0]] = a[i]; if ((cnt <= (n >> 1)) ^ fl) { int j = 0; for (int i = 1; i <= cnt; i++) a[++j] = c[i]; for (int i = cnt + 1; i <= n; i++) a[++j] = c[i]; a[0] = cnt; } else { int j = 0; for (int i = cnt + 1; i <= n; i++) a[++j] = c[i]; for (int i = 1; i <= cnt; i++) a[++j] = c[i]; a[0] = n - cnt; } return true; } inline bool cmp(const int &a, const int &b) { return a > b; } int main() { for (scanf("%d", &te); te; te--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); if (n != m || !Make(a, 0) || !Make(b, 1)) { puts("No"); continue; } sort(a + 1, a + a[0] + 1, cmp); sort(a + a[0] + 1, a + n + 1, cmp); sort(b + 1, b + b[0] + 1); sort(b + b[0] + 1, b + n + 1); puts("Yes"); int x = 0, y = 0; for (int i = 1; i <= a[0]; i++) printf("%d %d\n", x += a[i], y), printf("%d %d\n", x, y += b[i]); for (int i = a[0] + 1; i <= b[0]; i++) printf("%d %d\n", x -= a[i], y), printf("%d %d\n", x, y += b[i]); for (int i = b[0] + 1; i <= n; i++) printf("%d %d\n", x -= a[i], y), printf("%d %d\n", x, y -= b[i]); } return 0; }
### Prompt Please create a solution in CPP to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 1000, maxt = maxn * maxn; int te, n, a[maxn + 5], m, b[maxn + 5], f[maxt + 5], c[maxn + 5], ti, vis[maxn + 5]; bool Make(int *a, bool fl) { int C = 0; for (int i = 1; i <= n; i++) C += a[i]; if (C & 1) return false; C >>= 1; f[0] = true; for (int i = 1; i <= C; i++) f[i] = 0; for (int i = 1; i <= n; i++) for (int j = C; j >= a[i]; j--) if (!f[j] && f[j - a[i]]) f[j] = i; if (!f[C]) return false; c[0] = 0; ti++; for (int x = C; x; x -= a[f[x]]) c[++c[0]] = a[f[x]], vis[f[x]] = ti; int cnt = c[0]; for (int i = 1; i <= n; i++) if (vis[i] < ti) c[++c[0]] = a[i]; if ((cnt <= (n >> 1)) ^ fl) { int j = 0; for (int i = 1; i <= cnt; i++) a[++j] = c[i]; for (int i = cnt + 1; i <= n; i++) a[++j] = c[i]; a[0] = cnt; } else { int j = 0; for (int i = cnt + 1; i <= n; i++) a[++j] = c[i]; for (int i = 1; i <= cnt; i++) a[++j] = c[i]; a[0] = n - cnt; } return true; } inline bool cmp(const int &a, const int &b) { return a > b; } int main() { for (scanf("%d", &te); te; te--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); if (n != m || !Make(a, 0) || !Make(b, 1)) { puts("No"); continue; } sort(a + 1, a + a[0] + 1, cmp); sort(a + a[0] + 1, a + n + 1, cmp); sort(b + 1, b + b[0] + 1); sort(b + b[0] + 1, b + n + 1); puts("Yes"); int x = 0, y = 0; for (int i = 1; i <= a[0]; i++) printf("%d %d\n", x += a[i], y), printf("%d %d\n", x, y += b[i]); for (int i = a[0] + 1; i <= b[0]; i++) printf("%d %d\n", x -= a[i], y), printf("%d %d\n", x, y += b[i]); for (int i = b[0] + 1; i <= n; i++) printf("%d %d\n", x -= a[i], y), printf("%d %d\n", x, y -= b[i]); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int l[1000], p[1000]; bitset<500001> poss[1001]; int main() { int i; int t, h, v; scanf("%d", &t); while (t--) { scanf("%d", &h); for (i = 0; i < h; i++) scanf("%d", &l[i]); scanf("%d", &v); for (i = 0; i < v; i++) scanf("%d", &p[i]); if (h != v) { printf("No\n"); continue; } int sl = 0, sp = 0; for (i = 0; i < h; i++) sl += l[i], sp += p[i]; if ((sl & 1) || (sp & 1)) { printf("No\n"); continue; } poss[0][0] = 1; for (i = 0; i < h; i++) poss[i + 1] = poss[i] | (poss[i] << l[i]); if (!poss[h][sl / 2]) { printf("No\n"); continue; } int u = sl / 2; vector<int> ll, pp; for (i = h - 1; i >= 0; i--) { if ((u >= l[i]) && poss[i][u - l[i]]) ll.push_back(l[i]), u -= l[i]; else ll.push_back(-l[i]); } poss[0][0] = 1; for (i = 0; i < h; i++) poss[i + 1] = poss[i] | (poss[i] << p[i]); if (!poss[h][sp / 2]) { printf("No\n"); continue; } u = sp / 2; for (i = h - 1; i >= 0; i--) { if ((u >= p[i]) && poss[i][u - p[i]]) pp.push_back(p[i]), u -= p[i]; else pp.push_back(-p[i]); } int pos1 = 0, pos2 = 0; for (i = 0; i < ll.size(); i++) { if (ll[i] > 0) pos1++; } for (i = 0; i < pp.size(); i++) { if (pp[i] > 0) pos2++; } int s = 0; if (pos1 > pos2) swap(ll, pp), s = 1; sort(pp.begin(), pp.end(), greater<int>()); for (i = 0; i < pp.size(); i++) { if (pp[i] < 0) { reverse(pp.begin(), pp.begin() + i); break; } } sort(ll.begin(), ll.end(), greater<int>()); for (i = 0; i < ll.size(); i++) { if (ll[i] < 0) { reverse(ll.begin() + i, ll.end()); break; } } printf("Yes\n"); int x = 0, y = 0; for (i = 0; i < h + v; i++) { if (i & 1) y += pp[i / 2]; else x += ll[i / 2]; if (s) printf("%d %d\n", y, x); else printf("%d %d\n", x, y); } } return 0; }
### Prompt Create a solution in cpp for the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; int l[1000], p[1000]; bitset<500001> poss[1001]; int main() { int i; int t, h, v; scanf("%d", &t); while (t--) { scanf("%d", &h); for (i = 0; i < h; i++) scanf("%d", &l[i]); scanf("%d", &v); for (i = 0; i < v; i++) scanf("%d", &p[i]); if (h != v) { printf("No\n"); continue; } int sl = 0, sp = 0; for (i = 0; i < h; i++) sl += l[i], sp += p[i]; if ((sl & 1) || (sp & 1)) { printf("No\n"); continue; } poss[0][0] = 1; for (i = 0; i < h; i++) poss[i + 1] = poss[i] | (poss[i] << l[i]); if (!poss[h][sl / 2]) { printf("No\n"); continue; } int u = sl / 2; vector<int> ll, pp; for (i = h - 1; i >= 0; i--) { if ((u >= l[i]) && poss[i][u - l[i]]) ll.push_back(l[i]), u -= l[i]; else ll.push_back(-l[i]); } poss[0][0] = 1; for (i = 0; i < h; i++) poss[i + 1] = poss[i] | (poss[i] << p[i]); if (!poss[h][sp / 2]) { printf("No\n"); continue; } u = sp / 2; for (i = h - 1; i >= 0; i--) { if ((u >= p[i]) && poss[i][u - p[i]]) pp.push_back(p[i]), u -= p[i]; else pp.push_back(-p[i]); } int pos1 = 0, pos2 = 0; for (i = 0; i < ll.size(); i++) { if (ll[i] > 0) pos1++; } for (i = 0; i < pp.size(); i++) { if (pp[i] > 0) pos2++; } int s = 0; if (pos1 > pos2) swap(ll, pp), s = 1; sort(pp.begin(), pp.end(), greater<int>()); for (i = 0; i < pp.size(); i++) { if (pp[i] < 0) { reverse(pp.begin(), pp.begin() + i); break; } } sort(ll.begin(), ll.end(), greater<int>()); for (i = 0; i < ll.size(); i++) { if (ll[i] < 0) { reverse(ll.begin() + i, ll.end()); break; } } printf("Yes\n"); int x = 0, y = 0; for (i = 0; i < h + v; i++) { if (i & 1) y += pp[i / 2]; else x += ll[i / 2]; if (s) printf("%d %d\n", y, x); else printf("%d %d\n", x, y); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chkmax(T1 &a, T2 b) { if (a < b) a = b; } using ll = long long; using ld = long double; const string FILENAME = "input"; const int MAXN = 1005; int h; int l[MAXN]; int v; int p[MAXN]; bitset<1000228> can[MAXN]; void solve() { cin >> h; int sum = 0; for (int i = 0; i < h; i++) { cin >> l[i]; sum += l[i]; } cin >> v; int sum1 = 0; for (int i = 0; i < v; i++) { cin >> p[i]; sum1 += p[i]; } if (h != v || sum & 1 || sum1 & 1) { cout << "No\n"; return; } can[0].reset(); can[0][0] = true; for (int i = 0; i < h; i++) { can[i + 1] = (can[i] << l[i]) | can[i]; } if (!can[h][sum >> 1]) { cout << "No\n"; return; } int need = sum / 2; vector<int> st, st1; for (int i = h; i >= 1; i--) { if (need >= l[i - 1] && can[i - 1][need - l[i - 1]]) { st.push_back(l[i - 1]); need -= l[i - 1]; } else { st1.push_back(l[i - 1]); } } can[0].reset(); can[0][0] = true; for (int i = 0; i < v; i++) { can[i + 1] = (can[i] << p[i]) | can[i]; } if (!can[v][sum1 >> 1]) { cout << "No\n"; return; } need = sum1 / 2; vector<int> st2, st3; for (int i = v; i >= 1; i--) { if (need >= p[i - 1] && can[i - 1][need - p[i - 1]]) { st2.push_back(p[i - 1]); need -= p[i - 1]; } else { st3.push_back(p[i - 1]); } } sort((st).begin(), (st).end()); sort((st1).begin(), (st1).end()); sort((st2).begin(), (st2).end()); sort((st3).begin(), (st3).end()); if ((int)(st).size() < (int)(st1).size()) { swap(st, st1); } if ((int)(st2).size() < (int)(st3).size()) { swap(st2, st3); } cout << "Yes\n"; if ((int)(st).size() > (int)(st2).size()) { reverse((st2).begin(), (st2).end()); reverse((st1).begin(), (st1).end()); reverse((st).begin(), (st).end()); int cx = 0; int cy = 0; for (int i = 0; i < (int)(st2).size(); i++) { cy += st2[i]; cout << cx << ' ' << cy << '\n'; cx += st.back(); cout << cx << ' ' << cy << '\n'; st.pop_back(); } for (int i = 0; i < (int)(st3).size(); i++) { cy -= st3[i]; cout << cx << ' ' << cy << '\n'; if (!st.empty()) { cx += st.back(); cout << cx << ' ' << cy << '\n'; st.pop_back(); } else { cx -= st1.back(); cout << cx << ' ' << cy << '\n'; st1.pop_back(); } } } else { reverse((st).begin(), (st).end()); reverse((st3).begin(), (st3).end()); reverse((st2).begin(), (st2).end()); int cx = 0; int cy = 0; for (int i = 0; i < (int)(st).size(); i++) { cx += st[i]; cout << cx << ' ' << cy << '\n'; cy += st2.back(); cout << cx << ' ' << cy << '\n'; st2.pop_back(); } for (int i = 0; i < (int)(st1).size(); i++) { cx -= st1[i]; cout << cx << ' ' << cy << '\n'; if (!st2.empty()) { cy += st2.back(); cout << cx << ' ' << cy << '\n'; st2.pop_back(); } else { cy -= st3.back(); cout << cx << ' ' << cy << '\n'; st3.pop_back(); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } }
### Prompt Construct a CPP code solution to the problem outlined: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chkmax(T1 &a, T2 b) { if (a < b) a = b; } using ll = long long; using ld = long double; const string FILENAME = "input"; const int MAXN = 1005; int h; int l[MAXN]; int v; int p[MAXN]; bitset<1000228> can[MAXN]; void solve() { cin >> h; int sum = 0; for (int i = 0; i < h; i++) { cin >> l[i]; sum += l[i]; } cin >> v; int sum1 = 0; for (int i = 0; i < v; i++) { cin >> p[i]; sum1 += p[i]; } if (h != v || sum & 1 || sum1 & 1) { cout << "No\n"; return; } can[0].reset(); can[0][0] = true; for (int i = 0; i < h; i++) { can[i + 1] = (can[i] << l[i]) | can[i]; } if (!can[h][sum >> 1]) { cout << "No\n"; return; } int need = sum / 2; vector<int> st, st1; for (int i = h; i >= 1; i--) { if (need >= l[i - 1] && can[i - 1][need - l[i - 1]]) { st.push_back(l[i - 1]); need -= l[i - 1]; } else { st1.push_back(l[i - 1]); } } can[0].reset(); can[0][0] = true; for (int i = 0; i < v; i++) { can[i + 1] = (can[i] << p[i]) | can[i]; } if (!can[v][sum1 >> 1]) { cout << "No\n"; return; } need = sum1 / 2; vector<int> st2, st3; for (int i = v; i >= 1; i--) { if (need >= p[i - 1] && can[i - 1][need - p[i - 1]]) { st2.push_back(p[i - 1]); need -= p[i - 1]; } else { st3.push_back(p[i - 1]); } } sort((st).begin(), (st).end()); sort((st1).begin(), (st1).end()); sort((st2).begin(), (st2).end()); sort((st3).begin(), (st3).end()); if ((int)(st).size() < (int)(st1).size()) { swap(st, st1); } if ((int)(st2).size() < (int)(st3).size()) { swap(st2, st3); } cout << "Yes\n"; if ((int)(st).size() > (int)(st2).size()) { reverse((st2).begin(), (st2).end()); reverse((st1).begin(), (st1).end()); reverse((st).begin(), (st).end()); int cx = 0; int cy = 0; for (int i = 0; i < (int)(st2).size(); i++) { cy += st2[i]; cout << cx << ' ' << cy << '\n'; cx += st.back(); cout << cx << ' ' << cy << '\n'; st.pop_back(); } for (int i = 0; i < (int)(st3).size(); i++) { cy -= st3[i]; cout << cx << ' ' << cy << '\n'; if (!st.empty()) { cx += st.back(); cout << cx << ' ' << cy << '\n'; st.pop_back(); } else { cx -= st1.back(); cout << cx << ' ' << cy << '\n'; st1.pop_back(); } } } else { reverse((st).begin(), (st).end()); reverse((st3).begin(), (st3).end()); reverse((st2).begin(), (st2).end()); int cx = 0; int cy = 0; for (int i = 0; i < (int)(st).size(); i++) { cx += st[i]; cout << cx << ' ' << cy << '\n'; cy += st2.back(); cout << cx << ' ' << cy << '\n'; st2.pop_back(); } for (int i = 0; i < (int)(st1).size(); i++) { cx -= st1[i]; cout << cx << ' ' << cy << '\n'; if (!st2.empty()) { cy += st2.back(); cout << cx << ' ' << cy << '\n'; st2.pop_back(); } else { cy -= st3.back(); cout << cx << ' ' << cy << '\n'; st3.pop_back(); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; using i64 = long long; const int T = 501; bitset<T * 1000> dp[1111]; int h, w, a[1111], b[1111]; void partition(int n, int *a, vector<int> &plus, vector<int> &minus) { int s = 0; dp[0].reset(); dp[0][0] = 1; for (int i = 0; i < n; i++) s += a[i]; for (int i = 0; i < n; i++) dp[i + 1] = dp[i] | dp[i] << a[i]; if (s % 2 != 0 || !dp[n][s / 2]) return; vector<int> v; if (dp[n][s / 2]) { int t = s / 2, m = n; while (m) { if (dp[m - 1][t]) m -= 1, minus.push_back(a[m]); else m -= 1, t -= a[m], plus.push_back(a[m]); } } } void solve() { scanf("%d", &h); for (int i = 0; i < h; i++) scanf("%d", &a[i]); scanf("%d", &w); for (int i = 0; i < w; i++) scanf("%d", &b[i]); if (h != w) { printf("No\n"); return; } vector<int> hplus, hminus, wplus, wminus; partition(h, a, hplus, hminus); partition(w, b, wplus, wminus); if (hplus.empty() || wplus.empty()) { printf("No\n"); return; } vector<int> RU[2], LU[2], RD[2], LD[2]; while (!hplus.empty() && !wplus.empty()) { RU[0].push_back(hplus.back()); RU[1].push_back(wplus.back()); hplus.pop_back(); wplus.pop_back(); } while (!hminus.empty() && !wplus.empty()) { LU[0].push_back(hminus.back()); LU[1].push_back(wplus.back()); hminus.pop_back(); wplus.pop_back(); } while (!hplus.empty() && !wminus.empty()) { RD[0].push_back(hplus.back()); RD[1].push_back(wminus.back()); hplus.pop_back(); wminus.pop_back(); } while (!hminus.empty() && !wminus.empty()) { LD[0].push_back(hminus.back()); LD[1].push_back(wminus.back()); hminus.pop_back(); wminus.pop_back(); } assert(RU[0].empty() || LU[0].empty() || LD[0].empty() || RD[0].empty()); printf("Yes\n"); int x = 0, y = 0; auto print = [&](vector<int> h, vector<int> w, int dh, int dw) { sort(h.begin(), h.end()); sort(w.begin(), w.end()); reverse(h.begin(), h.end()); for (int i = 0; i < h.size(); i++) { printf("%d %d\n", x, y); x += h[i] * dh; printf("%d %d\n", x, y); y += w[i] * dw; } }; if (RD[0].empty() || LU[0].empty()) { print(RU[0], RU[1], 1, 1); print(LU[0], LU[1], -1, 1); print(LD[0], LD[1], -1, -1); print(RD[0], RD[1], 1, -1); } else { print(LU[0], LU[1], 1, -1); print(RU[0], RU[1], -1, -1); print(RD[0], RD[1], -1, 1); print(LD[0], LD[1], 1, 1); } } int main() { int T; scanf("%d", &T); for (int tc = 0; tc < T; tc++) { solve(); } }
### Prompt Generate a Cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; using i64 = long long; const int T = 501; bitset<T * 1000> dp[1111]; int h, w, a[1111], b[1111]; void partition(int n, int *a, vector<int> &plus, vector<int> &minus) { int s = 0; dp[0].reset(); dp[0][0] = 1; for (int i = 0; i < n; i++) s += a[i]; for (int i = 0; i < n; i++) dp[i + 1] = dp[i] | dp[i] << a[i]; if (s % 2 != 0 || !dp[n][s / 2]) return; vector<int> v; if (dp[n][s / 2]) { int t = s / 2, m = n; while (m) { if (dp[m - 1][t]) m -= 1, minus.push_back(a[m]); else m -= 1, t -= a[m], plus.push_back(a[m]); } } } void solve() { scanf("%d", &h); for (int i = 0; i < h; i++) scanf("%d", &a[i]); scanf("%d", &w); for (int i = 0; i < w; i++) scanf("%d", &b[i]); if (h != w) { printf("No\n"); return; } vector<int> hplus, hminus, wplus, wminus; partition(h, a, hplus, hminus); partition(w, b, wplus, wminus); if (hplus.empty() || wplus.empty()) { printf("No\n"); return; } vector<int> RU[2], LU[2], RD[2], LD[2]; while (!hplus.empty() && !wplus.empty()) { RU[0].push_back(hplus.back()); RU[1].push_back(wplus.back()); hplus.pop_back(); wplus.pop_back(); } while (!hminus.empty() && !wplus.empty()) { LU[0].push_back(hminus.back()); LU[1].push_back(wplus.back()); hminus.pop_back(); wplus.pop_back(); } while (!hplus.empty() && !wminus.empty()) { RD[0].push_back(hplus.back()); RD[1].push_back(wminus.back()); hplus.pop_back(); wminus.pop_back(); } while (!hminus.empty() && !wminus.empty()) { LD[0].push_back(hminus.back()); LD[1].push_back(wminus.back()); hminus.pop_back(); wminus.pop_back(); } assert(RU[0].empty() || LU[0].empty() || LD[0].empty() || RD[0].empty()); printf("Yes\n"); int x = 0, y = 0; auto print = [&](vector<int> h, vector<int> w, int dh, int dw) { sort(h.begin(), h.end()); sort(w.begin(), w.end()); reverse(h.begin(), h.end()); for (int i = 0; i < h.size(); i++) { printf("%d %d\n", x, y); x += h[i] * dh; printf("%d %d\n", x, y); y += w[i] * dw; } }; if (RD[0].empty() || LU[0].empty()) { print(RU[0], RU[1], 1, 1); print(LU[0], LU[1], -1, 1); print(LD[0], LD[1], -1, -1); print(RD[0], RD[1], 1, -1); } else { print(LU[0], LU[1], 1, -1); print(RU[0], RU[1], -1, -1); print(RD[0], RD[1], -1, 1); print(LD[0], LD[1], 1, 1); } } int main() { int T; scanf("%d", &T); for (int tc = 0; tc < T; tc++) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1005; int h[MAXN], v[MAXN]; struct vec { int x, y; bool operator<(const vec& other) const { return y * other.x < x * other.y; } }; bitset<MAXN * MAXN> dph[MAXN + 5]; bitset<MAXN * MAXN> dpv[MAXN + 5]; void solve() { int n, sh = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &h[i]); sh += h[i]; } int m, sv = 0; scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d", &v[i]); sv += v[i]; } if (n != m || sh % 2 || sv % 2) { printf("NO\n"); return; } dph[0][0] = 1; for (int i = 1; i <= n; ++i) { dph[i].reset(); dph[i] = (dph[i - 1] | (dph[i - 1] << h[i])); } if (dph[n][sh / 2] == 0) { printf("NO\n"); return; } vector<int> h1, h2; sh /= 2; for (int i = n; i >= 1; --i) { if (sh >= h[i] && dph[i - 1][sh - h[i]]) { sh -= h[i]; h1.push_back(h[i]); } else { h2.push_back(h[i]); } } if (h1.size() > h2.size()) swap(h1, h2); dpv[0][0] = 1; for (int i = 1; i <= m; ++i) { dpv[i].reset(); dpv[i] = (dpv[i - 1] | (dpv[i - 1] << v[i])); } if (dpv[m][sv / 2] == 0) { printf("NO\n"); return; } vector<int> v1, v2; sv /= 2; for (int i = m; i >= 1; --i) { if (sv >= v[i] && dpv[i - 1][sv - v[i]]) { sv -= v[i]; v1.push_back(v[i]); } else { v2.push_back(v[i]); } } if (v1.size() > v2.size()) swap(v1, v2); vector<vec> a, b, c; for (int i = 0; i < h1.size(); ++i) a.push_back({h1[i], v2[i]}); for (int i = h1.size(); i < v2.size(); ++i) b.push_back({-h2[i - h1.size()], v2[i]}); for (int i = v2.size() - h1.size(); i < h2.size(); ++i) c.push_back({-h2[i], -v1[i - v2.size() + h1.size()]}); sort(a.begin(), a.end()); sort(c.begin(), c.end()); vector<vec> op; for (auto it : a) op.push_back(it); for (auto it : b) op.push_back(it); for (auto it : c) op.push_back(it); printf("YES\n"); int x = 0, y = 0; for (auto it : op) { printf("%d %d\n", x, y); x += it.x; printf("%d %d\n", x, y); y += it.y; } } int main() { int t; scanf("%d", &t); while (t--) solve(); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1005; int h[MAXN], v[MAXN]; struct vec { int x, y; bool operator<(const vec& other) const { return y * other.x < x * other.y; } }; bitset<MAXN * MAXN> dph[MAXN + 5]; bitset<MAXN * MAXN> dpv[MAXN + 5]; void solve() { int n, sh = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &h[i]); sh += h[i]; } int m, sv = 0; scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d", &v[i]); sv += v[i]; } if (n != m || sh % 2 || sv % 2) { printf("NO\n"); return; } dph[0][0] = 1; for (int i = 1; i <= n; ++i) { dph[i].reset(); dph[i] = (dph[i - 1] | (dph[i - 1] << h[i])); } if (dph[n][sh / 2] == 0) { printf("NO\n"); return; } vector<int> h1, h2; sh /= 2; for (int i = n; i >= 1; --i) { if (sh >= h[i] && dph[i - 1][sh - h[i]]) { sh -= h[i]; h1.push_back(h[i]); } else { h2.push_back(h[i]); } } if (h1.size() > h2.size()) swap(h1, h2); dpv[0][0] = 1; for (int i = 1; i <= m; ++i) { dpv[i].reset(); dpv[i] = (dpv[i - 1] | (dpv[i - 1] << v[i])); } if (dpv[m][sv / 2] == 0) { printf("NO\n"); return; } vector<int> v1, v2; sv /= 2; for (int i = m; i >= 1; --i) { if (sv >= v[i] && dpv[i - 1][sv - v[i]]) { sv -= v[i]; v1.push_back(v[i]); } else { v2.push_back(v[i]); } } if (v1.size() > v2.size()) swap(v1, v2); vector<vec> a, b, c; for (int i = 0; i < h1.size(); ++i) a.push_back({h1[i], v2[i]}); for (int i = h1.size(); i < v2.size(); ++i) b.push_back({-h2[i - h1.size()], v2[i]}); for (int i = v2.size() - h1.size(); i < h2.size(); ++i) c.push_back({-h2[i], -v1[i - v2.size() + h1.size()]}); sort(a.begin(), a.end()); sort(c.begin(), c.end()); vector<vec> op; for (auto it : a) op.push_back(it); for (auto it : b) op.push_back(it); for (auto it : c) op.push_back(it); printf("YES\n"); int x = 0, y = 0; for (auto it : op) { printf("%d %d\n", x, y); x += it.x; printf("%d %d\n", x, y); y += it.y; } } int main() { int t; scanf("%d", &t); while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using std::bitset; using std::sort; using std::swap; using std::vector; const int N = 1005, M = 185; int T, n, m, a[N], b[N], t[N]; bitset<N * N> f[N]; bitset<M * N> g[M]; vector<int> la, ra, lb, rb; int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); if (n != m) { puts("No"); continue; } else if (n <= 180) { int sum = 0; for (int i = 1; i <= n; i++) sum += a[i], t[i] = 0; if (sum & 1) { puts("No"); continue; } g[0].reset(); g[0][0] = 1; for (int i = 1; i <= n; i++) g[i] = g[i - 1] << a[i] | g[i - 1]; if (g[n][sum / 2]) { for (int i = n, now = sum / 2; i; i--) if (a[i] <= now && g[i - 1][now - a[i]]) t[i] = 1, now -= a[i]; } else { puts("No"); continue; } la.clear(), ra.clear(); for (int i = 1; i <= n; i++) t[i] ? la.push_back(a[i]) : ra.push_back(a[i]); sum = 0; for (int i = 1; i <= n; i++) sum += b[i], t[i] = 0; if (sum & 1) { puts("No"); continue; } g[0].reset(); g[0][0] = 1; for (int i = 1; i <= n; i++) g[i] = g[i - 1] << b[i] | g[i - 1]; if (g[n][sum / 2]) { for (int i = n, now = sum / 2; i; i--) if (b[i] <= now && g[i - 1][now - b[i]]) t[i] = 1, now -= b[i]; } else { puts("No"); continue; } lb.clear(), rb.clear(); for (int i = 1; i <= n; i++) t[i] ? lb.push_back(b[i]) : rb.push_back(b[i]); } else { int sum = 0; for (int i = 1; i <= n; i++) sum += a[i], t[i] = 0; if (sum & 1) { puts("No"); continue; } f[0].reset(); f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] << a[i] | f[i - 1]; if (f[n][sum / 2]) { for (int i = n, now = sum / 2; i; i--) if (a[i] <= now && f[i - 1][now - a[i]]) t[i] = 1, now -= a[i]; } else { puts("No"); continue; } la.clear(), ra.clear(); for (int i = 1; i <= n; i++) t[i] ? la.push_back(a[i]) : ra.push_back(a[i]); sum = 0; for (int i = 1; i <= n; i++) sum += b[i], t[i] = 0; if (sum & 1) { puts("No"); continue; } f[0].reset(); f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] << b[i] | f[i - 1]; if (f[n][sum / 2]) { for (int i = n, now = sum / 2; i; i--) if (b[i] <= now && f[i - 1][now - b[i]]) t[i] = 1, now -= b[i]; } else { puts("No"); continue; } lb.clear(), rb.clear(); for (int i = 1; i <= n; i++) t[i] ? lb.push_back(b[i]) : rb.push_back(b[i]); } puts("Yes"); sort(la.begin(), la.end()); sort(lb.begin(), lb.end()); sort(ra.begin(), ra.end()); sort(rb.begin(), rb.end()); int x = 0, y = 0; if (la.size() > ra.size()) swap(la, ra); if (lb.size() < rb.size()) swap(lb, rb); for (int i = 0; i < n; i++) { int dx, dy; if (i < la.size()) dx = la[la.size() - i - 1]; else dx = -ra[ra.size() - (i - la.size()) - 1]; if (i < lb.size()) dy = lb[i]; else dy = -rb[i - lb.size()]; x += dx; printf("%d %d\n", x, y); y += dy; printf("%d %d\n", x, y); } } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using std::bitset; using std::sort; using std::swap; using std::vector; const int N = 1005, M = 185; int T, n, m, a[N], b[N], t[N]; bitset<N * N> f[N]; bitset<M * N> g[M]; vector<int> la, ra, lb, rb; int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); if (n != m) { puts("No"); continue; } else if (n <= 180) { int sum = 0; for (int i = 1; i <= n; i++) sum += a[i], t[i] = 0; if (sum & 1) { puts("No"); continue; } g[0].reset(); g[0][0] = 1; for (int i = 1; i <= n; i++) g[i] = g[i - 1] << a[i] | g[i - 1]; if (g[n][sum / 2]) { for (int i = n, now = sum / 2; i; i--) if (a[i] <= now && g[i - 1][now - a[i]]) t[i] = 1, now -= a[i]; } else { puts("No"); continue; } la.clear(), ra.clear(); for (int i = 1; i <= n; i++) t[i] ? la.push_back(a[i]) : ra.push_back(a[i]); sum = 0; for (int i = 1; i <= n; i++) sum += b[i], t[i] = 0; if (sum & 1) { puts("No"); continue; } g[0].reset(); g[0][0] = 1; for (int i = 1; i <= n; i++) g[i] = g[i - 1] << b[i] | g[i - 1]; if (g[n][sum / 2]) { for (int i = n, now = sum / 2; i; i--) if (b[i] <= now && g[i - 1][now - b[i]]) t[i] = 1, now -= b[i]; } else { puts("No"); continue; } lb.clear(), rb.clear(); for (int i = 1; i <= n; i++) t[i] ? lb.push_back(b[i]) : rb.push_back(b[i]); } else { int sum = 0; for (int i = 1; i <= n; i++) sum += a[i], t[i] = 0; if (sum & 1) { puts("No"); continue; } f[0].reset(); f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] << a[i] | f[i - 1]; if (f[n][sum / 2]) { for (int i = n, now = sum / 2; i; i--) if (a[i] <= now && f[i - 1][now - a[i]]) t[i] = 1, now -= a[i]; } else { puts("No"); continue; } la.clear(), ra.clear(); for (int i = 1; i <= n; i++) t[i] ? la.push_back(a[i]) : ra.push_back(a[i]); sum = 0; for (int i = 1; i <= n; i++) sum += b[i], t[i] = 0; if (sum & 1) { puts("No"); continue; } f[0].reset(); f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] << b[i] | f[i - 1]; if (f[n][sum / 2]) { for (int i = n, now = sum / 2; i; i--) if (b[i] <= now && f[i - 1][now - b[i]]) t[i] = 1, now -= b[i]; } else { puts("No"); continue; } lb.clear(), rb.clear(); for (int i = 1; i <= n; i++) t[i] ? lb.push_back(b[i]) : rb.push_back(b[i]); } puts("Yes"); sort(la.begin(), la.end()); sort(lb.begin(), lb.end()); sort(ra.begin(), ra.end()); sort(rb.begin(), rb.end()); int x = 0, y = 0; if (la.size() > ra.size()) swap(la, ra); if (lb.size() < rb.size()) swap(lb, rb); for (int i = 0; i < n; i++) { int dx, dy; if (i < la.size()) dx = la[la.size() - i - 1]; else dx = -ra[ra.size() - (i - la.size()) - 1]; if (i < lb.size()) dy = lb[i]; else dy = -rb[i - lb.size()]; x += dx; printf("%d %d\n", x, y); y += dy; printf("%d %d\n", x, y); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<bool> clr; vector<int> v, acum; vector<bitset<1000001>> dp; bool divide(int pos, int need) { if (pos == ((int)v.size())) return need == 0; if (need < v[pos] || need > v[pos] + acum.back() - acum[pos]) return false; if (dp[pos][need]) return false; dp[pos].set(need); if (divide(pos + 1, need)) { clr[pos] = 1; return true; } if (divide(pos + 1, need - v[pos])) { clr[pos] = 0; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; for (int T = (0); T < (t); T++) { int nh, nv, sumh = 0, sumv = 0; cin >> nh; vector<int> vh(nh); for (int i = (0); i < (nh); i++) { cin >> vh[i]; sumh += vh[i]; } cin >> nv; vector<int> vv(nv); for (int i = (0); i < (nv); i++) { cin >> vv[i]; sumv += vv[i]; } if (nh != nv || sumh % 2 || sumv % 2) { cout << "No\n"; continue; } sort(vh.begin(), vh.end()); sort(vv.begin(), vv.end()); int n = nh; acum.clear(); acum.resize(n); clr.clear(); clr.resize(n); for (int i = (0); i < (n); i++) { acum[i] = vh[i]; if (i) acum[i] += acum[i - 1]; } v = vh; dp = vector<bitset<1000001>>(n); if (!divide(0, sumh / 2)) { cout << "No\n"; continue; } vector<int> h1, h2, v1, v2; for (int i = (0); i < (n); i++) if (clr[i]) h1.push_back(vh[i]); else h2.push_back(vh[i]); for (int i = (0); i < (n); i++) { acum[i] = vv[i]; if (i) acum[i] += acum[i - 1]; } v = vv; dp = vector<bitset<1000001>>(n); if (!divide(0, sumv / 2)) { cout << "No\n"; continue; } for (int i = (0); i < (n); i++) if (clr[i]) v1.push_back(vv[i]); else v2.push_back(vv[i]); reverse(h1.begin(), h1.end()); reverse(h2.begin(), h2.end()); if (((int)h1.size()) > ((int)h2.size())) swap(h1, h2); if (((int)v1.size()) < ((int)v2.size())) swap(v1, v2); cout << "Yes\n"; int x = 0, y = 0, posh = 0, posv = 0; for (int i = (0); i < (n); i++) { cout << x << ' ' << y << '\n'; if (posh < ((int)h1.size())) x += h1[posh]; else x -= h2[posh - ((int)h1.size())]; cout << x << ' ' << y << '\n'; if (posv < ((int)v1.size())) y += v1[posv]; else y -= v2[posv - ((int)v1.size())]; posh++; posv++; } } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; vector<bool> clr; vector<int> v, acum; vector<bitset<1000001>> dp; bool divide(int pos, int need) { if (pos == ((int)v.size())) return need == 0; if (need < v[pos] || need > v[pos] + acum.back() - acum[pos]) return false; if (dp[pos][need]) return false; dp[pos].set(need); if (divide(pos + 1, need)) { clr[pos] = 1; return true; } if (divide(pos + 1, need - v[pos])) { clr[pos] = 0; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; for (int T = (0); T < (t); T++) { int nh, nv, sumh = 0, sumv = 0; cin >> nh; vector<int> vh(nh); for (int i = (0); i < (nh); i++) { cin >> vh[i]; sumh += vh[i]; } cin >> nv; vector<int> vv(nv); for (int i = (0); i < (nv); i++) { cin >> vv[i]; sumv += vv[i]; } if (nh != nv || sumh % 2 || sumv % 2) { cout << "No\n"; continue; } sort(vh.begin(), vh.end()); sort(vv.begin(), vv.end()); int n = nh; acum.clear(); acum.resize(n); clr.clear(); clr.resize(n); for (int i = (0); i < (n); i++) { acum[i] = vh[i]; if (i) acum[i] += acum[i - 1]; } v = vh; dp = vector<bitset<1000001>>(n); if (!divide(0, sumh / 2)) { cout << "No\n"; continue; } vector<int> h1, h2, v1, v2; for (int i = (0); i < (n); i++) if (clr[i]) h1.push_back(vh[i]); else h2.push_back(vh[i]); for (int i = (0); i < (n); i++) { acum[i] = vv[i]; if (i) acum[i] += acum[i - 1]; } v = vv; dp = vector<bitset<1000001>>(n); if (!divide(0, sumv / 2)) { cout << "No\n"; continue; } for (int i = (0); i < (n); i++) if (clr[i]) v1.push_back(vv[i]); else v2.push_back(vv[i]); reverse(h1.begin(), h1.end()); reverse(h2.begin(), h2.end()); if (((int)h1.size()) > ((int)h2.size())) swap(h1, h2); if (((int)v1.size()) < ((int)v2.size())) swap(v1, v2); cout << "Yes\n"; int x = 0, y = 0, posh = 0, posv = 0; for (int i = (0); i < (n); i++) { cout << x << ' ' << y << '\n'; if (posh < ((int)h1.size())) x += h1[posh]; else x -= h2[posh - ((int)h1.size())]; cout << x << ' ' << y << '\n'; if (posv < ((int)v1.size())) y += v1[posv]; else y -= v2[posv - ((int)v1.size())]; posh++; posv++; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; bitset<250005> b1[501], b2[501]; void solve() { srand(time(NULL)); int h; vector<int> l; int suml = 0; int v; vector<int> p; int sump = 0; scanf("%d", &h); for (int i = 0; i < h; i++) { int first = i + 1; scanf("%d", &first); l.push_back(first); suml += first; } scanf("%d", &v); for (int i = 0; i < v; i++) { int first = i + 1; scanf("%d", &first); p.push_back(first); sump += first; } if (h != v || suml % 2 || sump % 2) { printf("No\n"); return; } for (int i = 0; i <= h; i++) { b1[i] = 0; b2[i] = 0; } b1[0][0] = 1; for (int i = 0; i < h; i++) { b1[i + 1] = b1[i] | (b1[i] << l[i]); } b2[0][0] = 1; for (int i = 0; i < v; i++) { b2[i + 1] = b2[i] | (b2[i] << p[i]); } if (b1[h][suml / 2] && b2[v][sump / 2]) { printf("Yes\n"); vector<int> l1, l2, p1, p2; int now = suml / 2; for (int j = h - 1; j >= 0; j--) { if (now >= l[j] && b1[j][now - l[j]]) { l1.push_back(l[j]); now -= l[j]; } else { l2.push_back(l[j]); } } now = sump / 2; for (int j = h - 1; j >= 0; j--) { if (now >= p[j] && b2[j][now - p[j]]) { p1.push_back(p[j]); now -= p[j]; } else { p2.push_back(p[j]); } } sort(l1.begin(), l1.end()); sort(l2.begin(), l2.end()); if (l1.size() > l2.size()) swap(l1, l2); sort(p1.begin(), p1.end()); sort(p2.begin(), p2.end()); if (p1.size() > p2.size()) swap(p1, p2); if (l1.size() < p1.size()) { reverse(l1.begin(), l1.end()); reverse(l2.begin(), l2.end()); for (auto it : l2) l1.push_back(-it); for (auto it : p2) p1.push_back(-it); printf("0 0\n"); int first = 0, second = 0; for (int i = 0; i < l1.size(); i++) { first += l1[i]; printf("%d %d\n", first, second); second += p1[i]; if (i != l1.size() - 1) printf("%d %d\n", first, second); } } else { reverse(p1.begin(), p1.end()); reverse(p2.begin(), p2.end()); for (auto it : l2) l1.push_back(-it); for (auto it : p2) p1.push_back(-it); printf("0 0\n"); int first = 0, second = 0; for (int i = 0; i < l1.size(); i++) { second += p1[i]; printf("%d %d\n", first, second); first += l1[i]; if (i != l1.size() - 1) printf("%d %d\n", first, second); } } } else printf("No\n"); } int main() { int t = 1; scanf("%d", &t); while (t--) { solve(); } }
### Prompt Your challenge is to write a cpp solution to the following problem: One drew a closed polyline on a plane, that consisted only of vertical and horizontal segments (parallel to the coordinate axes). The segments alternated between horizontal and vertical ones (a horizontal segment was always followed by a vertical one, and vice versa). The polyline did not contain strict self-intersections, which means that in case any two segments shared a common point, that point was an endpoint for both of them (please consult the examples in the notes section). Unfortunately, the polyline was erased, and you only know the lengths of the horizonal and vertical segments. Please construct any polyline matching the description with such segments, or determine that it does not exist. Input The first line contains one integer t (1 ≀ t ≀ 200) β€”the number of test cases. The first line of each test case contains one integer h (1 ≀ h ≀ 1000) β€” the number of horizontal segments. The following line contains h integers l_1, l_2, ..., l_h (1 ≀ l_i ≀ 1000) β€” lengths of the horizontal segments of the polyline, in arbitrary order. The following line contains an integer v (1 ≀ v ≀ 1000) β€” the number of vertical segments, which is followed by a line containing v integers p_1, p_2, ..., p_v (1 ≀ p_i ≀ 1000) β€” lengths of the vertical segments of the polyline, in arbitrary order. Test cases are separated by a blank line, and the sum of values h + v over all test cases does not exceed 1000. Output For each test case output Yes, if there exists at least one polyline satisfying the requirements, or No otherwise. If it does exist, in the following n lines print the coordinates of the polyline vertices, in order of the polyline traversal: the i-th line should contain two integers x_i and y_i β€” coordinates of the i-th vertex. Note that, each polyline segment must be either horizontal or vertical, and the segments should alternate between horizontal and vertical. The coordinates should not exceed 10^9 by their absolute value. Examples Input 2 2 1 1 2 1 1 2 1 2 2 3 3 Output Yes 1 0 1 1 0 1 0 0 No Input 2 4 1 1 1 1 4 1 1 1 1 3 2 1 1 3 2 1 1 Output Yes 1 0 1 1 2 1 2 2 1 2 1 1 0 1 0 0 Yes 0 -2 2 -2 2 -1 1 -1 1 0 0 0 Input 2 4 1 4 1 2 4 3 4 5 12 4 1 2 3 6 2 1 3 Output Yes 2 0 2 3 3 3 3 7 4 7 4 12 0 12 0 0 No Note In the first test case of the first example, the answer is Yes β€” for example, the following picture illustrates a square that satisfies the requirements: <image> In the first test case of the second example, the desired polyline also exists. Note that, the polyline contains self-intersections, but only in the endpoints: <image> In the second test case of the second example, the desired polyline could be like the one below: <image> Note that the following polyline is not a valid one, since it contains self-intersections that are not endpoints for some of the segments: <image> ### Response ```cpp #include <bits/stdc++.h> using namespace std; bitset<250005> b1[501], b2[501]; void solve() { srand(time(NULL)); int h; vector<int> l; int suml = 0; int v; vector<int> p; int sump = 0; scanf("%d", &h); for (int i = 0; i < h; i++) { int first = i + 1; scanf("%d", &first); l.push_back(first); suml += first; } scanf("%d", &v); for (int i = 0; i < v; i++) { int first = i + 1; scanf("%d", &first); p.push_back(first); sump += first; } if (h != v || suml % 2 || sump % 2) { printf("No\n"); return; } for (int i = 0; i <= h; i++) { b1[i] = 0; b2[i] = 0; } b1[0][0] = 1; for (int i = 0; i < h; i++) { b1[i + 1] = b1[i] | (b1[i] << l[i]); } b2[0][0] = 1; for (int i = 0; i < v; i++) { b2[i + 1] = b2[i] | (b2[i] << p[i]); } if (b1[h][suml / 2] && b2[v][sump / 2]) { printf("Yes\n"); vector<int> l1, l2, p1, p2; int now = suml / 2; for (int j = h - 1; j >= 0; j--) { if (now >= l[j] && b1[j][now - l[j]]) { l1.push_back(l[j]); now -= l[j]; } else { l2.push_back(l[j]); } } now = sump / 2; for (int j = h - 1; j >= 0; j--) { if (now >= p[j] && b2[j][now - p[j]]) { p1.push_back(p[j]); now -= p[j]; } else { p2.push_back(p[j]); } } sort(l1.begin(), l1.end()); sort(l2.begin(), l2.end()); if (l1.size() > l2.size()) swap(l1, l2); sort(p1.begin(), p1.end()); sort(p2.begin(), p2.end()); if (p1.size() > p2.size()) swap(p1, p2); if (l1.size() < p1.size()) { reverse(l1.begin(), l1.end()); reverse(l2.begin(), l2.end()); for (auto it : l2) l1.push_back(-it); for (auto it : p2) p1.push_back(-it); printf("0 0\n"); int first = 0, second = 0; for (int i = 0; i < l1.size(); i++) { first += l1[i]; printf("%d %d\n", first, second); second += p1[i]; if (i != l1.size() - 1) printf("%d %d\n", first, second); } } else { reverse(p1.begin(), p1.end()); reverse(p2.begin(), p2.end()); for (auto it : l2) l1.push_back(-it); for (auto it : p2) p1.push_back(-it); printf("0 0\n"); int first = 0, second = 0; for (int i = 0; i < l1.size(); i++) { second += p1[i]; printf("%d %d\n", first, second); first += l1[i]; if (i != l1.size() - 1) printf("%d %d\n", first, second); } } } else printf("No\n"); } int main() { int t = 1; scanf("%d", &t); while (t--) { solve(); } } ```