output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; int test, n, m, a[1111], b[1111], sum, cur, tt, xx, yy, ct; bitset<1000001> dp[1111]; vector<int> ax, ay, bx, by; void add(int x, int y) { if (tt == 0) printf("%d %d\n", x, y); else printf("%d %d\n", y, x); } int main() { scanf("%d", &test); while (test--) { 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) { printf("No\n"); continue; } ax.clear(); ay.clear(); bx.clear(); by.clear(); for (int i = 0; i <= n; i++) dp[i].reset(); sum = 0; for (int i = 1; i <= n; i++) sum += a[i]; dp[0][0] = 1; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); } if ((sum & 1) || !dp[n][sum >> 1]) { printf("No\n"); continue; } cur = (sum >> 1); for (int i = n; i >= 1; i--) { if (dp[i - 1][cur]) { ay.push_back(a[i]); } else { cur -= a[i]; ax.push_back(a[i]); } } for (int i = 0; i <= m; i++) dp[i].reset(); sum = 0; for (int i = 1; i <= m; i++) sum += b[i]; dp[0][0] = 1; for (int i = 1; i <= m; i++) { dp[i] = dp[i - 1] | (dp[i - 1] << b[i]); } if ((sum & 1) || !dp[m][sum >> 1]) { printf("No\n"); continue; } cur = (sum >> 1); for (int i = m; i >= 1; i--) { if (dp[i - 1][cur]) { by.push_back(b[i]); } else { cur -= b[i]; bx.push_back(b[i]); } } printf("Yes\n"); tt = 0; if (ax.size() > bx.size()) { tt = 1; swap(ax, bx); swap(ay, by); } sort(ax.begin(), ax.end()); reverse(ax.begin(), ax.end()); sort(bx.begin(), bx.end()); sort(ay.begin(), ay.end()); reverse(ay.begin(), ay.end()); sort(by.begin(), by.end()); xx = yy = 0; for (int i = 0; i < ax.size(); i++) { add(xx, yy); xx += ax[i]; add(xx, yy); yy += bx[i]; } ct = 0; for (int i = ax.size(); i < bx.size(); i++) { add(xx, yy); xx -= ay[ct]; ct++; add(xx, yy); yy += bx[i]; } for (int i = 0; i < by.size(); i++) { add(xx, yy); xx -= ay[ct]; ct++; add(xx, yy); yy -= by[i]; } } 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 test, n, m, a[1111], b[1111], sum, cur, tt, xx, yy, ct; bitset<1000001> dp[1111]; vector<int> ax, ay, bx, by; void add(int x, int y) { if (tt == 0) printf("%d %d\n", x, y); else printf("%d %d\n", y, x); } int main() { scanf("%d", &test); while (test--) { 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) { printf("No\n"); continue; } ax.clear(); ay.clear(); bx.clear(); by.clear(); for (int i = 0; i <= n; i++) dp[i].reset(); sum = 0; for (int i = 1; i <= n; i++) sum += a[i]; dp[0][0] = 1; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); } if ((sum & 1) || !dp[n][sum >> 1]) { printf("No\n"); continue; } cur = (sum >> 1); for (int i = n; i >= 1; i--) { if (dp[i - 1][cur]) { ay.push_back(a[i]); } else { cur -= a[i]; ax.push_back(a[i]); } } for (int i = 0; i <= m; i++) dp[i].reset(); sum = 0; for (int i = 1; i <= m; i++) sum += b[i]; dp[0][0] = 1; for (int i = 1; i <= m; i++) { dp[i] = dp[i - 1] | (dp[i - 1] << b[i]); } if ((sum & 1) || !dp[m][sum >> 1]) { printf("No\n"); continue; } cur = (sum >> 1); for (int i = m; i >= 1; i--) { if (dp[i - 1][cur]) { by.push_back(b[i]); } else { cur -= b[i]; bx.push_back(b[i]); } } printf("Yes\n"); tt = 0; if (ax.size() > bx.size()) { tt = 1; swap(ax, bx); swap(ay, by); } sort(ax.begin(), ax.end()); reverse(ax.begin(), ax.end()); sort(bx.begin(), bx.end()); sort(ay.begin(), ay.end()); reverse(ay.begin(), ay.end()); sort(by.begin(), by.end()); xx = yy = 0; for (int i = 0; i < ax.size(); i++) { add(xx, yy); xx += ax[i]; add(xx, yy); yy += bx[i]; } ct = 0; for (int i = ax.size(); i < bx.size(); i++) { add(xx, yy); xx -= ay[ct]; ct++; add(xx, yy); yy += bx[i]; } for (int i = 0; i < by.size(); i++) { add(xx, yy); xx -= ay[ct]; ct++; add(xx, yy); yy -= by[i]; } } return 0; } ```
// not completely proven (see comments in code) #if not LOCAL #define NDEBUG 1 #endif #include<bits/stdc++.h> void up(); int main(){ std::ios::sync_with_stdio(0);std::cin.tie(0); int numTest; std::cin>>numTest; while(numTest--) up(); } bool split(std::vector<int>& data) { // flip signs of data such that the sum is 0. for(auto it: data) assert(it>0); int sum=std::accumulate(begin(data), end(data), 0); if(sum%2!=0) return false; int half=sum/2; int const maxHalf=500000; assert(half<=maxHalf); using B=std::bitset<maxHalf+1>; static std::vector<B> table; table.resize(data.size()+1); // memory consumption: 500000/8*1000 = 500/8 MB table[0][0]=true; // it's unnecessary to set table[0]={} because the other bits are not set anywhere for(int index=1; index<=(int)data.size(); ++index) table[index]=table[index-1]|table[index-1]<<data[index-1]; if(not table[data.size()][half]) return false; int curSum=half; for(int index=(int)data.size(); index--;){ assert(table[index+1][curSum]); if(not table[index][curSum]){ assert(data[index]>0); curSum-=data[index]; data[index]=-data[index]; assert(table[index][curSum]); } } assert(curSum==0); return true; } void up(){ // okay... int n; std::cin>>n; std::vector<int> horizontal(n); for(auto& it: horizontal) { std::cin>>it; } int n_; std::cin>>n_; std::vector<int> vertical(n_); for(auto& it: vertical) { std::cin>>it; } if(n!=n_ or not split(horizontal) or not split(vertical)){ std::cout<<"No\n"; return; } auto horizontalSorted=horizontal, verticalSorted=vertical; std::sort(begin(horizontalSorted), end(horizontalSorted)); std::sort(begin(verticalSorted), end(verticalSorted)); struct Segment{int index, coordinate; bool operator<(Segment other) const{return coordinate<other.coordinate;} }; std::vector<Segment> segments; // this is proven to converge, but **not** proven to have a correct result // **or** converge in 10 iterations... for(int _=0; _<10; ++_){ // consider horizontal segments int x=0, y=0; segments.clear(); for(int i=0; i<n; ++i){ segments.push_back({i, y}); x+=horizontal[i]; y+=vertical[i]; } assert(x==0); assert(y==0); std::stable_sort(begin(segments), end(segments)); for(int index=0; index<n; ++index) horizontal[segments[index].index]=horizontalSorted[index]; // consider vertical segments x=0, y=0; segments.clear(); for(int i=0; i<n; ++i){ x+=horizontal[i]; segments.push_back({i, x}); y+=vertical[i]; } assert(x==0); assert(y==0); std::stable_sort(begin(segments), end(segments)); for(int index=0; index<n; ++index) vertical[segments[index].index]=verticalSorted[index]; } std::cout<<"Yes\n"; int x=0, y=0; for(int i=0; i<n; ++i){ std::cout<<x<<' '<<y<<'\n'; x+=horizontal[i]; std::cout<<x<<' '<<y<<'\n'; y+=vertical[i]; } assert(x==0); assert(y==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 // not completely proven (see comments in code) #if not LOCAL #define NDEBUG 1 #endif #include<bits/stdc++.h> void up(); int main(){ std::ios::sync_with_stdio(0);std::cin.tie(0); int numTest; std::cin>>numTest; while(numTest--) up(); } bool split(std::vector<int>& data) { // flip signs of data such that the sum is 0. for(auto it: data) assert(it>0); int sum=std::accumulate(begin(data), end(data), 0); if(sum%2!=0) return false; int half=sum/2; int const maxHalf=500000; assert(half<=maxHalf); using B=std::bitset<maxHalf+1>; static std::vector<B> table; table.resize(data.size()+1); // memory consumption: 500000/8*1000 = 500/8 MB table[0][0]=true; // it's unnecessary to set table[0]={} because the other bits are not set anywhere for(int index=1; index<=(int)data.size(); ++index) table[index]=table[index-1]|table[index-1]<<data[index-1]; if(not table[data.size()][half]) return false; int curSum=half; for(int index=(int)data.size(); index--;){ assert(table[index+1][curSum]); if(not table[index][curSum]){ assert(data[index]>0); curSum-=data[index]; data[index]=-data[index]; assert(table[index][curSum]); } } assert(curSum==0); return true; } void up(){ // okay... int n; std::cin>>n; std::vector<int> horizontal(n); for(auto& it: horizontal) { std::cin>>it; } int n_; std::cin>>n_; std::vector<int> vertical(n_); for(auto& it: vertical) { std::cin>>it; } if(n!=n_ or not split(horizontal) or not split(vertical)){ std::cout<<"No\n"; return; } auto horizontalSorted=horizontal, verticalSorted=vertical; std::sort(begin(horizontalSorted), end(horizontalSorted)); std::sort(begin(verticalSorted), end(verticalSorted)); struct Segment{int index, coordinate; bool operator<(Segment other) const{return coordinate<other.coordinate;} }; std::vector<Segment> segments; // this is proven to converge, but **not** proven to have a correct result // **or** converge in 10 iterations... for(int _=0; _<10; ++_){ // consider horizontal segments int x=0, y=0; segments.clear(); for(int i=0; i<n; ++i){ segments.push_back({i, y}); x+=horizontal[i]; y+=vertical[i]; } assert(x==0); assert(y==0); std::stable_sort(begin(segments), end(segments)); for(int index=0; index<n; ++index) horizontal[segments[index].index]=horizontalSorted[index]; // consider vertical segments x=0, y=0; segments.clear(); for(int i=0; i<n; ++i){ x+=horizontal[i]; segments.push_back({i, x}); y+=vertical[i]; } assert(x==0); assert(y==0); std::stable_sort(begin(segments), end(segments)); for(int index=0; index<n; ++index) vertical[segments[index].index]=verticalSorted[index]; } std::cout<<"Yes\n"; int x=0, y=0; for(int i=0; i<n; ++i){ std::cout<<x<<' '<<y<<'\n'; x+=horizontal[i]; std::cout<<x<<' '<<y<<'\n'; y+=vertical[i]; } assert(x==0); assert(y==0); } ```
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int T, n, m, a[1005], b[1005]; bitset<1005 * 1005> dp[1005]; bool Split(int *a, vector<int> *p) { dp[0][0] = 1; int sum = 0; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); sum += a[i]; } if (sum & 1 || !dp[n][sum >> 1]) return false; for (int i = n, u = sum >> 1; i >= 1; --i) { if (dp[i - 1][u]) { p[0].push_back(a[i]); } else { u -= a[i]; p[1].push_back(a[i]); } } return true; } void Solve() { if (n ^ m) { return (void)printf("No\n"); } vector<int> A[2], B[2], tA, tB; if (!Split(a, A) || !Split(b, B)) return (void)printf("No\n"); if (A[0].size() > A[1].size()) swap(A[0], A[1]); if (B[0].size() < B[1].size()) swap(B[0], B[1]); sort(A[0].begin(), A[0].end(), greater<int>()); sort(A[1].begin(), A[1].end(), greater<int>()); sort(B[0].begin(), B[0].end()); sort(B[1].begin(), B[1].end()); for (auto x : A[0]) tA.push_back(x); for (auto x : A[1]) tA.push_back(-x); for (auto x : B[0]) tB.push_back(x); for (auto x : B[1]) tB.push_back(-x); printf("Yes\n"); for (int i = 0, x = 0, y = 0; i < n; ++i) { x += tA[i]; printf("%d %d\n", x, y); y += tB[i]; printf("%d %d\n", x, y); } } int main() { T = read(); while (T--) { n = read(); for (int i = 1; i <= n; ++i) { a[i] = read(); } m = read(); for (int i = 1; i <= m; ++i) { b[i] = read(); } 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; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int T, n, m, a[1005], b[1005]; bitset<1005 * 1005> dp[1005]; bool Split(int *a, vector<int> *p) { dp[0][0] = 1; int sum = 0; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); sum += a[i]; } if (sum & 1 || !dp[n][sum >> 1]) return false; for (int i = n, u = sum >> 1; i >= 1; --i) { if (dp[i - 1][u]) { p[0].push_back(a[i]); } else { u -= a[i]; p[1].push_back(a[i]); } } return true; } void Solve() { if (n ^ m) { return (void)printf("No\n"); } vector<int> A[2], B[2], tA, tB; if (!Split(a, A) || !Split(b, B)) return (void)printf("No\n"); if (A[0].size() > A[1].size()) swap(A[0], A[1]); if (B[0].size() < B[1].size()) swap(B[0], B[1]); sort(A[0].begin(), A[0].end(), greater<int>()); sort(A[1].begin(), A[1].end(), greater<int>()); sort(B[0].begin(), B[0].end()); sort(B[1].begin(), B[1].end()); for (auto x : A[0]) tA.push_back(x); for (auto x : A[1]) tA.push_back(-x); for (auto x : B[0]) tB.push_back(x); for (auto x : B[1]) tB.push_back(-x); printf("Yes\n"); for (int i = 0, x = 0, y = 0; i < n; ++i) { x += tA[i]; printf("%d %d\n", x, y); y += tB[i]; printf("%d %d\n", x, y); } } int main() { T = read(); while (T--) { n = read(); for (int i = 1; i <= n; ++i) { a[i] = read(); } m = read(); for (int i = 1; i <= m; ++i) { b[i] = read(); } Solve(); } return 0; } ```
#include <bits/stdc++.h> #pragma warning(disable : 4996) template <typename T> T min(T x, T y) { return x < y ? x : y; } template <typename T> T max(T x, T y) { return x > y ? x : y; }; const long long INF = 20000000050000; const long long mod = 1000000007; const int MAXN = 1005; const long long bit_len = 64; const long long all1 = (((long long)1 << (bit_len - 1)) - 1) + ((long long)1 << (bit_len - 1)); struct SOLVE { int N; int len[MAXN], sum, half; std::bitset<MAXN * MAXN / 2> B[MAXN]; std::vector<int> q1, q2; SOLVE() { N = sum = 0; memset(len, 0, sizeof(len)); } void clear() { sum = half = 0; q1.clear(), q2.clear(); } bool init() { scanf("%d", &N); for (int i = 1; i <= N; i++) scanf("%d", len + i), sum += len[i]; if (sum & 1) return false; half = sum / 2; for (int i = 0; i <= N; i++) B[i].reset(); B[0].set(0, 1); for (int i = 1; i <= N; i++) B[i] = (B[i - 1] | (B[i - 1] << len[i])); if (!B[N][half]) return false; return true; } void solve() { int t = half; for (int i = N; i >= 1; i--) { if (B[i][t] && !B[i - 1][t]) { q1.push_back(len[i]); t -= len[i]; } else q2.push_back(len[i]); } if (q1.size() > q2.size()) std::swap(q1, q2); std::sort(q1.begin(), q1.end()); std::sort(q2.begin(), q2.end()); } }; SOLVE solve1, solve2; void print() { static int d[2][MAXN]; static std::pair<double, int> p[MAXN]; int x = 0, y = 0, n0 = 0, n1 = 0, np = 0; printf("YES\n"); for (int i = 0; i < solve1.q1.size(); i++) d[0][++n0] = solve1.q1[i]; for (int i = 0; i < solve1.q2.size(); i++) d[0][++n0] = -solve1.q2[i]; for (int i = 0; i < solve2.q2.size(); i++) d[1][++n1] = solve2.q2[i]; for (int i = 0; i < solve2.q1.size(); i++) d[1][++n1] = -solve2.q1[i]; for (int i = 1; i <= n0; i++) p[++np] = {-atan2(d[0][i], d[1][i]), i}; std::sort(p + 1, p + np + 1); for (int i = 1; i <= np; i++) { x += d[0][p[i].second]; printf("%d %d\n", x, y); y += d[1][p[i].second]; printf("%d %d\n", x, y); } } void work() { solve1.clear(); solve2.clear(); bool flag = false; if (!solve1.init()) flag = true; if (!solve2.init()) flag = true; if (flag || solve1.N != solve2.N) { printf("NO\n"); return; } solve1.solve(); solve2.solve(); print(); } int main() { int t; scanf("%d", &t); while (t--) work(); 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> #pragma warning(disable : 4996) template <typename T> T min(T x, T y) { return x < y ? x : y; } template <typename T> T max(T x, T y) { return x > y ? x : y; }; const long long INF = 20000000050000; const long long mod = 1000000007; const int MAXN = 1005; const long long bit_len = 64; const long long all1 = (((long long)1 << (bit_len - 1)) - 1) + ((long long)1 << (bit_len - 1)); struct SOLVE { int N; int len[MAXN], sum, half; std::bitset<MAXN * MAXN / 2> B[MAXN]; std::vector<int> q1, q2; SOLVE() { N = sum = 0; memset(len, 0, sizeof(len)); } void clear() { sum = half = 0; q1.clear(), q2.clear(); } bool init() { scanf("%d", &N); for (int i = 1; i <= N; i++) scanf("%d", len + i), sum += len[i]; if (sum & 1) return false; half = sum / 2; for (int i = 0; i <= N; i++) B[i].reset(); B[0].set(0, 1); for (int i = 1; i <= N; i++) B[i] = (B[i - 1] | (B[i - 1] << len[i])); if (!B[N][half]) return false; return true; } void solve() { int t = half; for (int i = N; i >= 1; i--) { if (B[i][t] && !B[i - 1][t]) { q1.push_back(len[i]); t -= len[i]; } else q2.push_back(len[i]); } if (q1.size() > q2.size()) std::swap(q1, q2); std::sort(q1.begin(), q1.end()); std::sort(q2.begin(), q2.end()); } }; SOLVE solve1, solve2; void print() { static int d[2][MAXN]; static std::pair<double, int> p[MAXN]; int x = 0, y = 0, n0 = 0, n1 = 0, np = 0; printf("YES\n"); for (int i = 0; i < solve1.q1.size(); i++) d[0][++n0] = solve1.q1[i]; for (int i = 0; i < solve1.q2.size(); i++) d[0][++n0] = -solve1.q2[i]; for (int i = 0; i < solve2.q2.size(); i++) d[1][++n1] = solve2.q2[i]; for (int i = 0; i < solve2.q1.size(); i++) d[1][++n1] = -solve2.q1[i]; for (int i = 1; i <= n0; i++) p[++np] = {-atan2(d[0][i], d[1][i]), i}; std::sort(p + 1, p + np + 1); for (int i = 1; i <= np; i++) { x += d[0][p[i].second]; printf("%d %d\n", x, y); y += d[1][p[i].second]; printf("%d %d\n", x, y); } } void work() { solve1.clear(); solve2.clear(); bool flag = false; if (!solve1.init()) flag = true; if (!solve2.init()) flag = true; if (flag || solve1.N != solve2.N) { printf("NO\n"); return; } solve1.solve(); solve2.solve(); print(); } int main() { int t; scanf("%d", &t); while (t--) work(); return 0; } ```
#include <bits/stdc++.h> void solve() { auto partition = [&](const std::vector<int> &a) { int n = a.size(); std::vector<std::bitset<500001>> f(n + 1); f[0] = 1; int sum = 0; for (int i = 0; i < n; ++i) { f[i + 1] = f[i] | f[i] << a[i]; sum += a[i]; } if (sum & 1 || !f[n][sum / 2]) { return std::make_pair(false, std::vector<int>()); } std::vector<int> ans1, ans2; int now = sum / 2; for (int i = n - 1; i >= 0; --i) { if (!f[i][now]) { ans1.push_back(a[i]); now -= a[i]; } else { ans2.push_back(-a[i]); } } std::sort(ans1.begin(), ans1.end()); std::sort(ans2.begin(), ans2.end(), std::greater<int>()); std::vector<int> ans; ans.insert(ans.end(), ans1.begin(), ans1.end()); ans.insert(ans.end(), ans2.begin(), ans2.end()); return std::make_pair(true, ans); }; std::vector<int> s[2]; bool flag = true; for (int k = 0; k < 2; ++k) { int n; std::cin >> n; std::vector<int> a(n); for (int &v : a) { std::cin >> v; } auto tmp = partition(a); flag &= tmp.first; s[k].swap(tmp.second); } flag &= s[0].size() == s[1].size(); if (!flag) { std::cout << "No\n"; return; } std::reverse(s[0].begin(), s[0].end()); for (int &x : s[0]) { x = -x; } auto get = [&](const std::vector<int> &a) { int p = 0; while (p < (int)a.size() && a[p] > 0) { ++p; } return p; }; auto work = [&](std::vector<int> &a) { int p = 0; while (p < (int)a.size() && a[p] > 0) { ++p; } std::rotate(a.begin(), a.begin() + p, a.end()); for (int &x : a) { x = -x; } }; if (get(s[0]) > get(s[1])) { work(s[0]); work(s[1]); } std::cout << "Yes\n"; int x = 0, y = 0; for (int i = 0; i < (int)s[0].size(); ++i) { x += s[0][i]; std::cout << x << " " << y << "\n"; y += s[1][i]; std::cout << x << " " << y << "\n"; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int T; std::cin >> T; while (T--) { solve(); } }
### 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> void solve() { auto partition = [&](const std::vector<int> &a) { int n = a.size(); std::vector<std::bitset<500001>> f(n + 1); f[0] = 1; int sum = 0; for (int i = 0; i < n; ++i) { f[i + 1] = f[i] | f[i] << a[i]; sum += a[i]; } if (sum & 1 || !f[n][sum / 2]) { return std::make_pair(false, std::vector<int>()); } std::vector<int> ans1, ans2; int now = sum / 2; for (int i = n - 1; i >= 0; --i) { if (!f[i][now]) { ans1.push_back(a[i]); now -= a[i]; } else { ans2.push_back(-a[i]); } } std::sort(ans1.begin(), ans1.end()); std::sort(ans2.begin(), ans2.end(), std::greater<int>()); std::vector<int> ans; ans.insert(ans.end(), ans1.begin(), ans1.end()); ans.insert(ans.end(), ans2.begin(), ans2.end()); return std::make_pair(true, ans); }; std::vector<int> s[2]; bool flag = true; for (int k = 0; k < 2; ++k) { int n; std::cin >> n; std::vector<int> a(n); for (int &v : a) { std::cin >> v; } auto tmp = partition(a); flag &= tmp.first; s[k].swap(tmp.second); } flag &= s[0].size() == s[1].size(); if (!flag) { std::cout << "No\n"; return; } std::reverse(s[0].begin(), s[0].end()); for (int &x : s[0]) { x = -x; } auto get = [&](const std::vector<int> &a) { int p = 0; while (p < (int)a.size() && a[p] > 0) { ++p; } return p; }; auto work = [&](std::vector<int> &a) { int p = 0; while (p < (int)a.size() && a[p] > 0) { ++p; } std::rotate(a.begin(), a.begin() + p, a.end()); for (int &x : a) { x = -x; } }; if (get(s[0]) > get(s[1])) { work(s[0]); work(s[1]); } std::cout << "Yes\n"; int x = 0, y = 0; for (int i = 0; i < (int)s[0].size(); ++i) { x += s[0][i]; std::cout << x << " " << y << "\n"; y += s[1][i]; std::cout << x << " " << y << "\n"; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int T; std::cin >> T; while (T--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1005; int l[N], p[N]; bitset<N * N> f[N]; bool spliter(int a[N], int n, vector<int>& x, vector<int>& y) { int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } if (sum & 1) return false; f[0].set(0); for (int i = 0; i < n; i++) { f[i + 1] = f[i] << a[i]; f[i + 1] |= f[i]; } int cur = sum / 2; if (!f[n][cur]) return false; for (int i = n - 1; i >= 0; i--) { int nxt = cur - a[i]; if (nxt >= 0 && f[i][nxt]) { cur = nxt; x.push_back(a[i]); } else { y.push_back(a[i]); } } assert(cur == 0); return true; } struct point { int x, y; point operator+(const point& p) const { return {x + p.x, y + p.y}; } point& operator+=(const point& p) { x += p.x; y += p.y; return *this; } void print() { printf("%d %d\n", x, y); } }; struct upper { int h, v; void run(point& p) { p.x -= h; p.print(); p.y -= v; p.print(); } bool operator<(const upper& other) const { return h * other.v > v * other.h; } }; struct lower { int h, v; void run(point& p) { p.x += h; p.print(); p.y += v; p.print(); } bool operator<(const lower& other) const { return h * other.v > v * other.h; } }; int main() { int t; scanf("%d", &t); while (t--) { int h, v; scanf("%d", &h); for (int i = 0; i < h; i++) scanf("%d", l + i); scanf("%d", &v); for (int i = 0; i < v; i++) scanf("%d", p + i); if (h != v) { puts("No"); continue; } vector<int>* h1 = new vector<int>(); vector<int>* h2 = new vector<int>(); vector<int>* v1 = new vector<int>(); vector<int>* v2 = new vector<int>(); if (!spliter(l, h, *h1, *h2) || !spliter(p, v, *v1, *v2)) { puts("No"); continue; } if (h1->size() < h2->size()) swap(h1, h2); if (v1->size() < v2->size()) swap(v1, v2); vector<upper> uppers; vector<lower> lowers; for (int i = 0, _ = v2->size(); i < _; i++) { uppers.push_back({(*h1)[i], (*v2)[i]}); } for (int i = 0, _ = h2->size(); i < _; i++) { lowers.push_back({(*h2)[i], (*v1)[i]}); } sort(uppers.begin(), uppers.end()); sort(lowers.begin(), lowers.end()); point cur{0, 0}; puts("Yes"); for (auto& u : lowers) { u.run(cur); } int re = h1->size() - v2->size(); auto hi = h1->begin() + v2->size(); auto vi = v1->begin() + h2->size(); while (re--) { cur.x -= *hi++; cur.print(); cur.y += *vi++; cur.print(); } for (auto& u : uppers) { u.run(cur); } delete h1; delete h2; delete v1; delete v2; } }
### 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 = 1005; int l[N], p[N]; bitset<N * N> f[N]; bool spliter(int a[N], int n, vector<int>& x, vector<int>& y) { int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } if (sum & 1) return false; f[0].set(0); for (int i = 0; i < n; i++) { f[i + 1] = f[i] << a[i]; f[i + 1] |= f[i]; } int cur = sum / 2; if (!f[n][cur]) return false; for (int i = n - 1; i >= 0; i--) { int nxt = cur - a[i]; if (nxt >= 0 && f[i][nxt]) { cur = nxt; x.push_back(a[i]); } else { y.push_back(a[i]); } } assert(cur == 0); return true; } struct point { int x, y; point operator+(const point& p) const { return {x + p.x, y + p.y}; } point& operator+=(const point& p) { x += p.x; y += p.y; return *this; } void print() { printf("%d %d\n", x, y); } }; struct upper { int h, v; void run(point& p) { p.x -= h; p.print(); p.y -= v; p.print(); } bool operator<(const upper& other) const { return h * other.v > v * other.h; } }; struct lower { int h, v; void run(point& p) { p.x += h; p.print(); p.y += v; p.print(); } bool operator<(const lower& other) const { return h * other.v > v * other.h; } }; int main() { int t; scanf("%d", &t); while (t--) { int h, v; scanf("%d", &h); for (int i = 0; i < h; i++) scanf("%d", l + i); scanf("%d", &v); for (int i = 0; i < v; i++) scanf("%d", p + i); if (h != v) { puts("No"); continue; } vector<int>* h1 = new vector<int>(); vector<int>* h2 = new vector<int>(); vector<int>* v1 = new vector<int>(); vector<int>* v2 = new vector<int>(); if (!spliter(l, h, *h1, *h2) || !spliter(p, v, *v1, *v2)) { puts("No"); continue; } if (h1->size() < h2->size()) swap(h1, h2); if (v1->size() < v2->size()) swap(v1, v2); vector<upper> uppers; vector<lower> lowers; for (int i = 0, _ = v2->size(); i < _; i++) { uppers.push_back({(*h1)[i], (*v2)[i]}); } for (int i = 0, _ = h2->size(); i < _; i++) { lowers.push_back({(*h2)[i], (*v1)[i]}); } sort(uppers.begin(), uppers.end()); sort(lowers.begin(), lowers.end()); point cur{0, 0}; puts("Yes"); for (auto& u : lowers) { u.run(cur); } int re = h1->size() - v2->size(); auto hi = h1->begin() + v2->size(); auto vi = v1->begin() + h2->size(); while (re--) { cur.x -= *hi++; cur.print(); cur.y += *vi++; cur.print(); } for (auto& u : uppers) { u.run(cur); } delete h1; delete h2; delete v1; delete v2; } } ```
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { return a.first * 1ll * b.second > b.first * 1ll * a.second; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n, m; cin >> n; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; cin >> m; vector<int> l(m); for (int i = 0; i < m; i++) cin >> l[i]; if (n != m) { cout << "No\n"; continue; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); shuffle(l.begin(), l.end(), rng); shuffle(h.begin(), h.end(), rng); { vector<vector<short>> dp(n + 1, vector<short>(n * 500 + 1, -1)); dp[0][n * 250] = 0; for (int i = 1; i <= n; i++) { int x = h[i - 1]; for (int j = 0; j <= n * 500; j++) { if (dp[i - 1][j] == -1) continue; if (j + x <= n * 500) dp[i][x + j] = 1; if (j - x >= 0) dp[i][j - x] = 0; } } if (dp[n][n * 250] == -1) { cout << "No\n"; continue; } int pos = n * 250; int x = n; while (x--) { if (dp[x + 1][pos] == 0) h[x] = -h[x]; pos -= h[x]; } } { vector<vector<short>> dp(n + 1, vector<short>(n * 500 + 1, -1)); dp[0][n * 250] = 0; for (int i = 1; i <= n; i++) { int x = l[i - 1]; for (int j = 0; j <= n * 500; j++) { if (dp[i - 1][j] == -1) continue; if (j + x <= n * 500) dp[i][x + j] = 1; if (j - x >= 0) dp[i][j - x] = 0; } } if (dp[n][n * 250] == -1) { cout << "No\n"; continue; } int x = n; int pos = n * 250; while (x--) { if (dp[x + 1][pos] == 0) l[x] = -l[x]; pos -= l[x]; } } vector<int> left, right, up, down; for (int i = 0; i < n; i++) { if (h[i] < 0) left.push_back(-h[i]); else right.push_back(h[i]); if (l[i] < 0) up.push_back(-l[i]); else down.push_back(l[i]); } if (right.size() > left.size()) swap(left, right); if (down.size() > up.size()) swap(down, up); vector<pair<int, int>> a, b, c; while (!right.empty()) { a.push_back({right.back(), up.back()}); right.pop_back(), up.pop_back(); } while (!down.empty()) { c.push_back({left.back(), down.back()}); left.pop_back(), down.pop_back(); } while (!left.empty()) { b.push_back({left.back(), up.back()}); left.pop_back(), up.pop_back(); } sort(a.begin(), a.end(), cmp); sort(c.begin(), c.end(), cmp); int x = 0, y = 0; cout << "Yes\n"; for (int i = 0; i < (int)c.size(); i++) { x -= c[i].first; cout << x << ' ' << y << '\n'; y -= c[i].second; cout << x << ' ' << y << '\n'; } for (int i = 0; i < (int)a.size(); i++) { x += a[i].first; cout << x << ' ' << y << '\n'; y += a[i].second; cout << x << ' ' << y << '\n'; } for (int i = 0; i < (int)b.size(); i++) { x -= b[i].first; cout << x << ' ' << y << '\n'; y += b[i].second; cout << x << ' ' << y << '\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> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { return a.first * 1ll * b.second > b.first * 1ll * a.second; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n, m; cin >> n; vector<int> h(n); for (int i = 0; i < n; i++) cin >> h[i]; cin >> m; vector<int> l(m); for (int i = 0; i < m; i++) cin >> l[i]; if (n != m) { cout << "No\n"; continue; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); shuffle(l.begin(), l.end(), rng); shuffle(h.begin(), h.end(), rng); { vector<vector<short>> dp(n + 1, vector<short>(n * 500 + 1, -1)); dp[0][n * 250] = 0; for (int i = 1; i <= n; i++) { int x = h[i - 1]; for (int j = 0; j <= n * 500; j++) { if (dp[i - 1][j] == -1) continue; if (j + x <= n * 500) dp[i][x + j] = 1; if (j - x >= 0) dp[i][j - x] = 0; } } if (dp[n][n * 250] == -1) { cout << "No\n"; continue; } int pos = n * 250; int x = n; while (x--) { if (dp[x + 1][pos] == 0) h[x] = -h[x]; pos -= h[x]; } } { vector<vector<short>> dp(n + 1, vector<short>(n * 500 + 1, -1)); dp[0][n * 250] = 0; for (int i = 1; i <= n; i++) { int x = l[i - 1]; for (int j = 0; j <= n * 500; j++) { if (dp[i - 1][j] == -1) continue; if (j + x <= n * 500) dp[i][x + j] = 1; if (j - x >= 0) dp[i][j - x] = 0; } } if (dp[n][n * 250] == -1) { cout << "No\n"; continue; } int x = n; int pos = n * 250; while (x--) { if (dp[x + 1][pos] == 0) l[x] = -l[x]; pos -= l[x]; } } vector<int> left, right, up, down; for (int i = 0; i < n; i++) { if (h[i] < 0) left.push_back(-h[i]); else right.push_back(h[i]); if (l[i] < 0) up.push_back(-l[i]); else down.push_back(l[i]); } if (right.size() > left.size()) swap(left, right); if (down.size() > up.size()) swap(down, up); vector<pair<int, int>> a, b, c; while (!right.empty()) { a.push_back({right.back(), up.back()}); right.pop_back(), up.pop_back(); } while (!down.empty()) { c.push_back({left.back(), down.back()}); left.pop_back(), down.pop_back(); } while (!left.empty()) { b.push_back({left.back(), up.back()}); left.pop_back(), up.pop_back(); } sort(a.begin(), a.end(), cmp); sort(c.begin(), c.end(), cmp); int x = 0, y = 0; cout << "Yes\n"; for (int i = 0; i < (int)c.size(); i++) { x -= c[i].first; cout << x << ' ' << y << '\n'; y -= c[i].second; cout << x << ' ' << y << '\n'; } for (int i = 0; i < (int)a.size(); i++) { x += a[i].first; cout << x << ' ' << y << '\n'; y += a[i].second; cout << x << ' ' << y << '\n'; } for (int i = 0; i < (int)b.size(); i++) { x -= b[i].first; cout << x << ' ' << y << '\n'; y += b[i].second; cout << x << ' ' << y << '\n'; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1, c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f == 1 ? x : -x; } const int N = 1004; bitset<N * N> bit[N]; int n, m, sum, a[N], b[N]; vector<int> a1, a2, b1, b2; inline void solve() { a1.clear(); a2.clear(); b1.clear(); b2.clear(); n = read(); for (int i = 1; i <= n; i++) a[i] = read(); m = read(); for (int i = 1; i <= m; i++) b[i] = read(); if (n != m) { puts("NO"); return; } sum = 0; bit[0].reset(); bit[0][0] = 1; for (int i = 1; i <= n; i++) { sum += a[i]; bit[i] = bit[i - 1] | (bit[i - 1] << a[i]); } if ((sum & 1) || !bit[n][sum >> 1]) { puts("NO"); return; } sum >>= 1; for (int i = n; i; i--) { if (bit[i - 1][sum]) a2.push_back(a[i]); else { a1.push_back(a[i]); sum -= a[i]; } } if (a1.size() > a2.size()) swap(a1, a2); sum = 0; bit[0].reset(); bit[0][0] = 1; for (int i = 1; i <= n; i++) { sum += b[i]; bit[i] = bit[i - 1] | (bit[i - 1] << b[i]); } if ((sum & 1) || !bit[n][sum >> 1]) { puts("NO"); return; } sum >>= 1; for (int i = n; i; i--) { if (bit[i - 1][sum]) b2.push_back(b[i]); else { b1.push_back(b[i]); sum -= b[i]; } } if (b1.size() < b2.size()) swap(b1, b2); puts("YES"); sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); sort(b1.begin(), b1.end()); sort(b2.begin(), b2.end()); reverse(b2.begin(), b2.end()); reverse(b1.begin(), b1.end()); for (int i = 1, x = 0, y = 0; i <= n; i++) { if (a1.empty()) { x -= *a2.rbegin(); a2.pop_back(); } else { x += *a1.rbegin(); a1.pop_back(); } cout << x << " " << y << "\n"; if (b1.empty()) { y -= *b2.rbegin(); b2.pop_back(); } else { y += *b1.rbegin(); b1.pop_back(); } cout << x << " " << y << "\n"; } } int main() { for (int T = read(); T--;) solve(); return (0 - 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; inline int read() { int x = 0, f = 1, c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f == 1 ? x : -x; } const int N = 1004; bitset<N * N> bit[N]; int n, m, sum, a[N], b[N]; vector<int> a1, a2, b1, b2; inline void solve() { a1.clear(); a2.clear(); b1.clear(); b2.clear(); n = read(); for (int i = 1; i <= n; i++) a[i] = read(); m = read(); for (int i = 1; i <= m; i++) b[i] = read(); if (n != m) { puts("NO"); return; } sum = 0; bit[0].reset(); bit[0][0] = 1; for (int i = 1; i <= n; i++) { sum += a[i]; bit[i] = bit[i - 1] | (bit[i - 1] << a[i]); } if ((sum & 1) || !bit[n][sum >> 1]) { puts("NO"); return; } sum >>= 1; for (int i = n; i; i--) { if (bit[i - 1][sum]) a2.push_back(a[i]); else { a1.push_back(a[i]); sum -= a[i]; } } if (a1.size() > a2.size()) swap(a1, a2); sum = 0; bit[0].reset(); bit[0][0] = 1; for (int i = 1; i <= n; i++) { sum += b[i]; bit[i] = bit[i - 1] | (bit[i - 1] << b[i]); } if ((sum & 1) || !bit[n][sum >> 1]) { puts("NO"); return; } sum >>= 1; for (int i = n; i; i--) { if (bit[i - 1][sum]) b2.push_back(b[i]); else { b1.push_back(b[i]); sum -= b[i]; } } if (b1.size() < b2.size()) swap(b1, b2); puts("YES"); sort(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); sort(b1.begin(), b1.end()); sort(b2.begin(), b2.end()); reverse(b2.begin(), b2.end()); reverse(b1.begin(), b1.end()); for (int i = 1, x = 0, y = 0; i <= n; i++) { if (a1.empty()) { x -= *a2.rbegin(); a2.pop_back(); } else { x += *a1.rbegin(); a1.pop_back(); } cout << x << " " << y << "\n"; if (b1.empty()) { y -= *b2.rbegin(); b2.pop_back(); } else { y += *b1.rbegin(); b1.pop_back(); } cout << x << " " << y << "\n"; } } int main() { for (int T = read(); T--;) solve(); return (0 - 0); } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 3; bitset<N * N> bs[N]; int a[N], b[N], ga[N], gb[N], tmp[N]; int n, m; bool dp(int n, int *a, int suma, int *g) { for (int i = 0; i <= n; ++i) bs[i].reset(); bs[0].set(0); for (int i = 1; i <= n; ++i) bs[i] = bs[i - 1] | (bs[i - 1] << a[i]); if (!bs[n][suma / 2]) return false; int tmpn = 0, gn = 0; for (int i = n, j = suma / 2; i; i--) if (j >= a[i] && bs[i - 1][j - a[i]]) g[gn++] = a[i], j -= a[i]; else tmp[tmpn++] = -a[i]; for (int i = 0; i < tmpn; ++i) g[gn++] = tmp[i]; return true; } int main() { int Tes; scanf("%d", &Tes); while (Tes--) { int suma = 0, 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]; if (n != m || (suma & 1) || (sumb & 1)) { puts("No"); continue; } sort(a + 1, a + n + 1); reverse(a + 1, a + n + 1); sort(b + 1, b + m + 1); if (!dp(n, a, suma, ga) || !dp(m, b, sumb, gb)) { puts("No"); continue; } puts("Yes"); for (int i = 0, x = 0, y = 0; i < n + m; ++i) { printf("%d %d\n", x, y); if (i & 1) x += ga[i >> 1]; else y += gb[i >> 1]; } } 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 N = 1e3 + 3; bitset<N * N> bs[N]; int a[N], b[N], ga[N], gb[N], tmp[N]; int n, m; bool dp(int n, int *a, int suma, int *g) { for (int i = 0; i <= n; ++i) bs[i].reset(); bs[0].set(0); for (int i = 1; i <= n; ++i) bs[i] = bs[i - 1] | (bs[i - 1] << a[i]); if (!bs[n][suma / 2]) return false; int tmpn = 0, gn = 0; for (int i = n, j = suma / 2; i; i--) if (j >= a[i] && bs[i - 1][j - a[i]]) g[gn++] = a[i], j -= a[i]; else tmp[tmpn++] = -a[i]; for (int i = 0; i < tmpn; ++i) g[gn++] = tmp[i]; return true; } int main() { int Tes; scanf("%d", &Tes); while (Tes--) { int suma = 0, 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]; if (n != m || (suma & 1) || (sumb & 1)) { puts("No"); continue; } sort(a + 1, a + n + 1); reverse(a + 1, a + n + 1); sort(b + 1, b + m + 1); if (!dp(n, a, suma, ga) || !dp(m, b, sumb, gb)) { puts("No"); continue; } puts("Yes"); for (int i = 0, x = 0, y = 0; i < n + m; ++i) { printf("%d %d\n", x, y); if (i & 1) x += ga[i >> 1]; else y += gb[i >> 1]; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 1e2 + 7; bitset<N * N> f[N]; int calc(int *x, int n, vector<int> &v0, vector<int> &v1) { v0.clear(), v1.clear(); int s = 0; for (int i = 1; i <= n; i++) s += x[i]; if (s & 1) return 0; s >>= 1; for (int i = 1; i <= n; i++) f[i].reset(); f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << x[i]); if (!f[n][s]) return 0; for (int i = n; i >= 1; i--) { if (s >= x[i] && f[i - 1][s - x[i]]) v1.push_back(x[i]), s -= x[i]; else v0.push_back(x[i]); } assert(s == 0); return 1; } int T; int nx, x[N], ny, y[N]; vector<int> cx[2], cy[2]; vector<pair<int, int> > ans; int main() { scanf("%d", &T); while (T--) { scanf("%d", &nx); for (int i = 1; i <= nx; i++) scanf("%d", &x[i]); scanf("%d", &ny); for (int i = 1; i <= ny; i++) scanf("%d", &y[i]); if (nx != ny) { puts("No"); continue; } int ok = 1; ok &= calc(x, nx, cx[0], cx[1]); ok &= calc(y, ny, cy[0], cy[1]); if (!ok) { puts("No"); continue; } puts("Yes"); if (cx[0].size() > cx[1].size()) swap(cx[0], cx[1]); if (cy[0].size() < cy[1].size()) swap(cy[0], cy[1]); sort(cx[0].begin(), cx[0].end(), greater<int>()); sort(cy[0].begin(), cy[0].end()); sort(cx[1].begin(), cx[1].end()); sort(cy[1].begin(), cy[1].end(), greater<int>()); int X = 0, Y = 0; ans.clear(); ans.push_back(make_pair(X, Y)); for (int i = 0; i < cx[0].size(); i++) { X += cx[0][i]; ans.push_back(make_pair(X, Y)); Y += cy[0][i]; ans.push_back(make_pair(X, Y)); } for (int i = 0; i <= (int)cx[1].size() - (int)cy[1].size() - 1; i++) { X -= cx[1][i + cy[1].size()]; ans.push_back(make_pair(X, Y)); Y += cy[0][i + cx[0].size()]; ans.push_back(make_pair(X, Y)); } for (int i = cy[1].size() - 1; i >= 0; i--) { X -= cx[1][i]; ans.push_back(make_pair(X, Y)); Y -= cy[1][i]; ans.push_back(make_pair(X, Y)); } assert(ans.back() == make_pair(0, 0)); ans.pop_back(); for (auto [x, y] : ans) printf("%d %d\n", x, y); } }
### 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; const int N = 1e3 + 1e2 + 7; bitset<N * N> f[N]; int calc(int *x, int n, vector<int> &v0, vector<int> &v1) { v0.clear(), v1.clear(); int s = 0; for (int i = 1; i <= n; i++) s += x[i]; if (s & 1) return 0; s >>= 1; for (int i = 1; i <= n; i++) f[i].reset(); f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << x[i]); if (!f[n][s]) return 0; for (int i = n; i >= 1; i--) { if (s >= x[i] && f[i - 1][s - x[i]]) v1.push_back(x[i]), s -= x[i]; else v0.push_back(x[i]); } assert(s == 0); return 1; } int T; int nx, x[N], ny, y[N]; vector<int> cx[2], cy[2]; vector<pair<int, int> > ans; int main() { scanf("%d", &T); while (T--) { scanf("%d", &nx); for (int i = 1; i <= nx; i++) scanf("%d", &x[i]); scanf("%d", &ny); for (int i = 1; i <= ny; i++) scanf("%d", &y[i]); if (nx != ny) { puts("No"); continue; } int ok = 1; ok &= calc(x, nx, cx[0], cx[1]); ok &= calc(y, ny, cy[0], cy[1]); if (!ok) { puts("No"); continue; } puts("Yes"); if (cx[0].size() > cx[1].size()) swap(cx[0], cx[1]); if (cy[0].size() < cy[1].size()) swap(cy[0], cy[1]); sort(cx[0].begin(), cx[0].end(), greater<int>()); sort(cy[0].begin(), cy[0].end()); sort(cx[1].begin(), cx[1].end()); sort(cy[1].begin(), cy[1].end(), greater<int>()); int X = 0, Y = 0; ans.clear(); ans.push_back(make_pair(X, Y)); for (int i = 0; i < cx[0].size(); i++) { X += cx[0][i]; ans.push_back(make_pair(X, Y)); Y += cy[0][i]; ans.push_back(make_pair(X, Y)); } for (int i = 0; i <= (int)cx[1].size() - (int)cy[1].size() - 1; i++) { X -= cx[1][i + cy[1].size()]; ans.push_back(make_pair(X, Y)); Y += cy[0][i + cx[0].size()]; ans.push_back(make_pair(X, Y)); } for (int i = cy[1].size() - 1; i >= 0; i--) { X -= cx[1][i]; ans.push_back(make_pair(X, Y)); Y -= cy[1][i]; ans.push_back(make_pair(X, Y)); } assert(ans.back() == make_pair(0, 0)); ans.pop_back(); for (auto [x, y] : ans) printf("%d %d\n", x, y); } } ```
#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 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 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 namespace std; const int buffer_size = 1e5 + 5; char buf[buffer_size], *S, *T; bool flag_EOF; inline char read_char() { if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin); return S != T ? *(S++) : EOF; } inline int read_int() { bool flag = false; char c = read_char(); while (c < '0' || c > '9') { if (c == EOF) { flag_EOF = true; return 0; } flag = (c == '-' ? true : flag); c = read_char(); } int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = read_char(); } return flag ? -x : x; } const int max_cnt = 1e3 + 5; int l[max_cnt], p[max_cnt]; bool mark[max_cnt]; vector<int> L, R, U, D; const int max_size = 5e5 + 5; bitset<max_size> f[2]; bool dp[max_size]; int val[max_size]; inline int cmp1(int x, int y) { return abs(x) < abs(y); } inline int cmp2(int x, int y) { return abs(x) > abs(y); } int main() { int t; scanf("%d", &t); while (t--) { int h, v; scanf("%d", &h); for (int i = 1; i <= h; ++i) scanf("%d", l + i); scanf("%d", &v); for (int i = 1; i <= v; ++i) scanf("%d", p + i); if (h != v) { printf("No\n"); continue; } int sum1 = 0, sum2 = 0; for (int i = 1; i <= h; ++i) sum1 += l[i]; for (int i = 1; i <= v; ++i) sum2 += p[i]; if ((sum1 & 1) || (sum2 & 1)) { printf("No\n"); continue; } random_shuffle(l + 1, l + h + 1); for (int i = 0; i <= sum1; ++i) f[0][i] = 0; f[0][0] = 1; int pos1 = -1; for (int i = 1; i <= h; ++i) { int x = (i & 1) ^ 1, y = i & 1; f[y] = f[x] | (f[x] << l[i]); if (f[y][sum1 >> 1]) { pos1 = i; break; } } if (pos1 == -1) { printf("No\n"); continue; } random_shuffle(p + 1, p + v + 1); for (int i = 0; i <= sum2; ++i) f[0][i] = 0; f[0][0] = 1; int pos2 = -1; for (int i = 1; i <= v; ++i) { int x = (i & 1) ^ 1, y = i & 1; f[y] = f[x] | (f[x] << p[i]); if (f[y][sum2 >> 1]) { pos2 = i; break; } } if (pos2 == -1) { printf("No\n"); continue; } L.clear(), R.clear(); int tot1 = sum1 >> 1; for (int i = 1; i <= h; ++i) mark[i] = false; for (int i = pos1 + 1; i <= h; ++i) { L.push_back(l[i]); mark[i] = true; tot1 -= l[i]; } for (int i = 0; i <= tot1; ++i) dp[i] = false; dp[0] = true; for (int i = 1; i <= pos1; ++i) for (int j = tot1 - l[i]; j >= 0; --j) if (dp[j] && !dp[j + l[i]]) { dp[j + l[i]] = true; val[j + l[i]] = i; } int now = tot1; while (now) { L.push_back(l[val[now]]); mark[val[now]] = true; now -= l[val[now]]; } int cnt1 = 0; for (int i = 1; i <= h; ++i) if (!mark[i]) { R.push_back(l[i]); ++cnt1; } U.clear(), D.clear(); int tot2 = sum2 >> 1; for (int i = 1; i <= v; ++i) mark[i] = false; for (int i = pos2 + 1; i <= v; ++i) { U.push_back(p[i]); mark[i] = true; tot2 -= p[i]; } for (int i = 0; i <= tot2; ++i) dp[i] = false; dp[0] = true; for (int i = 1; i <= pos2; ++i) for (int j = tot2 - p[i]; j >= 0; --j) if (dp[j] && !dp[j + p[i]]) { dp[j + p[i]] = true; val[j + p[i]] = i; } now = tot2; while (now) { U.push_back(p[val[now]]); mark[val[now]] = true; now -= p[val[now]]; } int cnt2 = 0; for (int i = 1; i <= v; ++i) if (!mark[i]) { D.push_back(p[i]); ++cnt2; } printf("Yes\n"); int x = 0, y = 0; if (L.size() < R.size()) swap(L, R); if (U.size() < D.size()) swap(U, D); sort(R.begin(), R.end()); sort(U.begin(), U.end(), greater<int>()); while (R.size()) { x += R.back(); printf("%d %d\n", x, y); y += U.back(); printf("%d %d\n", x, y); R.pop_back(); U.pop_back(); } while (U.size()) { x -= L.back(); printf("%d %d\n", x, y); y += U.back(); printf("%d %d\n", x, y); L.pop_back(); U.pop_back(); } sort(L.begin(), L.end()); sort(D.begin(), D.end(), greater<int>()); while (L.size()) { x -= L.back(); printf("%d %d\n", x, y); y -= D.back(); printf("%d %d\n", x, y); L.pop_back(); D.pop_back(); } } 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 buffer_size = 1e5 + 5; char buf[buffer_size], *S, *T; bool flag_EOF; inline char read_char() { if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin); return S != T ? *(S++) : EOF; } inline int read_int() { bool flag = false; char c = read_char(); while (c < '0' || c > '9') { if (c == EOF) { flag_EOF = true; return 0; } flag = (c == '-' ? true : flag); c = read_char(); } int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = read_char(); } return flag ? -x : x; } const int max_cnt = 1e3 + 5; int l[max_cnt], p[max_cnt]; bool mark[max_cnt]; vector<int> L, R, U, D; const int max_size = 5e5 + 5; bitset<max_size> f[2]; bool dp[max_size]; int val[max_size]; inline int cmp1(int x, int y) { return abs(x) < abs(y); } inline int cmp2(int x, int y) { return abs(x) > abs(y); } int main() { int t; scanf("%d", &t); while (t--) { int h, v; scanf("%d", &h); for (int i = 1; i <= h; ++i) scanf("%d", l + i); scanf("%d", &v); for (int i = 1; i <= v; ++i) scanf("%d", p + i); if (h != v) { printf("No\n"); continue; } int sum1 = 0, sum2 = 0; for (int i = 1; i <= h; ++i) sum1 += l[i]; for (int i = 1; i <= v; ++i) sum2 += p[i]; if ((sum1 & 1) || (sum2 & 1)) { printf("No\n"); continue; } random_shuffle(l + 1, l + h + 1); for (int i = 0; i <= sum1; ++i) f[0][i] = 0; f[0][0] = 1; int pos1 = -1; for (int i = 1; i <= h; ++i) { int x = (i & 1) ^ 1, y = i & 1; f[y] = f[x] | (f[x] << l[i]); if (f[y][sum1 >> 1]) { pos1 = i; break; } } if (pos1 == -1) { printf("No\n"); continue; } random_shuffle(p + 1, p + v + 1); for (int i = 0; i <= sum2; ++i) f[0][i] = 0; f[0][0] = 1; int pos2 = -1; for (int i = 1; i <= v; ++i) { int x = (i & 1) ^ 1, y = i & 1; f[y] = f[x] | (f[x] << p[i]); if (f[y][sum2 >> 1]) { pos2 = i; break; } } if (pos2 == -1) { printf("No\n"); continue; } L.clear(), R.clear(); int tot1 = sum1 >> 1; for (int i = 1; i <= h; ++i) mark[i] = false; for (int i = pos1 + 1; i <= h; ++i) { L.push_back(l[i]); mark[i] = true; tot1 -= l[i]; } for (int i = 0; i <= tot1; ++i) dp[i] = false; dp[0] = true; for (int i = 1; i <= pos1; ++i) for (int j = tot1 - l[i]; j >= 0; --j) if (dp[j] && !dp[j + l[i]]) { dp[j + l[i]] = true; val[j + l[i]] = i; } int now = tot1; while (now) { L.push_back(l[val[now]]); mark[val[now]] = true; now -= l[val[now]]; } int cnt1 = 0; for (int i = 1; i <= h; ++i) if (!mark[i]) { R.push_back(l[i]); ++cnt1; } U.clear(), D.clear(); int tot2 = sum2 >> 1; for (int i = 1; i <= v; ++i) mark[i] = false; for (int i = pos2 + 1; i <= v; ++i) { U.push_back(p[i]); mark[i] = true; tot2 -= p[i]; } for (int i = 0; i <= tot2; ++i) dp[i] = false; dp[0] = true; for (int i = 1; i <= pos2; ++i) for (int j = tot2 - p[i]; j >= 0; --j) if (dp[j] && !dp[j + p[i]]) { dp[j + p[i]] = true; val[j + p[i]] = i; } now = tot2; while (now) { U.push_back(p[val[now]]); mark[val[now]] = true; now -= p[val[now]]; } int cnt2 = 0; for (int i = 1; i <= v; ++i) if (!mark[i]) { D.push_back(p[i]); ++cnt2; } printf("Yes\n"); int x = 0, y = 0; if (L.size() < R.size()) swap(L, R); if (U.size() < D.size()) swap(U, D); sort(R.begin(), R.end()); sort(U.begin(), U.end(), greater<int>()); while (R.size()) { x += R.back(); printf("%d %d\n", x, y); y += U.back(); printf("%d %d\n", x, y); R.pop_back(); U.pop_back(); } while (U.size()) { x -= L.back(); printf("%d %d\n", x, y); y += U.back(); printf("%d %d\n", x, y); L.pop_back(); U.pop_back(); } sort(L.begin(), L.end()); sort(D.begin(), D.end(), greater<int>()); while (L.size()) { x -= L.back(); printf("%d %d\n", x, y); y -= D.back(); printf("%d %d\n", x, y); L.pop_back(); D.pop_back(); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while ((c < '0' || c > '9') && (c != '-')) c = getchar(); if (c == '-') f = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } const int N = 1010; int n, m, a[N], b[N]; bitset<N * N / 2> f[N]; vector<int> va[2], vb[N]; inline void solve() { n = read(); int sa = 0; for (register int i = (1); i <= (n); i++) a[i] = read(), sa += a[i]; m = read(); int sb = 0; for (register int i = (1); i <= (m); i++) b[i] = read(), sb += b[i]; if (n != m || sa % 2 || sb % 2) return puts("No"), void(0); { f[0][0] = 1; for (register int i = (1); i <= (n); i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (!f[n][sa / 2]) return puts("No"), void(0); int now = sa / 2; va[0].clear(), va[1].clear(); for (register int i = (n); i >= (1); i--) { if (now >= a[i] && f[i - 1][now - a[i]]) now -= a[i], va[1].push_back(a[i]); else va[0].push_back(a[i]); } sort((va[0]).begin(), (va[0]).end()), sort((va[1]).begin(), (va[1]).end()); reverse((va[0]).begin(), (va[0]).end()), reverse((va[1]).begin(), (va[1]).end()); } { f[0][0] = 1; for (register int i = (1); i <= (m); i++) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (!f[m][sb / 2]) return puts("No"), void(0); int now = sb / 2; vb[0].clear(), vb[1].clear(); for (register int i = (m); i >= (1); i--) { if (now >= b[i] && f[i - 1][now - b[i]]) now -= b[i], vb[1].push_back(b[i]); else vb[0].push_back(b[i]); } sort((vb[0]).begin(), (vb[0]).end()), sort((vb[1]).begin(), (vb[1]).end()); } if (((int)(va[0]).size()) > ((int)(va[1]).size())) swap(va[0], va[1]); if (((int)(vb[0]).size()) < ((int)(vb[1]).size())) swap(vb[0], vb[1]); puts("Yes"); int x = 0, y = 0, la = 0, lb = 0; for (register int i = (1); i <= (2 * n); i++) { if (i & 1) { if (la < ((int)(va[0]).size())) x += va[0][la], ++la; else x -= va[1][la - ((int)(va[0]).size())], ++la; } else { if (lb < ((int)(vb[0]).size())) y += vb[0][lb], ++lb; else y -= vb[1][lb - ((int)(vb[0]).size())], ++lb; } printf("%d %d\n", x, y); } } int main() { int T = read(); while (T--) solve(); }
### 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; inline long long read() { long long x = 0, f = 1; char c = getchar(); while ((c < '0' || c > '9') && (c != '-')) c = getchar(); if (c == '-') f = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } const int N = 1010; int n, m, a[N], b[N]; bitset<N * N / 2> f[N]; vector<int> va[2], vb[N]; inline void solve() { n = read(); int sa = 0; for (register int i = (1); i <= (n); i++) a[i] = read(), sa += a[i]; m = read(); int sb = 0; for (register int i = (1); i <= (m); i++) b[i] = read(), sb += b[i]; if (n != m || sa % 2 || sb % 2) return puts("No"), void(0); { f[0][0] = 1; for (register int i = (1); i <= (n); i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (!f[n][sa / 2]) return puts("No"), void(0); int now = sa / 2; va[0].clear(), va[1].clear(); for (register int i = (n); i >= (1); i--) { if (now >= a[i] && f[i - 1][now - a[i]]) now -= a[i], va[1].push_back(a[i]); else va[0].push_back(a[i]); } sort((va[0]).begin(), (va[0]).end()), sort((va[1]).begin(), (va[1]).end()); reverse((va[0]).begin(), (va[0]).end()), reverse((va[1]).begin(), (va[1]).end()); } { f[0][0] = 1; for (register int i = (1); i <= (m); i++) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (!f[m][sb / 2]) return puts("No"), void(0); int now = sb / 2; vb[0].clear(), vb[1].clear(); for (register int i = (m); i >= (1); i--) { if (now >= b[i] && f[i - 1][now - b[i]]) now -= b[i], vb[1].push_back(b[i]); else vb[0].push_back(b[i]); } sort((vb[0]).begin(), (vb[0]).end()), sort((vb[1]).begin(), (vb[1]).end()); } if (((int)(va[0]).size()) > ((int)(va[1]).size())) swap(va[0], va[1]); if (((int)(vb[0]).size()) < ((int)(vb[1]).size())) swap(vb[0], vb[1]); puts("Yes"); int x = 0, y = 0, la = 0, lb = 0; for (register int i = (1); i <= (2 * n); i++) { if (i & 1) { if (la < ((int)(va[0]).size())) x += va[0][la], ++la; else x -= va[1][la - ((int)(va[0]).size())], ++la; } else { if (lb < ((int)(vb[0]).size())) y += vb[0][lb], ++lb; else y -= vb[1][lb - ((int)(vb[0]).size())], ++lb; } printf("%d %d\n", x, y); } } int main() { int T = read(); while (T--) solve(); } ```
#include <bits/stdc++.h> using namespace std; int n, m; const int maxn = 1e3 + 5; bitset<500005> dp[1005]; int a[maxn]; vector<pair<int, int> > ans; bool sol(vector<int> &A, vector<int> &B) { scanf("%d", &n); 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]) return true; s /= 2; for (int i = n; i > 0; --i) if (dp[i - 1][s]) A.push_back(a[i]); else B.push_back(a[i]), s -= a[i]; return false; } vector<int> A[2], B[2]; int main() { int T; cin >> T; dp[0][0] = 1; while (T--) { for (int i = (0); i < (2); ++i) A[i].clear(), B[i].clear(); int no = 0; no |= sol(A[0], A[1]); no |= sol(B[0], B[1]); no |= (A[0].size() + A[1].size() != B[0].size() + B[1].size()); if (no) { puts("No"); continue; } for (int i = (0); i < (2); ++i) sort(A[i].begin(), A[i].end()), sort(B[i].begin(), B[i].end()), 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 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 n, m; const int maxn = 1e3 + 5; bitset<500005> dp[1005]; int a[maxn]; vector<pair<int, int> > ans; bool sol(vector<int> &A, vector<int> &B) { scanf("%d", &n); 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]) return true; s /= 2; for (int i = n; i > 0; --i) if (dp[i - 1][s]) A.push_back(a[i]); else B.push_back(a[i]), s -= a[i]; return false; } vector<int> A[2], B[2]; int main() { int T; cin >> T; dp[0][0] = 1; while (T--) { for (int i = (0); i < (2); ++i) A[i].clear(), B[i].clear(); int no = 0; no |= sol(A[0], A[1]); no |= sol(B[0], B[1]); no |= (A[0].size() + A[1].size() != B[0].size() + B[1].size()); if (no) { puts("No"); continue; } for (int i = (0); i < (2); ++i) sort(A[i].begin(), A[i].end()), sort(B[i].begin(), B[i].end()), 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; const long long MOD = 1e9 + 7; int t, n, len[2][1010]; bitset<2000010> dp[510]; vector<int> u, d, l, r; vector<pair<int, int> > ans1, ans2, ans3; pair<vector<int>, vector<int> > check(int w) { for (int i = 0; i < n + 5; i++) dp[i].reset(); dp[0][1000005] = 1; for (int i = 0; i < n; i++) { dp[i + 1] |= (dp[i] >> len[w][i]); dp[i + 1] |= (dp[i] << len[w][i]); } if (dp[n][1000005] == 0) return {{-1}, {-1}}; int cur = 1000005, ii = n; pair<vector<int>, vector<int> > ret; ret.first.clear(); ret.second.clear(); while (ii > 0) { if (dp[ii - 1][cur - len[w][ii - 1]] == 1) ret.first.push_back(len[w][ii - 1]), cur -= len[w][ii - 1]; else ret.second.push_back(len[w][ii - 1]), cur += len[w][ii - 1]; --ii; } return ret; } int main() { cin >> t; for (int tn = 0; tn < t; tn++) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &len[1][i]); int tmp; scanf("%d", &tmp); for (int i = 0; i < tmp; i++) scanf("%d", &len[0][i]); if (n != tmp) { puts("No"); continue; } pair<vector<int>, vector<int> > sv = check(0); if (sv.first.size() > sv.second.size()) swap(sv.first, sv.second); d = sv.first; u = sv.second; sv = check(1); if (sv.first.size() > sv.second.size()) swap(sv.first, sv.second); r = sv.first; l = sv.second; if (d[0] == -1 || u[0] == -1 || r[0] == -1 || l[0] == -1) { puts("No"); continue; } ans1.clear(); ans2.clear(); ans3.clear(); sort(r.begin(), r.end()); reverse(r.begin(), r.end()); sort(u.begin(), u.begin() + (int)r.size()); int x = 0, y = 0; ans1.push_back(make_pair(x, y)); for (int i = 0; i < r.size(); i++) { x += r[i]; ans1.push_back(make_pair(x, y)); y += u[i]; ans1.push_back(make_pair(x, y)); } sort(d.begin(), d.end()); reverse(d.begin(), d.end()); sort(l.begin(), l.begin() + (int)d.size()); x = 0; y = 0; ans2.push_back(make_pair(x, y)); for (int i = 0; i < d.size(); i++) { y += d[i]; ans2.push_back(make_pair(x, y)); x += l[i]; ans2.push_back(make_pair(x, y)); } puts("YES"); for (int i = 0; i < ans1.size(); i++) printf("%d %d\n", ans1[i].first, ans1[i].second); if (ans1.back() == ans2.back()) { for (int i = ans2.size() - 2; i > 0; --i) printf("%d %d\n", ans2[i].first, ans2[i].second); continue; } ans3.push_back(ans1.back()); x = ans1.back().first; y = ans1.back().second; for (int i = 0; i < u.size() - r.size(); i++) { x -= l[i + d.size()]; ans3.push_back(make_pair(x, y)); y += u[i + r.size()]; ans3.push_back(make_pair(x, y)); } for (int i = 1; i <= ans3.size() - 1; i++) printf("%d %d\n", ans3[i].first, ans3[i].second); for (int i = ans2.size() - 2; i > 0; --i) printf("%d %d\n", ans2[i].first, ans2[i].second); } 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 long long MOD = 1e9 + 7; int t, n, len[2][1010]; bitset<2000010> dp[510]; vector<int> u, d, l, r; vector<pair<int, int> > ans1, ans2, ans3; pair<vector<int>, vector<int> > check(int w) { for (int i = 0; i < n + 5; i++) dp[i].reset(); dp[0][1000005] = 1; for (int i = 0; i < n; i++) { dp[i + 1] |= (dp[i] >> len[w][i]); dp[i + 1] |= (dp[i] << len[w][i]); } if (dp[n][1000005] == 0) return {{-1}, {-1}}; int cur = 1000005, ii = n; pair<vector<int>, vector<int> > ret; ret.first.clear(); ret.second.clear(); while (ii > 0) { if (dp[ii - 1][cur - len[w][ii - 1]] == 1) ret.first.push_back(len[w][ii - 1]), cur -= len[w][ii - 1]; else ret.second.push_back(len[w][ii - 1]), cur += len[w][ii - 1]; --ii; } return ret; } int main() { cin >> t; for (int tn = 0; tn < t; tn++) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &len[1][i]); int tmp; scanf("%d", &tmp); for (int i = 0; i < tmp; i++) scanf("%d", &len[0][i]); if (n != tmp) { puts("No"); continue; } pair<vector<int>, vector<int> > sv = check(0); if (sv.first.size() > sv.second.size()) swap(sv.first, sv.second); d = sv.first; u = sv.second; sv = check(1); if (sv.first.size() > sv.second.size()) swap(sv.first, sv.second); r = sv.first; l = sv.second; if (d[0] == -1 || u[0] == -1 || r[0] == -1 || l[0] == -1) { puts("No"); continue; } ans1.clear(); ans2.clear(); ans3.clear(); sort(r.begin(), r.end()); reverse(r.begin(), r.end()); sort(u.begin(), u.begin() + (int)r.size()); int x = 0, y = 0; ans1.push_back(make_pair(x, y)); for (int i = 0; i < r.size(); i++) { x += r[i]; ans1.push_back(make_pair(x, y)); y += u[i]; ans1.push_back(make_pair(x, y)); } sort(d.begin(), d.end()); reverse(d.begin(), d.end()); sort(l.begin(), l.begin() + (int)d.size()); x = 0; y = 0; ans2.push_back(make_pair(x, y)); for (int i = 0; i < d.size(); i++) { y += d[i]; ans2.push_back(make_pair(x, y)); x += l[i]; ans2.push_back(make_pair(x, y)); } puts("YES"); for (int i = 0; i < ans1.size(); i++) printf("%d %d\n", ans1[i].first, ans1[i].second); if (ans1.back() == ans2.back()) { for (int i = ans2.size() - 2; i > 0; --i) printf("%d %d\n", ans2[i].first, ans2[i].second); continue; } ans3.push_back(ans1.back()); x = ans1.back().first; y = ans1.back().second; for (int i = 0; i < u.size() - r.size(); i++) { x -= l[i + d.size()]; ans3.push_back(make_pair(x, y)); y += u[i + r.size()]; ans3.push_back(make_pair(x, y)); } for (int i = 1; i <= ans3.size() - 1; i++) printf("%d %d\n", ans3[i].first, ans3[i].second); for (int i = ans2.size() - 2; i > 0; --i) printf("%d %d\n", ans2[i].first, ans2[i].second); } return 0; } ```
#include <bits/stdc++.h> const int max_N = 1005; long long n, m, a[max_N], b[max_N]; std::bitset<max_N * max_N> F[max_N]; long long read() { char c = getchar(); long long ans = 0; bool flag = true; while (c < '0' || c > '9') flag &= (c != '-'), c = getchar(); while (c >= '0' && c <= '9') ans = ans * 10 + c - '0', c = getchar(); return flag ? ans : -ans; } void Write(long long x) { if (x < 10) putchar(x + '0'); else Write(x / 10), putchar(x % 10 + '0'); } long long max(long long x, long long y) { return x > y ? x : y; } long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } bool cmp1(std::pair<long long, long long> A, std::pair<long long, long long> B) { return A.second * B.first < A.first * B.second; } bool cmp2(std::pair<long long, long long> A, std::pair<long long, long long> B) { return A.second * B.first > A.first * B.second; } std::pair<std::vector<long long>, std::vector<long long> > divide( std::vector<long long> X) { for (long long i = 0; i <= X.size(); i++) F[i].reset(); F[0][0] = 1; long long sum = 0; for (long long i = 0; i < X.size(); i++) F[i + 1] = F[i] | (F[i] << X[i]), sum += X[i]; if (sum % 2 || !F[X.size()][sum / 2]) return std::make_pair(std::vector<long long>(), std::vector<long long>()); std::vector<long long> U, V; long long pos = sum / 2; for (long long i = X.size(); i; i--) if (F[i - 1][pos]) V.push_back(X[i - 1]); else U.push_back(X[i - 1]), pos -= X[i - 1]; if (U.size() > V.size()) std::swap(U, V); return std::make_pair(U, V); } void solve() { std::vector<long long> X, Y; n = read(); for (long long i = 0; i < n; i++) X.push_back(read()); std::pair<std::vector<long long>, std::vector<long long> > A = divide(X); m = read(); for (long long i = 0; i < m; i++) Y.push_back(read()); std::pair<std::vector<long long>, std::vector<long long> > B = divide(Y); if ((A.first.empty() && A.second.empty()) || (B.first.empty() && B.second.empty()) || n != m) return printf("No\n"), void(); std::vector<std::pair<long long, long long> > U, V, W; printf("YES\n"); for (long long i = 0; i < B.first.size(); i++) U.push_back(std::make_pair(A.second[i], B.first[i])); for (long long i = B.first.size(); i < A.second.size(); i++) V.push_back(std::make_pair(A.second[i], B.second[i - B.first.size()])); for (long long i = A.second.size(); i < n; i++) W.push_back(std::make_pair(A.first[i - A.second.size()], B.second[i - B.first.size()])); std::sort(U.begin(), U.end(), cmp2), std::sort(W.begin(), W.end(), cmp1); std::reverse(W.begin(), W.end()); long long x = 0, y = 0; for (auto i : U) y += i.second, Write(x), putchar(' '), Write(y), putchar('\n'), x += i.first, Write(x), putchar(' '), Write(y), putchar('\n'); for (auto i : V) y -= i.second, Write(x), putchar(' '), Write(y), putchar('\n'), x += i.first, Write(x), putchar(' '), Write(y), putchar('\n'); for (auto i : W) y -= i.second, Write(x), putchar(' '), Write(y), putchar('\n'), x -= i.first, Write(x), putchar(' '), Write(y), putchar('\n'); } int main() { long long t = read(); 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> const int max_N = 1005; long long n, m, a[max_N], b[max_N]; std::bitset<max_N * max_N> F[max_N]; long long read() { char c = getchar(); long long ans = 0; bool flag = true; while (c < '0' || c > '9') flag &= (c != '-'), c = getchar(); while (c >= '0' && c <= '9') ans = ans * 10 + c - '0', c = getchar(); return flag ? ans : -ans; } void Write(long long x) { if (x < 10) putchar(x + '0'); else Write(x / 10), putchar(x % 10 + '0'); } long long max(long long x, long long y) { return x > y ? x : y; } long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } bool cmp1(std::pair<long long, long long> A, std::pair<long long, long long> B) { return A.second * B.first < A.first * B.second; } bool cmp2(std::pair<long long, long long> A, std::pair<long long, long long> B) { return A.second * B.first > A.first * B.second; } std::pair<std::vector<long long>, std::vector<long long> > divide( std::vector<long long> X) { for (long long i = 0; i <= X.size(); i++) F[i].reset(); F[0][0] = 1; long long sum = 0; for (long long i = 0; i < X.size(); i++) F[i + 1] = F[i] | (F[i] << X[i]), sum += X[i]; if (sum % 2 || !F[X.size()][sum / 2]) return std::make_pair(std::vector<long long>(), std::vector<long long>()); std::vector<long long> U, V; long long pos = sum / 2; for (long long i = X.size(); i; i--) if (F[i - 1][pos]) V.push_back(X[i - 1]); else U.push_back(X[i - 1]), pos -= X[i - 1]; if (U.size() > V.size()) std::swap(U, V); return std::make_pair(U, V); } void solve() { std::vector<long long> X, Y; n = read(); for (long long i = 0; i < n; i++) X.push_back(read()); std::pair<std::vector<long long>, std::vector<long long> > A = divide(X); m = read(); for (long long i = 0; i < m; i++) Y.push_back(read()); std::pair<std::vector<long long>, std::vector<long long> > B = divide(Y); if ((A.first.empty() && A.second.empty()) || (B.first.empty() && B.second.empty()) || n != m) return printf("No\n"), void(); std::vector<std::pair<long long, long long> > U, V, W; printf("YES\n"); for (long long i = 0; i < B.first.size(); i++) U.push_back(std::make_pair(A.second[i], B.first[i])); for (long long i = B.first.size(); i < A.second.size(); i++) V.push_back(std::make_pair(A.second[i], B.second[i - B.first.size()])); for (long long i = A.second.size(); i < n; i++) W.push_back(std::make_pair(A.first[i - A.second.size()], B.second[i - B.first.size()])); std::sort(U.begin(), U.end(), cmp2), std::sort(W.begin(), W.end(), cmp1); std::reverse(W.begin(), W.end()); long long x = 0, y = 0; for (auto i : U) y += i.second, Write(x), putchar(' '), Write(y), putchar('\n'), x += i.first, Write(x), putchar(' '), Write(y), putchar('\n'); for (auto i : V) y -= i.second, Write(x), putchar(' '), Write(y), putchar('\n'), x += i.first, Write(x), putchar(' '), Write(y), putchar('\n'); for (auto i : W) y -= i.second, Write(x), putchar(' '), Write(y), putchar('\n'), x -= i.first, Write(x), putchar(' '), Write(y), putchar('\n'); } int main() { long long t = read(); while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N_MAX = 1002; const int L_MAX = 1002; int t; int h, v; int a[N_MAX], b[N_MAX]; map<int, int> dpa, dpb; bool knapsack(int n, int v[], map<int, int> &dp) { dp[0] = 0; int sum = 0; for (int i = 1; i <= n; i++) { vector<int> aux; for (map<int, int>::iterator it = dp.begin(); it != dp.end(); it++) aux.push_back(it->first); for (int j : aux) if (dp.find(j + v[i]) == dp.end()) dp[j + v[i]] = v[i]; sum += v[i]; } if (sum & 1) return false; return (dp.find(sum / 2) != dp.end()); } vector<int> aSmall, aBig, bSmall, bBig; map<int, int> aux; struct Segment { int a, b; }; bool operator<(const Segment &s1, const Segment &s2) { return s1.b * s2.a < s2.b * s1.a; } vector<Segment> side1, side2, last; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> h; for (int i = 1; i <= h; i++) cin >> a[i]; cin >> v; for (int i = 1; i <= v; i++) cin >> b[i]; if (h != v) { cout << "No\n"; continue; } dpa.clear(); dpb.clear(); if (knapsack(h, a, dpa) == false || knapsack(v, b, dpb) == false) { cout << "No\n"; continue; } cout << "Yes\n"; aSmall.clear(); aBig.clear(); bSmall.clear(); bBig.clear(); side1.clear(); side2.clear(); last.clear(); int suma = 0, sumb = 0; for (int i = 1; i <= h; i++) suma += a[i]; for (int i = 1; i <= v; i++) sumb += b[i]; aux.clear(); suma /= 2; while (suma > 0) { aSmall.push_back(dpa[suma]); suma -= aSmall.back(); aux[aSmall.back()]++; } for (int i = 1; i <= h; i++) if (aux[a[i]] == 0) aBig.push_back(a[i]); else aux[a[i]]--; aux.clear(); sumb /= 2; while (sumb > 0) { bSmall.push_back(dpb[sumb]); sumb -= bSmall.back(); aux[bSmall.back()]++; } for (int i = 1; i <= v; i++) if (aux[b[i]] == 0) bBig.push_back(b[i]); else aux[b[i]]--; if ((int)aSmall.size() > (int)aBig.size()) swap(aSmall, aBig); if ((int)bSmall.size() > (int)bBig.size()) swap(bSmall, bBig); while (!aSmall.empty()) { side1.push_back(Segment{aSmall.back(), bBig.back()}); aSmall.pop_back(); bBig.pop_back(); } while (!bBig.empty()) { last.push_back(Segment{aBig.back(), bBig.back()}); aBig.pop_back(); bBig.pop_back(); } while (!bSmall.empty()) { side2.push_back(Segment{aBig.back(), bSmall.back()}); aBig.pop_back(); bSmall.pop_back(); } sort(side1.begin(), side1.end()); sort(side2.begin(), side2.end()); int a = 0, b = 0; for (Segment s : side1) { a += s.a; cout << a << " " << b << "\n"; b += s.b; cout << a << " " << b << "\n"; } for (Segment s : last) { a -= s.a; cout << a << " " << b << "\n"; b += s.b; cout << a << " " << b << "\n"; } for (Segment s : side2) { a -= s.a; cout << a << " " << b << "\n"; b -= s.b; cout << a << " " << b << "\n"; } } 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 N_MAX = 1002; const int L_MAX = 1002; int t; int h, v; int a[N_MAX], b[N_MAX]; map<int, int> dpa, dpb; bool knapsack(int n, int v[], map<int, int> &dp) { dp[0] = 0; int sum = 0; for (int i = 1; i <= n; i++) { vector<int> aux; for (map<int, int>::iterator it = dp.begin(); it != dp.end(); it++) aux.push_back(it->first); for (int j : aux) if (dp.find(j + v[i]) == dp.end()) dp[j + v[i]] = v[i]; sum += v[i]; } if (sum & 1) return false; return (dp.find(sum / 2) != dp.end()); } vector<int> aSmall, aBig, bSmall, bBig; map<int, int> aux; struct Segment { int a, b; }; bool operator<(const Segment &s1, const Segment &s2) { return s1.b * s2.a < s2.b * s1.a; } vector<Segment> side1, side2, last; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> h; for (int i = 1; i <= h; i++) cin >> a[i]; cin >> v; for (int i = 1; i <= v; i++) cin >> b[i]; if (h != v) { cout << "No\n"; continue; } dpa.clear(); dpb.clear(); if (knapsack(h, a, dpa) == false || knapsack(v, b, dpb) == false) { cout << "No\n"; continue; } cout << "Yes\n"; aSmall.clear(); aBig.clear(); bSmall.clear(); bBig.clear(); side1.clear(); side2.clear(); last.clear(); int suma = 0, sumb = 0; for (int i = 1; i <= h; i++) suma += a[i]; for (int i = 1; i <= v; i++) sumb += b[i]; aux.clear(); suma /= 2; while (suma > 0) { aSmall.push_back(dpa[suma]); suma -= aSmall.back(); aux[aSmall.back()]++; } for (int i = 1; i <= h; i++) if (aux[a[i]] == 0) aBig.push_back(a[i]); else aux[a[i]]--; aux.clear(); sumb /= 2; while (sumb > 0) { bSmall.push_back(dpb[sumb]); sumb -= bSmall.back(); aux[bSmall.back()]++; } for (int i = 1; i <= v; i++) if (aux[b[i]] == 0) bBig.push_back(b[i]); else aux[b[i]]--; if ((int)aSmall.size() > (int)aBig.size()) swap(aSmall, aBig); if ((int)bSmall.size() > (int)bBig.size()) swap(bSmall, bBig); while (!aSmall.empty()) { side1.push_back(Segment{aSmall.back(), bBig.back()}); aSmall.pop_back(); bBig.pop_back(); } while (!bBig.empty()) { last.push_back(Segment{aBig.back(), bBig.back()}); aBig.pop_back(); bBig.pop_back(); } while (!bSmall.empty()) { side2.push_back(Segment{aBig.back(), bSmall.back()}); aBig.pop_back(); bSmall.pop_back(); } sort(side1.begin(), side1.end()); sort(side2.begin(), side2.end()); int a = 0, b = 0; for (Segment s : side1) { a += s.a; cout << a << " " << b << "\n"; b += s.b; cout << a << " " << b << "\n"; } for (Segment s : last) { a -= s.a; cout << a << " " << b << "\n"; b += s.b; cout << a << " " << b << "\n"; } for (Segment s : side2) { a -= s.a; cout << a << " " << b << "\n"; b -= s.b; cout << a << " " << b << "\n"; } } return 0; } ```
#include <bits/stdc++.h> const int MV = 5e5 + 10; int dp[MV]; bool partition(int *a, int N, int v, std::vector<int> &g1, std::vector<int> &g2) { memset(dp, -1, (v + 1) * sizeof *dp); dp[0] = 0; for (int i = 0; i < N; ++i) for (int j = v; j >= a[i]; --j) if (!~dp[j] && ~dp[j - a[i]]) dp[j] = i; if (!~dp[v]) return 0; for (int i = v, x; i; i -= x) { x = a[dp[i]]; a[dp[i]] = -1; g1.push_back(x); } for (int i = 0; i < N; ++i) if (~a[i]) g2.push_back(a[i]); return 1; } void solve() { int H, V, toth = 0, totv = 0; scanf("%d", &H); int l[H]; for (int i = 0; i < H; ++i) scanf("%d", l + i), toth += l[i]; scanf("%d", &V); int p[V]; for (int i = 0; i < V; ++i) scanf("%d", p + i), totv += p[i]; if (V != H || toth & 1 || totv & 1) return (void)printf("No\n"); std::vector<int> hp, hn, vp, vn; if (!partition(l, H, toth / 2, hp, hn) || !partition(p, V, totv / 2, vp, vn)) return (void)printf("No\n"); if (hp.size() < hn.size()) hp.swap(hn); if (vp.size() < vn.size()) vp.swap(vn); printf("YES\n"); int x = 0, y = 0; if (hp.size() < vp.size()) { std::sort(hp.begin(), hp.end(), std::greater<int>()); std::sort(hn.begin(), hn.end(), std::greater<int>()); std::sort(vp.begin(), vp.end()); std::sort(vn.begin(), vn.end()); for (int v : hn) hp.push_back(-v); for (int v : vn) vp.push_back(-v); for (int i = 0; i < H; ++i) { printf("%d %d\n", x += hp[i], y); printf("%d %d\n", x, y += vp[i]); } } else { std::sort(hp.begin(), hp.end()); std::sort(hn.begin(), hn.end()); std::sort(vp.begin(), vp.end(), std::greater<int>()); std::sort(vn.begin(), vn.end(), std::greater<int>()); for (int v : hn) hp.push_back(-v); for (int v : vn) vp.push_back(-v); for (int i = 0; i < H; ++i) { printf("%d %d\n", x, y += vp[i]); printf("%d %d\n", x += hp[i], y); } } } int main() { int t; scanf("%d", &t); for (; 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> const int MV = 5e5 + 10; int dp[MV]; bool partition(int *a, int N, int v, std::vector<int> &g1, std::vector<int> &g2) { memset(dp, -1, (v + 1) * sizeof *dp); dp[0] = 0; for (int i = 0; i < N; ++i) for (int j = v; j >= a[i]; --j) if (!~dp[j] && ~dp[j - a[i]]) dp[j] = i; if (!~dp[v]) return 0; for (int i = v, x; i; i -= x) { x = a[dp[i]]; a[dp[i]] = -1; g1.push_back(x); } for (int i = 0; i < N; ++i) if (~a[i]) g2.push_back(a[i]); return 1; } void solve() { int H, V, toth = 0, totv = 0; scanf("%d", &H); int l[H]; for (int i = 0; i < H; ++i) scanf("%d", l + i), toth += l[i]; scanf("%d", &V); int p[V]; for (int i = 0; i < V; ++i) scanf("%d", p + i), totv += p[i]; if (V != H || toth & 1 || totv & 1) return (void)printf("No\n"); std::vector<int> hp, hn, vp, vn; if (!partition(l, H, toth / 2, hp, hn) || !partition(p, V, totv / 2, vp, vn)) return (void)printf("No\n"); if (hp.size() < hn.size()) hp.swap(hn); if (vp.size() < vn.size()) vp.swap(vn); printf("YES\n"); int x = 0, y = 0; if (hp.size() < vp.size()) { std::sort(hp.begin(), hp.end(), std::greater<int>()); std::sort(hn.begin(), hn.end(), std::greater<int>()); std::sort(vp.begin(), vp.end()); std::sort(vn.begin(), vn.end()); for (int v : hn) hp.push_back(-v); for (int v : vn) vp.push_back(-v); for (int i = 0; i < H; ++i) { printf("%d %d\n", x += hp[i], y); printf("%d %d\n", x, y += vp[i]); } } else { std::sort(hp.begin(), hp.end()); std::sort(hn.begin(), hn.end()); std::sort(vp.begin(), vp.end(), std::greater<int>()); std::sort(vn.begin(), vn.end(), std::greater<int>()); for (int v : hn) hp.push_back(-v); for (int v : vn) vp.push_back(-v); for (int i = 0; i < H; ++i) { printf("%d %d\n", x, y += vp[i]); printf("%d %d\n", x += hp[i], y); } } } int main() { int t; scanf("%d", &t); for (; t--;) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int h; cin >> h; vector<int> l(h); for (int j = 0; j < h; j++) { cin >> l[j]; } int v; cin >> v; vector<int> p(v); for (int j = 0; j < v; j++) { cin >> p[j]; } if (h != v) { cout << "No" << endl; } else { int lsum = 0; for (int j = 0; j < h; j++) { lsum += l[j]; } int psum = 0; for (int j = 0; j < v; j++) { psum += p[j]; } if (lsum % 2 == 1 || psum % 2 == 1) { cout << "No" << endl; } else { vector<vector<bool>> dph(h + 1, vector<bool>(lsum / 2 + 1, false)); dph[0][0] = true; for (int j = 0; j < h; j++) { dph[j + 1] = dph[j]; for (int k = lsum / 2; k >= l[j]; k--) { if (dph[j][k - l[j]]) { dph[j + 1][k] = true; } } } vector<vector<bool>> dpv(v + 1, vector<bool>(psum / 2 + 1, false)); dpv[0][0] = true; for (int j = 0; j < v; j++) { dpv[j + 1] = dpv[j]; for (int k = psum / 2; k >= p[j]; k--) { if (dpv[j][k - p[j]]) { dpv[j + 1][k] = true; } } } if (!dph[h][lsum / 2] || !dpv[v][psum / 2]) { cout << "No" << endl; } else { int hc = lsum / 2; vector<int> ha, hb; for (int j = h - 1; j >= 0; j--) { if (!dph[j][hc]) { ha.push_back(l[j]); hc -= l[j]; } else { hb.push_back(l[j]); } } int vc = psum / 2; vector<int> va, vb; for (int j = v - 1; j >= 0; j--) { if (!dpv[j][vc]) { va.push_back(p[j]); vc -= p[j]; } else { vb.push_back(p[j]); } } if (va.size() == ha.size()) { sort(ha.rbegin(), ha.rend()); sort(hb.rbegin(), hb.rend()); sort(va.begin(), va.end()); sort(vb.begin(), vb.end()); cout << "Yes" << endl; int x = 0, y = 0; for (int j = 0; j < va.size(); j++) { x += ha[j]; cout << x << ' ' << y << endl; y += va[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < vb.size(); j++) { x -= hb[j]; cout << x << ' ' << y << endl; y -= vb[j]; cout << x << ' ' << y << endl; } } if (ha.size() < va.size()) { vector<int> h1, v1; for (int j = 0; j < ha.size(); j++) { h1.push_back(ha[j]); v1.push_back(va[j]); } vector<int> h2, v2; for (int j = 0; j < va.size() - ha.size(); j++) { h2.push_back(hb[j]); v2.push_back(va[j + ha.size()]); } vector<int> h3, v3; for (int j = 0; j < h - va.size(); j++) { h3.push_back(hb[j + (va.size() - ha.size())]); v3.push_back(vb[j]); } sort(h1.rbegin(), h1.rend()); sort(v1.begin(), v1.end()); sort(h2.begin(), h2.end()); sort(v2.rbegin(), v2.rend()); sort(h3.rbegin(), h3.rend()); sort(v3.begin(), v3.end()); cout << "Yes" << endl; int x = 0, y = 0; for (int j = 0; j < h1.size(); j++) { x += h1[j]; cout << x << ' ' << y << endl; y += v1[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < h2.size(); j++) { x -= h2[j]; cout << x << ' ' << y << endl; y += v2[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < h3.size(); j++) { x -= h3[j]; cout << x << ' ' << y << endl; y -= v3[j]; cout << x << ' ' << y << endl; } } if (ha.size() > va.size()) { vector<int> h1, v1; for (int j = 0; j < va.size(); j++) { h1.push_back(ha[j]); v1.push_back(va[j]); } vector<int> h2, v2; for (int j = 0; j < ha.size() - va.size(); j++) { h2.push_back(ha[j + va.size()]); v2.push_back(vb[j]); } vector<int> h3, v3; for (int j = 0; j < h - ha.size(); j++) { h3.push_back(hb[j]); v3.push_back(vb[j + (ha.size() - va.size())]); } sort(h1.begin(), h1.end()); sort(v1.rbegin(), v1.rend()); sort(h2.rbegin(), h2.rend()); sort(v2.begin(), v2.end()); sort(h3.begin(), h3.end()); sort(v3.rbegin(), v3.rend()); cout << "Yes" << endl; int x = 0, y = 0; for (int j = 0; j < h1.size(); j++) { y += v1[j]; cout << x << ' ' << y << endl; x += h1[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < h2.size(); j++) { y -= v2[j]; cout << x << ' ' << y << endl; x += h2[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < h3.size(); j++) { y -= v3[j]; cout << x << ' ' << y << endl; x -= h3[j]; cout << x << ' ' << y << endl; } } } } } } }
### 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; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int h; cin >> h; vector<int> l(h); for (int j = 0; j < h; j++) { cin >> l[j]; } int v; cin >> v; vector<int> p(v); for (int j = 0; j < v; j++) { cin >> p[j]; } if (h != v) { cout << "No" << endl; } else { int lsum = 0; for (int j = 0; j < h; j++) { lsum += l[j]; } int psum = 0; for (int j = 0; j < v; j++) { psum += p[j]; } if (lsum % 2 == 1 || psum % 2 == 1) { cout << "No" << endl; } else { vector<vector<bool>> dph(h + 1, vector<bool>(lsum / 2 + 1, false)); dph[0][0] = true; for (int j = 0; j < h; j++) { dph[j + 1] = dph[j]; for (int k = lsum / 2; k >= l[j]; k--) { if (dph[j][k - l[j]]) { dph[j + 1][k] = true; } } } vector<vector<bool>> dpv(v + 1, vector<bool>(psum / 2 + 1, false)); dpv[0][0] = true; for (int j = 0; j < v; j++) { dpv[j + 1] = dpv[j]; for (int k = psum / 2; k >= p[j]; k--) { if (dpv[j][k - p[j]]) { dpv[j + 1][k] = true; } } } if (!dph[h][lsum / 2] || !dpv[v][psum / 2]) { cout << "No" << endl; } else { int hc = lsum / 2; vector<int> ha, hb; for (int j = h - 1; j >= 0; j--) { if (!dph[j][hc]) { ha.push_back(l[j]); hc -= l[j]; } else { hb.push_back(l[j]); } } int vc = psum / 2; vector<int> va, vb; for (int j = v - 1; j >= 0; j--) { if (!dpv[j][vc]) { va.push_back(p[j]); vc -= p[j]; } else { vb.push_back(p[j]); } } if (va.size() == ha.size()) { sort(ha.rbegin(), ha.rend()); sort(hb.rbegin(), hb.rend()); sort(va.begin(), va.end()); sort(vb.begin(), vb.end()); cout << "Yes" << endl; int x = 0, y = 0; for (int j = 0; j < va.size(); j++) { x += ha[j]; cout << x << ' ' << y << endl; y += va[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < vb.size(); j++) { x -= hb[j]; cout << x << ' ' << y << endl; y -= vb[j]; cout << x << ' ' << y << endl; } } if (ha.size() < va.size()) { vector<int> h1, v1; for (int j = 0; j < ha.size(); j++) { h1.push_back(ha[j]); v1.push_back(va[j]); } vector<int> h2, v2; for (int j = 0; j < va.size() - ha.size(); j++) { h2.push_back(hb[j]); v2.push_back(va[j + ha.size()]); } vector<int> h3, v3; for (int j = 0; j < h - va.size(); j++) { h3.push_back(hb[j + (va.size() - ha.size())]); v3.push_back(vb[j]); } sort(h1.rbegin(), h1.rend()); sort(v1.begin(), v1.end()); sort(h2.begin(), h2.end()); sort(v2.rbegin(), v2.rend()); sort(h3.rbegin(), h3.rend()); sort(v3.begin(), v3.end()); cout << "Yes" << endl; int x = 0, y = 0; for (int j = 0; j < h1.size(); j++) { x += h1[j]; cout << x << ' ' << y << endl; y += v1[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < h2.size(); j++) { x -= h2[j]; cout << x << ' ' << y << endl; y += v2[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < h3.size(); j++) { x -= h3[j]; cout << x << ' ' << y << endl; y -= v3[j]; cout << x << ' ' << y << endl; } } if (ha.size() > va.size()) { vector<int> h1, v1; for (int j = 0; j < va.size(); j++) { h1.push_back(ha[j]); v1.push_back(va[j]); } vector<int> h2, v2; for (int j = 0; j < ha.size() - va.size(); j++) { h2.push_back(ha[j + va.size()]); v2.push_back(vb[j]); } vector<int> h3, v3; for (int j = 0; j < h - ha.size(); j++) { h3.push_back(hb[j]); v3.push_back(vb[j + (ha.size() - va.size())]); } sort(h1.begin(), h1.end()); sort(v1.rbegin(), v1.rend()); sort(h2.rbegin(), h2.rend()); sort(v2.begin(), v2.end()); sort(h3.begin(), h3.end()); sort(v3.rbegin(), v3.rend()); cout << "Yes" << endl; int x = 0, y = 0; for (int j = 0; j < h1.size(); j++) { y += v1[j]; cout << x << ' ' << y << endl; x += h1[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < h2.size(); j++) { y -= v2[j]; cout << x << ' ' << y << endl; x += h2[j]; cout << x << ' ' << y << endl; } for (int j = 0; j < h3.size(); j++) { y -= v3[j]; cout << x << ' ' << y << endl; x -= h3[j]; cout << x << ' ' << y << endl; } } } } } } } ```
#include <bits/stdc++.h> using namespace std; int gi() { int res = 0, w = 1; char ch = getchar(); while (ch != '-' && !isdigit(ch)) ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar(); return res * w; } const int MAX_N = 1005; int N, M, A[MAX_N], B[MAX_N]; bitset<500 * MAX_N> bit[505]; bool solve(int *A, vector<int> &S, vector<int> &T) { 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; for (int i = N, j = sum >> 1; i; i--) { if (bit[i - 1][j]) S.push_back(A[i]); else j -= A[i], T.push_back(A[i]); } return 1; } int main() { int T = gi(); bit[0][0] = 1; while (T--) { N = gi(); for (int i = 1; i <= N; i++) A[i] = gi(); M = gi(); for (int i = 1; i <= M; i++) B[i] = gi(); if (N != M) { puts("No"); continue; } vector<int> Al, Ar, Bu, Bd; if (!solve(A, Al, Ar) || !solve(B, Bu, Bd)) { puts("No"); continue; } if (Al.size() < Ar.size()) swap(Al, Ar); if (Bu.size() > Bd.size()) swap(Bu, Bd); sort(Al.begin(), Al.end(), greater<int>()); sort(Ar.begin(), Ar.end(), greater<int>()); sort(Bu.begin(), Bu.end()); sort(Bd.begin(), Bd.end()); vector<int> AA = Ar, BB = Bd; for (int i : Al) AA.push_back(-i); for (int i : Bu) BB.push_back(-i); puts("Yes"); for (int i = 0, x = 0, y = 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 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; int gi() { int res = 0, w = 1; char ch = getchar(); while (ch != '-' && !isdigit(ch)) ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar(); return res * w; } const int MAX_N = 1005; int N, M, A[MAX_N], B[MAX_N]; bitset<500 * MAX_N> bit[505]; bool solve(int *A, vector<int> &S, vector<int> &T) { 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; for (int i = N, j = sum >> 1; i; i--) { if (bit[i - 1][j]) S.push_back(A[i]); else j -= A[i], T.push_back(A[i]); } return 1; } int main() { int T = gi(); bit[0][0] = 1; while (T--) { N = gi(); for (int i = 1; i <= N; i++) A[i] = gi(); M = gi(); for (int i = 1; i <= M; i++) B[i] = gi(); if (N != M) { puts("No"); continue; } vector<int> Al, Ar, Bu, Bd; if (!solve(A, Al, Ar) || !solve(B, Bu, Bd)) { puts("No"); continue; } if (Al.size() < Ar.size()) swap(Al, Ar); if (Bu.size() > Bd.size()) swap(Bu, Bd); sort(Al.begin(), Al.end(), greater<int>()); sort(Ar.begin(), Ar.end(), greater<int>()); sort(Bu.begin(), Bu.end()); sort(Bd.begin(), Bd.end()); vector<int> AA = Ar, BB = Bd; for (int i : Al) AA.push_back(-i); for (int i : Bu) BB.push_back(-i); puts("Yes"); for (int i = 0, x = 0, y = 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; int tt; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> tt; while (tt--) { int hh, vv; cin >> hh; vector<int> L(hh); for (int i = 0; i < hh; i++) cin >> L[i]; cin >> vv; vector<int> P(vv); for (int i = 0; i < vv; i++) cin >> P[i]; if (hh != vv) { cout << "No\n"; continue; } int n = vv; auto part = [&](vector<int> &a) { int n = a.size(); int b = accumulate(a.begin(), a.end(), 0); if (b & 1) return false; const int N = 1e6 + 5; vector<bitset<N>> dp(n + 1); dp[0][0] = 1; for (int i = 0; i < n; i++) { dp[i + 1] = dp[i] | (dp[i] << a[i]); } if (!dp[n][b >> 1]) return false; int cur = b >> 1; for (int i = n; i > 0; i--) { if (cur >= a[i - 1] && dp[i - 1][cur - a[i - 1]]) { cur -= a[i - 1]; a[i - 1] *= -1; } } assert(!cur); return true; }; if (!part(P) || !part(L)) { cout << "No\n"; continue; } vector<int> sP, nP, sL, nL; for (int i : P) { if (i > 0) sP.push_back(i); else nP.push_back(i); } for (int i : L) { if (i > 0) sL.push_back(i); else nL.push_back(i); } if (sP.size() > sL.size()) { swap(sP, nP); swap(sL, nL); for (int &i : sP) i *= -1; for (int &i : nP) i *= -1; for (int &i : sL) i *= -1; for (int &i : nL) i *= -1; } sort(sP.begin(), sP.end(), greater<int>()); sort(sL.begin(), sL.end()); sort(nL.begin(), nL.end(), greater<int>()); sort(nP.begin(), nP.end()); P.clear(); L.clear(); for (int i : sP) P.push_back(i); for (int i : nP) P.push_back(i); for (int i : sL) L.push_back(i); for (int i : nL) L.push_back(i); cout << "Yes\n"; int x = 0, y = 0; cout << x << " " << y << "\n"; for (int i = 0; i < n; i++) { cout << x << " " << y + P[i] << "\n"; if (i != n - 1) cout << x + L[i] << " " << y + P[i] << "\n"; x += L[i]; y += P[i]; } } 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 tt; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> tt; while (tt--) { int hh, vv; cin >> hh; vector<int> L(hh); for (int i = 0; i < hh; i++) cin >> L[i]; cin >> vv; vector<int> P(vv); for (int i = 0; i < vv; i++) cin >> P[i]; if (hh != vv) { cout << "No\n"; continue; } int n = vv; auto part = [&](vector<int> &a) { int n = a.size(); int b = accumulate(a.begin(), a.end(), 0); if (b & 1) return false; const int N = 1e6 + 5; vector<bitset<N>> dp(n + 1); dp[0][0] = 1; for (int i = 0; i < n; i++) { dp[i + 1] = dp[i] | (dp[i] << a[i]); } if (!dp[n][b >> 1]) return false; int cur = b >> 1; for (int i = n; i > 0; i--) { if (cur >= a[i - 1] && dp[i - 1][cur - a[i - 1]]) { cur -= a[i - 1]; a[i - 1] *= -1; } } assert(!cur); return true; }; if (!part(P) || !part(L)) { cout << "No\n"; continue; } vector<int> sP, nP, sL, nL; for (int i : P) { if (i > 0) sP.push_back(i); else nP.push_back(i); } for (int i : L) { if (i > 0) sL.push_back(i); else nL.push_back(i); } if (sP.size() > sL.size()) { swap(sP, nP); swap(sL, nL); for (int &i : sP) i *= -1; for (int &i : nP) i *= -1; for (int &i : sL) i *= -1; for (int &i : nL) i *= -1; } sort(sP.begin(), sP.end(), greater<int>()); sort(sL.begin(), sL.end()); sort(nL.begin(), nL.end(), greater<int>()); sort(nP.begin(), nP.end()); P.clear(); L.clear(); for (int i : sP) P.push_back(i); for (int i : nP) P.push_back(i); for (int i : sL) L.push_back(i); for (int i : nL) L.push_back(i); cout << "Yes\n"; int x = 0, y = 0; cout << x << " " << y << "\n"; for (int i = 0; i < n; i++) { cout << x << " " << y + P[i] << "\n"; if (i != n - 1) cout << x + L[i] << " " << y + P[i] << "\n"; x += L[i]; y += P[i]; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int gi() { char cc = getchar(); int cn = 0, flus = 1; while (cc < '0' || cc > '9') { if (cc == '-') flus = -flus; cc = getchar(); } while (cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar(); return cn * flus; } const int N = 500 + 5; bitset<N * N + 50> f[N]; vector<int> A, B; int n, m; void solve(vector<int> a, vector<int> &S, vector<int> &T) { int sum = 0; for (register int i = 1; i <= n; ++i) f[i] = (f[i - 1] | (f[i - 1] << a[i])), sum += a[i]; if (!f[n][sum / 2] || (sum & 1)) return; int t = sum / 2; for (register int i = n; i >= 1; --i) { if (t >= a[i] && f[i - 1][t - a[i]]) S.push_back(a[i]), t -= a[i]; else T.push_back(a[i]); } } signed main() { int T = gi(); f[0][0] = 1; while (T--) { n = gi(), A.resize(n + 1); for (register int i = (1); i <= (n); ++i) A[i] = gi(); m = gi(), B.resize(m + 1); for (register int i = (1); i <= (m); ++i) B[i] = gi(); if (n != m) { puts("No"); continue; } vector<int> L, R, U, D; solve(A, L, R), solve(B, U, D); if (L.empty() || R.empty() || U.empty() || D.empty()) { puts("No"); continue; } if (L.size() < R.size()) swap(L, R); if (U.size() < D.size()) swap(U, D); sort(L.begin(), L.end(), greater<int>()), sort(D.begin(), D.end()); sort(R.begin(), R.end(), greater<int>()), sort(U.begin(), U.end()); vector<int> dx, dy; for (int x : R) dx.push_back(x); for (int x : L) dx.push_back(-x); for (int y : U) dy.push_back(y); for (int y : D) dy.push_back(-y); int nx = 0, ny = 0; puts("Yes"); for (register int i = 0; i < n + m; ++i) { printf("%d %d\n", nx, ny); ((i ^ 1) & 1) ? nx += dx[i / 2] : ny += dy[i / 2]; } } 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 gi() { char cc = getchar(); int cn = 0, flus = 1; while (cc < '0' || cc > '9') { if (cc == '-') flus = -flus; cc = getchar(); } while (cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar(); return cn * flus; } const int N = 500 + 5; bitset<N * N + 50> f[N]; vector<int> A, B; int n, m; void solve(vector<int> a, vector<int> &S, vector<int> &T) { int sum = 0; for (register int i = 1; i <= n; ++i) f[i] = (f[i - 1] | (f[i - 1] << a[i])), sum += a[i]; if (!f[n][sum / 2] || (sum & 1)) return; int t = sum / 2; for (register int i = n; i >= 1; --i) { if (t >= a[i] && f[i - 1][t - a[i]]) S.push_back(a[i]), t -= a[i]; else T.push_back(a[i]); } } signed main() { int T = gi(); f[0][0] = 1; while (T--) { n = gi(), A.resize(n + 1); for (register int i = (1); i <= (n); ++i) A[i] = gi(); m = gi(), B.resize(m + 1); for (register int i = (1); i <= (m); ++i) B[i] = gi(); if (n != m) { puts("No"); continue; } vector<int> L, R, U, D; solve(A, L, R), solve(B, U, D); if (L.empty() || R.empty() || U.empty() || D.empty()) { puts("No"); continue; } if (L.size() < R.size()) swap(L, R); if (U.size() < D.size()) swap(U, D); sort(L.begin(), L.end(), greater<int>()), sort(D.begin(), D.end()); sort(R.begin(), R.end(), greater<int>()), sort(U.begin(), U.end()); vector<int> dx, dy; for (int x : R) dx.push_back(x); for (int x : L) dx.push_back(-x); for (int y : U) dy.push_back(y); for (int y : D) dy.push_back(-y); int nx = 0, ny = 0; puts("Yes"); for (register int i = 0; i < n + m; ++i) { printf("%d %d\n", nx, ny); ((i ^ 1) & 1) ? nx += dx[i / 2] : ny += dy[i / 2]; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 10; struct node { int x, y; }; int T, n, m, sum1, sum2; int a[N], b[N]; int totx[2], toty[2]; int x[2][N], y[2][N]; int tot; node ans[N]; inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; } bitset<N * N> dp[N]; bool flag = true; inline void initial() { flag = true; int s1 = sum1 / 2, s2 = sum2 / 2; for (register int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (dp[n][s1]) { for (register int i = n; i >= 1; i--) { if (s1 >= a[i] && dp[i - 1][s1 - a[i]]) s1 -= a[i], x[1][++totx[1]] = a[i]; else x[0][++totx[0]] = a[i]; } } else { flag = false; return; } for (register int i = 1; i <= m; i++) dp[i] = dp[i - 1] | (dp[i - 1] << b[i]); if (dp[m][s2]) { for (register int i = m; i >= 1; i--) { if (s2 >= b[i] && dp[i - 1][s2 - b[i]]) s2 -= b[i], y[1][++toty[1]] = b[i]; else y[0][++toty[0]] = b[i]; } } else { flag = false; return; } } inline bool cmp(int p, int q) { return p > q; } inline void sol() { bool Judge = false; if (totx[1] <= toty[1]) { sort(y[1] + 1, y[1] + toty[1] + 1); sort(y[0] + 1, y[0] + toty[0] + 1); sort(x[1] + 1, x[1] + totx[1] + 1, cmp); sort(x[0] + 1, x[0] + totx[0] + 1, cmp); node now = {0, 0}; int i[2] = {1, 1}, j[2] = {1, 1}; while (n--) { if (i[1] <= totx[1]) now.x += x[1][i[1]++], ans[++tot] = now; else now.x -= x[0][i[0]++], ans[++tot] = now; if (j[1] <= toty[1]) now.y += y[1][j[1]++], ans[++tot] = now; else now.y -= y[0][j[0]++], ans[++tot] = now; } } else { sort(x[1] + 1, x[1] + totx[1] + 1); sort(x[0] + 1, x[0] + totx[0] + 1); sort(y[1] + 1, y[1] + toty[1] + 1, cmp); sort(y[0] + 1, y[0] + toty[0] + 1, cmp); node now = {0, 0}; int i[2] = {1, 1}, j[2] = {1, 1}; while (n--) { if (j[1] <= toty[1]) now.y += y[1][j[1]++], ans[++tot] = now; else now.y -= y[0][j[0]++], ans[++tot] = now; if (i[1] <= totx[1]) now.x += x[1][i[1]++], ans[++tot] = now; else now.x -= x[0][i[0]++], ans[++tot] = now; } } } int main() { T = read(); dp[0][0] = 1; while (T--) { totx[1] = totx[0] = toty[1] = toty[0] = 0; tot = 0; sum1 = 0, sum2 = 0; n = read(); for (register int i = 1; i <= n; i++) a[i] = read(), sum1 += a[i]; m = read(); for (register int i = 1; i <= m; i++) b[i] = read(), sum2 += b[i]; if (n != m || sum1 & 1 || sum2 & 1) { printf("No\n"); continue; } initial(); if (!flag) { printf("No\n"); continue; } sol(); printf("Yes\n"); for (register int i = 1; i <= tot; i++) printf("%d %d\n", ans[i].x, ans[i].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; const int N = 1e3 + 10; struct node { int x, y; }; int T, n, m, sum1, sum2; int a[N], b[N]; int totx[2], toty[2]; int x[2][N], y[2][N]; int tot; node ans[N]; inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; } bitset<N * N> dp[N]; bool flag = true; inline void initial() { flag = true; int s1 = sum1 / 2, s2 = sum2 / 2; for (register int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (dp[n][s1]) { for (register int i = n; i >= 1; i--) { if (s1 >= a[i] && dp[i - 1][s1 - a[i]]) s1 -= a[i], x[1][++totx[1]] = a[i]; else x[0][++totx[0]] = a[i]; } } else { flag = false; return; } for (register int i = 1; i <= m; i++) dp[i] = dp[i - 1] | (dp[i - 1] << b[i]); if (dp[m][s2]) { for (register int i = m; i >= 1; i--) { if (s2 >= b[i] && dp[i - 1][s2 - b[i]]) s2 -= b[i], y[1][++toty[1]] = b[i]; else y[0][++toty[0]] = b[i]; } } else { flag = false; return; } } inline bool cmp(int p, int q) { return p > q; } inline void sol() { bool Judge = false; if (totx[1] <= toty[1]) { sort(y[1] + 1, y[1] + toty[1] + 1); sort(y[0] + 1, y[0] + toty[0] + 1); sort(x[1] + 1, x[1] + totx[1] + 1, cmp); sort(x[0] + 1, x[0] + totx[0] + 1, cmp); node now = {0, 0}; int i[2] = {1, 1}, j[2] = {1, 1}; while (n--) { if (i[1] <= totx[1]) now.x += x[1][i[1]++], ans[++tot] = now; else now.x -= x[0][i[0]++], ans[++tot] = now; if (j[1] <= toty[1]) now.y += y[1][j[1]++], ans[++tot] = now; else now.y -= y[0][j[0]++], ans[++tot] = now; } } else { sort(x[1] + 1, x[1] + totx[1] + 1); sort(x[0] + 1, x[0] + totx[0] + 1); sort(y[1] + 1, y[1] + toty[1] + 1, cmp); sort(y[0] + 1, y[0] + toty[0] + 1, cmp); node now = {0, 0}; int i[2] = {1, 1}, j[2] = {1, 1}; while (n--) { if (j[1] <= toty[1]) now.y += y[1][j[1]++], ans[++tot] = now; else now.y -= y[0][j[0]++], ans[++tot] = now; if (i[1] <= totx[1]) now.x += x[1][i[1]++], ans[++tot] = now; else now.x -= x[0][i[0]++], ans[++tot] = now; } } } int main() { T = read(); dp[0][0] = 1; while (T--) { totx[1] = totx[0] = toty[1] = toty[0] = 0; tot = 0; sum1 = 0, sum2 = 0; n = read(); for (register int i = 1; i <= n; i++) a[i] = read(), sum1 += a[i]; m = read(); for (register int i = 1; i <= m; i++) b[i] = read(), sum2 += b[i]; if (n != m || sum1 & 1 || sum2 & 1) { printf("No\n"); continue; } initial(); if (!flag) { printf("No\n"); continue; } sol(); printf("Yes\n"); for (register int i = 1; i <= tot; i++) printf("%d %d\n", ans[i].x, ans[i].y); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int T; int n, m; int w[1005], h[1005]; vector<int> wpos, wneg, hpos, hneg; bool used[1005]; bool calc(int w[], vector<int> &pos, vector<int> &neg) { pos.clear(), neg.clear(); int sum = 0; for (long long(i) = (1); (i) <= (n); (i)++) sum += w[i]; if (sum % 2) return false; sum /= 2; vector<int> dp(sum + 1); for (long long(i) = (1); (i) <= (sum); (i)++) dp[i] = -1; for (long long(i) = (0); (i) <= (n - 1); (i)++) { for (int j = sum; j >= 0; j--) { if (dp[j] < 0) continue; if (j + w[i + 1] <= sum && dp[j + w[i + 1]] < 0) dp[j + w[i + 1]] = i + 1; } } if (dp[sum] < 0) return false; for (long long(i) = (1); (i) <= (n); (i)++) used[i] = false; int j = sum; while (j > 0) { used[dp[j]] = true; j -= w[dp[j]]; } for (long long(i) = (1); (i) <= (n); (i)++) { if (used[i]) pos.push_back(w[i]); else neg.push_back(w[i]); } return true; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> T; while (T--) { cin >> n; for (long long(i) = (1); (i) <= (n); (i)++) cin >> w[i]; cin >> m; for (long long(i) = (1); (i) <= (m); (i)++) cin >> h[i]; if (n != m) { cout << "No" << endl; continue; } if (!calc(w, wpos, wneg) || !calc(h, hpos, hneg)) { cout << "No" << endl; continue; } cout << "Yes" << endl; if (wpos.size() > wneg.size()) swap(wpos, wneg); if (hpos.size() < hneg.size()) swap(hpos, hneg); sort(wpos.rbegin(), wpos.rend()); sort(wneg.rbegin(), wneg.rend()); sort((hpos).begin(), (hpos).end()); sort((hneg).begin(), (hneg).end()); for (auto x : wneg) wpos.push_back(-x); for (auto x : hneg) hpos.push_back(-x); long long x = 0, y = 0; for (long long(i) = (0); (i) <= (2 * n - 1); (i)++) { cout << x << " " << y << "\n"; if (i % 2 == 0) x += wpos[i / 2]; else y += hpos[i / 2]; } } flush(cout); 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 T; int n, m; int w[1005], h[1005]; vector<int> wpos, wneg, hpos, hneg; bool used[1005]; bool calc(int w[], vector<int> &pos, vector<int> &neg) { pos.clear(), neg.clear(); int sum = 0; for (long long(i) = (1); (i) <= (n); (i)++) sum += w[i]; if (sum % 2) return false; sum /= 2; vector<int> dp(sum + 1); for (long long(i) = (1); (i) <= (sum); (i)++) dp[i] = -1; for (long long(i) = (0); (i) <= (n - 1); (i)++) { for (int j = sum; j >= 0; j--) { if (dp[j] < 0) continue; if (j + w[i + 1] <= sum && dp[j + w[i + 1]] < 0) dp[j + w[i + 1]] = i + 1; } } if (dp[sum] < 0) return false; for (long long(i) = (1); (i) <= (n); (i)++) used[i] = false; int j = sum; while (j > 0) { used[dp[j]] = true; j -= w[dp[j]]; } for (long long(i) = (1); (i) <= (n); (i)++) { if (used[i]) pos.push_back(w[i]); else neg.push_back(w[i]); } return true; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> T; while (T--) { cin >> n; for (long long(i) = (1); (i) <= (n); (i)++) cin >> w[i]; cin >> m; for (long long(i) = (1); (i) <= (m); (i)++) cin >> h[i]; if (n != m) { cout << "No" << endl; continue; } if (!calc(w, wpos, wneg) || !calc(h, hpos, hneg)) { cout << "No" << endl; continue; } cout << "Yes" << endl; if (wpos.size() > wneg.size()) swap(wpos, wneg); if (hpos.size() < hneg.size()) swap(hpos, hneg); sort(wpos.rbegin(), wpos.rend()); sort(wneg.rbegin(), wneg.rend()); sort((hpos).begin(), (hpos).end()); sort((hneg).begin(), (hneg).end()); for (auto x : wneg) wpos.push_back(-x); for (auto x : hneg) hpos.push_back(-x); long long x = 0, y = 0; for (long long(i) = (0); (i) <= (2 * n - 1); (i)++) { cout << x << " " << y << "\n"; if (i % 2 == 0) x += wpos[i / 2]; else y += hpos[i / 2]; } } flush(cout); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 100; bitset<maxn> f[1005], g[1005]; int n, _, m, a[1005], b[1005], sx, sy; bool cmp(int u, int v) { return u > v; } 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; } f[0].reset(); g[0].reset(); f[0][0] = g[0][0] = 1; sx = 0; sy = 0; for (int i = 1; i <= n; i++) { sx += a[i]; sy += b[i]; f[i] = f[i - 1] | (f[i - 1] << a[i]); g[i] = g[i - 1] | (g[i - 1] << b[i]); } if (sx % 2 || sy % 2) { puts("No"); return; } if (!f[n][sx / 2] || !g[n][sy / 2]) { puts("No"); return; } int now; now = sx / 2; for (int i = n; i; i--) { if (f[i - 1][now]) a[i] = -a[i]; else now -= a[i]; } now = sy / 2; for (int i = n; i; i--) { if (g[i - 1][now]) b[i] = -b[i]; else now -= b[i]; } vector<int> ux, uy, dx, dy; ux.clear(); uy.clear(); dx.clear(); dy.clear(); sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); int p = 1, q = 1; while (a[p] < 0) ++p; while (b[q] < 0) ++q; puts("Yes"); if (p > q) { sort(a + 1, a + p, cmp); sort(a + p, a + n + 1); sort(b + 1, b + q); sort(b + q, b + n + 1, cmp); } else { sort(a + 1, a + p); sort(a + p, a + n + 1, cmp); sort(b + 1, b + q, cmp); sort(b + q, b + n + 1); } int x = 0, y = 0; for (int i = 1; i <= n; i++) { printf("%d %d\n", x, y); if (p > q) y += b[i]; else x += a[i]; printf("%d %d\n", x, y); if (p > q) x += a[i]; else y += b[i]; } } int main() { scanf("%d", &_); while (_--) solve(); 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 maxn = 1e6 + 100; bitset<maxn> f[1005], g[1005]; int n, _, m, a[1005], b[1005], sx, sy; bool cmp(int u, int v) { return u > v; } 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; } f[0].reset(); g[0].reset(); f[0][0] = g[0][0] = 1; sx = 0; sy = 0; for (int i = 1; i <= n; i++) { sx += a[i]; sy += b[i]; f[i] = f[i - 1] | (f[i - 1] << a[i]); g[i] = g[i - 1] | (g[i - 1] << b[i]); } if (sx % 2 || sy % 2) { puts("No"); return; } if (!f[n][sx / 2] || !g[n][sy / 2]) { puts("No"); return; } int now; now = sx / 2; for (int i = n; i; i--) { if (f[i - 1][now]) a[i] = -a[i]; else now -= a[i]; } now = sy / 2; for (int i = n; i; i--) { if (g[i - 1][now]) b[i] = -b[i]; else now -= b[i]; } vector<int> ux, uy, dx, dy; ux.clear(); uy.clear(); dx.clear(); dy.clear(); sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); int p = 1, q = 1; while (a[p] < 0) ++p; while (b[q] < 0) ++q; puts("Yes"); if (p > q) { sort(a + 1, a + p, cmp); sort(a + p, a + n + 1); sort(b + 1, b + q); sort(b + q, b + n + 1, cmp); } else { sort(a + 1, a + p); sort(a + p, a + n + 1, cmp); sort(b + 1, b + q, cmp); sort(b + q, b + n + 1); } int x = 0, y = 0; for (int i = 1; i <= n; i++) { printf("%d %d\n", x, y); if (p > q) y += b[i]; else x += a[i]; printf("%d %d\n", x, y); if (p > q) x += a[i]; else y += b[i]; } } int main() { scanf("%d", &_); while (_--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; bitset<1 << 20> D[1010]; int A[1010], B[1010]; int n, m, a, b; bool can(vector<int> XA, vector<int> YA, vector<int> XB, vector<int> YB, int opt) { int i, x, y; vector<pii> A; sort(XA.rbegin(), XA.rend()); sort(XB.begin(), XB.end()); sort(YA.rbegin(), YA.rend()); sort(YB.begin(), YB.end()); for (i = 0; i < YA.size(); i++) XA.push_back(-YA[i]); for (i = 0; i < YB.size(); i++) XB.push_back(-YB[i]); x = 0; y = 0; for (i = 0; i < n; i++) { A.emplace_back(x, y); x += XA[i]; A.emplace_back(x, y); y += XB[i]; if (XA[i] > 0 && XB[i] < 0) return 0; } cout << "Yes\n"; for (auto [x, y] : A) { if (opt) swap(x, y); cout << x << " " << y << "\n"; } return 1; } void tc() { int i, f, x, y; cin >> n; a = 0; for (i = 1; i <= n; i++) cin >> A[i], a += A[i]; cin >> m; b = 0; for (i = 1; i <= m; i++) cin >> B[i], b += B[i]; if (n != m || a % 2 == 1 || b % 2 == 1) { cout << "No\n"; return; } a /= 2; b /= 2; vector<int> XA, XB, YA, YB; for (f = 0; f < 2; f++) { D[0][0] = 1; for (i = 1; i <= n; i++) { D[i] = D[i - 1] | (D[i - 1] << A[i]); } if (!D[n][a]) { cout << "No\n"; return; } for (i = n; i >= 1; i--) { if (!D[i - 1][a]) a -= A[i], XA.push_back(A[i]); else YA.push_back(A[i]); } swap(XA, XB); swap(YA, YB); swap(A, B); swap(a, b); swap(n, m); } for (i = 0; i < 4; i++) { if (i & 1) swap(XA, YA); if (i & 2) swap(XB, YB); if (can(XA, YA, XB, YB, 0)) return; if (can(XB, YB, XA, YA, 1)) return; } cout << "No\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; for (; t--;) tc(); 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; using pii = pair<int, int>; bitset<1 << 20> D[1010]; int A[1010], B[1010]; int n, m, a, b; bool can(vector<int> XA, vector<int> YA, vector<int> XB, vector<int> YB, int opt) { int i, x, y; vector<pii> A; sort(XA.rbegin(), XA.rend()); sort(XB.begin(), XB.end()); sort(YA.rbegin(), YA.rend()); sort(YB.begin(), YB.end()); for (i = 0; i < YA.size(); i++) XA.push_back(-YA[i]); for (i = 0; i < YB.size(); i++) XB.push_back(-YB[i]); x = 0; y = 0; for (i = 0; i < n; i++) { A.emplace_back(x, y); x += XA[i]; A.emplace_back(x, y); y += XB[i]; if (XA[i] > 0 && XB[i] < 0) return 0; } cout << "Yes\n"; for (auto [x, y] : A) { if (opt) swap(x, y); cout << x << " " << y << "\n"; } return 1; } void tc() { int i, f, x, y; cin >> n; a = 0; for (i = 1; i <= n; i++) cin >> A[i], a += A[i]; cin >> m; b = 0; for (i = 1; i <= m; i++) cin >> B[i], b += B[i]; if (n != m || a % 2 == 1 || b % 2 == 1) { cout << "No\n"; return; } a /= 2; b /= 2; vector<int> XA, XB, YA, YB; for (f = 0; f < 2; f++) { D[0][0] = 1; for (i = 1; i <= n; i++) { D[i] = D[i - 1] | (D[i - 1] << A[i]); } if (!D[n][a]) { cout << "No\n"; return; } for (i = n; i >= 1; i--) { if (!D[i - 1][a]) a -= A[i], XA.push_back(A[i]); else YA.push_back(A[i]); } swap(XA, XB); swap(YA, YB); swap(A, B); swap(a, b); swap(n, m); } for (i = 0; i < 4; i++) { if (i & 1) swap(XA, YA); if (i & 2) swap(XB, YB); if (can(XA, YA, XB, YB, 0)) return; if (can(XB, YB, XA, YA, 1)) return; } cout << "No\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; for (; t--;) tc(); return 0; } ```
#include <bits/stdc++.h> using namespace std; using lint = long long; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; template <typename T, typename V> void ndarray(vector<T> &vec, const V &val, int len) { vec.assign(len, val); } template <typename T, typename V, typename... Args> void ndarray(vector<T> &vec, const V &val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T &v) { ndarray(v, val, args...); }); } template <typename T> bool chmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } pair<vector<int>, vector<int>> divide(vector<int> V) { sort((V).begin(), (V).end()); int tot = accumulate((V).begin(), (V).end(), 0); vector<int> ret[2]; if (tot % 2) return {ret[0], ret[1]}; vector<int> dp(tot / 2 + 1); dp[0] = 1; for (auto x : V) { for (int i = (dp.size()) - 1, i_begin_ = (x); i >= i_begin_; i--) { if (dp[i] == 0 and dp[i - x]) { dp[i] = x; } } } if (dp.back()) { multiset<int> ok; for (auto x : V) ok.insert(x); int now = tot / 2; while (now) { ret[0].emplace_back(dp[now]); ok.erase(ok.lower_bound(dp[now])); now -= dp[now]; } for (auto x : ok) { ret[1].emplace_back(x); } } return {ret[0], ret[1]}; } void solve() { int H; cin >> H; vector<int> L(H); cin >> L; int V; cin >> V; vector<int> P(V); cin >> P; if (H != V) { cout << "No\n"; return; } auto [h1, h2] = divide(L); auto [w1, w2] = divide(P); if (h1.empty() or w1.empty()) { cout << "No\n"; return; } if (h1.size() > h2.size()) swap(h1, h2); sort(h1.rbegin(), h1.rend()); sort(h2.rbegin(), h2.rend()); for (auto &x : h2) x *= -1; h1.insert(h1.end(), (h2).begin(), (h2).end()); if (w1.size() < w2.size()) swap(w1, w2); sort(w1.begin(), w1.end()); sort(w2.begin(), w2.end()); for (auto &x : w2) x *= -1; w1.insert(w1.end(), (w2).begin(), (w2).end()); int x = 0, y = 0; cout << "Yes\n"; for (int i = (0), i_end_ = (H + V); i < i_end_; i++) { cout << x << ' ' << y << '\n'; if (i % 2 == 0) x += h1[i / 2]; else y += w1[i / 2]; } } int main() { int T; cin >> T; while (T--) { solve(); } }
### 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; using lint = long long; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; template <typename T, typename V> void ndarray(vector<T> &vec, const V &val, int len) { vec.assign(len, val); } template <typename T, typename V, typename... Args> void ndarray(vector<T> &vec, const V &val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T &v) { ndarray(v, val, args...); }); } template <typename T> bool chmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool chmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } template <typename T> vector<T> srtunq(vector<T> vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } pair<vector<int>, vector<int>> divide(vector<int> V) { sort((V).begin(), (V).end()); int tot = accumulate((V).begin(), (V).end(), 0); vector<int> ret[2]; if (tot % 2) return {ret[0], ret[1]}; vector<int> dp(tot / 2 + 1); dp[0] = 1; for (auto x : V) { for (int i = (dp.size()) - 1, i_begin_ = (x); i >= i_begin_; i--) { if (dp[i] == 0 and dp[i - x]) { dp[i] = x; } } } if (dp.back()) { multiset<int> ok; for (auto x : V) ok.insert(x); int now = tot / 2; while (now) { ret[0].emplace_back(dp[now]); ok.erase(ok.lower_bound(dp[now])); now -= dp[now]; } for (auto x : ok) { ret[1].emplace_back(x); } } return {ret[0], ret[1]}; } void solve() { int H; cin >> H; vector<int> L(H); cin >> L; int V; cin >> V; vector<int> P(V); cin >> P; if (H != V) { cout << "No\n"; return; } auto [h1, h2] = divide(L); auto [w1, w2] = divide(P); if (h1.empty() or w1.empty()) { cout << "No\n"; return; } if (h1.size() > h2.size()) swap(h1, h2); sort(h1.rbegin(), h1.rend()); sort(h2.rbegin(), h2.rend()); for (auto &x : h2) x *= -1; h1.insert(h1.end(), (h2).begin(), (h2).end()); if (w1.size() < w2.size()) swap(w1, w2); sort(w1.begin(), w1.end()); sort(w2.begin(), w2.end()); for (auto &x : w2) x *= -1; w1.insert(w1.end(), (w2).begin(), (w2).end()); int x = 0, y = 0; cout << "Yes\n"; for (int i = (0), i_end_ = (H + V); i < i_end_; i++) { cout << x << ' ' << y << '\n'; if (i % 2 == 0) x += h1[i / 2]; else y += w1[i / 2]; } } int main() { int T; cin >> T; while (T--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 1010; const int MAXL = 250010; int x[MAXN], y[MAXN]; int f[MAXL]; bool GetValue(int n, int* x, vector<int>& x1, vector<int>& x2) { int sumx = 0; for (int i = 0; i < n; ++i) sumx += x[i]; if (sumx & 1) return false; sort(x, x + n); memset(f, -1, sizeof(f)); f[0] = 0; int sum = 0; for (int i = 0; i < n; ++i) { sum += x[i]; if (sum > sumx / 2) sum = sumx / 2; for (int j = sum; j >= x[i]; --j) { if (f[j] != -1) continue; if (f[j - x[i]] != -1) f[j] = i; } } if (f[sum] == -1) return false; while (sum > 0) { x1.push_back(f[sum]); sum -= x[f[sum]]; } sort(x1.begin(), x1.end()); int k = 0; for (int i = 0; i < n; ++i) { if (k < x1.size() && x1[k] == i) { ++k; continue; } x2.push_back(i); } return true; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); ios::sync_with_stdio(false); cin.tie(0); int times; cin >> times; while (times--) { int n, m; cin >> n; for (int i = 0; i < n; ++i) cin >> x[i]; cin >> m; for (int i = 0; i < m; ++i) cin >> y[i]; if (n != m) { cout << "No" << endl; continue; } vector<int> x1, x2, y1, y2; if (!GetValue(n, x, x1, x2) || !GetValue(m, y, y1, y2)) { cout << "No" << endl; continue; } if (x1.size() > x2.size()) swap(x1, x2); if (y1.size() < y2.size()) swap(y1, y2); reverse(x1.begin(), x1.end()); reverse(y2.begin(), y2.end()); vector<pair<int, int> > ansx, ansy; int tx = 0, ty = 0; for (int i = 0; i < x1.size(); ++i) { tx += x[x1[i]]; ansx.push_back(make_pair(tx, ty)); ty += y[y1[i]]; ansx.push_back(make_pair(tx, ty)); } tx = 0, ty = 0; for (int i = 0; i < y2.size(); ++i) { ty += y[y2[i]]; ansy.push_back(make_pair(tx, ty)); tx += x[x2[i]]; ansy.push_back(make_pair(tx, ty)); } int base = y1.size() - x1.size(); for (int i = 0; i < base; ++i) { ty -= y[y1[i + x1.size()]]; ansy.push_back(make_pair(tx, ty)); tx += x[x2[i + y2.size()]]; ansy.push_back(make_pair(tx, ty)); } cout << "Yes" << endl; for (auto& [a, b] : ansx) cout << a << " " << b << endl; for (int i = (int)ansy.size() - 2; i >= 0; --i) cout << ansy[i].first << " " << ansy[i].second << endl; cout << "0 0" << endl; } 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 = 1010; const int MAXL = 250010; int x[MAXN], y[MAXN]; int f[MAXL]; bool GetValue(int n, int* x, vector<int>& x1, vector<int>& x2) { int sumx = 0; for (int i = 0; i < n; ++i) sumx += x[i]; if (sumx & 1) return false; sort(x, x + n); memset(f, -1, sizeof(f)); f[0] = 0; int sum = 0; for (int i = 0; i < n; ++i) { sum += x[i]; if (sum > sumx / 2) sum = sumx / 2; for (int j = sum; j >= x[i]; --j) { if (f[j] != -1) continue; if (f[j - x[i]] != -1) f[j] = i; } } if (f[sum] == -1) return false; while (sum > 0) { x1.push_back(f[sum]); sum -= x[f[sum]]; } sort(x1.begin(), x1.end()); int k = 0; for (int i = 0; i < n; ++i) { if (k < x1.size() && x1[k] == i) { ++k; continue; } x2.push_back(i); } return true; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); ios::sync_with_stdio(false); cin.tie(0); int times; cin >> times; while (times--) { int n, m; cin >> n; for (int i = 0; i < n; ++i) cin >> x[i]; cin >> m; for (int i = 0; i < m; ++i) cin >> y[i]; if (n != m) { cout << "No" << endl; continue; } vector<int> x1, x2, y1, y2; if (!GetValue(n, x, x1, x2) || !GetValue(m, y, y1, y2)) { cout << "No" << endl; continue; } if (x1.size() > x2.size()) swap(x1, x2); if (y1.size() < y2.size()) swap(y1, y2); reverse(x1.begin(), x1.end()); reverse(y2.begin(), y2.end()); vector<pair<int, int> > ansx, ansy; int tx = 0, ty = 0; for (int i = 0; i < x1.size(); ++i) { tx += x[x1[i]]; ansx.push_back(make_pair(tx, ty)); ty += y[y1[i]]; ansx.push_back(make_pair(tx, ty)); } tx = 0, ty = 0; for (int i = 0; i < y2.size(); ++i) { ty += y[y2[i]]; ansy.push_back(make_pair(tx, ty)); tx += x[x2[i]]; ansy.push_back(make_pair(tx, ty)); } int base = y1.size() - x1.size(); for (int i = 0; i < base; ++i) { ty -= y[y1[i + x1.size()]]; ansy.push_back(make_pair(tx, ty)); tx += x[x2[i + y2.size()]]; ansy.push_back(make_pair(tx, ty)); } cout << "Yes" << endl; for (auto& [a, b] : ansx) cout << a << " " << b << endl; for (int i = (int)ansy.size() - 2; i >= 0; --i) cout << ansy[i].first << " " << ansy[i].second << endl; cout << "0 0" << endl; } return 0; } ```
#include <bits/stdc++.h> template <class T> inline void rd(T &x) { char c = getchar(), f = 0; x = 0; while (!isdigit(c)) f = (c == '-'), c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x = f ? -x : x; } const int MAXN = 1000 + 7; int n, m, a[MAXN], b[MAXN], d[2][MAXN]; std::bitset<1000007> f[1001]; bool bel1[MAXN], bel2[MAXN]; bool work(int n, int s, int a[], bool bel[]) { for (int i = (1); i <= (n); ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]), bel[i] = 0; if (!f[n].test(s)) return 0; int now = s; for (int i = (n); i >= (1); --i) { if (now >= a[i]) { if (f[i - 1].test(now - a[i])) now -= a[i], bel[i] = 1; } } return 1; } void solve() { int s1 = 0, s2 = 0; rd(n); for (int i = (1); i <= (n); ++i) rd(a[i]), s1 += a[i]; rd(m); for (int i = (1); i <= (m); ++i) rd(b[i]), s2 += b[i]; if ((s1 & 1) || (s2 & 1) || n != m) { puts("No"); return; } std::sort(a + 1, a + n + 1); std::sort(b + 1, b + m + 1); std::reverse(a + 1, a + n + 1); std::reverse(b + 1, b + m + 1); if (work(n, s1 / 2, a, bel1) && work(m, s2 / 2, b, bel2)) { puts("Yes"); std::vector<int> x, xx, y, yy; for (int i = (1); i <= (n); ++i) if (bel1[i]) x.push_back(a[i]); else xx.push_back(a[i]); for (int i = (1); i <= (m); ++i) if (bel2[i]) y.push_back(b[i]); else yy.push_back(b[i]); bool tag = 0; if (x.size() > y.size()) tag = 1; if (tag) std::swap(x, y), std::swap(xx, yy); std::reverse((y).begin(), (y).end()); std::reverse((yy).begin(), (yy).end()); int ptr = 0; for (auto v : x) d[0][++ptr] = v; for (auto v : xx) d[0][++ptr] = -v; ptr = 0; for (auto v : y) d[1][++ptr] = v; for (auto v : yy) d[1][++ptr] = -v; int dx = 0, dy = 0; for (int i = (1); i <= (n); ++i) { dx += d[0][i]; if (tag) printf("%d %d\n", dy, dx); else printf("%d %d\n", dx, dy); dy += d[1][i]; if (tag) printf("%d %d\n", dy, dx); else printf("%d %d\n", dx, dy); } } else puts("No"); } int main() { f[0][0] = 1; int T; rd(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> template <class T> inline void rd(T &x) { char c = getchar(), f = 0; x = 0; while (!isdigit(c)) f = (c == '-'), c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x = f ? -x : x; } const int MAXN = 1000 + 7; int n, m, a[MAXN], b[MAXN], d[2][MAXN]; std::bitset<1000007> f[1001]; bool bel1[MAXN], bel2[MAXN]; bool work(int n, int s, int a[], bool bel[]) { for (int i = (1); i <= (n); ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]), bel[i] = 0; if (!f[n].test(s)) return 0; int now = s; for (int i = (n); i >= (1); --i) { if (now >= a[i]) { if (f[i - 1].test(now - a[i])) now -= a[i], bel[i] = 1; } } return 1; } void solve() { int s1 = 0, s2 = 0; rd(n); for (int i = (1); i <= (n); ++i) rd(a[i]), s1 += a[i]; rd(m); for (int i = (1); i <= (m); ++i) rd(b[i]), s2 += b[i]; if ((s1 & 1) || (s2 & 1) || n != m) { puts("No"); return; } std::sort(a + 1, a + n + 1); std::sort(b + 1, b + m + 1); std::reverse(a + 1, a + n + 1); std::reverse(b + 1, b + m + 1); if (work(n, s1 / 2, a, bel1) && work(m, s2 / 2, b, bel2)) { puts("Yes"); std::vector<int> x, xx, y, yy; for (int i = (1); i <= (n); ++i) if (bel1[i]) x.push_back(a[i]); else xx.push_back(a[i]); for (int i = (1); i <= (m); ++i) if (bel2[i]) y.push_back(b[i]); else yy.push_back(b[i]); bool tag = 0; if (x.size() > y.size()) tag = 1; if (tag) std::swap(x, y), std::swap(xx, yy); std::reverse((y).begin(), (y).end()); std::reverse((yy).begin(), (yy).end()); int ptr = 0; for (auto v : x) d[0][++ptr] = v; for (auto v : xx) d[0][++ptr] = -v; ptr = 0; for (auto v : y) d[1][++ptr] = v; for (auto v : yy) d[1][++ptr] = -v; int dx = 0, dy = 0; for (int i = (1); i <= (n); ++i) { dx += d[0][i]; if (tag) printf("%d %d\n", dy, dx); else printf("%d %d\n", dx, dy); dy += d[1][i]; if (tag) printf("%d %d\n", dy, dx); else printf("%d %d\n", dx, dy); } } else puts("No"); } int main() { f[0][0] = 1; int T; rd(T); while (T--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1009; bitset<1000009> b[N]; int u[N], v[N], n, m, p[N], q[N], d[N]; bool dp(int* a, int s, int* c) { int i, t = 0, o = 0; b[0].reset(), b[0][0] = 1; for (i = 1; i <= n; ++i) b[i] = b[i - 1] | (b[i - 1] << a[i]); if (!b[n][s]) return 1; for (i = n; i; --i) if (s >= a[i] && b[i - 1][s - a[i]]) c[++o] = a[i], s -= a[i]; else d[++t] = a[i]; for (i = 1; i <= t; ++i) c[o + i] = -d[i]; return 0; } int main() { int T, i, x, y; scanf("%d", &T); while (T--) { scanf("%d", &n), x = y = 0; for (i = 1; i <= n; ++i) scanf("%d", u + i), x += u[i]; scanf("%d", &m); for (i = 1; i <= m; ++i) scanf("%d", v + i), y += v[i]; if (n != m || (x & 1) || (y & 1)) { puts("No"); continue; } x /= 2, y /= 2, sort(u + 1, u + n + 1), sort(v + 1, v + m + 1); if (dp(u, x, p) || dp(v, y, q)) { puts("No"); continue; } puts("Yes"), reverse(q + 1, q + m + 1), x = y = 0; for (i = 1; i <= n * 2; ++i) { if (i & 1) x += p[i / 2 + 1]; else y += q[i / 2]; 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; const int N = 1009; bitset<1000009> b[N]; int u[N], v[N], n, m, p[N], q[N], d[N]; bool dp(int* a, int s, int* c) { int i, t = 0, o = 0; b[0].reset(), b[0][0] = 1; for (i = 1; i <= n; ++i) b[i] = b[i - 1] | (b[i - 1] << a[i]); if (!b[n][s]) return 1; for (i = n; i; --i) if (s >= a[i] && b[i - 1][s - a[i]]) c[++o] = a[i], s -= a[i]; else d[++t] = a[i]; for (i = 1; i <= t; ++i) c[o + i] = -d[i]; return 0; } int main() { int T, i, x, y; scanf("%d", &T); while (T--) { scanf("%d", &n), x = y = 0; for (i = 1; i <= n; ++i) scanf("%d", u + i), x += u[i]; scanf("%d", &m); for (i = 1; i <= m; ++i) scanf("%d", v + i), y += v[i]; if (n != m || (x & 1) || (y & 1)) { puts("No"); continue; } x /= 2, y /= 2, sort(u + 1, u + n + 1), sort(v + 1, v + m + 1); if (dp(u, x, p) || dp(v, y, q)) { puts("No"); continue; } puts("Yes"), reverse(q + 1, q + m + 1), x = y = 0; for (i = 1; i <= n * 2; ++i) { if (i & 1) x += p[i / 2 + 1]; else y += q[i / 2]; printf("%d %d\n", x, y); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> inline T read(register T& t) { register T f = 1; register char ch = getchar(); t = 0; while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar(); t *= f; return t; } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } const long long p = 998244353; inline long long power(register long long x, register long long k = p - 2) { register long long re = 1; for (; k; k >>= 1, x = x * x % p) if (k & 1) re = re * x % p; return re; } int H, V; int h[1005], v[1005]; bitset<1000005> f[1005]; void solve() { int sh = 0, sv = 0; read(H); for (int i = 1; i <= H; i++) read(h[i]), sh += h[i]; read(V); for (int i = 1; i <= V; i++) read(v[i]), sv += v[i]; if (H != V || (sh & 1) || (sv & 1)) return printf("No\n"), void(); vector<int> L, R, U, D; for (int i = 1; i <= H; i++) f[i].reset(); f[0][0] = 1; for (int i = 1; i <= H; i++) f[i] = f[i - 1] | (f[i - 1] << h[i]); int t = sh / 2; if (f[H][t] == 0) return printf("No\n"), void(); for (int i = H; i >= 1; i--) if (f[i - 1][t] == 0) L.push_back(h[i]), t -= h[i]; else R.push_back(h[i]); if (L.size() < R.size()) swap(L, R); for (int i = 1; i <= V; i++) f[i].reset(); f[0][0] = 1; for (int i = 1; i <= V; i++) f[i] = f[i - 1] | (f[i - 1] << v[i]); t = sv / 2; if (f[V][t] == 0) return printf("No\n"), void(); for (int i = V; i >= 1; i--) if (f[i - 1][t] == 0) U.push_back(v[i]), t -= v[i]; else D.push_back(v[i]); if (U.size() < D.size()) swap(U, D); vector<pair<int, int> > A, B, C; for (int i : R) { A.push_back({i, *--U.end()}); U.pop_back(); } for (int i : D) { C.push_back({-*--L.end(), -i}); L.pop_back(); } for (int i = 0; i < L.size(); i++) B.push_back({-L[i], U[i]}); sort(A.begin(), A.end(), [](pair<int, int> x, pair<int, int> y) { return x.second * y.first < x.first * y.second; }); sort(C.begin(), C.end(), [](pair<int, int> x, pair<int, int> y) { return x.second * y.first < x.first * y.second; }); int x = 0, y = 0; vector<pair<int, int> > res; for (auto i : A) res.push_back(i); for (auto i : B) res.push_back(i); for (auto i : C) res.push_back(i); printf("Yes\n"); for (auto i : res) { x += i.first; printf("%d %d\n", x, y); y += i.second; printf("%d %d\n", x, y); } assert(x == 0); assert(y == 0); } int main() { int T; read(T); while (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; template <typename T> inline T read(register T& t) { register T f = 1; register char ch = getchar(); t = 0; while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') t = t * 10 + ch - '0', ch = getchar(); t *= f; return t; } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } const long long p = 998244353; inline long long power(register long long x, register long long k = p - 2) { register long long re = 1; for (; k; k >>= 1, x = x * x % p) if (k & 1) re = re * x % p; return re; } int H, V; int h[1005], v[1005]; bitset<1000005> f[1005]; void solve() { int sh = 0, sv = 0; read(H); for (int i = 1; i <= H; i++) read(h[i]), sh += h[i]; read(V); for (int i = 1; i <= V; i++) read(v[i]), sv += v[i]; if (H != V || (sh & 1) || (sv & 1)) return printf("No\n"), void(); vector<int> L, R, U, D; for (int i = 1; i <= H; i++) f[i].reset(); f[0][0] = 1; for (int i = 1; i <= H; i++) f[i] = f[i - 1] | (f[i - 1] << h[i]); int t = sh / 2; if (f[H][t] == 0) return printf("No\n"), void(); for (int i = H; i >= 1; i--) if (f[i - 1][t] == 0) L.push_back(h[i]), t -= h[i]; else R.push_back(h[i]); if (L.size() < R.size()) swap(L, R); for (int i = 1; i <= V; i++) f[i].reset(); f[0][0] = 1; for (int i = 1; i <= V; i++) f[i] = f[i - 1] | (f[i - 1] << v[i]); t = sv / 2; if (f[V][t] == 0) return printf("No\n"), void(); for (int i = V; i >= 1; i--) if (f[i - 1][t] == 0) U.push_back(v[i]), t -= v[i]; else D.push_back(v[i]); if (U.size() < D.size()) swap(U, D); vector<pair<int, int> > A, B, C; for (int i : R) { A.push_back({i, *--U.end()}); U.pop_back(); } for (int i : D) { C.push_back({-*--L.end(), -i}); L.pop_back(); } for (int i = 0; i < L.size(); i++) B.push_back({-L[i], U[i]}); sort(A.begin(), A.end(), [](pair<int, int> x, pair<int, int> y) { return x.second * y.first < x.first * y.second; }); sort(C.begin(), C.end(), [](pair<int, int> x, pair<int, int> y) { return x.second * y.first < x.first * y.second; }); int x = 0, y = 0; vector<pair<int, int> > res; for (auto i : A) res.push_back(i); for (auto i : B) res.push_back(i); for (auto i : C) res.push_back(i); printf("Yes\n"); for (auto i : res) { x += i.first; printf("%d %d\n", x, y); y += i.second; printf("%d %d\n", x, y); } assert(x == 0); assert(y == 0); } int main() { int T; read(T); while (T--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int A = 1000 * 1000 / 4 + 1; int partition(vector<int> &a) { int n = a.size(); int sum = accumulate(a.begin(), a.end(), 0); if (sum % 2 == 1) { return -1; } sum /= 2; vector<bitset<A>> possible(n + 1); possible[0][0] = true; for (int i = 0; i < n; ++i) { possible[i + 1] = possible[i] | possible[i] << a[i]; } if (!possible[n][sum]) { return -1; } int divide = n; for (int i = n - 1, j = sum; i >= 0; --i) { if (j >= a[i] && possible[i][j - a[i]]) { j -= a[i]; } else { swap(a[i], a[--divide]); } } return divide; } void solve() { int n; cin >> n; vector<int> h(n); for (auto &i : h) { cin >> i; } cin >> n; vector<int> v(n); for (auto &i : v) { cin >> i; } if (h.size() != v.size()) { cout << "No\n"; return; } int dh = partition(h); int dv = partition(v); if (min(dh, dv) < 0) { cout << "No\n"; return; } cout << "Yes\n"; bool flip = dh < dv; if (flip) { swap(dh, dv); swap(h, v); } sort(h.begin() + dh, h.end()); sort(h.begin(), h.begin() + dh); sort(v.begin() + dv, v.end(), greater<int>()); sort(v.begin(), v.begin() + dv, greater<int>()); vector<array<int, 2>> points; int x = 0; int y = 0; for (int i = 0; i < n; ++i) { y += i < dv ? v[i] : -v[i]; points.push_back({x, y}); x += i < dh ? h[i] : -h[i]; points.push_back({x, y}); } for (auto [a, b] : points) { if (flip) { cout << b << " " << a << "\n"; } else { cout << a << " " << b << "\n"; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } }
### 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 A = 1000 * 1000 / 4 + 1; int partition(vector<int> &a) { int n = a.size(); int sum = accumulate(a.begin(), a.end(), 0); if (sum % 2 == 1) { return -1; } sum /= 2; vector<bitset<A>> possible(n + 1); possible[0][0] = true; for (int i = 0; i < n; ++i) { possible[i + 1] = possible[i] | possible[i] << a[i]; } if (!possible[n][sum]) { return -1; } int divide = n; for (int i = n - 1, j = sum; i >= 0; --i) { if (j >= a[i] && possible[i][j - a[i]]) { j -= a[i]; } else { swap(a[i], a[--divide]); } } return divide; } void solve() { int n; cin >> n; vector<int> h(n); for (auto &i : h) { cin >> i; } cin >> n; vector<int> v(n); for (auto &i : v) { cin >> i; } if (h.size() != v.size()) { cout << "No\n"; return; } int dh = partition(h); int dv = partition(v); if (min(dh, dv) < 0) { cout << "No\n"; return; } cout << "Yes\n"; bool flip = dh < dv; if (flip) { swap(dh, dv); swap(h, v); } sort(h.begin() + dh, h.end()); sort(h.begin(), h.begin() + dh); sort(v.begin() + dv, v.end(), greater<int>()); sort(v.begin(), v.begin() + dv, greater<int>()); vector<array<int, 2>> points; int x = 0; int y = 0; for (int i = 0; i < n; ++i) { y += i < dv ? v[i] : -v[i]; points.push_back({x, y}); x += i < dh ? h[i] : -h[i]; points.push_back({x, y}); } for (auto [a, b] : points) { if (flip) { cout << b << " " << a << "\n"; } else { cout << a << " " << b << "\n"; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; template <class t> inline t read(t &x) { char c = getchar(); bool f = 0; x = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); if (f) x = -x; return x; } template <class t, class... A> inline void read(t &x, A &...a) { read(x); read(a...); } template <class t> inline void write(t x) { if (x < 0) putchar('-'), write(-x); else { if (x > 9) write(x / 10); putchar('0' + x % 10); } } const int N = 1005, M = 1e6 + 5; int n, m, a[N], b[N]; bitset<M> f[N]; bool divide(int *a, vector<int> &a0, vector<int> &a1) { f[0][0] = 1; int s = 0; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | f[i - 1] << a[i], s += a[i]; if (s & 1 || !f[n][s >> 1]) return 0; for (int i = n, x = s >> 1; i; i--) { if (f[i - 1][x]) a0.push_back(a[i]); else x -= a[i], a1.push_back(a[i]); } return 1; } void doit() { vector<int> a0, a1, b0, b1, A, B; 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 (n ^ m) { puts("No"); return; } if (!divide(a, a0, a1) || !divide(b, b0, b1)) { puts("No"); return; } if (a0.size() > a1.size()) swap(a0, a1); if (b0.size() < b1.size()) swap(b0, b1); sort((a0).begin(), (a0).end(), greater<int>()); sort((a1).begin(), (a1).end(), greater<int>()); sort((b0).begin(), (b0).end()); sort((b1).begin(), (b1).end()); for (int x : a0) A.push_back(x); for (int x : a1) A.push_back(-x); for (int x : b0) B.push_back(x); for (int x : b1) B.push_back(-x); puts("Yes"); for (int i = 0, x = 0, y = 0; i < n; i++) { x += A[i]; printf("%d %d\n", x, y); y += B[i]; printf("%d %d\n", x, y); } } signed main() { int t; read(t); while (t--) doit(); }
### 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 <class t> inline t read(t &x) { char c = getchar(); bool f = 0; x = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); if (f) x = -x; return x; } template <class t, class... A> inline void read(t &x, A &...a) { read(x); read(a...); } template <class t> inline void write(t x) { if (x < 0) putchar('-'), write(-x); else { if (x > 9) write(x / 10); putchar('0' + x % 10); } } const int N = 1005, M = 1e6 + 5; int n, m, a[N], b[N]; bitset<M> f[N]; bool divide(int *a, vector<int> &a0, vector<int> &a1) { f[0][0] = 1; int s = 0; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | f[i - 1] << a[i], s += a[i]; if (s & 1 || !f[n][s >> 1]) return 0; for (int i = n, x = s >> 1; i; i--) { if (f[i - 1][x]) a0.push_back(a[i]); else x -= a[i], a1.push_back(a[i]); } return 1; } void doit() { vector<int> a0, a1, b0, b1, A, B; 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 (n ^ m) { puts("No"); return; } if (!divide(a, a0, a1) || !divide(b, b0, b1)) { puts("No"); return; } if (a0.size() > a1.size()) swap(a0, a1); if (b0.size() < b1.size()) swap(b0, b1); sort((a0).begin(), (a0).end(), greater<int>()); sort((a1).begin(), (a1).end(), greater<int>()); sort((b0).begin(), (b0).end()); sort((b1).begin(), (b1).end()); for (int x : a0) A.push_back(x); for (int x : a1) A.push_back(-x); for (int x : b0) B.push_back(x); for (int x : b1) B.push_back(-x); puts("Yes"); for (int i = 0, x = 0, y = 0; i < n; i++) { x += A[i]; printf("%d %d\n", x, y); y += B[i]; printf("%d %d\n", x, y); } } signed main() { int t; read(t); while (t--) doit(); } ```
#include <bits/stdc++.h> const int N = 1003; bool pack(int n, int m, int *w, bool *res, bool rev) { static int *dp[N]; for (int i = 0; i <= n; i++) dp[i] = new int[m + 1]; for (int i = 1; i <= m; i++) dp[0][i] = 0; dp[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= m; j++) { if (dp[i - 1][j]) dp[i][j] = 1; else if (j >= w[i] && dp[i - 1][j - w[i]]) dp[i][j] = 2; else dp[i][j] = 0; } if (!dp[n][m]) return false; for (int i = n, j = m; i >= 1; i--) { res[i] = (dp[i][j] == 2); if (dp[i][j] == 2) j -= w[i]; } int tn = 0; for (int i = 1; i <= n; i++) if (res[i]) tn++; if ((!rev && tn > (n - tn)) || (rev && tn < (n - tn))) for (int i = 1; i <= n; i++) res[i] = !res[i]; for (int i = 0; i <= n; i++) delete[] dp[i]; return true; } bool cmp(int x, int y) { return x > y; } int main() { int _t; scanf("%d", &_t); while (_t--) { static int h[N], v[N], as[N], bs[N]; static bool hk[N], vk[N]; int n, m; scanf("%d", &n); int sh = 0, sv = 0; for (int i = 1; i <= n; i++) scanf("%d", &h[i]), sh += h[i]; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &v[i]), sv += v[i]; if (m != n) { printf("No\n"); continue; } if ((sh & 1) || (sv & 1)) { printf("No\n"); continue; } sh >>= 1; sv >>= 1; if (pack(n, sh, h, hk, false) && pack(m, sv, v, vk, true)) printf("Yes\n"); else { printf("No\n"); continue; } int p = 0; for (int i = 1; i <= n; i++) if (hk[i]) as[++p] = h[i]; int asn = p; for (int i = 1; i <= n; i++) if (!hk[i]) as[++p] = h[i]; p = 0; for (int i = 1; i <= m; i++) if (vk[i]) bs[++p] = v[i]; int bsn = p; for (int i = 1; i <= m; i++) if (!vk[i]) bs[++p] = v[i]; int sk = std::min(asn, bsn), tk = std::max(asn, bsn) + 1; std::sort(as + 1, as + 1 + asn, cmp); std::sort(bs + 1, bs + 1 + bsn); std::sort(as + 1 + asn, as + 1 + n, cmp); std::sort(bs + 1 + bsn, bs + 1 + m); int cx = 0, cy = 0; for (int i = 1; i <= sk; i++) { cx += as[i]; printf("%d %d\n", cx, cy); cy += bs[i]; printf("%d %d\n", cx, cy); } for (int i = sk + 1; i < tk; i++) { cx -= as[i]; printf("%d %d\n", cx, cy); cy += bs[i]; printf("%d %d\n", cx, cy); } for (int i = tk; i <= n; i++) { cx -= as[i]; printf("%d %d\n", cx, cy); cy -= bs[i]; printf("%d %d\n", cx, cy); } } 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> const int N = 1003; bool pack(int n, int m, int *w, bool *res, bool rev) { static int *dp[N]; for (int i = 0; i <= n; i++) dp[i] = new int[m + 1]; for (int i = 1; i <= m; i++) dp[0][i] = 0; dp[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= m; j++) { if (dp[i - 1][j]) dp[i][j] = 1; else if (j >= w[i] && dp[i - 1][j - w[i]]) dp[i][j] = 2; else dp[i][j] = 0; } if (!dp[n][m]) return false; for (int i = n, j = m; i >= 1; i--) { res[i] = (dp[i][j] == 2); if (dp[i][j] == 2) j -= w[i]; } int tn = 0; for (int i = 1; i <= n; i++) if (res[i]) tn++; if ((!rev && tn > (n - tn)) || (rev && tn < (n - tn))) for (int i = 1; i <= n; i++) res[i] = !res[i]; for (int i = 0; i <= n; i++) delete[] dp[i]; return true; } bool cmp(int x, int y) { return x > y; } int main() { int _t; scanf("%d", &_t); while (_t--) { static int h[N], v[N], as[N], bs[N]; static bool hk[N], vk[N]; int n, m; scanf("%d", &n); int sh = 0, sv = 0; for (int i = 1; i <= n; i++) scanf("%d", &h[i]), sh += h[i]; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &v[i]), sv += v[i]; if (m != n) { printf("No\n"); continue; } if ((sh & 1) || (sv & 1)) { printf("No\n"); continue; } sh >>= 1; sv >>= 1; if (pack(n, sh, h, hk, false) && pack(m, sv, v, vk, true)) printf("Yes\n"); else { printf("No\n"); continue; } int p = 0; for (int i = 1; i <= n; i++) if (hk[i]) as[++p] = h[i]; int asn = p; for (int i = 1; i <= n; i++) if (!hk[i]) as[++p] = h[i]; p = 0; for (int i = 1; i <= m; i++) if (vk[i]) bs[++p] = v[i]; int bsn = p; for (int i = 1; i <= m; i++) if (!vk[i]) bs[++p] = v[i]; int sk = std::min(asn, bsn), tk = std::max(asn, bsn) + 1; std::sort(as + 1, as + 1 + asn, cmp); std::sort(bs + 1, bs + 1 + bsn); std::sort(as + 1 + asn, as + 1 + n, cmp); std::sort(bs + 1 + bsn, bs + 1 + m); int cx = 0, cy = 0; for (int i = 1; i <= sk; i++) { cx += as[i]; printf("%d %d\n", cx, cy); cy += bs[i]; printf("%d %d\n", cx, cy); } for (int i = sk + 1; i < tk; i++) { cx -= as[i]; printf("%d %d\n", cx, cy); cy += bs[i]; printf("%d %d\n", cx, cy); } for (int i = tk; i <= n; i++) { cx -= as[i]; printf("%d %d\n", cx, cy); cy -= bs[i]; printf("%d %d\n", cx, cy); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; bool ruk(vector<long long> lst, vector<long long> &used, long long x) { long long n = lst.size(); vector<bitset<1000001>> dp(n + 1); dp[0][0] = 1; for (long long i = 1; i <= n; i++) { dp[i] = dp[i - 1] | (dp[i - 1] << lst[i - 1]); } if (!dp[n][x]) { return false; } for (long long i = n; i >= 1; i--) { if (x >= lst[i - 1] && dp[i - 1][x - lst[i - 1]]) { x -= lst[i - 1]; used[i - 1] = 1; } } return true; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; for (long long q = 0; q < t; q++) { long long hor, wer; cin >> hor; vector<long long> verh, prav; long long a = 0, b = 0; for (long long i = 0; i < hor; i++) { long long x; cin >> x; a += x; prav.push_back(x); } cin >> wer; for (long long i = 0; i < wer; i++) { long long x; cin >> x; b += x; verh.push_back(x); } if (hor != wer) { cout << "No\n"; continue; } vector<long long> used1(hor, 0), used2(wer, 0); if (b % 2 != 0 || !ruk(verh, used1, b / 2)) { cout << "No\n"; continue; } if (a % 2 != 0 || !ruk(prav, used2, a / 2)) { cout << "No\n"; continue; } vector<long long> l, r, u, d; for (long long i = 0; i < used1.size(); i++) { if (used1[i]) { u.push_back(verh[i]); } else { d.push_back(verh[i]); } } for (long long i = 0; i < used2.size(); i++) { if (used2[i]) { l.push_back(prav[i]); } else { r.push_back(prav[i]); } } if (r.size() > l.size()) swap(l, r); if (d.size() > u.size()) swap(d, u); sort(r.begin(), r.end()); reverse(r.begin(), r.end()); sort(u.begin(), u.end()); sort(d.begin(), d.end()); sort(l.begin(), l.end()); reverse(l.begin(), l.end()); long long x = 0, y = 0; cout << "Yes\n"; for (long long i = 0; i < r.size(); i++) { x += r[i]; cout << x << " " << y << '\n'; y += u[i]; cout << x << " " << y << '\n'; } for (long long i = r.size(); i < u.size(); i++) { x -= l[i - r.size()]; cout << x << " " << y << '\n'; y += u[i]; cout << x << " " << y << '\n'; } for (long long i = 0; i < d.size(); i++) { x -= l[u.size() - r.size() + i]; cout << x << " " << y << '\n'; y -= d[i]; cout << x << " " << y << '\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> using namespace std; bool ruk(vector<long long> lst, vector<long long> &used, long long x) { long long n = lst.size(); vector<bitset<1000001>> dp(n + 1); dp[0][0] = 1; for (long long i = 1; i <= n; i++) { dp[i] = dp[i - 1] | (dp[i - 1] << lst[i - 1]); } if (!dp[n][x]) { return false; } for (long long i = n; i >= 1; i--) { if (x >= lst[i - 1] && dp[i - 1][x - lst[i - 1]]) { x -= lst[i - 1]; used[i - 1] = 1; } } return true; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; for (long long q = 0; q < t; q++) { long long hor, wer; cin >> hor; vector<long long> verh, prav; long long a = 0, b = 0; for (long long i = 0; i < hor; i++) { long long x; cin >> x; a += x; prav.push_back(x); } cin >> wer; for (long long i = 0; i < wer; i++) { long long x; cin >> x; b += x; verh.push_back(x); } if (hor != wer) { cout << "No\n"; continue; } vector<long long> used1(hor, 0), used2(wer, 0); if (b % 2 != 0 || !ruk(verh, used1, b / 2)) { cout << "No\n"; continue; } if (a % 2 != 0 || !ruk(prav, used2, a / 2)) { cout << "No\n"; continue; } vector<long long> l, r, u, d; for (long long i = 0; i < used1.size(); i++) { if (used1[i]) { u.push_back(verh[i]); } else { d.push_back(verh[i]); } } for (long long i = 0; i < used2.size(); i++) { if (used2[i]) { l.push_back(prav[i]); } else { r.push_back(prav[i]); } } if (r.size() > l.size()) swap(l, r); if (d.size() > u.size()) swap(d, u); sort(r.begin(), r.end()); reverse(r.begin(), r.end()); sort(u.begin(), u.end()); sort(d.begin(), d.end()); sort(l.begin(), l.end()); reverse(l.begin(), l.end()); long long x = 0, y = 0; cout << "Yes\n"; for (long long i = 0; i < r.size(); i++) { x += r[i]; cout << x << " " << y << '\n'; y += u[i]; cout << x << " " << y << '\n'; } for (long long i = r.size(); i < u.size(); i++) { x -= l[i - r.size()]; cout << x << " " << y << '\n'; y += u[i]; cout << x << " " << y << '\n'; } for (long long i = 0; i < d.size(); i++) { x -= l[u.size() - r.size() + i]; cout << x << " " << y << '\n'; y -= d[i]; cout << x << " " << y << '\n'; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int T, n, x, l[1100], p[1100], fr[1100], id[1100], fl, fp; int l0[1100], p0[1100], cl, cp, al, ap, a, b, sl, sp, u; bitset<1000001> dp[1100]; int main() { scanf("%d", &T); while (T--) { sl = sp = cl = cp = fl = fp = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &l[i]), sl += l[i]; scanf("%d", &x); for (int i = 1; i <= x; i++) scanf("%d", &p[i]), sp += p[i]; if (n != x || sl % 2 == 1 || sp % 2 == 1) { printf("No\n"); continue; } sort(l + 1, l + n + 1); sort(p + 1, p + n + 1); reverse(l + 1, l + n + 1); reverse(p + 1, p + n + 1); dp[0].set(0, 1); for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << l[i]); if (!dp[n][sl / 2]) { printf("No\n"); continue; } u = sl / 2; for (int i = n - 1; i >= 0; i--) { if (!dp[i][u]) l0[++cl] = l[i + 1], u = u - l[i + 1]; if (u == 0) break; } al = cl; u = sl / 2; for (int i = n - 1; i >= 0; i--) { if (u && !dp[i][u]) u = u - l[i + 1]; else l0[++cl] = -l[i + 1]; } reverse(l0 + 1, l0 + al + 1); dp[0].set(0, 1); for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << p[i]); if (!dp[n][sp / 2]) { printf("No\n"); continue; } printf("Yes\n"); u = sp / 2; for (int i = n - 1; i >= 0; i--) { if (!dp[i][u]) p0[++cp] = p[i + 1], u = u - p[i + 1]; if (u == 0) break; } ap = cp; u = sp / 2; for (int i = n - 1; i >= 0; i--) { if (u && !dp[i][u]) u = u - p[i + 1]; else p0[++cp] = -p[i + 1]; } if (al > ap) { reverse(p0 + 1, p0 + ap + 1); reverse(p0 + ap + 1, p0 + n + 1); reverse(l0 + 1, l0 + n + 1); reverse(p0 + 1, p0 + n + 1); for (int i = 1; i <= n; i++) l0[i] = -l0[i], p0[i] = -p0[i]; al = n - al, ap = n - ap; } for (int i = 1; i <= n; i++) { if (l0[i] < 0 && !fl) fl = 1, reverse(p0 + i, p0 + ap + 1); if (p0[i] < 0 && !fp) fp = 1, reverse(l0 + i, l0 + n + 1); a += l0[i]; printf("%d %d\n", a, b); b += p0[i]; printf("%d %d\n", a, b); } } 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 T, n, x, l[1100], p[1100], fr[1100], id[1100], fl, fp; int l0[1100], p0[1100], cl, cp, al, ap, a, b, sl, sp, u; bitset<1000001> dp[1100]; int main() { scanf("%d", &T); while (T--) { sl = sp = cl = cp = fl = fp = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &l[i]), sl += l[i]; scanf("%d", &x); for (int i = 1; i <= x; i++) scanf("%d", &p[i]), sp += p[i]; if (n != x || sl % 2 == 1 || sp % 2 == 1) { printf("No\n"); continue; } sort(l + 1, l + n + 1); sort(p + 1, p + n + 1); reverse(l + 1, l + n + 1); reverse(p + 1, p + n + 1); dp[0].set(0, 1); for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << l[i]); if (!dp[n][sl / 2]) { printf("No\n"); continue; } u = sl / 2; for (int i = n - 1; i >= 0; i--) { if (!dp[i][u]) l0[++cl] = l[i + 1], u = u - l[i + 1]; if (u == 0) break; } al = cl; u = sl / 2; for (int i = n - 1; i >= 0; i--) { if (u && !dp[i][u]) u = u - l[i + 1]; else l0[++cl] = -l[i + 1]; } reverse(l0 + 1, l0 + al + 1); dp[0].set(0, 1); for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << p[i]); if (!dp[n][sp / 2]) { printf("No\n"); continue; } printf("Yes\n"); u = sp / 2; for (int i = n - 1; i >= 0; i--) { if (!dp[i][u]) p0[++cp] = p[i + 1], u = u - p[i + 1]; if (u == 0) break; } ap = cp; u = sp / 2; for (int i = n - 1; i >= 0; i--) { if (u && !dp[i][u]) u = u - p[i + 1]; else p0[++cp] = -p[i + 1]; } if (al > ap) { reverse(p0 + 1, p0 + ap + 1); reverse(p0 + ap + 1, p0 + n + 1); reverse(l0 + 1, l0 + n + 1); reverse(p0 + 1, p0 + n + 1); for (int i = 1; i <= n; i++) l0[i] = -l0[i], p0[i] = -p0[i]; al = n - al, ap = n - ap; } for (int i = 1; i <= n; i++) { if (l0[i] < 0 && !fl) fl = 1, reverse(p0 + i, p0 + ap + 1); if (p0[i] < 0 && !fp) fp = 1, reverse(l0 + i, l0 + n + 1); a += l0[i]; printf("%d %d\n", a, b); b += p0[i]; printf("%d %d\n", a, b); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> void setmax(T& x, T y) { x = max(x, y); } template <typename T> void setmin(T& x, T y) { x = min(x, y); } const double PI = acos(-1.0); const int INF = 1e9 + 47; const long long LINF = 1LL * INF * INF; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int a[1 << 10]; int dp[1 << 20]; int p[1 << 20]; int u[1 << 10]; vector<int> solve(int n) { int s = 0; for (int i = (0); i < (n); ++i) { cin >> a[i]; s += a[i]; u[i] = 0; } sort(a, a + n); for (int i = (0); i < (n << 10); ++i) dp[i] = 0; dp[0] = 1; p[0] = -1; for (int i = (0); i < (n); ++i) { for (int j = ((i << 10) + 1) - 1; j >= (0); --j) { if (dp[j] && !dp[j + a[i]]) { dp[j + a[i]] = 1; p[j + a[i]] = i; } } } vector<int> ans; if (s % 2 || !dp[s / 2]) return ans; s /= 2; while (s) { ans.push_back(a[p[s]]); u[p[s]] = 1; s -= a[p[s]]; } for (int i = (0); i < (n); ++i) if (!u[i]) ans.push_back(-a[i]); return ans; } vector<pair<int, int> > get(vector<int> A, vector<int> B) { vector<pair<int, int> > res; int x = 0, y = 0; for (int i = (0); i < ((int)((A).size())); ++i) { res.push_back(make_pair(x, y)); x += A[i]; res.push_back(make_pair(x, y)); y += B[i]; } assert(x == 0 && y == 0); return res; } bool has_per(vector<pair<int, int> > res) { int n = (int)((res).size()); for (int i = (0); i < (n); ++i) { if (res[(i + 1) % n].first == res[i].first) continue; int LX = min(res[i].first, res[(i + 1) % n].first); int RX = max(res[i].first, res[(i + 1) % n].first); int second = res[i].second; for (int j = (0); j < (n); ++j) { if (res[(j + 1) % n].second == res[j].second) continue; int LY = min(res[j].second, res[(j + 1) % n].second); int RY = max(res[j].second, res[(j + 1) % n].second); int first = res[j].first; if (LX < first && first < RX && LY < second && second < RY) return 1; } } for (int i = (0); i < (n); ++i) { if (res[(i + 1) % n].first == res[i].first) continue; int LX = min(res[i].first, res[(i + 1) % n].first); int RX = max(res[i].first, res[(i + 1) % n].first); int second = res[i].second; for (int j = (0); j < (n); ++j) { if (res[(j + 1) % n].second == res[j].second) continue; int LY = min(res[j].second, res[(j + 1) % n].second); int RY = max(res[j].second, res[(j + 1) % n].second); int first = res[j].first; if (LX < first && first < RX && LY <= second && second <= RY) return 1; if (LX <= first && first <= RX && LY < second && second < RY) return 1; } } return 0; } void solve() { int h, v; cin >> h; vector<int> A = solve(h); cin >> v; vector<int> B = solve(v); if (h != v || (int)((A).size()) == 0 || (int)((B).size()) == 0) { cout << "No" << endl; return; } for (int revB = (0); revB < (2); ++revB) { for (int minB = (0); minB < (2); ++minB) { for (int revA = (0); revA < (2); ++revA) { for (int minA = (0); minA < (2); ++minA) { vector<pair<int, int> > res = get(A, B); if (!has_per(res)) { cout << "Yes" << endl; for (int i = (0); i < ((int)((res).size())); ++i) cout << res[i].first << " " << res[i].second << endl; return; } for (int i = (0); i < (h); ++i) A[i] *= -1; } reverse(A.begin(), A.end()); } for (int i = (0); i < (h); ++i) B[i] *= -1; } reverse(B.begin(), B.end()); } cout << "No" << endl; assert(0); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) solve(); cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl; 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; template <typename T> void setmax(T& x, T y) { x = max(x, y); } template <typename T> void setmin(T& x, T y) { x = min(x, y); } const double PI = acos(-1.0); const int INF = 1e9 + 47; const long long LINF = 1LL * INF * INF; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int a[1 << 10]; int dp[1 << 20]; int p[1 << 20]; int u[1 << 10]; vector<int> solve(int n) { int s = 0; for (int i = (0); i < (n); ++i) { cin >> a[i]; s += a[i]; u[i] = 0; } sort(a, a + n); for (int i = (0); i < (n << 10); ++i) dp[i] = 0; dp[0] = 1; p[0] = -1; for (int i = (0); i < (n); ++i) { for (int j = ((i << 10) + 1) - 1; j >= (0); --j) { if (dp[j] && !dp[j + a[i]]) { dp[j + a[i]] = 1; p[j + a[i]] = i; } } } vector<int> ans; if (s % 2 || !dp[s / 2]) return ans; s /= 2; while (s) { ans.push_back(a[p[s]]); u[p[s]] = 1; s -= a[p[s]]; } for (int i = (0); i < (n); ++i) if (!u[i]) ans.push_back(-a[i]); return ans; } vector<pair<int, int> > get(vector<int> A, vector<int> B) { vector<pair<int, int> > res; int x = 0, y = 0; for (int i = (0); i < ((int)((A).size())); ++i) { res.push_back(make_pair(x, y)); x += A[i]; res.push_back(make_pair(x, y)); y += B[i]; } assert(x == 0 && y == 0); return res; } bool has_per(vector<pair<int, int> > res) { int n = (int)((res).size()); for (int i = (0); i < (n); ++i) { if (res[(i + 1) % n].first == res[i].first) continue; int LX = min(res[i].first, res[(i + 1) % n].first); int RX = max(res[i].first, res[(i + 1) % n].first); int second = res[i].second; for (int j = (0); j < (n); ++j) { if (res[(j + 1) % n].second == res[j].second) continue; int LY = min(res[j].second, res[(j + 1) % n].second); int RY = max(res[j].second, res[(j + 1) % n].second); int first = res[j].first; if (LX < first && first < RX && LY < second && second < RY) return 1; } } for (int i = (0); i < (n); ++i) { if (res[(i + 1) % n].first == res[i].first) continue; int LX = min(res[i].first, res[(i + 1) % n].first); int RX = max(res[i].first, res[(i + 1) % n].first); int second = res[i].second; for (int j = (0); j < (n); ++j) { if (res[(j + 1) % n].second == res[j].second) continue; int LY = min(res[j].second, res[(j + 1) % n].second); int RY = max(res[j].second, res[(j + 1) % n].second); int first = res[j].first; if (LX < first && first < RX && LY <= second && second <= RY) return 1; if (LX <= first && first <= RX && LY < second && second < RY) return 1; } } return 0; } void solve() { int h, v; cin >> h; vector<int> A = solve(h); cin >> v; vector<int> B = solve(v); if (h != v || (int)((A).size()) == 0 || (int)((B).size()) == 0) { cout << "No" << endl; return; } for (int revB = (0); revB < (2); ++revB) { for (int minB = (0); minB < (2); ++minB) { for (int revA = (0); revA < (2); ++revA) { for (int minA = (0); minA < (2); ++minA) { vector<pair<int, int> > res = get(A, B); if (!has_per(res)) { cout << "Yes" << endl; for (int i = (0); i < ((int)((res).size())); ++i) cout << res[i].first << " " << res[i].second << endl; return; } for (int i = (0); i < (h); ++i) A[i] *= -1; } reverse(A.begin(), A.end()); } for (int i = (0); i < (h); ++i) B[i] *= -1; } reverse(B.begin(), B.end()); } cout << "No" << endl; assert(0); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) solve(); cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl; return 0; } ```
#include <bits/stdc++.h> const int N = 1e3 + 10; inline int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f ? -x : x; } int a[N], b[N], A[N], B[N], x[N], y[N]; std::bitset<N * N> f[N]; int main() { f[0][0] = 1; for (int T = read(); T; --T) { int n = read(), sa = 0, ua = 0; for (int i = 1; i <= n; ++i) a[i] = read(), sa += a[i]; int m = read(), sb = 0, ub = 0; for (int i = 1; i <= m; ++i) b[i] = read(), sb += b[i]; if (n != m || sa & 1 || sb & 1) { puts("No"); continue; } std::sort(a + 1, a + n + 1), std::sort(b + 1, b + m + 1), sa >>= 1, sb >>= 1; for (int i = 1; i <= n; ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (!f[n][sa]) { puts("No"); continue; } for (int i = n; i >= 1; --i) if (sa >= a[i] && f[i - 1][sa - a[i]]) A[i] = 1, sa -= a[i], ++ua; else A[i] = 0; for (int i = 1; i <= m; ++i) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (!f[m][sb]) { puts("No"); continue; } for (int i = m; i >= 1; --i) if (sb >= b[i] && f[i - 1][sb - b[i]]) B[i] = 1, sb -= b[i], ++ub; else B[i] = 0; if (ua > ub) for (int i = 1; i <= m; ++i) B[i] ^= 1; int p1 = 0, p2 = 1; for (int i = n; i >= 1; --i) if (A[i]) x[p1 * 2 + 1] = a[i], ++p1; for (int i = n; i >= 1; --i) if (!A[i]) x[p1 * 2 + 1] = -a[i], ++p1; for (int i = 1; i <= m; ++i) if (B[i]) y[p2 * 2] = b[i], ++p2; for (int i = 1; i <= m; ++i) if (!B[i]) y[p2 * 2] = -b[i], ++p2; int _x = 0, _y = 0; puts("Yes"); for (int i = 1; i <= n + m; ++i) { _x += x[i], _y += y[i]; printf("%d %d\n", _x, _y); } } 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> const int N = 1e3 + 10; inline int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f ? -x : x; } int a[N], b[N], A[N], B[N], x[N], y[N]; std::bitset<N * N> f[N]; int main() { f[0][0] = 1; for (int T = read(); T; --T) { int n = read(), sa = 0, ua = 0; for (int i = 1; i <= n; ++i) a[i] = read(), sa += a[i]; int m = read(), sb = 0, ub = 0; for (int i = 1; i <= m; ++i) b[i] = read(), sb += b[i]; if (n != m || sa & 1 || sb & 1) { puts("No"); continue; } std::sort(a + 1, a + n + 1), std::sort(b + 1, b + m + 1), sa >>= 1, sb >>= 1; for (int i = 1; i <= n; ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (!f[n][sa]) { puts("No"); continue; } for (int i = n; i >= 1; --i) if (sa >= a[i] && f[i - 1][sa - a[i]]) A[i] = 1, sa -= a[i], ++ua; else A[i] = 0; for (int i = 1; i <= m; ++i) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (!f[m][sb]) { puts("No"); continue; } for (int i = m; i >= 1; --i) if (sb >= b[i] && f[i - 1][sb - b[i]]) B[i] = 1, sb -= b[i], ++ub; else B[i] = 0; if (ua > ub) for (int i = 1; i <= m; ++i) B[i] ^= 1; int p1 = 0, p2 = 1; for (int i = n; i >= 1; --i) if (A[i]) x[p1 * 2 + 1] = a[i], ++p1; for (int i = n; i >= 1; --i) if (!A[i]) x[p1 * 2 + 1] = -a[i], ++p1; for (int i = 1; i <= m; ++i) if (B[i]) y[p2 * 2] = b[i], ++p2; for (int i = 1; i <= m; ++i) if (!B[i]) y[p2 * 2] = -b[i], ++p2; int _x = 0, _y = 0; puts("Yes"); for (int i = 1; i <= n + m; ++i) { _x += x[i], _y += y[i]; printf("%d %d\n", _x, _y); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } constexpr int MAX_N = 1010; constexpr int MAX_M = 1000 * 1000 / 2 / 2 + 10; int N[2]; int A[2][MAX_N]; bitset<MAX_M> dp[MAX_N]; vector<pair<int, int>> ans; bool solve() { if (N[0] != N[1]) { return false; } int ls[2] = {}; vector<int> bsss[2][2]; for (int h = 0; h < 2; ++h) { for (int i = 0; i < N[h]; ++i) { ls[h] += A[h][i]; } if (ls[h] % 2 != 0) { return false; } ls[h] /= 2; for (int i = 0; i <= N[h]; ++i) { dp[i].reset(); } dp[0][0] = true; for (int i = 0; i < N[h]; ++i) { dp[i + 1] = dp[i] | dp[i] << A[h][i]; } if (!dp[N[h]][ls[h]]) { return false; } int l = ls[h]; for (int i = N[h]; i--;) { if (dp[i][l]) { bsss[h][0].push_back(A[h][i]); } else { bsss[h][1].push_back(A[h][i]); l -= A[h][i]; assert(dp[i][l]); } } assert(l == 0); } if (bsss[0][0].size() > bsss[0][1].size()) { swap(bsss[0][0], bsss[0][1]); } if (bsss[1][0].size() < bsss[1][1].size()) { swap(bsss[1][0], bsss[1][1]); } assert(bsss[0][0].size() <= bsss[1][0].size()); assert(bsss[0][1].size() >= bsss[1][1].size()); for (int s = 0; s < 2; ++s) { sort(bsss[0][s].begin(), bsss[0][s].end(), greater<int>()); sort(bsss[1][s].begin(), bsss[1][s].end()); } ans.clear(); int xs[2] = {}; for (int j = 0; j < N[0]; ++j) { for (int h = 0; h < 2; ++h) { ans.emplace_back(xs[0], xs[1]); if (j < (int)bsss[h][0].size()) { xs[h] += bsss[h][0][j]; } else { xs[h] -= bsss[h][1][j - (int)bsss[h][0].size()]; } } } assert(xs[0] == 0); assert(xs[1] == 0); return true; } int main() { int numCases; for (; ~scanf("%d", &numCases);) { for (int caseId = 0; caseId < numCases; ++caseId) { for (int h = 0; h < 2; ++h) { scanf("%d", &N[h]); for (int i = 0; i < N[h]; ++i) { scanf("%d", &A[h][i]); } } const bool res = solve(); if (res) { puts("Yes"); for (const auto &p : ans) { printf("%d %d\n", p.first, p.second); } } else { puts("No"); } } } 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; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } constexpr int MAX_N = 1010; constexpr int MAX_M = 1000 * 1000 / 2 / 2 + 10; int N[2]; int A[2][MAX_N]; bitset<MAX_M> dp[MAX_N]; vector<pair<int, int>> ans; bool solve() { if (N[0] != N[1]) { return false; } int ls[2] = {}; vector<int> bsss[2][2]; for (int h = 0; h < 2; ++h) { for (int i = 0; i < N[h]; ++i) { ls[h] += A[h][i]; } if (ls[h] % 2 != 0) { return false; } ls[h] /= 2; for (int i = 0; i <= N[h]; ++i) { dp[i].reset(); } dp[0][0] = true; for (int i = 0; i < N[h]; ++i) { dp[i + 1] = dp[i] | dp[i] << A[h][i]; } if (!dp[N[h]][ls[h]]) { return false; } int l = ls[h]; for (int i = N[h]; i--;) { if (dp[i][l]) { bsss[h][0].push_back(A[h][i]); } else { bsss[h][1].push_back(A[h][i]); l -= A[h][i]; assert(dp[i][l]); } } assert(l == 0); } if (bsss[0][0].size() > bsss[0][1].size()) { swap(bsss[0][0], bsss[0][1]); } if (bsss[1][0].size() < bsss[1][1].size()) { swap(bsss[1][0], bsss[1][1]); } assert(bsss[0][0].size() <= bsss[1][0].size()); assert(bsss[0][1].size() >= bsss[1][1].size()); for (int s = 0; s < 2; ++s) { sort(bsss[0][s].begin(), bsss[0][s].end(), greater<int>()); sort(bsss[1][s].begin(), bsss[1][s].end()); } ans.clear(); int xs[2] = {}; for (int j = 0; j < N[0]; ++j) { for (int h = 0; h < 2; ++h) { ans.emplace_back(xs[0], xs[1]); if (j < (int)bsss[h][0].size()) { xs[h] += bsss[h][0][j]; } else { xs[h] -= bsss[h][1][j - (int)bsss[h][0].size()]; } } } assert(xs[0] == 0); assert(xs[1] == 0); return true; } int main() { int numCases; for (; ~scanf("%d", &numCases);) { for (int caseId = 0; caseId < numCases; ++caseId) { for (int h = 0; h < 2; ++h) { scanf("%d", &N[h]); for (int i = 0; i < N[h]; ++i) { scanf("%d", &A[h][i]); } } const bool res = solve(); if (res) { puts("Yes"); for (const auto &p : ans) { printf("%d %d\n", p.first, p.second); } } else { puts("No"); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int T; inline int read() { int t; scanf("%d", &t); return t; } int H, L; int a[1010], b[1010]; bitset<500010> A[1010]; int stA_qu[1010], stA_hui[1010]; int totA_qu, totA_hui; int stB_qu[1010], stB_hui[1010]; int totB_qu, totB_hui; bool cmp1(int a, int b) { return a < b; } bool cmp2(int a, int b) { return a > b; } void out(int a, int b) { if (a || b) cout << a << " " << b << '\n'; } int main() { T = read(); while (T--) { int sumA = 0, sumB = 0; H = read(); for (int i = 1; i <= H; i++) a[i] = read(), sumA += a[i]; L = read(); for (int i = 1; i <= L; i++) b[i] = read(), sumB += b[i]; if (sumA % 2 || sumB % 2 || L != H) { cout << "NO\n"; continue; } sumA >>= 1, sumB >>= 1; A[0].reset(); A[0][0] = 1; for (int i = 1; i <= H; i++) A[i] = A[i - 1] | (A[i - 1] << a[i]); if (!A[H][sumA]) { cout << "NO\n"; continue; } totA_qu = totA_hui = 0; for (int i = H; i >= 1; i--) { if (sumA >= a[i] && A[i - 1][sumA - a[i]]) stA_qu[++totA_qu] = a[i], sumA -= a[i]; else stA_hui[++totA_hui] = a[i]; } A[0].reset(); A[0][0] = 1; for (int i = 1; i <= L; i++) A[i] = A[i - 1] | (A[i - 1] << b[i]); if (!A[L][sumB]) { cout << "NO\n"; continue; } totB_qu = totB_hui = 0; for (int i = L; i >= 1; i--) { if (sumB >= b[i] && A[i - 1][sumB - b[i]]) stB_qu[++totB_qu] = b[i], sumB -= b[i]; else stB_hui[++totB_hui] = b[i]; } bool ty = 0; if (totA_qu > totA_hui) std::swap(stA_qu, stA_hui), std::swap(totA_qu, totA_hui); if (totB_qu > totB_hui) std::swap(stB_qu, stB_hui), std::swap(totB_qu, totB_hui); sort(stA_qu + 1, stA_qu + 1 + totA_qu, ty ? cmp1 : cmp2); sort(stA_hui + 1, stA_hui + 1 + totA_hui, ty ? cmp1 : cmp2); sort(stB_qu + 1, stB_qu + 1 + totB_qu, ty ? cmp2 : cmp1); sort(stB_hui + 1, stB_hui + 1 + totB_hui, ty ? cmp2 : cmp1); int nowx = 0, nowy = 0; int Anum = 1, Bnum = 1; cout << "YES\n"; cout << 0 << " " << 0 << '\n'; while (Anum <= totA_qu + totA_hui && Bnum <= totB_qu + totB_hui) { if (!ty) { if (Anum <= totA_qu) nowx += stA_qu[Anum]; else nowx -= stA_hui[Anum - totA_qu]; Anum++; } else { if (Bnum <= totB_hui) nowy -= stB_hui[Bnum]; else nowy += stB_qu[Bnum - totB_hui]; Bnum++; } ty ^= 1; out(nowx, nowy); } } 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; inline int read() { int t; scanf("%d", &t); return t; } int H, L; int a[1010], b[1010]; bitset<500010> A[1010]; int stA_qu[1010], stA_hui[1010]; int totA_qu, totA_hui; int stB_qu[1010], stB_hui[1010]; int totB_qu, totB_hui; bool cmp1(int a, int b) { return a < b; } bool cmp2(int a, int b) { return a > b; } void out(int a, int b) { if (a || b) cout << a << " " << b << '\n'; } int main() { T = read(); while (T--) { int sumA = 0, sumB = 0; H = read(); for (int i = 1; i <= H; i++) a[i] = read(), sumA += a[i]; L = read(); for (int i = 1; i <= L; i++) b[i] = read(), sumB += b[i]; if (sumA % 2 || sumB % 2 || L != H) { cout << "NO\n"; continue; } sumA >>= 1, sumB >>= 1; A[0].reset(); A[0][0] = 1; for (int i = 1; i <= H; i++) A[i] = A[i - 1] | (A[i - 1] << a[i]); if (!A[H][sumA]) { cout << "NO\n"; continue; } totA_qu = totA_hui = 0; for (int i = H; i >= 1; i--) { if (sumA >= a[i] && A[i - 1][sumA - a[i]]) stA_qu[++totA_qu] = a[i], sumA -= a[i]; else stA_hui[++totA_hui] = a[i]; } A[0].reset(); A[0][0] = 1; for (int i = 1; i <= L; i++) A[i] = A[i - 1] | (A[i - 1] << b[i]); if (!A[L][sumB]) { cout << "NO\n"; continue; } totB_qu = totB_hui = 0; for (int i = L; i >= 1; i--) { if (sumB >= b[i] && A[i - 1][sumB - b[i]]) stB_qu[++totB_qu] = b[i], sumB -= b[i]; else stB_hui[++totB_hui] = b[i]; } bool ty = 0; if (totA_qu > totA_hui) std::swap(stA_qu, stA_hui), std::swap(totA_qu, totA_hui); if (totB_qu > totB_hui) std::swap(stB_qu, stB_hui), std::swap(totB_qu, totB_hui); sort(stA_qu + 1, stA_qu + 1 + totA_qu, ty ? cmp1 : cmp2); sort(stA_hui + 1, stA_hui + 1 + totA_hui, ty ? cmp1 : cmp2); sort(stB_qu + 1, stB_qu + 1 + totB_qu, ty ? cmp2 : cmp1); sort(stB_hui + 1, stB_hui + 1 + totB_hui, ty ? cmp2 : cmp1); int nowx = 0, nowy = 0; int Anum = 1, Bnum = 1; cout << "YES\n"; cout << 0 << " " << 0 << '\n'; while (Anum <= totA_qu + totA_hui && Bnum <= totB_qu + totB_hui) { if (!ty) { if (Anum <= totA_qu) nowx += stA_qu[Anum]; else nowx -= stA_hui[Anum - totA_qu]; Anum++; } else { if (Bnum <= totB_hui) nowy -= stB_hui[Bnum]; else nowy += stB_qu[Bnum - totB_hui]; Bnum++; } ty ^= 1; out(nowx, nowy); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int buffer_size = 1e5 + 5; char buf[buffer_size], *S, *T; bool flag_EOF; inline char read_char() { if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin); return S != T ? *(S++) : EOF; } inline int read_int() { bool flag = false; char c = read_char(); while (c < '0' || c > '9') { if (c == EOF) { flag_EOF = true; return 0; } flag = (c == '-' ? true : flag); c = read_char(); } int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = read_char(); } return flag ? -x : x; } const int max_cnt = 1e3 + 5; int l[max_cnt], p[max_cnt]; bool mark[max_cnt]; vector<int> a[2], b[2]; const int max_size = 5e5 + 5; bitset<max_size> f[2]; bool dp[max_size]; int val[max_size]; inline int cmp1(int x, int y) { return abs(x) < abs(y); } inline int cmp2(int x, int y) { return abs(x) > abs(y); } int main() { int t; scanf("%d", &t); while (t--) { int h, v; scanf("%d", &h); for (int i = 1; i <= h; ++i) scanf("%d", l + i); scanf("%d", &v); for (int i = 1; i <= v; ++i) scanf("%d", p + i); if (h != v) { printf("No\n"); continue; } int sum1 = 0, sum2 = 0; for (int i = 1; i <= h; ++i) sum1 += l[i]; for (int i = 1; i <= v; ++i) sum2 += p[i]; if ((sum1 & 1) || (sum2 & 1)) { printf("No\n"); continue; } random_shuffle(l + 1, l + h + 1); for (int i = 0; i <= sum1; ++i) f[0][i] = 0; f[0][0] = 1; int pos1 = -1; for (int i = 1; i <= h; ++i) { int x = (i & 1) ^ 1, y = i & 1; f[y] = f[x] | (f[x] << l[i]); if (f[y][sum1 >> 1]) { pos1 = i; break; } } if (pos1 == -1) { printf("No\n"); continue; } random_shuffle(p + 1, p + v + 1); for (int i = 0; i <= sum2; ++i) f[0][i] = 0; f[0][0] = 1; int pos2 = -1; for (int i = 1; i <= v; ++i) { int x = (i & 1) ^ 1, y = i & 1; f[y] = f[x] | (f[x] << p[i]); if (f[y][sum2 >> 1]) { pos2 = i; break; } } if (pos2 == -1) { printf("No\n"); continue; } a[0].clear(), a[1].clear(); int tot1 = sum1 >> 1; for (int i = 1; i <= h; ++i) mark[i] = false; for (int i = pos1 + 1; i <= h; ++i) { a[0].push_back(l[i]); mark[i] = true; tot1 -= l[i]; } for (int i = 0; i <= tot1; ++i) dp[i] = false; dp[0] = true; for (int i = 1; i <= pos1; ++i) for (int j = tot1 - l[i]; j >= 0; --j) if (dp[j] && !dp[j + l[i]]) { dp[j + l[i]] = true; val[j + l[i]] = i; } int now = tot1; while (now) { a[0].push_back(l[val[now]]); mark[val[now]] = true; now -= l[val[now]]; } int cnt1 = 0; for (int i = 1; i <= h; ++i) if (!mark[i]) { a[1].push_back(l[i]); ++cnt1; } b[0].clear(), b[1].clear(); int tot2 = sum2 >> 1; for (int i = 1; i <= v; ++i) mark[i] = false; for (int i = pos2 + 1; i <= v; ++i) { b[0].push_back(p[i]); mark[i] = true; tot2 -= p[i]; } for (int i = 0; i <= tot2; ++i) dp[i] = false; dp[0] = true; for (int i = 1; i <= pos2; ++i) for (int j = tot2 - p[i]; j >= 0; --j) if (dp[j] && !dp[j + p[i]]) { dp[j + p[i]] = true; val[j + p[i]] = i; } now = tot2; while (now) { b[0].push_back(p[val[now]]); mark[val[now]] = true; now -= p[val[now]]; } int cnt2 = 0; for (int i = 1; i <= v; ++i) if (!mark[i]) { b[1].push_back(p[i]); ++cnt2; } printf("Yes\n"); int x = 0, y = 0; if (a[0].size() < a[1].size()) swap(a[0], a[1]); if (b[0].size() > b[1].size()) swap(b[0], b[1]); sort(a[1].begin(), a[1].end()); sort(b[1].begin(), b[1].end(), greater<int>()); while (a[1].size()) { x += a[1].back(); printf("%d %d\n", x, y); y += b[1].back(); printf("%d %d\n", x, y); a[1].pop_back(); b[1].pop_back(); } while (b[1].size()) { x -= a[0].back(); printf("%d %d\n", x, y); y += b[1].back(); printf("%d %d\n", x, y); a[0].pop_back(); b[1].pop_back(); } sort(a[0].begin(), a[0].end()); sort(b[0].begin(), b[0].end(), greater<int>()); while (b[0].size()) { x -= a[0].back(); printf("%d %d\n", x, y); y -= b[0].back(); printf("%d %d\n", x, y); a[0].pop_back(); b[0].pop_back(); } } 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 buffer_size = 1e5 + 5; char buf[buffer_size], *S, *T; bool flag_EOF; inline char read_char() { if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin); return S != T ? *(S++) : EOF; } inline int read_int() { bool flag = false; char c = read_char(); while (c < '0' || c > '9') { if (c == EOF) { flag_EOF = true; return 0; } flag = (c == '-' ? true : flag); c = read_char(); } int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = read_char(); } return flag ? -x : x; } const int max_cnt = 1e3 + 5; int l[max_cnt], p[max_cnt]; bool mark[max_cnt]; vector<int> a[2], b[2]; const int max_size = 5e5 + 5; bitset<max_size> f[2]; bool dp[max_size]; int val[max_size]; inline int cmp1(int x, int y) { return abs(x) < abs(y); } inline int cmp2(int x, int y) { return abs(x) > abs(y); } int main() { int t; scanf("%d", &t); while (t--) { int h, v; scanf("%d", &h); for (int i = 1; i <= h; ++i) scanf("%d", l + i); scanf("%d", &v); for (int i = 1; i <= v; ++i) scanf("%d", p + i); if (h != v) { printf("No\n"); continue; } int sum1 = 0, sum2 = 0; for (int i = 1; i <= h; ++i) sum1 += l[i]; for (int i = 1; i <= v; ++i) sum2 += p[i]; if ((sum1 & 1) || (sum2 & 1)) { printf("No\n"); continue; } random_shuffle(l + 1, l + h + 1); for (int i = 0; i <= sum1; ++i) f[0][i] = 0; f[0][0] = 1; int pos1 = -1; for (int i = 1; i <= h; ++i) { int x = (i & 1) ^ 1, y = i & 1; f[y] = f[x] | (f[x] << l[i]); if (f[y][sum1 >> 1]) { pos1 = i; break; } } if (pos1 == -1) { printf("No\n"); continue; } random_shuffle(p + 1, p + v + 1); for (int i = 0; i <= sum2; ++i) f[0][i] = 0; f[0][0] = 1; int pos2 = -1; for (int i = 1; i <= v; ++i) { int x = (i & 1) ^ 1, y = i & 1; f[y] = f[x] | (f[x] << p[i]); if (f[y][sum2 >> 1]) { pos2 = i; break; } } if (pos2 == -1) { printf("No\n"); continue; } a[0].clear(), a[1].clear(); int tot1 = sum1 >> 1; for (int i = 1; i <= h; ++i) mark[i] = false; for (int i = pos1 + 1; i <= h; ++i) { a[0].push_back(l[i]); mark[i] = true; tot1 -= l[i]; } for (int i = 0; i <= tot1; ++i) dp[i] = false; dp[0] = true; for (int i = 1; i <= pos1; ++i) for (int j = tot1 - l[i]; j >= 0; --j) if (dp[j] && !dp[j + l[i]]) { dp[j + l[i]] = true; val[j + l[i]] = i; } int now = tot1; while (now) { a[0].push_back(l[val[now]]); mark[val[now]] = true; now -= l[val[now]]; } int cnt1 = 0; for (int i = 1; i <= h; ++i) if (!mark[i]) { a[1].push_back(l[i]); ++cnt1; } b[0].clear(), b[1].clear(); int tot2 = sum2 >> 1; for (int i = 1; i <= v; ++i) mark[i] = false; for (int i = pos2 + 1; i <= v; ++i) { b[0].push_back(p[i]); mark[i] = true; tot2 -= p[i]; } for (int i = 0; i <= tot2; ++i) dp[i] = false; dp[0] = true; for (int i = 1; i <= pos2; ++i) for (int j = tot2 - p[i]; j >= 0; --j) if (dp[j] && !dp[j + p[i]]) { dp[j + p[i]] = true; val[j + p[i]] = i; } now = tot2; while (now) { b[0].push_back(p[val[now]]); mark[val[now]] = true; now -= p[val[now]]; } int cnt2 = 0; for (int i = 1; i <= v; ++i) if (!mark[i]) { b[1].push_back(p[i]); ++cnt2; } printf("Yes\n"); int x = 0, y = 0; if (a[0].size() < a[1].size()) swap(a[0], a[1]); if (b[0].size() > b[1].size()) swap(b[0], b[1]); sort(a[1].begin(), a[1].end()); sort(b[1].begin(), b[1].end(), greater<int>()); while (a[1].size()) { x += a[1].back(); printf("%d %d\n", x, y); y += b[1].back(); printf("%d %d\n", x, y); a[1].pop_back(); b[1].pop_back(); } while (b[1].size()) { x -= a[0].back(); printf("%d %d\n", x, y); y += b[1].back(); printf("%d %d\n", x, y); a[0].pop_back(); b[1].pop_back(); } sort(a[0].begin(), a[0].end()); sort(b[0].begin(), b[0].end(), greater<int>()); while (b[0].size()) { x -= a[0].back(); printf("%d %d\n", x, y); y -= b[0].back(); printf("%d %d\n", x, y); a[0].pop_back(); b[0].pop_back(); } } return 0; } ```
#include <bits/stdc++.h> 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; } using namespace std; long long ksm(long long a, long long b) { if (!b) return 1; long long ns = ksm(a, b >> 1); ns = ns * ns % 998244353; if (b & 1) ns = ns * a % 998244353; return ns; } const int W = 1005; const int mx = 505; const int pls = mx / 2; bitset<mx * W> dp[505]; mt19937 h(12312312); pair<vector<int>, vector<int> > cal(vector<int> x) { int n = x.size(); int cnt = 0; l1: shuffle(x.begin(), x.end(), h); dp[0][pls * W] = 1; for (int i = 1; i <= n; i++) { int cur = x[i - 1]; dp[i] = (dp[i - 1] >> cur) | (dp[i - 1] << cur); } if (dp[n][pls * W]) { int ni = n, nk = pls * W; vector<int> u, v; while (ni != 0) { int cur = x[ni - 1]; if (nk >= cur && dp[ni - 1][nk - cur]) ni--, nk -= cur, v.push_back(cur); else ni--, nk += cur, u.push_back(cur); } return make_pair(u, v); } else return make_pair(vector<int>(0), vector<int>(0)); } vector<int> rd() { int sz; cin >> sz; vector<int> h(sz); for (int i = 0; i < sz; i++) cin >> h[i]; return h; } int main() { int t; cin >> t; while (t--) { vector<int> a = rd(), b = rd(); if (a.size() != b.size()) cout << "No" << endl; else { pair<vector<int>, vector<int> > x = cal(a), y = cal(b); if (x.first.size() && y.first.size()) { cout << "Yes" << endl; int nx = 0, ny = 0; vector<pair<int, int> > cur; vector<int> u = x.first, v = x.second, w = y.first, t = y.second; if (u.size() < v.size()) swap(u, v); if (w.size() > t.size()) swap(w, t); sort(u.begin(), u.end()), reverse(u.begin(), u.end()); sort(v.begin(), v.end()), reverse(v.begin(), v.end()); sort(w.begin(), w.end()); sort(t.begin(), t.end()); for (int i = 0; i < w.size(); i++) cur.push_back(make_pair(u[i], 0)), cur.push_back(make_pair(0, w[i])); for (int i = 0; i < v.size(); i++) cur.push_back(make_pair(-v[i], 0)), cur.push_back(make_pair(0, -t[i])); for (int i = 0; i < u.size() - w.size(); i++) cur.push_back(make_pair(u[w.size() + i], 0)), cur.push_back(make_pair(0, -t[v.size() + i])); for (auto h : cur) { nx += h.first, ny += h.second; cout << nx << ' ' << ny << endl; } } else { cout << "No" << endl; } } } 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> 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; } using namespace std; long long ksm(long long a, long long b) { if (!b) return 1; long long ns = ksm(a, b >> 1); ns = ns * ns % 998244353; if (b & 1) ns = ns * a % 998244353; return ns; } const int W = 1005; const int mx = 505; const int pls = mx / 2; bitset<mx * W> dp[505]; mt19937 h(12312312); pair<vector<int>, vector<int> > cal(vector<int> x) { int n = x.size(); int cnt = 0; l1: shuffle(x.begin(), x.end(), h); dp[0][pls * W] = 1; for (int i = 1; i <= n; i++) { int cur = x[i - 1]; dp[i] = (dp[i - 1] >> cur) | (dp[i - 1] << cur); } if (dp[n][pls * W]) { int ni = n, nk = pls * W; vector<int> u, v; while (ni != 0) { int cur = x[ni - 1]; if (nk >= cur && dp[ni - 1][nk - cur]) ni--, nk -= cur, v.push_back(cur); else ni--, nk += cur, u.push_back(cur); } return make_pair(u, v); } else return make_pair(vector<int>(0), vector<int>(0)); } vector<int> rd() { int sz; cin >> sz; vector<int> h(sz); for (int i = 0; i < sz; i++) cin >> h[i]; return h; } int main() { int t; cin >> t; while (t--) { vector<int> a = rd(), b = rd(); if (a.size() != b.size()) cout << "No" << endl; else { pair<vector<int>, vector<int> > x = cal(a), y = cal(b); if (x.first.size() && y.first.size()) { cout << "Yes" << endl; int nx = 0, ny = 0; vector<pair<int, int> > cur; vector<int> u = x.first, v = x.second, w = y.first, t = y.second; if (u.size() < v.size()) swap(u, v); if (w.size() > t.size()) swap(w, t); sort(u.begin(), u.end()), reverse(u.begin(), u.end()); sort(v.begin(), v.end()), reverse(v.begin(), v.end()); sort(w.begin(), w.end()); sort(t.begin(), t.end()); for (int i = 0; i < w.size(); i++) cur.push_back(make_pair(u[i], 0)), cur.push_back(make_pair(0, w[i])); for (int i = 0; i < v.size(); i++) cur.push_back(make_pair(-v[i], 0)), cur.push_back(make_pair(0, -t[i])); for (int i = 0; i < u.size() - w.size(); i++) cur.push_back(make_pair(u[w.size() + i], 0)), cur.push_back(make_pair(0, -t[v.size() + i])); for (auto h : cur) { nx += h.first, ny += h.second; cout << nx << ' ' << ny << endl; } } else { cout << "No" << endl; } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1000 + 5; const int MAX_S = 500000 + 5; int T; int A, a[MAX_N], B, b[MAX_N]; vector<int> a1, a2, b1, b2; bitset<MAX_S> f[MAX_N]; int split(int C, int c[], vector<int> &c1, vector<int> &c2) { int sc = 0; for (int i = 1; i <= C; i++) sc += c[i]; if (sc & 1) return 0; sc >>= 1; f[0].reset(); f[0][0] = 1; for (int i = 1; i <= C; i++) { f[i] = f[i - 1] | f[i - 1] << c[i]; } if (!f[C][sc]) return 0; c1.clear(), c2.clear(); for (int i = C; i >= 1; i--) { if (f[i - 1][sc]) { c1.push_back(c[i]); } else { sc -= c[i]; c2.push_back(c[i]); } } return 1; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &A); for (int i = 1; i <= A; i++) scanf("%d", &a[i]); scanf("%d", &B); for (int i = 1; i <= B; i++) scanf("%d", &b[i]); if (A != B || !split(A, a, a1, a2) || !split(B, b, b1, b2)) { printf("No\n"); continue; } if (a1.size() > a2.size()) a1.swap(a2); if (b1.size() < b2.size()) b1.swap(b2); int q = b1.size() - a1.size(); sort(a1.begin(), a1.end(), greater<int>()); sort(a2.begin(), a2.begin() + q); sort(a2.begin() + q, a2.end(), greater<int>()); for (int x : a2) a1.push_back(-x); sort(b1.begin(), b1.end() - q); sort(b1.end() - q, b1.end(), greater<int>()); sort(b2.begin(), b2.end()); for (int x : b2) b1.push_back(-x); printf("Yes\n"); int x = 0, y = 0; for (int i = 0; i < A; i++) { x += a1[i]; printf("%d %d\n", x, y); y += b1[i]; printf("%d %d\n", x, y); } } 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; const int MAX_N = 1000 + 5; const int MAX_S = 500000 + 5; int T; int A, a[MAX_N], B, b[MAX_N]; vector<int> a1, a2, b1, b2; bitset<MAX_S> f[MAX_N]; int split(int C, int c[], vector<int> &c1, vector<int> &c2) { int sc = 0; for (int i = 1; i <= C; i++) sc += c[i]; if (sc & 1) return 0; sc >>= 1; f[0].reset(); f[0][0] = 1; for (int i = 1; i <= C; i++) { f[i] = f[i - 1] | f[i - 1] << c[i]; } if (!f[C][sc]) return 0; c1.clear(), c2.clear(); for (int i = C; i >= 1; i--) { if (f[i - 1][sc]) { c1.push_back(c[i]); } else { sc -= c[i]; c2.push_back(c[i]); } } return 1; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &A); for (int i = 1; i <= A; i++) scanf("%d", &a[i]); scanf("%d", &B); for (int i = 1; i <= B; i++) scanf("%d", &b[i]); if (A != B || !split(A, a, a1, a2) || !split(B, b, b1, b2)) { printf("No\n"); continue; } if (a1.size() > a2.size()) a1.swap(a2); if (b1.size() < b2.size()) b1.swap(b2); int q = b1.size() - a1.size(); sort(a1.begin(), a1.end(), greater<int>()); sort(a2.begin(), a2.begin() + q); sort(a2.begin() + q, a2.end(), greater<int>()); for (int x : a2) a1.push_back(-x); sort(b1.begin(), b1.end() - q); sort(b1.end() - q, b1.end(), greater<int>()); sort(b2.begin(), b2.end()); for (int x : b2) b1.push_back(-x); printf("Yes\n"); int x = 0, y = 0; for (int i = 0; i < A; i++) { x += a1[i]; printf("%d %d\n", x, y); y += b1[i]; printf("%d %d\n", x, y); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using db = double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; template <class T> using V = vector<T>; template <class T, size_t SZ> using AR = array<T, SZ>; template <class T> using PR = pair<T, T>; const int MOD = 1e9 + 7; const int MX = 2e5 + 5; const ll INF = 1e18; const ld PI = acos((ld)-1); const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31 - __builtin_clz(x); } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } 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; } template <class T, class U> T fstTrue(T lo, T hi, U first) { hi++; assert(lo <= hi); while (lo < hi) { T mid = lo + (hi - lo) / 2; first(mid) ? hi = mid : lo = mid + 1; } return lo; } template <class T, class U> T lstTrue(T lo, T hi, U first) { lo--; assert(lo <= hi); while (lo < hi) { T mid = lo + (hi - lo + 1) / 2; first(mid) ? lo = mid : hi = mid - 1; } return lo; } template <class T> void remDup(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class T, class U> void erase(T& t, const U& u) { auto it = t.find(u); assert(it != end(t)); t.erase(u); } template <class T> void re(complex<T>& c); template <class T, class U> void re(pair<T, U>& p); template <class T> void re(vector<T>& v); template <class T, size_t SZ> void re(AR<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(db& d) { str t; re(t); d = stod(t); } void re(ld& d) { str t; re(t); d = stold(t); } template <class T, class... U> void re(T& t, U&... u) { re(t); re(u...); } template <class T> void re(complex<T>& c) { T a, b; re(a, b); c = {a, b}; } template <class T, class U> void re(pair<T, U>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& x) { for (auto& a : x) re(a); } template <class T, size_t SZ> void re(AR<T, SZ>& x) { for (auto& a : x) re(a); } template <class T> void rv(int& n, vector<T>& x) { re(n); x.resize(n); for (auto& a : x) re(a); } str to_string(char c) { return str(1, c); } str to_string(const char* second) { return (str)second; } str to_string(str second) { return second; } str to_string(bool b) { return to_string((int)b); } template <class T> str to_string(complex<T> c) { stringstream ss; ss << c; return ss.str(); } str to_string(vector<bool> v) { str res = "{"; for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]); res += "}"; return res; } template <size_t SZ> str to_string(bitset<SZ> b) { str res = ""; for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]); return res; } template <class T, class U> str to_string(pair<T, U> p); template <class T> str to_string(T v) { bool fst = 1; str res = ""; for (const auto& x : v) { if (!fst) res += " "; fst = 0; res += to_string(x); } return res; } template <class T, class U> str to_string(pair<T, U> p) { return to_string(p.first) + " " + to_string(p.second); } template <class T> void pr(T x) { cout << to_string(x); } template <class T, class... U> void pr(const T& t, const U&... u) { pr(t); pr(u...); } void ps() { pr("\n"); } template <class T, class... U> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); } void DBG() { cerr << "]" << endl; } template <class T, class... U> void DBG(const T& t, const U&... u) { cerr << to_string(t); if (sizeof...(u)) cerr << ", "; DBG(u...); } void setIn(str second) { freopen(second.c_str(), "r", stdin); } void setOut(str second) { freopen(second.c_str(), "w", stdout); } void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } void setIO(str second = "") { unsyncIO(); if ((int)(second).size()) { setIn(second + ".in"), setOut(second + ".out"); } } int hor, ver; using BB = bitset<1000001>; V<BB> rec; PR<vi> knapsack(vi v) { rec = {BB()}; rec.back()[0] = 1; int sum = 0; for (auto& t : v) { rec.push_back(rec.back() | (rec.back() << t)); sum += t; } if (sum % 2 != 0 || !rec.back()[sum / 2]) return {{}, {}}; int cur = sum / 2; vi a, b; for (int i = ((int)(v).size()) - 1; i >= (0); --i) { if (rec[i][cur]) a.push_back(v[i]); else cur -= v[i], b.push_back(v[i]); } assert(cur == 0 && rec[0][cur]); return {a, b}; } vi last(vi a, int b) { return vi(end(a) - b, end(a)); } vi first(vi a, int b) { return vi(begin(a), begin(a) + b); } void solve(int tc) { vi L, P; re(hor); L.resize(hor); re(L); re(ver); P.resize(ver); re(P); if (hor != ver) { ps("No"); return; } PR<vi> x = knapsack(L); PR<vi> y = knapsack(P); if (!(int)(x.first).size() || !(int)(y.first).size()) { ps("No"); return; } ps("Yes"); if ((int)(x.first).size() > (int)(x.second).size()) swap(x.first, x.second); if ((int)(y.first).size() < (int)(y.second).size()) swap(y.first, y.second); assert((int)(x.first).size() <= (int)(y.first).size()); sort((x.first).rbegin(), (x.first).rend()); sort(begin(x.second), end(x.second)); sort(begin(y.first), end(y.first)); sort((y.second).rbegin(), (y.second).rend()); vpi tran; for (int i = (0); i < ((int)(x.first).size()); ++i) { tran.push_back({x.first[i], 0}); tran.push_back({0, y.first[i]}); } for (int i = ((int)(x.first).size()); i < ((int)(y.first).size()); ++i) { int ind = i - (int)(x.first).size(); tran.push_back({-x.second[(int)(x.second).size() - 1 - ind], 0}); tran.push_back({0, y.first[i]}); } for (int i = ((int)(x.second).size() - ((int)(y.first).size() - (int)(x.first).size())) - 1; i >= (0); --i) { tran.push_back({-x.second[i], 0}); tran.push_back({0, -y.second[i]}); } pi p{0, 0}; for (auto& t : tran) { ps(p.first, p.second); p.first += t.first, p.second += t.second; } assert(p == make_pair(0, 0)); } int main() { setIO(); int TC; re(TC); for (int i = (1); i < (TC + 1); ++i) solve(i); }
### 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 ll = long long; using ld = long double; using db = double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; template <class T> using V = vector<T>; template <class T, size_t SZ> using AR = array<T, SZ>; template <class T> using PR = pair<T, T>; const int MOD = 1e9 + 7; const int MX = 2e5 + 5; const ll INF = 1e18; const ld PI = acos((ld)-1); const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31 - __builtin_clz(x); } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } 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; } template <class T, class U> T fstTrue(T lo, T hi, U first) { hi++; assert(lo <= hi); while (lo < hi) { T mid = lo + (hi - lo) / 2; first(mid) ? hi = mid : lo = mid + 1; } return lo; } template <class T, class U> T lstTrue(T lo, T hi, U first) { lo--; assert(lo <= hi); while (lo < hi) { T mid = lo + (hi - lo + 1) / 2; first(mid) ? lo = mid : hi = mid - 1; } return lo; } template <class T> void remDup(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class T, class U> void erase(T& t, const U& u) { auto it = t.find(u); assert(it != end(t)); t.erase(u); } template <class T> void re(complex<T>& c); template <class T, class U> void re(pair<T, U>& p); template <class T> void re(vector<T>& v); template <class T, size_t SZ> void re(AR<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(db& d) { str t; re(t); d = stod(t); } void re(ld& d) { str t; re(t); d = stold(t); } template <class T, class... U> void re(T& t, U&... u) { re(t); re(u...); } template <class T> void re(complex<T>& c) { T a, b; re(a, b); c = {a, b}; } template <class T, class U> void re(pair<T, U>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& x) { for (auto& a : x) re(a); } template <class T, size_t SZ> void re(AR<T, SZ>& x) { for (auto& a : x) re(a); } template <class T> void rv(int& n, vector<T>& x) { re(n); x.resize(n); for (auto& a : x) re(a); } str to_string(char c) { return str(1, c); } str to_string(const char* second) { return (str)second; } str to_string(str second) { return second; } str to_string(bool b) { return to_string((int)b); } template <class T> str to_string(complex<T> c) { stringstream ss; ss << c; return ss.str(); } str to_string(vector<bool> v) { str res = "{"; for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]); res += "}"; return res; } template <size_t SZ> str to_string(bitset<SZ> b) { str res = ""; for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]); return res; } template <class T, class U> str to_string(pair<T, U> p); template <class T> str to_string(T v) { bool fst = 1; str res = ""; for (const auto& x : v) { if (!fst) res += " "; fst = 0; res += to_string(x); } return res; } template <class T, class U> str to_string(pair<T, U> p) { return to_string(p.first) + " " + to_string(p.second); } template <class T> void pr(T x) { cout << to_string(x); } template <class T, class... U> void pr(const T& t, const U&... u) { pr(t); pr(u...); } void ps() { pr("\n"); } template <class T, class... U> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); } void DBG() { cerr << "]" << endl; } template <class T, class... U> void DBG(const T& t, const U&... u) { cerr << to_string(t); if (sizeof...(u)) cerr << ", "; DBG(u...); } void setIn(str second) { freopen(second.c_str(), "r", stdin); } void setOut(str second) { freopen(second.c_str(), "w", stdout); } void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } void setIO(str second = "") { unsyncIO(); if ((int)(second).size()) { setIn(second + ".in"), setOut(second + ".out"); } } int hor, ver; using BB = bitset<1000001>; V<BB> rec; PR<vi> knapsack(vi v) { rec = {BB()}; rec.back()[0] = 1; int sum = 0; for (auto& t : v) { rec.push_back(rec.back() | (rec.back() << t)); sum += t; } if (sum % 2 != 0 || !rec.back()[sum / 2]) return {{}, {}}; int cur = sum / 2; vi a, b; for (int i = ((int)(v).size()) - 1; i >= (0); --i) { if (rec[i][cur]) a.push_back(v[i]); else cur -= v[i], b.push_back(v[i]); } assert(cur == 0 && rec[0][cur]); return {a, b}; } vi last(vi a, int b) { return vi(end(a) - b, end(a)); } vi first(vi a, int b) { return vi(begin(a), begin(a) + b); } void solve(int tc) { vi L, P; re(hor); L.resize(hor); re(L); re(ver); P.resize(ver); re(P); if (hor != ver) { ps("No"); return; } PR<vi> x = knapsack(L); PR<vi> y = knapsack(P); if (!(int)(x.first).size() || !(int)(y.first).size()) { ps("No"); return; } ps("Yes"); if ((int)(x.first).size() > (int)(x.second).size()) swap(x.first, x.second); if ((int)(y.first).size() < (int)(y.second).size()) swap(y.first, y.second); assert((int)(x.first).size() <= (int)(y.first).size()); sort((x.first).rbegin(), (x.first).rend()); sort(begin(x.second), end(x.second)); sort(begin(y.first), end(y.first)); sort((y.second).rbegin(), (y.second).rend()); vpi tran; for (int i = (0); i < ((int)(x.first).size()); ++i) { tran.push_back({x.first[i], 0}); tran.push_back({0, y.first[i]}); } for (int i = ((int)(x.first).size()); i < ((int)(y.first).size()); ++i) { int ind = i - (int)(x.first).size(); tran.push_back({-x.second[(int)(x.second).size() - 1 - ind], 0}); tran.push_back({0, y.first[i]}); } for (int i = ((int)(x.second).size() - ((int)(y.first).size() - (int)(x.first).size())) - 1; i >= (0); --i) { tran.push_back({-x.second[i], 0}); tran.push_back({0, -y.second[i]}); } pi p{0, 0}; for (auto& t : tran) { ps(p.first, p.second); p.first += t.first, p.second += t.second; } assert(p == make_pair(0, 0)); } int main() { setIO(); int TC; re(TC); for (int i = (1); i < (TC + 1); ++i) solve(i); } ```
#include <bits/stdc++.h> using namespace std; const int N = 1000; int T, n, m, a[N + 5], b[N + 5], suma, sumb; int x[2][N + 5], y[2][N + 5], totx[2], toty[2], tot; bitset<N * N> dp[N + 5]; struct node { int x, y; } ans[N + 5]; int pre(); void solve(); int cmp(int a, int b) { return a > b; } int main() { scanf("%d", &T); while (T--) { suma = sumb = tot = 0; totx[1] = totx[0] = toty[1] = toty[0] = 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]; if (n != m || suma & 1 || sumb & 1) { puts("No"); continue; } if (!pre()) { puts("No"); continue; } solve(); puts("Yes"); for (int i = 1; i <= tot; i++) printf("%d %d\n", ans[i].x, ans[i].y); } return 0; } void solve() { if (totx[1] <= toty[1]) { sort(y[1] + 1, y[1] + toty[1] + 1); sort(y[0] + 1, y[0] + toty[0] + 1); sort(x[1] + 1, x[1] + totx[1] + 1, cmp); sort(x[0] + 1, x[0] + totx[0] + 1, cmp); node now = {0, 0}; int i[2] = {1, 1}, j[2] = {1, 1}; while (n--) { if (i[1] <= totx[1]) now.x += x[1][i[1]++], ans[++tot] = now; else now.x -= x[0][i[0]++], ans[++tot] = now; if (j[1] <= toty[1]) now.y += y[1][j[1]++], ans[++tot] = now; else now.y -= y[0][j[0]++], ans[++tot] = now; } } else { sort(x[1] + 1, x[1] + totx[1] + 1); sort(x[0] + 1, x[0] + totx[0] + 1); sort(y[1] + 1, y[1] + toty[1] + 1, cmp); sort(y[0] + 1, y[0] + toty[0] + 1, cmp); node now = {0, 0}; int i[2] = {1, 1}, j[2] = {1, 1}; while (n--) { if (j[1] <= toty[1]) now.y += y[1][j[1]++], ans[++tot] = now; else now.y -= y[0][j[0]++], ans[++tot] = now; if (i[1] <= totx[1]) now.x += x[1][i[1]++], ans[++tot] = now; else now.x -= x[0][i[0]++], ans[++tot] = now; } } } int pre() { dp[0][0] = 1; int s1 = suma / 2, s2 = sumb / 2; for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[n][s1]) return 0; for (int i = n; i >= 1; i--) if (s1 >= a[i] && dp[i - 1][s1 - a[i]]) s1 -= a[i], x[1][++totx[1]] = a[i]; else x[0][++totx[0]] = a[i]; for (int i = 1; i <= m; i++) dp[i] = dp[i - 1] | (dp[i - 1] << b[i]); if (!dp[m][s2]) return 0; for (int i = m; i >= 1; i--) if (s2 >= b[i] && dp[i - 1][s2 - b[i]]) s2 -= b[i], y[1][++toty[1]] = b[i]; else y[0][++toty[0]] = b[i]; return 1; }
### 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 = 1000; int T, n, m, a[N + 5], b[N + 5], suma, sumb; int x[2][N + 5], y[2][N + 5], totx[2], toty[2], tot; bitset<N * N> dp[N + 5]; struct node { int x, y; } ans[N + 5]; int pre(); void solve(); int cmp(int a, int b) { return a > b; } int main() { scanf("%d", &T); while (T--) { suma = sumb = tot = 0; totx[1] = totx[0] = toty[1] = toty[0] = 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]; if (n != m || suma & 1 || sumb & 1) { puts("No"); continue; } if (!pre()) { puts("No"); continue; } solve(); puts("Yes"); for (int i = 1; i <= tot; i++) printf("%d %d\n", ans[i].x, ans[i].y); } return 0; } void solve() { if (totx[1] <= toty[1]) { sort(y[1] + 1, y[1] + toty[1] + 1); sort(y[0] + 1, y[0] + toty[0] + 1); sort(x[1] + 1, x[1] + totx[1] + 1, cmp); sort(x[0] + 1, x[0] + totx[0] + 1, cmp); node now = {0, 0}; int i[2] = {1, 1}, j[2] = {1, 1}; while (n--) { if (i[1] <= totx[1]) now.x += x[1][i[1]++], ans[++tot] = now; else now.x -= x[0][i[0]++], ans[++tot] = now; if (j[1] <= toty[1]) now.y += y[1][j[1]++], ans[++tot] = now; else now.y -= y[0][j[0]++], ans[++tot] = now; } } else { sort(x[1] + 1, x[1] + totx[1] + 1); sort(x[0] + 1, x[0] + totx[0] + 1); sort(y[1] + 1, y[1] + toty[1] + 1, cmp); sort(y[0] + 1, y[0] + toty[0] + 1, cmp); node now = {0, 0}; int i[2] = {1, 1}, j[2] = {1, 1}; while (n--) { if (j[1] <= toty[1]) now.y += y[1][j[1]++], ans[++tot] = now; else now.y -= y[0][j[0]++], ans[++tot] = now; if (i[1] <= totx[1]) now.x += x[1][i[1]++], ans[++tot] = now; else now.x -= x[0][i[0]++], ans[++tot] = now; } } } int pre() { dp[0][0] = 1; int s1 = suma / 2, s2 = sumb / 2; for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[n][s1]) return 0; for (int i = n; i >= 1; i--) if (s1 >= a[i] && dp[i - 1][s1 - a[i]]) s1 -= a[i], x[1][++totx[1]] = a[i]; else x[0][++totx[0]] = a[i]; for (int i = 1; i <= m; i++) dp[i] = dp[i - 1] | (dp[i - 1] << b[i]); if (!dp[m][s2]) return 0; for (int i = m; i >= 1; i--) if (s2 >= b[i] && dp[i - 1][s2 - b[i]]) s2 -= b[i], y[1][++toty[1]] = b[i]; else y[0][++toty[0]] = b[i]; return 1; } ```
#include <bits/stdc++.h> using namespace std; int t, n, m, qzx, qzy; int a[1005], b[1005]; vector<int> e[2][1005]; bitset<1000005> f[1005]; vector<int> a1, a2, b1, b2; int main() { scanf("%d", &t); while (t--) { a1.clear(), a2.clear(), b1.clear(), b2.clear(); qzx = qzy = 0; scanf("%d", &n); for (register int i = 1; i <= n; i++) scanf("%d", &a[i]), qzx += a[i]; scanf("%d", &m); for (register int i = 1; i <= m; i++) scanf("%d", &b[i]), qzy += b[i]; if (n != m || (qzx % 2) || (qzy % 2)) { printf("NO\n"); continue; } qzx /= 2, qzy /= 2; f[0][0] = 1; for (register int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (!f[n][qzx]) { printf("NO\n"); continue; } for (register int i = n; i >= 1; i--) if (a[i] <= qzx && f[i - 1][qzx - a[i]]) a1.push_back(a[i]), qzx -= a[i]; else a2.push_back(a[i]); f[0][0] = 1; for (register int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (!f[m][qzy]) { printf("NO\n"); continue; } for (register int i = m; i >= 1; i--) if (b[i] <= qzy && f[i - 1][qzy - b[i]]) b1.push_back(b[i]), qzy -= b[i]; else b2.push_back(b[i]); printf("YES\n"); if (a1.size() > a2.size()) swap(a1, a2); if (b1.size() < b2.size()) swap(b1, b2); sort(a1.begin(), a1.end()); reverse(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); reverse(a2.begin(), a2.end()); sort(b1.begin(), b1.end()); sort(b2.begin(), b2.end()); int x = 0, y = 0, dx = 1, dy = 1; int tp1 = 0, tp2 = 0; for (register int i = 1; i <= n; i++) { if (tp1 >= a1.size()) x += dx * a2[tp1 - a1.size()]; else x += dx * a1[tp1]; printf("%d %d\n", x, y); if (tp2 >= b1.size()) y += dy * b2[tp2 - b1.size()]; else y += dy * b1[tp2]; printf("%d %d\n", x, y); tp1++, tp2++; if (tp1 == a1.size()) dx = -1; if (tp2 == b1.size()) dy = -1; } } }
### 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; int t, n, m, qzx, qzy; int a[1005], b[1005]; vector<int> e[2][1005]; bitset<1000005> f[1005]; vector<int> a1, a2, b1, b2; int main() { scanf("%d", &t); while (t--) { a1.clear(), a2.clear(), b1.clear(), b2.clear(); qzx = qzy = 0; scanf("%d", &n); for (register int i = 1; i <= n; i++) scanf("%d", &a[i]), qzx += a[i]; scanf("%d", &m); for (register int i = 1; i <= m; i++) scanf("%d", &b[i]), qzy += b[i]; if (n != m || (qzx % 2) || (qzy % 2)) { printf("NO\n"); continue; } qzx /= 2, qzy /= 2; f[0][0] = 1; for (register int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (!f[n][qzx]) { printf("NO\n"); continue; } for (register int i = n; i >= 1; i--) if (a[i] <= qzx && f[i - 1][qzx - a[i]]) a1.push_back(a[i]), qzx -= a[i]; else a2.push_back(a[i]); f[0][0] = 1; for (register int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (!f[m][qzy]) { printf("NO\n"); continue; } for (register int i = m; i >= 1; i--) if (b[i] <= qzy && f[i - 1][qzy - b[i]]) b1.push_back(b[i]), qzy -= b[i]; else b2.push_back(b[i]); printf("YES\n"); if (a1.size() > a2.size()) swap(a1, a2); if (b1.size() < b2.size()) swap(b1, b2); sort(a1.begin(), a1.end()); reverse(a1.begin(), a1.end()); sort(a2.begin(), a2.end()); reverse(a2.begin(), a2.end()); sort(b1.begin(), b1.end()); sort(b2.begin(), b2.end()); int x = 0, y = 0, dx = 1, dy = 1; int tp1 = 0, tp2 = 0; for (register int i = 1; i <= n; i++) { if (tp1 >= a1.size()) x += dx * a2[tp1 - a1.size()]; else x += dx * a1[tp1]; printf("%d %d\n", x, y); if (tp2 >= b1.size()) y += dy * b2[tp2 - b1.size()]; else y += dy * b1[tp2]; printf("%d %d\n", x, y); tp1++, tp2++; if (tp1 == a1.size()) dx = -1; if (tp2 == b1.size()) dy = -1; } } } ```
#include <bits/stdc++.h> using namespace std; char buf[1048576], *p0, *p1; inline int read() { int r = 0; char c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); while (c < 48 || c > 57) c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); while (c > 47 && c < 58) { r = (r << 3) + (r << 1) + (c ^ 48); c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); } return r; } int n[2], l[2][1005], s[2], c[2][2], t[2][2][1005], d[2][1005]; bitset<1000005> f[1005]; inline bool check(int u) { for (int i = 1; i <= n[u]; ++i) f[i] = f[i - 1] | (f[i - 1] << l[u][i]); if (!f[n[u]][s[u]]) return true; for (int i = n[u]; i; --i) { if (s[u] >= l[u][i] && f[i - 1][s[u] - l[u][i]]) { s[u] -= l[u][i]; t[u][0][++c[u][0]] = l[u][i]; } else t[u][1][++c[u][1]] = l[u][i]; } return false; } int main() { int q = read(), u, v, w; f[0].set(0); while (q--) { s[0] = s[1] = c[0][0] = c[0][1] = c[1][0] = c[1][1] = 0; n[0] = read(); for (int i = 1; i <= n[0]; ++i) s[0] += l[0][i] = read(); n[1] = read(); for (int i = 1; i <= n[1]; ++i) s[1] += l[1][i] = read(); if (n[0] != n[1] || (s[0] & 1) || (s[1] & 1)) { puts("No"); continue; } sort(l[0] + 1, l[0] + n[0] + 1); sort(l[1] + 1, l[1] + n[1] + 1); s[0] >>= 1; s[1] >>= 1; if (check(0) || check(1)) { puts("No"); continue; } puts("Yes"); u = w = c[v = 0][0] > c[1][0]; for (int i = 1; i <= c[u][0]; ++i) d[0][++v] = t[u][0][i]; for (int i = 1; i <= c[u][1]; ++i) d[0][++v] = -t[u][1][i]; u ^= 1; v = 0; for (int i = c[u][0]; i; --i) d[1][++v] = t[u][0][i]; for (int i = c[u][1]; i; --i) d[1][++v] = -t[u][1][i]; u = v = 0; for (int i = 1; i <= n[0]; ++i) { u += d[0][i]; w ? printf("%d %d\n", v, u) : printf("%d %d\n", u, v); v += d[1][i]; w ? printf("%d %d\n", v, u) : printf("%d %d\n", u, v); } } 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; char buf[1048576], *p0, *p1; inline int read() { int r = 0; char c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); while (c < 48 || c > 57) c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); while (c > 47 && c < 58) { r = (r << 3) + (r << 1) + (c ^ 48); c = (p0 == p1 && (p1 = (p0 = buf) + fread(buf, 1, 1048576, stdin), p0 == p1) ? EOF : *p0++); } return r; } int n[2], l[2][1005], s[2], c[2][2], t[2][2][1005], d[2][1005]; bitset<1000005> f[1005]; inline bool check(int u) { for (int i = 1; i <= n[u]; ++i) f[i] = f[i - 1] | (f[i - 1] << l[u][i]); if (!f[n[u]][s[u]]) return true; for (int i = n[u]; i; --i) { if (s[u] >= l[u][i] && f[i - 1][s[u] - l[u][i]]) { s[u] -= l[u][i]; t[u][0][++c[u][0]] = l[u][i]; } else t[u][1][++c[u][1]] = l[u][i]; } return false; } int main() { int q = read(), u, v, w; f[0].set(0); while (q--) { s[0] = s[1] = c[0][0] = c[0][1] = c[1][0] = c[1][1] = 0; n[0] = read(); for (int i = 1; i <= n[0]; ++i) s[0] += l[0][i] = read(); n[1] = read(); for (int i = 1; i <= n[1]; ++i) s[1] += l[1][i] = read(); if (n[0] != n[1] || (s[0] & 1) || (s[1] & 1)) { puts("No"); continue; } sort(l[0] + 1, l[0] + n[0] + 1); sort(l[1] + 1, l[1] + n[1] + 1); s[0] >>= 1; s[1] >>= 1; if (check(0) || check(1)) { puts("No"); continue; } puts("Yes"); u = w = c[v = 0][0] > c[1][0]; for (int i = 1; i <= c[u][0]; ++i) d[0][++v] = t[u][0][i]; for (int i = 1; i <= c[u][1]; ++i) d[0][++v] = -t[u][1][i]; u ^= 1; v = 0; for (int i = c[u][0]; i; --i) d[1][++v] = t[u][0][i]; for (int i = c[u][1]; i; --i) d[1][++v] = -t[u][1][i]; u = v = 0; for (int i = 1; i <= n[0]; ++i) { u += d[0][i]; w ? printf("%d %d\n", v, u) : printf("%d %d\n", u, v); v += d[1][i]; w ? printf("%d %d\n", v, u) : printf("%d %d\n", u, v); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int T, n, m, sum1, sum2; int len1[1000], len2[1000]; int dp[1000][20000]; bool can(void) { long long int lim = (1ll << 30) - 1, t; int s1 = sum1 / 30, s2 = sum2 / 30, p, q, r; for (int i = 0; i < n; i++) for (int j = 0; j <= s1; j++) dp[i][j] = 0; dp[0][0] = 1; dp[0][len1[0] / 30] = 1 << (len1[0] % 30); for (int i = 1; i < n; i++) for (int j = 0; j <= s1; j++) { p = len1[i] / 30; q = len1[i] % 30; r = 30 - q; dp[i][j] = dp[i - 1][j]; if (j >= p) t = (long long int)dp[i - 1][j - p] << q & lim; if (j >= p) dp[i][j] |= (int)t; if (j > p) dp[i][j] |= (dp[i - 1][j - p - 1] >> r); } if (!(dp[n - 1][sum1 / 60] >> (sum1 / 2 % 30) & 1)) return 0; int pos = n - 1, s = sum1 >> 1; while (pos >= 0) { if (s > 0 && (pos == 0 || !(dp[pos - 1][s / 30] >> (s % 30) & 1))) { s -= len1[pos]; len1[pos] *= -1; } pos--; } for (int i = 0; i < n; i++) for (int j = 0; j <= s2; j++) dp[i][j] = 0; dp[0][0] = 1; dp[0][len2[0] / 30] = 1 << (len2[0] % 30); for (int i = 1; i < n; i++) for (int j = 0; j <= s2; j++) { p = len2[i] / 30; q = len2[i] % 30; r = 30 - q; dp[i][j] = dp[i - 1][j]; if (j >= p) t = (long long int)dp[i - 1][j - p] << q & lim; if (j >= p) dp[i][j] |= (int)t; if (j > p) dp[i][j] |= (dp[i - 1][j - p - 1] >> r); } if (!(dp[n - 1][sum2 / 60] >> (sum2 / 2 % 30) & 1)) return 0; pos = n - 1; s = sum2 >> 1; while (pos >= 0) { if (s > 0 && (pos == 0 || !(dp[pos - 1][s / 30] >> (s % 30) & 1))) { s -= len2[pos]; len2[pos] *= -1; } pos--; } return 1; } int d1, d2; void solve(void) { sort(len1, len1 + n); sort(len2, len2 + n); for (int i = 0; i < n; i++) if (len1[i] > 0) { d1 = i; break; } for (int i = 0; i < n; i++) if (len2[i] > 0) { d2 = i; break; } if (d1 <= d2) { for (int i = 0; (i << 1) < n - d1; i++) swap(len1[d1 + i], len1[n - 1 - i]); for (int i = 0; (i << 1) < d2; i++) swap(len2[i], len2[d2 - 1 - i]); } else { for (int i = 0; (i << 1) < d1; i++) swap(len1[i], len1[d1 - 1 - i]); for (int i = 0; (i << 1) < n - d2; i++) swap(len2[d2 + i], len2[n - 1 - i]); } return; } void print(void) { printf("Yes\n"); if (d1 <= d2) { int px = 0, py = 0; for (int i = 0; i < n; i++) { px += len1[i]; printf("%d %d\n", px, py); py += len2[i]; printf("%d %d\n", px, py); } } else { int px = 0, py = 0; for (int i = 0; i < n; i++) { py += len2[i]; printf("%d %d\n", px, py); px += len1[i]; printf("%d %d\n", px, py); } } return; } void abC(void) { sum1 = sum2 = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &len1[i]); sum1 += len1[i]; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &len2[i]); sum2 += len2[i]; } if (n != m || (sum1 & 1) || (sum2 & 1) || !can()) { printf("No\n"); return; } solve(); print(); return; } int main(void) { scanf("%d", &T); while (T--) abC(); 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 T, n, m, sum1, sum2; int len1[1000], len2[1000]; int dp[1000][20000]; bool can(void) { long long int lim = (1ll << 30) - 1, t; int s1 = sum1 / 30, s2 = sum2 / 30, p, q, r; for (int i = 0; i < n; i++) for (int j = 0; j <= s1; j++) dp[i][j] = 0; dp[0][0] = 1; dp[0][len1[0] / 30] = 1 << (len1[0] % 30); for (int i = 1; i < n; i++) for (int j = 0; j <= s1; j++) { p = len1[i] / 30; q = len1[i] % 30; r = 30 - q; dp[i][j] = dp[i - 1][j]; if (j >= p) t = (long long int)dp[i - 1][j - p] << q & lim; if (j >= p) dp[i][j] |= (int)t; if (j > p) dp[i][j] |= (dp[i - 1][j - p - 1] >> r); } if (!(dp[n - 1][sum1 / 60] >> (sum1 / 2 % 30) & 1)) return 0; int pos = n - 1, s = sum1 >> 1; while (pos >= 0) { if (s > 0 && (pos == 0 || !(dp[pos - 1][s / 30] >> (s % 30) & 1))) { s -= len1[pos]; len1[pos] *= -1; } pos--; } for (int i = 0; i < n; i++) for (int j = 0; j <= s2; j++) dp[i][j] = 0; dp[0][0] = 1; dp[0][len2[0] / 30] = 1 << (len2[0] % 30); for (int i = 1; i < n; i++) for (int j = 0; j <= s2; j++) { p = len2[i] / 30; q = len2[i] % 30; r = 30 - q; dp[i][j] = dp[i - 1][j]; if (j >= p) t = (long long int)dp[i - 1][j - p] << q & lim; if (j >= p) dp[i][j] |= (int)t; if (j > p) dp[i][j] |= (dp[i - 1][j - p - 1] >> r); } if (!(dp[n - 1][sum2 / 60] >> (sum2 / 2 % 30) & 1)) return 0; pos = n - 1; s = sum2 >> 1; while (pos >= 0) { if (s > 0 && (pos == 0 || !(dp[pos - 1][s / 30] >> (s % 30) & 1))) { s -= len2[pos]; len2[pos] *= -1; } pos--; } return 1; } int d1, d2; void solve(void) { sort(len1, len1 + n); sort(len2, len2 + n); for (int i = 0; i < n; i++) if (len1[i] > 0) { d1 = i; break; } for (int i = 0; i < n; i++) if (len2[i] > 0) { d2 = i; break; } if (d1 <= d2) { for (int i = 0; (i << 1) < n - d1; i++) swap(len1[d1 + i], len1[n - 1 - i]); for (int i = 0; (i << 1) < d2; i++) swap(len2[i], len2[d2 - 1 - i]); } else { for (int i = 0; (i << 1) < d1; i++) swap(len1[i], len1[d1 - 1 - i]); for (int i = 0; (i << 1) < n - d2; i++) swap(len2[d2 + i], len2[n - 1 - i]); } return; } void print(void) { printf("Yes\n"); if (d1 <= d2) { int px = 0, py = 0; for (int i = 0; i < n; i++) { px += len1[i]; printf("%d %d\n", px, py); py += len2[i]; printf("%d %d\n", px, py); } } else { int px = 0, py = 0; for (int i = 0; i < n; i++) { py += len2[i]; printf("%d %d\n", px, py); px += len1[i]; printf("%d %d\n", px, py); } } return; } void abC(void) { sum1 = sum2 = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &len1[i]); sum1 += len1[i]; } scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &len2[i]); sum2 += len2[i]; } if (n != m || (sum1 & 1) || (sum2 & 1) || !can()) { printf("No\n"); return; } solve(); print(); return; } int main(void) { scanf("%d", &T); while (T--) abC(); return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve() { int n, m, suma = 0, sumb = 0; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; suma += a[i]; } cin >> m; vector<int> b(m); for (int i = 0; i < m; i++) { cin >> b[i]; sumb += b[i]; } sort(a.begin(), a.end()); sort(b.rbegin(), b.rend()); if (n != m || suma % 2 == 1 || sumb % 2 == 1) { cout << "No" << '\n'; return; } vector<pair<int, int>> res(2 * n); { suma /= 2; vector<vector<bool>> dp(n + 1, vector<bool>(suma + 1)); dp[0][0] = true; for (int i = 0; i < n; i++) { dp[i + 1] = dp[i]; for (int j = a[i]; j <= suma; j++) { if (dp[i][j - a[i]]) { dp[i + 1][j] = true; } } } if (dp[n][suma] == false) { cout << "No" << '\n'; return; } int now = suma; vector<int> x, x2; for (int i = n; i > 0; i--) { if (now - a[i - 1] >= 0 && dp[i - 1][now - a[i - 1]]) { now -= a[i - 1]; x.push_back(a[i - 1]); } else { x2.push_back(a[i - 1]); } } int iter = 0; for (auto i : x) { res[iter] = {i, 0}; iter += 2; } for (auto i : x2) { res[iter] = {-i, 0}; iter += 2; } } { sumb /= 2; vector<vector<bool>> dp(n + 1, vector<bool>(sumb + 1)); dp[0][0] = true; for (int i = 0; i < n; i++) { dp[i + 1] = dp[i]; for (int j = b[i]; j <= sumb; j++) { if (dp[i][j - b[i]]) { dp[i + 1][j] = true; } } } if (dp[n][sumb] == false) { cout << "No" << '\n'; return; } int now = sumb; vector<int> x, x2; for (int i = n; i > 0; i--) { if (now - b[i - 1] >= 0 && dp[i - 1][now - b[i - 1]]) { now -= b[i - 1]; x.push_back(b[i - 1]); } else { x2.push_back(b[i - 1]); } } int iter = 1; for (auto i : x) { res[iter] = {0, i}; iter += 2; } for (auto i : x2) { res[iter] = {0, -i}; iter += 2; } } cout << "Yes" << '\n'; int p = 0, q = 0; for (auto i : res) { p += i.first; q += i.second; cout << p << ' ' << q << '\n'; } } int main() { ios_base::sync_with_stdio(false); int tt; cin >> tt; for (int i = 1; i <= tt; i++) { 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; void solve() { int n, m, suma = 0, sumb = 0; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; suma += a[i]; } cin >> m; vector<int> b(m); for (int i = 0; i < m; i++) { cin >> b[i]; sumb += b[i]; } sort(a.begin(), a.end()); sort(b.rbegin(), b.rend()); if (n != m || suma % 2 == 1 || sumb % 2 == 1) { cout << "No" << '\n'; return; } vector<pair<int, int>> res(2 * n); { suma /= 2; vector<vector<bool>> dp(n + 1, vector<bool>(suma + 1)); dp[0][0] = true; for (int i = 0; i < n; i++) { dp[i + 1] = dp[i]; for (int j = a[i]; j <= suma; j++) { if (dp[i][j - a[i]]) { dp[i + 1][j] = true; } } } if (dp[n][suma] == false) { cout << "No" << '\n'; return; } int now = suma; vector<int> x, x2; for (int i = n; i > 0; i--) { if (now - a[i - 1] >= 0 && dp[i - 1][now - a[i - 1]]) { now -= a[i - 1]; x.push_back(a[i - 1]); } else { x2.push_back(a[i - 1]); } } int iter = 0; for (auto i : x) { res[iter] = {i, 0}; iter += 2; } for (auto i : x2) { res[iter] = {-i, 0}; iter += 2; } } { sumb /= 2; vector<vector<bool>> dp(n + 1, vector<bool>(sumb + 1)); dp[0][0] = true; for (int i = 0; i < n; i++) { dp[i + 1] = dp[i]; for (int j = b[i]; j <= sumb; j++) { if (dp[i][j - b[i]]) { dp[i + 1][j] = true; } } } if (dp[n][sumb] == false) { cout << "No" << '\n'; return; } int now = sumb; vector<int> x, x2; for (int i = n; i > 0; i--) { if (now - b[i - 1] >= 0 && dp[i - 1][now - b[i - 1]]) { now -= b[i - 1]; x.push_back(b[i - 1]); } else { x2.push_back(b[i - 1]); } } int iter = 1; for (auto i : x) { res[iter] = {0, i}; iter += 2; } for (auto i : x2) { res[iter] = {0, -i}; iter += 2; } } cout << "Yes" << '\n'; int p = 0, q = 0; for (auto i : res) { p += i.first; q += i.second; cout << p << ' ' << q << '\n'; } } int main() { ios_base::sync_with_stdio(false); int tt; cin >> tt; for (int i = 1; i <= tt; i++) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<int> solveProblem(bool order) { int h, sum = 0, targetWeight; int l[1010]; cin >> h; for (int i = 0; i < h; i++) { cin >> l[i]; sum += l[i]; } if (sum % 2 == 1) return {}; targetWeight = sum / 2; vector<bitset<1010 * 1010>> package(h + 1); package[0].set(0); for (int i = 0; i < h; i++) { package[i + 1] = package[i] | (package[i] << l[i]); } if (!package[h].test(targetWeight)) return {}; vector<int> x, y; for (int i = h - 1; i >= 0; i--) { if (package[i].test(targetWeight)) y.emplace_back(l[i]); else { targetWeight -= l[i]; x.emplace_back(l[i]); } } sort(x.begin(), x.end()); sort(y.begin(), y.end()); if (order) { reverse(x.begin(), x.end()); reverse(y.begin(), y.end()); } if (x.size() < y.size()) swap(x, y); if (order) swap(x, y); for (int i = 0; i < y.size(); i++) y[i] = -y[i]; x.insert(x.end(), y.begin(), y.end()); return x; } int work() { auto a = solveProblem(true); auto b = solveProblem(false); if (a.size() != b.size() || a.size() == 0) { cout << "No" << endl; return 0; } int x = 0, y = 0; cout << "Yes" << endl; auto ita = a.begin(); auto itb = b.begin(); while (itb != b.end()) { x += *(ita++); cout << x << " " << y << endl; y += *(itb++); cout << x << " " << y << endl; } return 0; } int main() { int t; cin >> t; while (t--) { work(); } 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; vector<int> solveProblem(bool order) { int h, sum = 0, targetWeight; int l[1010]; cin >> h; for (int i = 0; i < h; i++) { cin >> l[i]; sum += l[i]; } if (sum % 2 == 1) return {}; targetWeight = sum / 2; vector<bitset<1010 * 1010>> package(h + 1); package[0].set(0); for (int i = 0; i < h; i++) { package[i + 1] = package[i] | (package[i] << l[i]); } if (!package[h].test(targetWeight)) return {}; vector<int> x, y; for (int i = h - 1; i >= 0; i--) { if (package[i].test(targetWeight)) y.emplace_back(l[i]); else { targetWeight -= l[i]; x.emplace_back(l[i]); } } sort(x.begin(), x.end()); sort(y.begin(), y.end()); if (order) { reverse(x.begin(), x.end()); reverse(y.begin(), y.end()); } if (x.size() < y.size()) swap(x, y); if (order) swap(x, y); for (int i = 0; i < y.size(); i++) y[i] = -y[i]; x.insert(x.end(), y.begin(), y.end()); return x; } int work() { auto a = solveProblem(true); auto b = solveProblem(false); if (a.size() != b.size() || a.size() == 0) { cout << "No" << endl; return 0; } int x = 0, y = 0; cout << "Yes" << endl; auto ita = a.begin(); auto itb = b.begin(); while (itb != b.end()) { x += *(ita++); cout << x << " " << y << endl; y += *(itb++); cout << x << " " << y << endl; } return 0; } int main() { int t; cin >> t; while (t--) { work(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int a[1005], b[1005], d[2][2][1005], D[2][1005]; int n, m, t[2][2], I, s1, s2, x, y, t2[2]; bitset<1000001> f[1005]; bool bz, flg; void work(int *a, int I, int s) { for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (f[n][s]) { for (int 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 (flg) printf("%d %d\n", y, x); else printf("%d %d\n", x, y); } int main() { int T; scanf("%d", &T); f[0][0] = 1; while (T--) { s1 = s2 = bz = flg = t[0][0] = t[0][1] = t[1][0] = t[1][1] = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s1 += a[i]; scanf("%d", &m); for (int 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 = flg = t[0][0] > t[1][0]; x = y = t2[0] = t2[1] = 0; for (int i = 1; i <= t[I][0]; i++) D[0][++t2[0]] = d[I][0][i]; for (int i = 1; i <= t[I][1]; i++) D[0][++t2[0]] = -d[I][1][i]; for (int i = t[I ^ 1][0]; i >= 1; i--) D[1][++t2[1]] = d[I ^ 1][0][i]; for (int i = t[I ^ 1][1]; i >= 1; i--) D[1][++t2[1]] = -d[I ^ 1][1][i]; for (int i = 1; i <= n; i++) { x += D[0][i], Write(); y += D[1][i], Write(); } } }
### 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 a[1005], b[1005], d[2][2][1005], D[2][1005]; int n, m, t[2][2], I, s1, s2, x, y, t2[2]; bitset<1000001> f[1005]; bool bz, flg; void work(int *a, int I, int s) { for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (f[n][s]) { for (int 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 (flg) printf("%d %d\n", y, x); else printf("%d %d\n", x, y); } int main() { int T; scanf("%d", &T); f[0][0] = 1; while (T--) { s1 = s2 = bz = flg = t[0][0] = t[0][1] = t[1][0] = t[1][1] = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s1 += a[i]; scanf("%d", &m); for (int 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 = flg = t[0][0] > t[1][0]; x = y = t2[0] = t2[1] = 0; for (int i = 1; i <= t[I][0]; i++) D[0][++t2[0]] = d[I][0][i]; for (int i = 1; i <= t[I][1]; i++) D[0][++t2[0]] = -d[I][1][i]; for (int i = t[I ^ 1][0]; i >= 1; i--) D[1][++t2[1]] = d[I ^ 1][0][i]; for (int i = t[I ^ 1][1]; i >= 1; i--) D[1][++t2[1]] = -d[I ^ 1][1][i]; for (int i = 1; i <= n; i++) { x += D[0][i], Write(); y += D[1][i], Write(); } } } ```
#include <bits/stdc++.h> using namespace std; const int N = 1010; int n, m, a[N], b[N], flag, Flag; int t[2][2], s1, s2, d[2][2][N], c[2][N]; int x, y, t2[2]; bitset<1000005> f[N]; void work(int *a, int I, int s) { for (int i = 1; i <= n; ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (f[n][s]) { for (int 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 flag = 1; } int I; int main() { int T; scanf("%d", &T); f[0][0] = 1; while (T--) { t[0][0] = t[0][1] = t[1][0] = t[1][1] = 0; Flag = flag = s1 = s2 = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), s1 += a[i]; scanf("%d", &m); for (int i = 1; i <= m; ++i) scanf("%d", &b[i]), s2 += b[i]; sort(a + 1, a + n + 1), sort(b + 1, b + m + 1); if ((s1 & 1) || (s2 & 1) || (n != m)) { puts("No"); continue; } s1 >>= 1, s2 >>= 1; work(a, 0, s1); work(b, 1, s2); if (flag) { puts("No"); continue; } I = Flag = (t[0][0] > t[1][0]); x = y = t2[0] = t2[1] = 0; for (int i = 1; i <= t[I][0]; ++i) c[0][++t2[0]] = d[I][0][i]; for (int i = 1; i <= t[I][1]; ++i) c[0][++t2[0]] = -d[I][1][i]; for (int i = t[I ^ 1][0]; i >= 1; --i) c[1][++t2[1]] = d[I ^ 1][0][i]; for (int i = t[I ^ 1][1]; i >= 1; --i) c[1][++t2[1]] = -d[I ^ 1][1][i]; puts("Yes"); for (int i = 1; i <= n; ++i) { x += c[0][i]; if (Flag) { printf("%d %d\n", y, x); } else printf("%d %d\n", x, y); y += c[1][i]; if (Flag) { printf("%d %d\n", y, x); } else { printf("%d %d\n", x, y); } } } 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 N = 1010; int n, m, a[N], b[N], flag, Flag; int t[2][2], s1, s2, d[2][2][N], c[2][N]; int x, y, t2[2]; bitset<1000005> f[N]; void work(int *a, int I, int s) { for (int i = 1; i <= n; ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]); if (f[n][s]) { for (int 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 flag = 1; } int I; int main() { int T; scanf("%d", &T); f[0][0] = 1; while (T--) { t[0][0] = t[0][1] = t[1][0] = t[1][1] = 0; Flag = flag = s1 = s2 = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), s1 += a[i]; scanf("%d", &m); for (int i = 1; i <= m; ++i) scanf("%d", &b[i]), s2 += b[i]; sort(a + 1, a + n + 1), sort(b + 1, b + m + 1); if ((s1 & 1) || (s2 & 1) || (n != m)) { puts("No"); continue; } s1 >>= 1, s2 >>= 1; work(a, 0, s1); work(b, 1, s2); if (flag) { puts("No"); continue; } I = Flag = (t[0][0] > t[1][0]); x = y = t2[0] = t2[1] = 0; for (int i = 1; i <= t[I][0]; ++i) c[0][++t2[0]] = d[I][0][i]; for (int i = 1; i <= t[I][1]; ++i) c[0][++t2[0]] = -d[I][1][i]; for (int i = t[I ^ 1][0]; i >= 1; --i) c[1][++t2[1]] = d[I ^ 1][0][i]; for (int i = t[I ^ 1][1]; i >= 1; --i) c[1][++t2[1]] = -d[I ^ 1][1][i]; puts("Yes"); for (int i = 1; i <= n; ++i) { x += c[0][i]; if (Flag) { printf("%d %d\n", y, x); } else printf("%d %d\n", x, y); y += c[1][i]; if (Flag) { printf("%d %d\n", y, x); } else { printf("%d %d\n", x, y); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int t; vector<int> h, v; vector<int> half_sum(vector<int> &v) { int n = (int)v.size(), s = 0; for (int i = 0; i < n; i++) s += v[i]; if (s & 1) return vector<int>{-1}; bitset<1000001> b[n + 1]; b[0].set(0); for (int i = 0; i < n; i++) b[i + 1] = b[i] | (b[i] << v[i]); if (!b[n].test(s / 2)) return vector<int>{-1}; int cur = s / 2; vector<int> tmp; for (int i = n - 1; i >= 0; i--) { if (cur >= v[i] && b[i].test(cur - v[i])) { tmp.push_back(i); cur -= v[i]; } } return tmp; } int main() { scanf("%d", &t); while (t--) { int n, m; h.clear(); v.clear(); scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); h.push_back(x); } scanf("%d", &m); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); v.push_back(x); } if (n != m) { puts("No"); continue; } vector<int> lh = half_sum(h); vector<int> lv = half_sum(v); if (lh[0] == -1 || lv[0] == -1) { puts("No"); continue; } vector<int> rh, rv; sort(lh.begin(), lh.end()); sort(lv.begin(), lv.end()); int cur = 0; for (int i = 0; i < n; i++) { if (cur < (int)lh.size() && lh[cur] == i) cur++; else rh.push_back(i); } cur = 0; for (int i = 0; i < m; i++) { if (cur < (int)lv.size() && lv[cur] == i) cur++; else rv.push_back(i); } puts("Yes"); if (lv.size() > rv.size()) swap(lv, rv); if (lh.size() < rh.size()) swap(lh, rh); for (int i = 0; i < (int)lv.size(); i++) lv[i] = v[lv[i]]; for (int i = 0; i < (int)rv.size(); i++) rv[i] = v[rv[i]]; for (int i = 0; i < (int)lh.size(); i++) lh[i] = h[lh[i]]; for (int i = 0; i < (int)rh.size(); i++) rh[i] = h[rh[i]]; sort(lv.begin(), lv.end(), greater<int>()); sort(lh.begin(), lh.end()); sort(rv.begin(), rv.end(), greater<int>()); sort(rh.begin(), rh.end()); assert(lv.size() + rv.size() == lh.size() + rh.size()); int nx = 0, ny = 0; for (int i = 0; i < (int)lv.size(); i++) { ny += lv[i]; printf("%d %d\n", nx, ny); nx += lh[i]; printf("%d %d\n", nx, ny); } for (int i = 0; i < (int)lh.size() - (int)lv.size(); i++) { ny -= rv[i]; printf("%d %d\n", nx, ny); nx += lh[i + (int)lv.size()]; printf("%d %d\n", nx, ny); } for (int i = 0; i < (int)rh.size(); i++) { ny -= rv[i + (int)lh.size() - (int)lv.size()]; printf("%d %d\n", nx, ny); nx -= rh[i]; printf("%d %d\n", nx, ny); } } 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 namespace std; int t; vector<int> h, v; vector<int> half_sum(vector<int> &v) { int n = (int)v.size(), s = 0; for (int i = 0; i < n; i++) s += v[i]; if (s & 1) return vector<int>{-1}; bitset<1000001> b[n + 1]; b[0].set(0); for (int i = 0; i < n; i++) b[i + 1] = b[i] | (b[i] << v[i]); if (!b[n].test(s / 2)) return vector<int>{-1}; int cur = s / 2; vector<int> tmp; for (int i = n - 1; i >= 0; i--) { if (cur >= v[i] && b[i].test(cur - v[i])) { tmp.push_back(i); cur -= v[i]; } } return tmp; } int main() { scanf("%d", &t); while (t--) { int n, m; h.clear(); v.clear(); scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); h.push_back(x); } scanf("%d", &m); for (int i = 0; i < m; i++) { int x; scanf("%d", &x); v.push_back(x); } if (n != m) { puts("No"); continue; } vector<int> lh = half_sum(h); vector<int> lv = half_sum(v); if (lh[0] == -1 || lv[0] == -1) { puts("No"); continue; } vector<int> rh, rv; sort(lh.begin(), lh.end()); sort(lv.begin(), lv.end()); int cur = 0; for (int i = 0; i < n; i++) { if (cur < (int)lh.size() && lh[cur] == i) cur++; else rh.push_back(i); } cur = 0; for (int i = 0; i < m; i++) { if (cur < (int)lv.size() && lv[cur] == i) cur++; else rv.push_back(i); } puts("Yes"); if (lv.size() > rv.size()) swap(lv, rv); if (lh.size() < rh.size()) swap(lh, rh); for (int i = 0; i < (int)lv.size(); i++) lv[i] = v[lv[i]]; for (int i = 0; i < (int)rv.size(); i++) rv[i] = v[rv[i]]; for (int i = 0; i < (int)lh.size(); i++) lh[i] = h[lh[i]]; for (int i = 0; i < (int)rh.size(); i++) rh[i] = h[rh[i]]; sort(lv.begin(), lv.end(), greater<int>()); sort(lh.begin(), lh.end()); sort(rv.begin(), rv.end(), greater<int>()); sort(rh.begin(), rh.end()); assert(lv.size() + rv.size() == lh.size() + rh.size()); int nx = 0, ny = 0; for (int i = 0; i < (int)lv.size(); i++) { ny += lv[i]; printf("%d %d\n", nx, ny); nx += lh[i]; printf("%d %d\n", nx, ny); } for (int i = 0; i < (int)lh.size() - (int)lv.size(); i++) { ny -= rv[i]; printf("%d %d\n", nx, ny); nx += lh[i + (int)lv.size()]; printf("%d %d\n", nx, ny); } for (int i = 0; i < (int)rh.size(); i++) { ny -= rv[i + (int)lh.size() - (int)lv.size()]; printf("%d %d\n", nx, ny); nx -= rh[i]; printf("%d %d\n", nx, ny); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int T, n, m, s1, s2, a[1003], b[1003]; bitset<1000003> f[1003]; int d[2][2][1003], cnt[2][2]; int D[2][1003]; int t[2]; bool crok; void sol() { scanf("%d", &n); s1 = s2 = 0; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s1 += a[i]; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]), s2 += b[i]; if ((s1 & 1) || (s2 & 1) || (n != m)) { puts("No"); return; } memset(cnt, 0, sizeof(cnt)); sort(a + 1, a + n + 1), sort(b + 1, b + n + 1); 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][s1 >> 1]) { puts("No"); return; } for (int i = n, s = s1 >> 1; i; i--) { if (s >= a[i] && f[i - 1][s - a[i]]) s -= a[i], d[0][0][++cnt[0][0]] = a[i]; else d[0][1][++cnt[0][1]] = a[i]; } f[0].reset(), f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | f[i - 1] << b[i]; if (!f[n][s2 >> 1]) { puts("No"); return; } for (int i = n, s = s2 >> 1; i; i--) { if (s >= b[i] && f[i - 1][s - b[i]]) s -= b[i], d[1][0][++cnt[1][0]] = b[i]; else d[1][1][++cnt[1][1]] = b[i]; } t[0] = t[1] = 0; crok = cnt[0][0] > cnt[1][0]; for (int i = 1; i <= cnt[crok][0]; i++) D[0][++t[0]] = d[crok][0][i]; for (int i = 1; i <= cnt[crok][1]; i++) D[0][++t[0]] = -d[crok][1][i]; for (int i = cnt[crok ^ 1][0]; i; i--) D[1][++t[1]] = d[crok ^ 1][0][i]; for (int i = cnt[crok ^ 1][1]; i; i--) D[1][++t[1]] = -d[crok ^ 1][1][i]; int X = 0, Y = 0; puts("Yes"); for (int i = 1; i <= n; i++) { X += D[0][i]; if (!crok) printf("%d %d\n", X, Y); else printf("%d %d\n", Y, X); Y += D[1][i]; if (!crok) printf("%d %d\n", X, Y); else printf("%d %d\n", Y, X); } } int main() { scanf("%d", &T); while (T--) sol(); }
### 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, n, m, s1, s2, a[1003], b[1003]; bitset<1000003> f[1003]; int d[2][2][1003], cnt[2][2]; int D[2][1003]; int t[2]; bool crok; void sol() { scanf("%d", &n); s1 = s2 = 0; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), s1 += a[i]; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]), s2 += b[i]; if ((s1 & 1) || (s2 & 1) || (n != m)) { puts("No"); return; } memset(cnt, 0, sizeof(cnt)); sort(a + 1, a + n + 1), sort(b + 1, b + n + 1); 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][s1 >> 1]) { puts("No"); return; } for (int i = n, s = s1 >> 1; i; i--) { if (s >= a[i] && f[i - 1][s - a[i]]) s -= a[i], d[0][0][++cnt[0][0]] = a[i]; else d[0][1][++cnt[0][1]] = a[i]; } f[0].reset(), f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | f[i - 1] << b[i]; if (!f[n][s2 >> 1]) { puts("No"); return; } for (int i = n, s = s2 >> 1; i; i--) { if (s >= b[i] && f[i - 1][s - b[i]]) s -= b[i], d[1][0][++cnt[1][0]] = b[i]; else d[1][1][++cnt[1][1]] = b[i]; } t[0] = t[1] = 0; crok = cnt[0][0] > cnt[1][0]; for (int i = 1; i <= cnt[crok][0]; i++) D[0][++t[0]] = d[crok][0][i]; for (int i = 1; i <= cnt[crok][1]; i++) D[0][++t[0]] = -d[crok][1][i]; for (int i = cnt[crok ^ 1][0]; i; i--) D[1][++t[1]] = d[crok ^ 1][0][i]; for (int i = cnt[crok ^ 1][1]; i; i--) D[1][++t[1]] = -d[crok ^ 1][1][i]; int X = 0, Y = 0; puts("Yes"); for (int i = 1; i <= n; i++) { X += D[0][i]; if (!crok) printf("%d %d\n", X, Y); else printf("%d %d\n", Y, X); Y += D[1][i]; if (!crok) printf("%d %d\n", X, Y); else printf("%d %d\n", Y, X); } } int main() { scanf("%d", &T); while (T--) sol(); } ```
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &num) { T x = 0, f = 1; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') f = -1; for (; ch <= '9' && ch >= '0'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ '0'); num = x * f; } int ttt, n, m, a[2005], a1[2005], a2[2005], b[2005], b1[2005], b2[2005], t1[2005], t2[2005], suma, sumb, X, Y; int ans[2005 << 1][2], cnt; bitset<1000005> dp[1005]; bool chk(int *c, int *c1, int *c2, int sumc) { for (int i = 1; i <= n; i++) dp[i].reset(); dp[0][0] = 1; for (int i = 1; i <= n; i++) dp[i] = (dp[i - 1] << c[i]) | dp[i - 1]; if (!dp[n][sumc]) return 1; c1[0] = c2[0] = 0; for (int i = n; i; i--) { if (sumc >= c[i] && dp[i - 1][sumc - c[i]]) c1[++c1[0]] = c[i], sumc -= c[i]; else c2[++c2[0]] = c[i]; } return 0; } void solve(int *c1, int *c2, int d1, int d2) { int l = c1[0]; if (!l) return; sort(c1 + 1, c1 + l + 1, greater<int>()); sort(c2 + 1, c2 + l + 1); for (int i = 1; i <= l; i++) { X += c1[i] * d1; ans[++cnt][0] = X; ans[cnt][1] = Y; Y += c2[i] * d2; ans[++cnt][0] = X; ans[cnt][1] = Y; } } bool flag = 0; int main() { read(ttt); while (ttt--) { suma = sumb = 0; read(n); for (int i = 1; i <= n; i++) read(a[i]), suma += a[i]; read(m); for (int i = 1; i <= m; i++) read(b[i]), sumb += b[i]; if (n == 375 && a[1] == 8) flag = 1; if (n != m || (suma & 1) || (sumb & 1)) { puts("NO"); continue; } if (chk(a, a1, a2, suma >> 1) || chk(b, b1, b2, sumb >> 1)) { puts("NO"); continue; } puts("YES"); if (a1[0] > a2[0]) swap(a1, a2); if (b1[0] < b2[0]) swap(b1, b2); X = Y = cnt = 0; t1[0] = t2[0] = 0; for (int i = 1; i <= a1[0]; i++) { t1[++t1[0]] = a1[i]; t2[++t2[0]] = b1[i]; } solve(t1, t2, 1, 1); t1[0] = t2[0] = 0; for (int i = 1; i <= b1[0] - a1[0]; i++) { t1[++t1[0]] = a2[i]; t2[++t2[0]] = b1[i + a1[0]]; } solve(t1, t2, -1, 1); t1[0] = t2[0] = 0; for (int i = 1; i <= b2[0]; i++) { t1[++t1[0]] = a2[i + b1[0] - a1[0]]; t2[++t2[0]] = b2[i]; } solve(t1, t2, -1, -1); for (int i = 1; i <= cnt; i++) { printf("%d %d\n", ans[i][0], ans[i][1]); } } 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; template <typename T> inline void read(T &num) { T x = 0, f = 1; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') f = -1; for (; ch <= '9' && ch >= '0'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ '0'); num = x * f; } int ttt, n, m, a[2005], a1[2005], a2[2005], b[2005], b1[2005], b2[2005], t1[2005], t2[2005], suma, sumb, X, Y; int ans[2005 << 1][2], cnt; bitset<1000005> dp[1005]; bool chk(int *c, int *c1, int *c2, int sumc) { for (int i = 1; i <= n; i++) dp[i].reset(); dp[0][0] = 1; for (int i = 1; i <= n; i++) dp[i] = (dp[i - 1] << c[i]) | dp[i - 1]; if (!dp[n][sumc]) return 1; c1[0] = c2[0] = 0; for (int i = n; i; i--) { if (sumc >= c[i] && dp[i - 1][sumc - c[i]]) c1[++c1[0]] = c[i], sumc -= c[i]; else c2[++c2[0]] = c[i]; } return 0; } void solve(int *c1, int *c2, int d1, int d2) { int l = c1[0]; if (!l) return; sort(c1 + 1, c1 + l + 1, greater<int>()); sort(c2 + 1, c2 + l + 1); for (int i = 1; i <= l; i++) { X += c1[i] * d1; ans[++cnt][0] = X; ans[cnt][1] = Y; Y += c2[i] * d2; ans[++cnt][0] = X; ans[cnt][1] = Y; } } bool flag = 0; int main() { read(ttt); while (ttt--) { suma = sumb = 0; read(n); for (int i = 1; i <= n; i++) read(a[i]), suma += a[i]; read(m); for (int i = 1; i <= m; i++) read(b[i]), sumb += b[i]; if (n == 375 && a[1] == 8) flag = 1; if (n != m || (suma & 1) || (sumb & 1)) { puts("NO"); continue; } if (chk(a, a1, a2, suma >> 1) || chk(b, b1, b2, sumb >> 1)) { puts("NO"); continue; } puts("YES"); if (a1[0] > a2[0]) swap(a1, a2); if (b1[0] < b2[0]) swap(b1, b2); X = Y = cnt = 0; t1[0] = t2[0] = 0; for (int i = 1; i <= a1[0]; i++) { t1[++t1[0]] = a1[i]; t2[++t2[0]] = b1[i]; } solve(t1, t2, 1, 1); t1[0] = t2[0] = 0; for (int i = 1; i <= b1[0] - a1[0]; i++) { t1[++t1[0]] = a2[i]; t2[++t2[0]] = b1[i + a1[0]]; } solve(t1, t2, -1, 1); t1[0] = t2[0] = 0; for (int i = 1; i <= b2[0]; i++) { t1[++t1[0]] = a2[i + b1[0] - a1[0]]; t2[++t2[0]] = b2[i]; } solve(t1, t2, -1, -1); for (int i = 1; i <= cnt; i++) { printf("%d %d\n", ans[i][0], ans[i][1]); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; bitset<500003> dph[503], dpu[503]; int NH, NU, H[1003], ph[1003], U[1003], pu[1003], T; int main() { for (cin >> T; T; --T) { cin >> NH; for (int i = 1; i <= NH; ++i) cin >> H[i]; cin >> NU; for (int i = 1; i <= NU; ++i) cin >> U[i]; if (NH != NU) { cout << "No" << endl; continue; } int sum = 0; for (int i = 1; i <= NH; ++i) sum += H[i]; dph[0].reset(); dph[0].set(0); for (int i = 1; i <= NH; ++i) dph[i] = dph[i - 1] | (dph[i - 1] << H[i]); if ((sum & 1) || !dph[NH][sum / 2]) { cout << "No" << endl; continue; } int cur = sum / 2; for (int i = NH; i; --i) if (!dph[i - 1][cur]) { ph[i] = 1; cur -= H[i]; } else ph[i] = 0; sum = 0; for (int i = 1; i <= NU; ++i) sum += U[i]; dpu[0].reset(); dpu[0].set(0); for (int i = 1; i <= NU; ++i) dpu[i] = dpu[i - 1] | (dpu[i - 1] << U[i]); if ((sum & 1) || !dpu[NU][sum / 2]) { cout << "No" << endl; continue; } cur = sum / 2; for (int i = NU; i; --i) if (!dpu[i - 1][cur]) { pu[i] = 1; cur -= U[i]; } else pu[i] = 0; vector<int> ph0, ph1, pu0, pu1; for (int i = 1; i <= NH; ++i) (ph[i] ? ph1 : ph0).push_back(H[i]); for (int i = 1; i <= NU; ++i) (pu[i] ? pu1 : pu0).push_back(U[i]); sort(ph0.begin(), ph0.end()); sort(ph1.begin(), ph1.end()); sort(pu0.begin(), pu0.end()); sort(pu1.begin(), pu1.end()); vector<pair<int, int> > pot; if (ph0.size() < pu0.size()) { reverse(pu0.begin(), pu0.end()); reverse(pu1.begin(), pu1.end()); int x = 0, y = 0; for (int i = 0; i < NH + NU; ++i) { if (!(i & 1)) { int X; if (ph0.size()) { X = ph0.back(); ph0.pop_back(); } else { X = -ph1.back(); ph1.pop_back(); } x += X; } else { int Y; if (pu0.size()) { Y = pu0.back(); pu0.pop_back(); } else { Y = -pu1.back(); pu1.pop_back(); } y += Y; } pot.push_back(make_pair(x, y)); } } else { reverse(ph0.begin(), ph0.end()); reverse(ph1.begin(), ph1.end()); int x = 0, y = 0; for (int i = 0; i < NH + NU; ++i) { if ((i & 1)) { int X; if (ph0.size()) { X = ph0.back(); ph0.pop_back(); } else { X = -ph1.back(); ph1.pop_back(); } x += X; } else { int Y; if (pu0.size()) { Y = pu0.back(); pu0.pop_back(); } else { Y = -pu1.back(); pu1.pop_back(); } y += Y; } pot.push_back(make_pair(x, y)); } } cout << "Yes" << endl; for (auto t : pot) cout << t.first << ' ' << t.second << endl; } 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; bitset<500003> dph[503], dpu[503]; int NH, NU, H[1003], ph[1003], U[1003], pu[1003], T; int main() { for (cin >> T; T; --T) { cin >> NH; for (int i = 1; i <= NH; ++i) cin >> H[i]; cin >> NU; for (int i = 1; i <= NU; ++i) cin >> U[i]; if (NH != NU) { cout << "No" << endl; continue; } int sum = 0; for (int i = 1; i <= NH; ++i) sum += H[i]; dph[0].reset(); dph[0].set(0); for (int i = 1; i <= NH; ++i) dph[i] = dph[i - 1] | (dph[i - 1] << H[i]); if ((sum & 1) || !dph[NH][sum / 2]) { cout << "No" << endl; continue; } int cur = sum / 2; for (int i = NH; i; --i) if (!dph[i - 1][cur]) { ph[i] = 1; cur -= H[i]; } else ph[i] = 0; sum = 0; for (int i = 1; i <= NU; ++i) sum += U[i]; dpu[0].reset(); dpu[0].set(0); for (int i = 1; i <= NU; ++i) dpu[i] = dpu[i - 1] | (dpu[i - 1] << U[i]); if ((sum & 1) || !dpu[NU][sum / 2]) { cout << "No" << endl; continue; } cur = sum / 2; for (int i = NU; i; --i) if (!dpu[i - 1][cur]) { pu[i] = 1; cur -= U[i]; } else pu[i] = 0; vector<int> ph0, ph1, pu0, pu1; for (int i = 1; i <= NH; ++i) (ph[i] ? ph1 : ph0).push_back(H[i]); for (int i = 1; i <= NU; ++i) (pu[i] ? pu1 : pu0).push_back(U[i]); sort(ph0.begin(), ph0.end()); sort(ph1.begin(), ph1.end()); sort(pu0.begin(), pu0.end()); sort(pu1.begin(), pu1.end()); vector<pair<int, int> > pot; if (ph0.size() < pu0.size()) { reverse(pu0.begin(), pu0.end()); reverse(pu1.begin(), pu1.end()); int x = 0, y = 0; for (int i = 0; i < NH + NU; ++i) { if (!(i & 1)) { int X; if (ph0.size()) { X = ph0.back(); ph0.pop_back(); } else { X = -ph1.back(); ph1.pop_back(); } x += X; } else { int Y; if (pu0.size()) { Y = pu0.back(); pu0.pop_back(); } else { Y = -pu1.back(); pu1.pop_back(); } y += Y; } pot.push_back(make_pair(x, y)); } } else { reverse(ph0.begin(), ph0.end()); reverse(ph1.begin(), ph1.end()); int x = 0, y = 0; for (int i = 0; i < NH + NU; ++i) { if ((i & 1)) { int X; if (ph0.size()) { X = ph0.back(); ph0.pop_back(); } else { X = -ph1.back(); ph1.pop_back(); } x += X; } else { int Y; if (pu0.size()) { Y = pu0.back(); pu0.pop_back(); } else { Y = -pu1.back(); pu1.pop_back(); } y += Y; } pot.push_back(make_pair(x, y)); } } cout << "Yes" << endl; for (auto t : pot) cout << t.first << ' ' << t.second << endl; } return 0; } ```
#include <bits/stdc++.h> const int MAXN = 1000 + 5; const int MAXM = 1e6 + 5; int n, m; int a[MAXN], b[MAXN]; std::vector<int> L, R, U, D; std::bitset<MAXM> f[MAXN]; inline bool work(int a[], std::vector<int> &v1, std::vector<int> &v2) { f[0][0] = 1; v1.clear(); v2.clear(); int sm = 0; for (int i = 1; i <= n; ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]), sm += a[i]; if (sm & 1) return 0; sm >>= 1; if (!f[n][sm]) return 0; for (int i = n; i >= 1; --i) { if (sm - a[i] >= 0 && f[i - 1][sm - a[i]]) v1.push_back(a[i]), sm -= a[i]; else v2.push_back(a[i]); } if (v1.size() > v2.size()) std::swap(v1, v2); return 1; } inline 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; } bool flag = 1; flag &= work(a, R, L); flag &= work(b, U, D); if (!flag) { puts("No"); return; } puts("Yes"); if (R.size() > U.size()) { std::swap(R, L); std::swap(U, D); } std::sort(R.begin(), R.end()); std::sort(U.begin(), U.end()); std::reverse(R.begin(), R.end()); std::vector<std::pair<int, int> > ans, ansr; ans.push_back(std::make_pair(0, 0)); for (int i = 0; i <= (int)std::min(R.size(), U.size()) - 1; ++i) { int x = ans.back().first, y = ans.back().second; x += R[i]; ans.push_back(std::make_pair(x, y)); y += U[i]; ans.push_back(std::make_pair(x, y)); } std::sort(L.begin(), L.end()); std::sort(D.begin(), D.end()); std::reverse(D.begin(), D.end()); ansr.push_back(std::make_pair(0, 0)); for (int i = 0; i <= (int)std::min(L.size(), D.size()) - 1; ++i) { int x = ansr.back().first, y = ansr.back().second; y += D[i]; ansr.push_back(std::make_pair(x, y)); x += L[i]; ansr.push_back(std::make_pair(x, y)); } std::vector<int> h, l; int x = ans.back().first, y = ans.back().second; if (R.size() < U.size()) for (int i = std::min(R.size(), U.size()); i <= (int)U.size() - 1; ++i) l.push_back(U[i]); else for (int i = std::min(R.size(), U.size()); i <= (int)R.size() - 1; ++i) h.push_back(R[i]); if (L.size() < D.size()) for (int i = std::min(L.size(), D.size()); i <= (int)D.size() - 1; ++i) l.push_back(-D[i]); else for (int i = std::min(L.size(), D.size()); i <= (int)L.size() - 1; ++i) h.push_back(-L[i]); assert(h.size() == l.size()); if (R.size() < U.size() || 1) { for (int i = 0; i <= (int)h.size() - 1; ++i) { int x = ans.back().first, y = ans.back().second; x += h[i]; ans.push_back(std::make_pair(x, y)); y += l[i]; ans.push_back(std::make_pair(x, y)); } } else { assert(L.size() <= D.size()); std::cerr << "ans.back().fi" << '=' << ans.back().first << std::endl; std::cerr << "ans.back().se" << '=' << ans.back().second << std::endl; std::cerr << "ansr.back().fi" << '=' << ansr.back().first << std::endl; std::cerr << "ansr.back().se" << '=' << ansr.back().second << std::endl; for (auto x : l) std::cerr << "x" << '=' << x << std::endl; for (int i = 0; i <= (int)h.size() - 1; ++i) { int x = ansr.back().first, y = ansr.back().second; y -= l[i]; ansr.push_back(std::make_pair(x, y)); x -= h[i]; ansr.push_back(std::make_pair(x, y)); } } assert(ansr.back() == ans.back()); ansr.pop_back(); std::reverse(ansr.begin(), ansr.end()); std::reverse(ans.begin(), ans.end()); ans.pop_back(); std::reverse(ans.begin(), ans.end()); int t = ans.size(); for (auto x : ansr) ans.push_back(x); for (auto x : ans) printf("%d %d\n", x.first, x.second); } int 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> const int MAXN = 1000 + 5; const int MAXM = 1e6 + 5; int n, m; int a[MAXN], b[MAXN]; std::vector<int> L, R, U, D; std::bitset<MAXM> f[MAXN]; inline bool work(int a[], std::vector<int> &v1, std::vector<int> &v2) { f[0][0] = 1; v1.clear(); v2.clear(); int sm = 0; for (int i = 1; i <= n; ++i) f[i] = f[i - 1] | (f[i - 1] << a[i]), sm += a[i]; if (sm & 1) return 0; sm >>= 1; if (!f[n][sm]) return 0; for (int i = n; i >= 1; --i) { if (sm - a[i] >= 0 && f[i - 1][sm - a[i]]) v1.push_back(a[i]), sm -= a[i]; else v2.push_back(a[i]); } if (v1.size() > v2.size()) std::swap(v1, v2); return 1; } inline 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; } bool flag = 1; flag &= work(a, R, L); flag &= work(b, U, D); if (!flag) { puts("No"); return; } puts("Yes"); if (R.size() > U.size()) { std::swap(R, L); std::swap(U, D); } std::sort(R.begin(), R.end()); std::sort(U.begin(), U.end()); std::reverse(R.begin(), R.end()); std::vector<std::pair<int, int> > ans, ansr; ans.push_back(std::make_pair(0, 0)); for (int i = 0; i <= (int)std::min(R.size(), U.size()) - 1; ++i) { int x = ans.back().first, y = ans.back().second; x += R[i]; ans.push_back(std::make_pair(x, y)); y += U[i]; ans.push_back(std::make_pair(x, y)); } std::sort(L.begin(), L.end()); std::sort(D.begin(), D.end()); std::reverse(D.begin(), D.end()); ansr.push_back(std::make_pair(0, 0)); for (int i = 0; i <= (int)std::min(L.size(), D.size()) - 1; ++i) { int x = ansr.back().first, y = ansr.back().second; y += D[i]; ansr.push_back(std::make_pair(x, y)); x += L[i]; ansr.push_back(std::make_pair(x, y)); } std::vector<int> h, l; int x = ans.back().first, y = ans.back().second; if (R.size() < U.size()) for (int i = std::min(R.size(), U.size()); i <= (int)U.size() - 1; ++i) l.push_back(U[i]); else for (int i = std::min(R.size(), U.size()); i <= (int)R.size() - 1; ++i) h.push_back(R[i]); if (L.size() < D.size()) for (int i = std::min(L.size(), D.size()); i <= (int)D.size() - 1; ++i) l.push_back(-D[i]); else for (int i = std::min(L.size(), D.size()); i <= (int)L.size() - 1; ++i) h.push_back(-L[i]); assert(h.size() == l.size()); if (R.size() < U.size() || 1) { for (int i = 0; i <= (int)h.size() - 1; ++i) { int x = ans.back().first, y = ans.back().second; x += h[i]; ans.push_back(std::make_pair(x, y)); y += l[i]; ans.push_back(std::make_pair(x, y)); } } else { assert(L.size() <= D.size()); std::cerr << "ans.back().fi" << '=' << ans.back().first << std::endl; std::cerr << "ans.back().se" << '=' << ans.back().second << std::endl; std::cerr << "ansr.back().fi" << '=' << ansr.back().first << std::endl; std::cerr << "ansr.back().se" << '=' << ansr.back().second << std::endl; for (auto x : l) std::cerr << "x" << '=' << x << std::endl; for (int i = 0; i <= (int)h.size() - 1; ++i) { int x = ansr.back().first, y = ansr.back().second; y -= l[i]; ansr.push_back(std::make_pair(x, y)); x -= h[i]; ansr.push_back(std::make_pair(x, y)); } } assert(ansr.back() == ans.back()); ansr.pop_back(); std::reverse(ansr.begin(), ansr.end()); std::reverse(ans.begin(), ans.end()); ans.pop_back(); std::reverse(ans.begin(), ans.end()); int t = ans.size(); for (auto x : ansr) ans.push_back(x); for (auto x : ans) printf("%d %d\n", x.first, x.second); } int main() { int T; scanf("%d", &T); while (T--) Solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1000 + 7; bool ok[N * N]; int last[N * N]; vector<int> get(int n, int sum, int a[]) { for (int i = 0; i <= sum; i++) { last[i] = 0; ok[i] = 0; } ok[0] = 1; int cur = 0; for (int j = 1; j <= n; j++) { int x = a[j]; for (int i = min(sum - x, cur); i >= 0; i--) { if (ok[i] && !ok[i + x]) { ok[i + x] = 1; last[i + x] = j; } } cur += x; } if (ok[sum] == 0) { return {}; } vector<int> ret; while (sum) { ret.push_back(last[sum]); sum -= a[last[sum]]; } return ret; } int n; int a[N]; int b[N]; bool inside[N]; vector<int> complement(vector<int> a) { for (int i = 1; i <= n; i++) { inside[i] = 0; } for (auto &i : a) { inside[i] = 1; } a.clear(); for (int i = 1; i <= n; i++) { if (inside[i] == 0) { a.push_back(i); } } return a; } bool cmp(pair<int, int> f, pair<int, int> s) { long long fx = f.first, fy = f.second; long long sx = s.first, sy = s.second; return fx * sy < fy * sx; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int h, v; cin >> h; for (int i = 1; i <= h; i++) { cin >> a[i]; } cin >> v; for (int i = 1; i <= v; i++) { cin >> b[i]; } if (h != v) { cout << "No\n"; continue; } n = h; sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); int sum_a = 0, sum_b = 0; for (int i = 1; i <= n; i++) { sum_a += a[i]; sum_b += b[i]; } if (sum_a % 2 == 1 || sum_b % 2 == 1) { cout << "No\n"; continue; } vector<int> r = get(n, sum_a / 2, a), l; vector<int> d = get(n, sum_b / 2, b), u; if (r.empty() || d.empty()) { cout << "No\n"; continue; } l = complement(r); u = complement(d); if ((int)r.size() > (int)l.size()) { swap(r, l); } if ((int)d.size() > (int)u.size()) { swap(d, u); } vector<pair<int, int>> ru; vector<pair<int, int>> lu; vector<pair<int, int>> ld; for (auto &i : r) { ru.push_back({a[i], b[u.back()]}); u.pop_back(); } for (auto &i : l) { if (!u.empty()) { lu.push_back({a[i], b[u.back()]}); u.pop_back(); } else { ld.push_back({a[i], b[d.back()]}); d.pop_back(); } } sort(ru.begin(), ru.end(), cmp); sort(ld.begin(), ld.end(), cmp); cout << "Yes\n"; int x = 0, y = 0; for (int i = 1; i <= n; i++) { cout << x << " " << y << "\n"; if (!ru.empty()) { x -= ru.back().first; cout << x << " " << y << "\n"; y -= ru.back().second; ru.pop_back(); continue; } if (!lu.empty()) { x += lu.back().first; cout << x << " " << y << "\n"; y -= lu.back().second; lu.pop_back(); continue; } if (!ld.empty()) { x += ld.back().first; cout << x << " " << y << "\n"; y += ld.back().second; ld.pop_back(); continue; } assert(0); } assert(x == 0 && y == 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 N = 1000 + 7; bool ok[N * N]; int last[N * N]; vector<int> get(int n, int sum, int a[]) { for (int i = 0; i <= sum; i++) { last[i] = 0; ok[i] = 0; } ok[0] = 1; int cur = 0; for (int j = 1; j <= n; j++) { int x = a[j]; for (int i = min(sum - x, cur); i >= 0; i--) { if (ok[i] && !ok[i + x]) { ok[i + x] = 1; last[i + x] = j; } } cur += x; } if (ok[sum] == 0) { return {}; } vector<int> ret; while (sum) { ret.push_back(last[sum]); sum -= a[last[sum]]; } return ret; } int n; int a[N]; int b[N]; bool inside[N]; vector<int> complement(vector<int> a) { for (int i = 1; i <= n; i++) { inside[i] = 0; } for (auto &i : a) { inside[i] = 1; } a.clear(); for (int i = 1; i <= n; i++) { if (inside[i] == 0) { a.push_back(i); } } return a; } bool cmp(pair<int, int> f, pair<int, int> s) { long long fx = f.first, fy = f.second; long long sx = s.first, sy = s.second; return fx * sy < fy * sx; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int h, v; cin >> h; for (int i = 1; i <= h; i++) { cin >> a[i]; } cin >> v; for (int i = 1; i <= v; i++) { cin >> b[i]; } if (h != v) { cout << "No\n"; continue; } n = h; sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); int sum_a = 0, sum_b = 0; for (int i = 1; i <= n; i++) { sum_a += a[i]; sum_b += b[i]; } if (sum_a % 2 == 1 || sum_b % 2 == 1) { cout << "No\n"; continue; } vector<int> r = get(n, sum_a / 2, a), l; vector<int> d = get(n, sum_b / 2, b), u; if (r.empty() || d.empty()) { cout << "No\n"; continue; } l = complement(r); u = complement(d); if ((int)r.size() > (int)l.size()) { swap(r, l); } if ((int)d.size() > (int)u.size()) { swap(d, u); } vector<pair<int, int>> ru; vector<pair<int, int>> lu; vector<pair<int, int>> ld; for (auto &i : r) { ru.push_back({a[i], b[u.back()]}); u.pop_back(); } for (auto &i : l) { if (!u.empty()) { lu.push_back({a[i], b[u.back()]}); u.pop_back(); } else { ld.push_back({a[i], b[d.back()]}); d.pop_back(); } } sort(ru.begin(), ru.end(), cmp); sort(ld.begin(), ld.end(), cmp); cout << "Yes\n"; int x = 0, y = 0; for (int i = 1; i <= n; i++) { cout << x << " " << y << "\n"; if (!ru.empty()) { x -= ru.back().first; cout << x << " " << y << "\n"; y -= ru.back().second; ru.pop_back(); continue; } if (!lu.empty()) { x += lu.back().first; cout << x << " " << y << "\n"; y -= lu.back().second; lu.pop_back(); continue; } if (!ld.empty()) { x += ld.back().first; cout << x << " " << y << "\n"; y += ld.back().second; ld.pop_back(); continue; } assert(0); } assert(x == 0 && y == 0); } } ```
#include <bits/stdc++.h> using namespace std; const int MAX = 501; const int MAXS = MAX * 1000; bitset<MAXS> dp[MAX]; int n[2]; int a[2][MAX]; bool b[2][MAX]; bool calcDP(int idx) { dp[0][0] = true; int s = 0; for (int i = (0); i < (n[idx]); ++i) { dp[i + 1] = dp[i] | (dp[i] << a[idx][i]); s += a[idx][i]; } if ((s & 1) || !dp[n[idx]][s >> 1]) return false; s >>= 1; for (int i = (n[idx]) - 1; i >= (0); --i) { if (s >= a[idx][i] && dp[i][s - a[idx][i]]) { s -= a[idx][i]; b[idx][i] = true; } else { b[idx][i] = false; } } return true; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { for (int it = (0); it < (2); ++it) { cin >> n[it]; for (int i = (0); i < (n[it]); ++i) cin >> a[it][min(i, MAX - 1)]; } if (n[0] != n[1] || !calcDP(0) || !calcDP(1)) { cout << "No\n"; continue; } for (int it = (0); it < (2); ++it) { int cntP = 0; for (int i = (0); i < (n[it]); ++i) if (b[it][i]) a[it][i] *= -1; else cntP++; if ((2 * cntP > n[0]) ^ it) for (int i = (0); i < (n[0]); ++i) a[it][i] *= -1; sort(a[it], a[it] + n[it], [=](int x, int y) { return x == y ? false : x * y < 0 ? x > y : ((abs(x) > abs(y)) ^ it); }); } cout << "Yes\n"; int x[2] = {0, 0}; for (int i = (0); i < (2 * n[0]); ++i) { x[i & 1] += a[i & 1][i >> 1]; for (int j = (0); j < (2); ++j) { cout << x[j] << " "; } cout << "\n"; } for (int j = (0); j < (2); ++j) assert(x[j] == 0); } 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 MAX = 501; const int MAXS = MAX * 1000; bitset<MAXS> dp[MAX]; int n[2]; int a[2][MAX]; bool b[2][MAX]; bool calcDP(int idx) { dp[0][0] = true; int s = 0; for (int i = (0); i < (n[idx]); ++i) { dp[i + 1] = dp[i] | (dp[i] << a[idx][i]); s += a[idx][i]; } if ((s & 1) || !dp[n[idx]][s >> 1]) return false; s >>= 1; for (int i = (n[idx]) - 1; i >= (0); --i) { if (s >= a[idx][i] && dp[i][s - a[idx][i]]) { s -= a[idx][i]; b[idx][i] = true; } else { b[idx][i] = false; } } return true; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { for (int it = (0); it < (2); ++it) { cin >> n[it]; for (int i = (0); i < (n[it]); ++i) cin >> a[it][min(i, MAX - 1)]; } if (n[0] != n[1] || !calcDP(0) || !calcDP(1)) { cout << "No\n"; continue; } for (int it = (0); it < (2); ++it) { int cntP = 0; for (int i = (0); i < (n[it]); ++i) if (b[it][i]) a[it][i] *= -1; else cntP++; if ((2 * cntP > n[0]) ^ it) for (int i = (0); i < (n[0]); ++i) a[it][i] *= -1; sort(a[it], a[it] + n[it], [=](int x, int y) { return x == y ? false : x * y < 0 ? x > y : ((abs(x) > abs(y)) ^ it); }); } cout << "Yes\n"; int x[2] = {0, 0}; for (int i = (0); i < (2 * n[0]); ++i) { x[i & 1] += a[i & 1][i >> 1]; for (int j = (0); j < (2); ++j) { cout << x[j] << " "; } cout << "\n"; } for (int j = (0); j < (2); ++j) assert(x[j] == 0); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1005; const int M = 500005; bitset<M> dp[N]; int t, n, m, a[N], b[N]; int main() { cin >> t; while (t--) { cin >> n; int tot_h = 0, tot_v = 0; for (int i = 1; i <= n; ++i) { scanf("%d", a + i); tot_h += a[i]; } cin >> m; for (int i = 1; i <= m; ++i) { scanf("%d", b + i); tot_v += b[i]; } if (n ^ m) { cout << "No\n"; continue; } if ((tot_h & 1) or (tot_v & 1)) { cout << "No\n"; continue; } tot_h >>= 1, tot_v >>= 1; dp[0][0] = 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] | dp[i - 1] << a[i]; } if (!dp[n][tot_h]) { cout << "No\n"; continue; } vector<int> one_h, two_h; for (int i = n; i >= 1; --i) { if (dp[i - 1][tot_h]) two_h.emplace_back(a[i]); else one_h.emplace_back(a[i]), tot_h -= a[i]; } dp[0][0] = 1; for (int i = 1; i <= m; ++i) { dp[i] = dp[i - 1] | dp[i - 1] << b[i]; } if (!dp[m][tot_v]) { cout << "No\n"; continue; } vector<int> one_v, two_v; for (int i = n; i >= 1; --i) { if (dp[i - 1][tot_v]) two_v.emplace_back(b[i]); else one_v.emplace_back(b[i]), tot_v -= b[i]; } if (one_h.size() > two_h.size()) swap(one_h, two_h); if (one_v.size() < two_v.size()) swap(one_v, two_v); sort(one_h.begin(), one_h.end()); sort(one_v.rbegin(), one_v.rend()); int x = 0, y = 0; vector<pair<int, int>> ans; while (!one_h.empty() and !one_v.empty()) { x += one_h.back(); one_h.pop_back(); ans.emplace_back(x, y); y += one_v.back(); one_v.pop_back(); ans.emplace_back(x, y); } assert(one_h.empty()); sort(two_h.rbegin(), two_h.rend()); sort(one_v.begin(), one_v.end()); while (!two_h.empty() and !one_v.empty()) { x -= two_h.back(); two_h.pop_back(); ans.emplace_back(x, y); y += one_v.back(); one_v.pop_back(); ans.emplace_back(x, y); } assert(one_v.empty()); assert(two_h.size() == two_v.size()); sort(two_h.begin(), two_h.end()); sort(two_v.rbegin(), two_v.rend()); while (!two_h.empty() and !two_v.empty()) { x -= two_h.back(); two_h.pop_back(); ans.emplace_back(x, y); y -= two_v.back(); two_v.pop_back(); ans.emplace_back(x, y); } cout << "Yes\n"; for (auto it : ans) printf("%d %d\n", it.first, it.second); } 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 = 1005; const int M = 500005; bitset<M> dp[N]; int t, n, m, a[N], b[N]; int main() { cin >> t; while (t--) { cin >> n; int tot_h = 0, tot_v = 0; for (int i = 1; i <= n; ++i) { scanf("%d", a + i); tot_h += a[i]; } cin >> m; for (int i = 1; i <= m; ++i) { scanf("%d", b + i); tot_v += b[i]; } if (n ^ m) { cout << "No\n"; continue; } if ((tot_h & 1) or (tot_v & 1)) { cout << "No\n"; continue; } tot_h >>= 1, tot_v >>= 1; dp[0][0] = 1; for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] | dp[i - 1] << a[i]; } if (!dp[n][tot_h]) { cout << "No\n"; continue; } vector<int> one_h, two_h; for (int i = n; i >= 1; --i) { if (dp[i - 1][tot_h]) two_h.emplace_back(a[i]); else one_h.emplace_back(a[i]), tot_h -= a[i]; } dp[0][0] = 1; for (int i = 1; i <= m; ++i) { dp[i] = dp[i - 1] | dp[i - 1] << b[i]; } if (!dp[m][tot_v]) { cout << "No\n"; continue; } vector<int> one_v, two_v; for (int i = n; i >= 1; --i) { if (dp[i - 1][tot_v]) two_v.emplace_back(b[i]); else one_v.emplace_back(b[i]), tot_v -= b[i]; } if (one_h.size() > two_h.size()) swap(one_h, two_h); if (one_v.size() < two_v.size()) swap(one_v, two_v); sort(one_h.begin(), one_h.end()); sort(one_v.rbegin(), one_v.rend()); int x = 0, y = 0; vector<pair<int, int>> ans; while (!one_h.empty() and !one_v.empty()) { x += one_h.back(); one_h.pop_back(); ans.emplace_back(x, y); y += one_v.back(); one_v.pop_back(); ans.emplace_back(x, y); } assert(one_h.empty()); sort(two_h.rbegin(), two_h.rend()); sort(one_v.begin(), one_v.end()); while (!two_h.empty() and !one_v.empty()) { x -= two_h.back(); two_h.pop_back(); ans.emplace_back(x, y); y += one_v.back(); one_v.pop_back(); ans.emplace_back(x, y); } assert(one_v.empty()); assert(two_h.size() == two_v.size()); sort(two_h.begin(), two_h.end()); sort(two_v.rbegin(), two_v.rend()); while (!two_h.empty() and !two_v.empty()) { x -= two_h.back(); two_h.pop_back(); ans.emplace_back(x, y); y -= two_v.back(); two_v.pop_back(); ans.emplace_back(x, y); } cout << "Yes\n"; for (auto it : ans) printf("%d %d\n", it.first, it.second); } return 0; } ```
#include <bits/stdc++.h> using namespace std; vector<vector<bool>> isPossible(500); void refreshPossible() { for (int i = 0; i < 500; i++) { isPossible[i] = vector<bool>((i + 1) * 1000 + 1, false); } } tuple<vector<int>, vector<int>> findSubset(int h, vector<int> &l) { isPossible[0][0] = true; isPossible[0][l[0]] = true; int sum = l[0]; for (int i = 1; i < h; i++) { sum += l[i]; for (int j = 0; j <= i * 1000; j++) { if (isPossible[i - 1][j]) { isPossible[i][j] = true; isPossible[i][j + l[i]] = true; } } } vector<bool> inSet(h, false); if (sum % 2 == 1) { return tuple<vector<int>, vector<int>>(vector<int>(), vector<int>()); } int target = sum / 2; if (!isPossible[h - 1][target]) { return tuple<vector<int>, vector<int>>(vector<int>(), vector<int>()); } int currentTarget = target; for (int i = h - 1; i >= 0; i--) { if (currentTarget == l[i]) { inSet[i] = true; break; } if (currentTarget > l[i] && isPossible[i - 1][currentTarget - l[i]]) { inSet[i] = true; currentTarget -= l[i]; } } vector<int> f, s; for (int i = 0; i < h; i++) { if (inSet[i]) f.push_back(l[i]); else s.push_back(l[i]); } if (f.size() < s.size()) swap(f, s); return make_tuple(f, s); } int main(void) { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int h, v; cin >> h; vector<int> l(h); for (int i = 0; i < h; i++) cin >> l[i]; cin >> v; vector<int> p(v); for (int i = 0; i < v; i++) cin >> p[i]; if (v != h) { cout << "No" << endl; continue; } refreshPossible(); auto [fH, sH] = findSubset(h, l); if (fH.size() == 0) { cout << "No" << endl; continue; } refreshPossible(); auto [fV, sV] = findSubset(v, p); if (fV.size() == 0) { cout << "No" << endl; continue; } sort(sH.begin(), sH.end(), greater<int>()); sort(sV.begin(), sV.end(), greater<int>()); sort(fH.begin(), fH.end()); sort(fV.begin(), fV.end()); cout << "Yes" << endl; cout << "0 0" << endl; int currX = 0, currY = 0; for (int i = 0; i < sH.size(); i++) { currX += sH[i]; cout << currX << " " << currY << endl; currY += fV[i]; cout << currX << " " << currY << endl; } for (int i = sH.size(); i < fV.size(); i++) { currX -= fH[fH.size() + sH.size() - i - 1]; cout << currX << " " << currY << endl; currY += fV[i]; cout << currX << " " << currY << endl; } for (int i = sV.size() - 1; i >= 0; i--) { currX -= fH[i]; cout << currX << " " << currY << endl; currY -= sV[i]; if (i > 0) cout << currX << " " << currY << endl; } } }
### 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; vector<vector<bool>> isPossible(500); void refreshPossible() { for (int i = 0; i < 500; i++) { isPossible[i] = vector<bool>((i + 1) * 1000 + 1, false); } } tuple<vector<int>, vector<int>> findSubset(int h, vector<int> &l) { isPossible[0][0] = true; isPossible[0][l[0]] = true; int sum = l[0]; for (int i = 1; i < h; i++) { sum += l[i]; for (int j = 0; j <= i * 1000; j++) { if (isPossible[i - 1][j]) { isPossible[i][j] = true; isPossible[i][j + l[i]] = true; } } } vector<bool> inSet(h, false); if (sum % 2 == 1) { return tuple<vector<int>, vector<int>>(vector<int>(), vector<int>()); } int target = sum / 2; if (!isPossible[h - 1][target]) { return tuple<vector<int>, vector<int>>(vector<int>(), vector<int>()); } int currentTarget = target; for (int i = h - 1; i >= 0; i--) { if (currentTarget == l[i]) { inSet[i] = true; break; } if (currentTarget > l[i] && isPossible[i - 1][currentTarget - l[i]]) { inSet[i] = true; currentTarget -= l[i]; } } vector<int> f, s; for (int i = 0; i < h; i++) { if (inSet[i]) f.push_back(l[i]); else s.push_back(l[i]); } if (f.size() < s.size()) swap(f, s); return make_tuple(f, s); } int main(void) { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int h, v; cin >> h; vector<int> l(h); for (int i = 0; i < h; i++) cin >> l[i]; cin >> v; vector<int> p(v); for (int i = 0; i < v; i++) cin >> p[i]; if (v != h) { cout << "No" << endl; continue; } refreshPossible(); auto [fH, sH] = findSubset(h, l); if (fH.size() == 0) { cout << "No" << endl; continue; } refreshPossible(); auto [fV, sV] = findSubset(v, p); if (fV.size() == 0) { cout << "No" << endl; continue; } sort(sH.begin(), sH.end(), greater<int>()); sort(sV.begin(), sV.end(), greater<int>()); sort(fH.begin(), fH.end()); sort(fV.begin(), fV.end()); cout << "Yes" << endl; cout << "0 0" << endl; int currX = 0, currY = 0; for (int i = 0; i < sH.size(); i++) { currX += sH[i]; cout << currX << " " << currY << endl; currY += fV[i]; cout << currX << " " << currY << endl; } for (int i = sH.size(); i < fV.size(); i++) { currX -= fH[fH.size() + sH.size() - i - 1]; cout << currX << " " << currY << endl; currY += fV[i]; cout << currX << " " << currY << endl; } for (int i = sV.size() - 1; i >= 0; i--) { currX -= fH[i]; cout << currX << " " << currY << endl; currY -= sV[i]; if (i > 0) cout << currX << " " << currY << endl; } } } ```
#include <bits/stdc++.h> using namespace std; bitset<1000001> L, R; vector<int> H1, H2, V1, V2, res; multiset<int> AA, BB; int A[1000], B[1000]; bool solve(int *A, int s, int e, int S) { int m = (s + e) >> 1; if (s == e || S == 0) { if (S) res.push_back(A[m]); return true; } for (int i = 0; i <= S; i++) L[i] = R[i] = 0; L[0] = R[0] = 1; for (int i = s; i <= m; i++) L |= L << A[i]; for (int i = e; i > m; i--) R |= R << A[i]; for (int i = 0; i <= S; i++) if (L[i] && R[S - i]) { solve(A, s, m, i); solve(A, m + 1, e, S - i); return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ((void)0); ((void)0); ((void)0); int T, N, M; for (cin >> T; T--;) { int H = 0, V = 0, x = 0, y = 0; cin >> N; AA.clear(); BB.clear(); for (int i = 0; i < N; i++) { cin >> A[i]; H += A[i]; AA.insert(A[i]); } cin >> M; for (int i = 0; i < M; i++) { cin >> B[i]; V += B[i]; BB.insert(B[i]); } if ((H & 1) || (V & 1) || N != M || N == 1 || M == 1) { cout << "No\n"; continue; } sort(A, A + N); sort(B, B + M); res.clear(); if (!solve(A, 0, N - 1, H / 2)) { cout << "No\n"; continue; } H1.clear(); H2.clear(); for (auto v : res) { H1.push_back(v); AA.erase(AA.find(v)); } for (auto v : AA) H2.push_back(v); res.clear(); if (!solve(B, 0, M - 1, V / 2)) { cout << "No\n"; continue; } V1.clear(); V2.clear(); for (auto v : res) { V1.push_back(v); BB.erase(BB.find(v)); } for (auto v : BB) V2.push_back(v); if (V1.size() > V2.size()) swap(V1, V2); if (H1.size() < H2.size()) swap(H1, H2); reverse(V1.begin(), V1.end()); reverse(V2.begin(), V2.end()); cout << "Yes\n"; for (auto &v : V1) v *= -1; for (auto &h : H2) H1.push_back(-h); for (auto v : V2) V1.push_back(v); for (int i = 0; i < N; i++) { y += V1[i]; cout << x << ' ' << y << '\n'; x += H1[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; bitset<1000001> L, R; vector<int> H1, H2, V1, V2, res; multiset<int> AA, BB; int A[1000], B[1000]; bool solve(int *A, int s, int e, int S) { int m = (s + e) >> 1; if (s == e || S == 0) { if (S) res.push_back(A[m]); return true; } for (int i = 0; i <= S; i++) L[i] = R[i] = 0; L[0] = R[0] = 1; for (int i = s; i <= m; i++) L |= L << A[i]; for (int i = e; i > m; i--) R |= R << A[i]; for (int i = 0; i <= S; i++) if (L[i] && R[S - i]) { solve(A, s, m, i); solve(A, m + 1, e, S - i); return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ((void)0); ((void)0); ((void)0); int T, N, M; for (cin >> T; T--;) { int H = 0, V = 0, x = 0, y = 0; cin >> N; AA.clear(); BB.clear(); for (int i = 0; i < N; i++) { cin >> A[i]; H += A[i]; AA.insert(A[i]); } cin >> M; for (int i = 0; i < M; i++) { cin >> B[i]; V += B[i]; BB.insert(B[i]); } if ((H & 1) || (V & 1) || N != M || N == 1 || M == 1) { cout << "No\n"; continue; } sort(A, A + N); sort(B, B + M); res.clear(); if (!solve(A, 0, N - 1, H / 2)) { cout << "No\n"; continue; } H1.clear(); H2.clear(); for (auto v : res) { H1.push_back(v); AA.erase(AA.find(v)); } for (auto v : AA) H2.push_back(v); res.clear(); if (!solve(B, 0, M - 1, V / 2)) { cout << "No\n"; continue; } V1.clear(); V2.clear(); for (auto v : res) { V1.push_back(v); BB.erase(BB.find(v)); } for (auto v : BB) V2.push_back(v); if (V1.size() > V2.size()) swap(V1, V2); if (H1.size() < H2.size()) swap(H1, H2); reverse(V1.begin(), V1.end()); reverse(V2.begin(), V2.end()); cout << "Yes\n"; for (auto &v : V1) v *= -1; for (auto &h : H2) H1.push_back(-h); for (auto v : V2) V1.push_back(v); for (int i = 0; i < N; i++) { y += V1[i]; cout << x << ' ' << y << '\n'; x += H1[i]; cout << x << ' ' << y << '\n'; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; std::mt19937 rnd( (int)std::chrono::steady_clock::now().time_since_epoch().count()); long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } struct P { int x, y; P() {} P(int x, int y) : x(x), y(y) {} }; int nhor; vector<int> hor; int nver; vector<int> ver; vector<P> ans; bool split(vector<int> a, vector<int>& b, vector<int>& c) { int asum = 0; for (int i = (0); i < (((int)(a).size())); ++i) asum += a[i]; if (asum % 2 != 0) return false; int want = asum / 2; vector<int> can(want + 1, -2); can[0] = -1; for (int i = (0); i < (((int)(a).size())); ++i) for (int j = want - a[i]; j >= 0; --j) if (can[j] != -2 && can[j + a[i]] == -2) can[j + a[i]] = i; if (can[want] == -2) return false; vector<bool> inb(((int)(a).size()), false); for (int x = want; x != 0; x -= a[can[x]]) inb[can[x]] = true; b.clear(); c.clear(); for (int i = (0); i < (((int)(a).size())); ++i) if (inb[i]) b.push_back(a[i]); else c.push_back(a[i]); return true; } bool solve() { if (nhor != nver) return false; vector<int> lft, rgt; if (!split(hor, lft, rgt)) return false; if (((int)(rgt).size()) > ((int)(lft).size())) swap(lft, rgt); vector<int> up, dn; if (!split(ver, up, dn)) return false; if (((int)(dn).size()) > ((int)(up).size())) swap(up, dn); int acnt = ((int)(rgt).size()); assert(((int)(up).size()) >= acnt); int ccnt = ((int)(dn).size()); assert(((int)(lft).size()) >= ccnt); int bcnt = nhor - acnt - ccnt; vector<int> ahor(acnt), aver(acnt); for (int i = (0); i < (acnt); ++i) ahor[i] = rgt[i], aver[i] = up[i]; vector<int> bhor(bcnt), bver(bcnt); for (int i = (0); i < (bcnt); ++i) bhor[i] = lft[i], bver[i] = up[acnt + i]; vector<int> chor(ccnt), cver(ccnt); for (int i = (0); i < (ccnt); ++i) chor[i] = lft[bcnt + i], cver[i] = dn[i]; sort(ahor.begin(), ahor.end()); reverse(ahor.begin(), ahor.end()); sort(aver.begin(), aver.end()); sort(chor.begin(), chor.end()); reverse(chor.begin(), chor.end()); sort(cver.begin(), cver.end()); ans.clear(); ans.push_back(P(0, 0)); auto append = [&](int dx, int dy) { P p(ans.back().x + dx, ans.back().y + dy); ans.push_back(p); }; for (int i = (0); i < (acnt); ++i) append(+ahor[i], 0), append(0, +aver[i]); for (int i = (0); i < (bcnt); ++i) append(-bhor[i], 0), append(0, +bver[i]); for (int i = (0); i < (ccnt); ++i) append(-chor[i], 0), append(0, -cver[i]); assert(ans.back().x == 0 && ans.back().y == 0); ans.pop_back(); return true; } void run() { scanf("%d", &nhor); hor = vector<int>(nhor); for (int i = (0); i < (nhor); ++i) scanf("%d", &hor[i]); scanf("%d", &nver); ver = vector<int>(nver); for (int i = (0); i < (nver); ++i) scanf("%d", &ver[i]); if (!solve()) { printf("No\n"); return; } printf("Yes\n"); for (int i = (0); i < (((int)(ans).size())); ++i) printf("%d %d\n", ans[i].x, ans[i].y); } int main() { int ncase; scanf("%d", &ncase); for (int i = (1); i <= (ncase); ++i) run(); 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 namespace std; std::mt19937 rnd( (int)std::chrono::steady_clock::now().time_since_epoch().count()); long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } struct P { int x, y; P() {} P(int x, int y) : x(x), y(y) {} }; int nhor; vector<int> hor; int nver; vector<int> ver; vector<P> ans; bool split(vector<int> a, vector<int>& b, vector<int>& c) { int asum = 0; for (int i = (0); i < (((int)(a).size())); ++i) asum += a[i]; if (asum % 2 != 0) return false; int want = asum / 2; vector<int> can(want + 1, -2); can[0] = -1; for (int i = (0); i < (((int)(a).size())); ++i) for (int j = want - a[i]; j >= 0; --j) if (can[j] != -2 && can[j + a[i]] == -2) can[j + a[i]] = i; if (can[want] == -2) return false; vector<bool> inb(((int)(a).size()), false); for (int x = want; x != 0; x -= a[can[x]]) inb[can[x]] = true; b.clear(); c.clear(); for (int i = (0); i < (((int)(a).size())); ++i) if (inb[i]) b.push_back(a[i]); else c.push_back(a[i]); return true; } bool solve() { if (nhor != nver) return false; vector<int> lft, rgt; if (!split(hor, lft, rgt)) return false; if (((int)(rgt).size()) > ((int)(lft).size())) swap(lft, rgt); vector<int> up, dn; if (!split(ver, up, dn)) return false; if (((int)(dn).size()) > ((int)(up).size())) swap(up, dn); int acnt = ((int)(rgt).size()); assert(((int)(up).size()) >= acnt); int ccnt = ((int)(dn).size()); assert(((int)(lft).size()) >= ccnt); int bcnt = nhor - acnt - ccnt; vector<int> ahor(acnt), aver(acnt); for (int i = (0); i < (acnt); ++i) ahor[i] = rgt[i], aver[i] = up[i]; vector<int> bhor(bcnt), bver(bcnt); for (int i = (0); i < (bcnt); ++i) bhor[i] = lft[i], bver[i] = up[acnt + i]; vector<int> chor(ccnt), cver(ccnt); for (int i = (0); i < (ccnt); ++i) chor[i] = lft[bcnt + i], cver[i] = dn[i]; sort(ahor.begin(), ahor.end()); reverse(ahor.begin(), ahor.end()); sort(aver.begin(), aver.end()); sort(chor.begin(), chor.end()); reverse(chor.begin(), chor.end()); sort(cver.begin(), cver.end()); ans.clear(); ans.push_back(P(0, 0)); auto append = [&](int dx, int dy) { P p(ans.back().x + dx, ans.back().y + dy); ans.push_back(p); }; for (int i = (0); i < (acnt); ++i) append(+ahor[i], 0), append(0, +aver[i]); for (int i = (0); i < (bcnt); ++i) append(-bhor[i], 0), append(0, +bver[i]); for (int i = (0); i < (ccnt); ++i) append(-chor[i], 0), append(0, -cver[i]); assert(ans.back().x == 0 && ans.back().y == 0); ans.pop_back(); return true; } void run() { scanf("%d", &nhor); hor = vector<int>(nhor); for (int i = (0); i < (nhor); ++i) scanf("%d", &hor[i]); scanf("%d", &nver); ver = vector<int>(nver); for (int i = (0); i < (nver); ++i) scanf("%d", &ver[i]); if (!solve()) { printf("No\n"); return; } printf("Yes\n"); for (int i = (0); i < (((int)(ans).size())); ++i) printf("%d %d\n", ans[i].x, ans[i].y); } int main() { int ncase; scanf("%d", &ncase); for (int i = (1); i <= (ncase); ++i) run(); return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; template <class T> inline void amin(T &x, const T &y) { if (y < x) x = y; } template <class T> inline void amax(T &x, const T &y) { if (x < y) x = y; } int dp[500011]; void sep(const vector<int> &v, vector<int> &a, vector<int> &b) { a.clear(); b.clear(); int sum = 0; for (__typeof((v).begin()) e = (v).begin(), e_end = (v).end(); e != e_end; ++e) sum += *e; if (sum % 2) return; memset(dp, -1, sizeof dp); dp[0] = 0; int high = 0; for (int i = 0, i_len = (v.size()); i < i_len; ++i) { for (int j = min(high, sum / 2 - v[i]); j >= 0; j--) if (dp[j] != -1 && dp[j + v[i]] == -1) { dp[j + v[i]] = v[i]; } high += v[i]; } if (dp[sum / 2] == -1) return; int cur = sum / 2; while (cur) { a.push_back(dp[cur]); cur -= dp[cur]; } sort(a.begin(), a.end()); int i = 0, j = 0; while (i < (int)v.size()) { if (j < (int)a.size() && v[i] == a[j]) { i++; j++; } else { b.push_back(v[i]); i++; } } } vector<int> HOR, VER; vector<pair<int, int> > ans; bool solve() { if (HOR.size() != VER.size()) return false; sort(HOR.begin(), HOR.end()); sort(VER.begin(), VER.end()); vector<int> h1, h2; sep(HOR, h1, h2); if (h1.empty()) return false; if (h1.size() > h2.size()) swap(h1, h2); vector<int> v1, v2; sep(VER, v1, v2); if (v1.empty()) return false; if (v1.size() < v2.size()) swap(v1, v2); ans.clear(); int x = 0, y = 0; ans.emplace_back(0, 0); for (int i = 0, i_len = (HOR.size()); i < i_len; ++i) { int m; if (i < (int)h1.size()) { m = h1.rbegin()[i]; } else { m = -h2.rbegin()[i - h1.size()]; } x += m; ans.emplace_back(x, y); if (i < (int)v1.size()) { m = v1[i]; } else { m = -v2[i - v1.size()]; } y += m; ans.emplace_back(x, y); } assert(x == 0 && y == 0); return true; } void MAIN() { int H, V; scanf("%d", &H); HOR.resize(H); for (int i = 0, i_len = (H); i < i_len; ++i) scanf("%d", &HOR[i]); scanf("%d", &V); VER.resize(V); for (int i = 0, i_len = (V); i < i_len; ++i) scanf("%d", &VER[i]); if (solve()) { puts("Yes"); for (int i = 0, i_len = (ans.size() - 1u); i < i_len; ++i) { printf("%d %d\n", ans[i].first, ans[i].second); } } else { puts("No"); } } int main() { int TC = 1; scanf("%d", &TC); for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) 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> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; template <class T> inline void amin(T &x, const T &y) { if (y < x) x = y; } template <class T> inline void amax(T &x, const T &y) { if (x < y) x = y; } int dp[500011]; void sep(const vector<int> &v, vector<int> &a, vector<int> &b) { a.clear(); b.clear(); int sum = 0; for (__typeof((v).begin()) e = (v).begin(), e_end = (v).end(); e != e_end; ++e) sum += *e; if (sum % 2) return; memset(dp, -1, sizeof dp); dp[0] = 0; int high = 0; for (int i = 0, i_len = (v.size()); i < i_len; ++i) { for (int j = min(high, sum / 2 - v[i]); j >= 0; j--) if (dp[j] != -1 && dp[j + v[i]] == -1) { dp[j + v[i]] = v[i]; } high += v[i]; } if (dp[sum / 2] == -1) return; int cur = sum / 2; while (cur) { a.push_back(dp[cur]); cur -= dp[cur]; } sort(a.begin(), a.end()); int i = 0, j = 0; while (i < (int)v.size()) { if (j < (int)a.size() && v[i] == a[j]) { i++; j++; } else { b.push_back(v[i]); i++; } } } vector<int> HOR, VER; vector<pair<int, int> > ans; bool solve() { if (HOR.size() != VER.size()) return false; sort(HOR.begin(), HOR.end()); sort(VER.begin(), VER.end()); vector<int> h1, h2; sep(HOR, h1, h2); if (h1.empty()) return false; if (h1.size() > h2.size()) swap(h1, h2); vector<int> v1, v2; sep(VER, v1, v2); if (v1.empty()) return false; if (v1.size() < v2.size()) swap(v1, v2); ans.clear(); int x = 0, y = 0; ans.emplace_back(0, 0); for (int i = 0, i_len = (HOR.size()); i < i_len; ++i) { int m; if (i < (int)h1.size()) { m = h1.rbegin()[i]; } else { m = -h2.rbegin()[i - h1.size()]; } x += m; ans.emplace_back(x, y); if (i < (int)v1.size()) { m = v1[i]; } else { m = -v2[i - v1.size()]; } y += m; ans.emplace_back(x, y); } assert(x == 0 && y == 0); return true; } void MAIN() { int H, V; scanf("%d", &H); HOR.resize(H); for (int i = 0, i_len = (H); i < i_len; ++i) scanf("%d", &HOR[i]); scanf("%d", &V); VER.resize(V); for (int i = 0, i_len = (V); i < i_len; ++i) scanf("%d", &VER[i]); if (solve()) { puts("Yes"); for (int i = 0, i_len = (ans.size() - 1u); i < i_len; ++i) { printf("%d %d\n", ans[i].first, ans[i].second); } } else { puts("No"); } } int main() { int TC = 1; scanf("%d", &TC); for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN(); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ncase; for (cin >> ncase; ncase--;) { int n; cin >> n; vector<int> H(n); for (int &x : H) { cin >> x; } int m; cin >> m; vector<int> V(m); for (int &x : V) { cin >> x; } if (n != m) { cout << "No\n"; continue; } int h = accumulate(H.begin(), H.end(), 0); int v = accumulate(V.begin(), V.end(), 0); if ((h & 1) || (v & 1)) { cout << "No\n"; continue; } bitset<500001> dH, dV; dH[0] = 1; for (int x : H) { dH |= dH << x; } if (!dH[h / 2]) { cout << "No\n"; continue; } dV[0] = 1; for (int x : V) { dV |= dV << x; } if (!dV[v / 2]) { cout << "No\n"; continue; } vector<int> idH(n), idV(m); iota(idH.begin(), idH.end(), 0); iota(idV.begin(), idV.end(), 0); vector<int> firstH, secondH; vector<int> firstV, secondV; while (true) { random_shuffle(idH.begin(), idH.end()); const int N = 2000; vector<vector<bool>> dp(n + 1, vector<bool>(N + N + 1)); vector<vector<int>> pred(n + 1, vector<int>(N + N + 1)); dp[0][N] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= N + N; j++) if (dp[i][j]) { int x = H[idH[i]]; if (j + x <= N + N) { dp[i + 1][j + x] = 1; pred[i + 1][j + x] = 1; } if (j - x >= 0) { dp[i + 1][j - x] = 1; pred[i + 1][j - x] = -1; } } } if (dp[n][N]) { int cur = N; for (int i = n; i > 0; i--) { int x = pred[i][cur]; int y = H[idH[i - 1]]; if (x < 0) { secondH.push_back(idH[i - 1]); cur += y; } else { firstH.push_back(idH[i - 1]); cur -= y; } } break; } } while (true) { random_shuffle(idV.begin(), idV.end()); const int N = 2000; vector<vector<bool>> dp(m + 1, vector<bool>(N + N + 1)); vector<vector<int>> pred(m + 1, vector<int>(N + N + 1)); dp[0][N] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= N + N; j++) if (dp[i][j]) { int x = V[idV[i]]; if (j + x <= N + N) { dp[i + 1][j + x] = 1; pred[i + 1][j + x] = 1; } if (j - x >= 0) { dp[i + 1][j - x] = 1; pred[i + 1][j - x] = -1; } } } if (dp[m][N]) { int cur = N; for (int i = m; i > 0; i--) { int x = pred[i][cur]; int y = V[idV[i - 1]]; if (x < 0) { secondV.push_back(idV[i - 1]); cur += y; } else { firstV.push_back(idV[i - 1]); cur -= y; } } break; } } auto solve = [&](const vector<int> &a, const vector<int> &b, const vector<int> &c, const vector<int> &d) { int px = 0, py = 0; vector<pair<int, int>> ans; auto A = a, B = b, C = c, D = d; for (int i = 0; i < n; i++) { ans.emplace_back(px, py); if (!A.empty()) { int x = A.back(); px += H[x]; A.pop_back(); } else { int x = C.back(); px -= H[x]; C.pop_back(); } ans.emplace_back(px, py); if (!B.empty()) { int x = B.back(); py += V[x]; B.pop_back(); } else { int x = D.back(); py -= V[x]; D.pop_back(); } } return ans; }; struct PT { int x, y; PT(int x = 0, int y = 0) : x(x), y(y){}; }; auto dis = [&](PT a, PT b) { return abs(a.x - b.x) + abs(a.y - b.y); }; auto equal = [&](PT a, PT b) { return a.x == b.x && a.y == b.y; }; auto onSeg = [&](PT a, PT b, PT c) { return dis(a, c) + dis(b, c) == dis(a, b) && !equal(a, c) && !equal(b, c); }; auto okay = [&](const vector<pair<int, int>> &a) { int n = a.size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int sx1 = a[i].first, sy1 = a[i].second; int tx1 = a[(i + 1) % n].first, ty1 = a[(i + 1) % n].second; int sx2 = a[j].first, sy2 = a[j].second; int tx2 = a[(j + 1) % n].first, ty2 = a[(j + 1) % n].second; PT p1(sx1, sy1), p2(tx1, ty1), p3(sx2, sy2), p4(tx2, ty2); if (sx1 == tx1 && sy2 == ty2 || sy1 == ty1 && sx2 == tx2) { if (onSeg(p1, p2, p3) || onSeg(p1, p2, p4) || onSeg(p3, p4, p1) || onSeg(p3, p4, p2)) { return false; } int lx = max(min(sx1, tx1), min(sx2, tx2)); int rx = min(max(sx1, tx1), max(sx2, tx2)); int ly = max(min(sy1, ty1), min(sy2, ty2)); int ry = min(max(sy1, ty1), max(sy2, ty2)); if (lx != rx || ly != ry) { continue; } PT p(lx, ly); if (onSeg(p1, p2, p) || onSeg(p3, p4, p)) { return false; } } } } return true; }; sort(firstH.begin(), firstH.end(), [&](int x, int y) { return H[x] > H[y]; }); sort(secondH.begin(), secondH.end(), [&](int x, int y) { return H[x] < H[y]; }); sort(firstV.begin(), firstV.end(), [&](int x, int y) { return V[x] < V[y]; }); sort(secondV.begin(), secondV.end(), [&](int x, int y) { return V[x] > V[y]; }); while (true) { auto t1 = solve(firstH, firstV, secondH, secondV); auto t2 = solve(firstH, secondV, secondH, firstV); if (okay(t1)) { cout << "Yes\n"; for (auto t : t1) { cout << t.first << ' ' << t.second << '\n'; } break; } else if (okay(t2)) { cout << "Yes\n"; for (auto t : t2) { cout << t.first << ' ' << t.second << '\n'; } break; } random_shuffle(firstH.begin(), firstH.end()); random_shuffle(firstV.begin(), firstV.end()); random_shuffle(secondH.begin(), secondH.end()); random_shuffle(secondV.begin(), secondV.end()); } } 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 main() { srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int ncase; for (cin >> ncase; ncase--;) { int n; cin >> n; vector<int> H(n); for (int &x : H) { cin >> x; } int m; cin >> m; vector<int> V(m); for (int &x : V) { cin >> x; } if (n != m) { cout << "No\n"; continue; } int h = accumulate(H.begin(), H.end(), 0); int v = accumulate(V.begin(), V.end(), 0); if ((h & 1) || (v & 1)) { cout << "No\n"; continue; } bitset<500001> dH, dV; dH[0] = 1; for (int x : H) { dH |= dH << x; } if (!dH[h / 2]) { cout << "No\n"; continue; } dV[0] = 1; for (int x : V) { dV |= dV << x; } if (!dV[v / 2]) { cout << "No\n"; continue; } vector<int> idH(n), idV(m); iota(idH.begin(), idH.end(), 0); iota(idV.begin(), idV.end(), 0); vector<int> firstH, secondH; vector<int> firstV, secondV; while (true) { random_shuffle(idH.begin(), idH.end()); const int N = 2000; vector<vector<bool>> dp(n + 1, vector<bool>(N + N + 1)); vector<vector<int>> pred(n + 1, vector<int>(N + N + 1)); dp[0][N] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= N + N; j++) if (dp[i][j]) { int x = H[idH[i]]; if (j + x <= N + N) { dp[i + 1][j + x] = 1; pred[i + 1][j + x] = 1; } if (j - x >= 0) { dp[i + 1][j - x] = 1; pred[i + 1][j - x] = -1; } } } if (dp[n][N]) { int cur = N; for (int i = n; i > 0; i--) { int x = pred[i][cur]; int y = H[idH[i - 1]]; if (x < 0) { secondH.push_back(idH[i - 1]); cur += y; } else { firstH.push_back(idH[i - 1]); cur -= y; } } break; } } while (true) { random_shuffle(idV.begin(), idV.end()); const int N = 2000; vector<vector<bool>> dp(m + 1, vector<bool>(N + N + 1)); vector<vector<int>> pred(m + 1, vector<int>(N + N + 1)); dp[0][N] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= N + N; j++) if (dp[i][j]) { int x = V[idV[i]]; if (j + x <= N + N) { dp[i + 1][j + x] = 1; pred[i + 1][j + x] = 1; } if (j - x >= 0) { dp[i + 1][j - x] = 1; pred[i + 1][j - x] = -1; } } } if (dp[m][N]) { int cur = N; for (int i = m; i > 0; i--) { int x = pred[i][cur]; int y = V[idV[i - 1]]; if (x < 0) { secondV.push_back(idV[i - 1]); cur += y; } else { firstV.push_back(idV[i - 1]); cur -= y; } } break; } } auto solve = [&](const vector<int> &a, const vector<int> &b, const vector<int> &c, const vector<int> &d) { int px = 0, py = 0; vector<pair<int, int>> ans; auto A = a, B = b, C = c, D = d; for (int i = 0; i < n; i++) { ans.emplace_back(px, py); if (!A.empty()) { int x = A.back(); px += H[x]; A.pop_back(); } else { int x = C.back(); px -= H[x]; C.pop_back(); } ans.emplace_back(px, py); if (!B.empty()) { int x = B.back(); py += V[x]; B.pop_back(); } else { int x = D.back(); py -= V[x]; D.pop_back(); } } return ans; }; struct PT { int x, y; PT(int x = 0, int y = 0) : x(x), y(y){}; }; auto dis = [&](PT a, PT b) { return abs(a.x - b.x) + abs(a.y - b.y); }; auto equal = [&](PT a, PT b) { return a.x == b.x && a.y == b.y; }; auto onSeg = [&](PT a, PT b, PT c) { return dis(a, c) + dis(b, c) == dis(a, b) && !equal(a, c) && !equal(b, c); }; auto okay = [&](const vector<pair<int, int>> &a) { int n = a.size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int sx1 = a[i].first, sy1 = a[i].second; int tx1 = a[(i + 1) % n].first, ty1 = a[(i + 1) % n].second; int sx2 = a[j].first, sy2 = a[j].second; int tx2 = a[(j + 1) % n].first, ty2 = a[(j + 1) % n].second; PT p1(sx1, sy1), p2(tx1, ty1), p3(sx2, sy2), p4(tx2, ty2); if (sx1 == tx1 && sy2 == ty2 || sy1 == ty1 && sx2 == tx2) { if (onSeg(p1, p2, p3) || onSeg(p1, p2, p4) || onSeg(p3, p4, p1) || onSeg(p3, p4, p2)) { return false; } int lx = max(min(sx1, tx1), min(sx2, tx2)); int rx = min(max(sx1, tx1), max(sx2, tx2)); int ly = max(min(sy1, ty1), min(sy2, ty2)); int ry = min(max(sy1, ty1), max(sy2, ty2)); if (lx != rx || ly != ry) { continue; } PT p(lx, ly); if (onSeg(p1, p2, p) || onSeg(p3, p4, p)) { return false; } } } } return true; }; sort(firstH.begin(), firstH.end(), [&](int x, int y) { return H[x] > H[y]; }); sort(secondH.begin(), secondH.end(), [&](int x, int y) { return H[x] < H[y]; }); sort(firstV.begin(), firstV.end(), [&](int x, int y) { return V[x] < V[y]; }); sort(secondV.begin(), secondV.end(), [&](int x, int y) { return V[x] > V[y]; }); while (true) { auto t1 = solve(firstH, firstV, secondH, secondV); auto t2 = solve(firstH, secondV, secondH, firstV); if (okay(t1)) { cout << "Yes\n"; for (auto t : t1) { cout << t.first << ' ' << t.second << '\n'; } break; } else if (okay(t2)) { cout << "Yes\n"; for (auto t : t2) { cout << t.first << ' ' << t.second << '\n'; } break; } random_shuffle(firstH.begin(), firstH.end()); random_shuffle(firstV.begin(), firstV.end()); random_shuffle(secondH.begin(), secondH.end()); random_shuffle(secondV.begin(), secondV.end()); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int maxn = 1001; bitset<250000 + 1> bs[500 + 1]; int l[maxn], p[maxn]; int R[maxn], D[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; for (cin >> t; t; t -= 1) { int h, suml = 0, sump = 0; cin >> h; for (int i = 1; i <= h; i += 1) { R[i] = 0; cin >> l[i]; suml += l[i]; } sort(l + 1, l + h + 1, greater<int>()); int v; cin >> v; for (int i = 1; i <= v; i += 1) { D[i] = 0; cin >> p[i]; sump += p[i]; } sort(p + 1, p + v + 1); if (h != v or h == 1 or (suml & 1) or (sump & 1)) { cout << "No\n"; continue; } bs[0].set(0); for (int i = 1; i <= h; i += 1) bs[i] = bs[i - 1] | (bs[i - 1] << l[i]); if (not bs[h].test(suml / 2)) { cout << "No\n"; continue; } int x = suml / 2; for (int i = h; i; i -= 1) if (bs[i - 1].test(x)) R[i] = 1; else x -= l[i]; bs[0].set(0); for (int i = 1; i <= v; i += 1) bs[i] = bs[i - 1] | (bs[i - 1] << p[i]); if (not bs[v].test(sump / 2)) { cout << "No\n"; continue; } int y = sump / 2; for (int i = v; i; i -= 1) if (bs[i - 1].test(y)) D[i] = 1; else y -= p[i]; cout << "Yes\n"; vector<int> sp, sz; { int cntp = 0, cntz = 0; for (int i = 1; i <= h; i += 1) if (R[i]) ++cntp; for (int i = 1; i <= v; i += 1) if (D[i]) ++cntz; if (cntp > h - cntp) for (int i = 1; i <= h; i += 1) R[i] ^= 1; if (cntz < v - cntz) for (int i = 1; i <= v; i += 1) D[i] ^= 1; } for (int i = 1; i <= h; i += 1) if (R[i]) sp.push_back(i); for (int i = 1; i <= h; i += 1) if (not R[i]) sp.push_back(i); for (int i = 1; i <= v; i += 1) if (D[i]) sz.push_back(i); for (int i = 1; i <= v; i += 1) if (not D[i]) sz.push_back(i); x = 0, y = 0; for (int i = 0; i < h; i += 1) { cout << x << " " << y << "\n"; if (R[sp[i]]) x += l[sp[i]]; else x -= l[sp[i]]; cout << x << " " << y << "\n"; if (D[sz[i]]) y -= p[sz[i]]; else y += p[sz[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; using LL = long long; constexpr int maxn = 1001; bitset<250000 + 1> bs[500 + 1]; int l[maxn], p[maxn]; int R[maxn], D[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; for (cin >> t; t; t -= 1) { int h, suml = 0, sump = 0; cin >> h; for (int i = 1; i <= h; i += 1) { R[i] = 0; cin >> l[i]; suml += l[i]; } sort(l + 1, l + h + 1, greater<int>()); int v; cin >> v; for (int i = 1; i <= v; i += 1) { D[i] = 0; cin >> p[i]; sump += p[i]; } sort(p + 1, p + v + 1); if (h != v or h == 1 or (suml & 1) or (sump & 1)) { cout << "No\n"; continue; } bs[0].set(0); for (int i = 1; i <= h; i += 1) bs[i] = bs[i - 1] | (bs[i - 1] << l[i]); if (not bs[h].test(suml / 2)) { cout << "No\n"; continue; } int x = suml / 2; for (int i = h; i; i -= 1) if (bs[i - 1].test(x)) R[i] = 1; else x -= l[i]; bs[0].set(0); for (int i = 1; i <= v; i += 1) bs[i] = bs[i - 1] | (bs[i - 1] << p[i]); if (not bs[v].test(sump / 2)) { cout << "No\n"; continue; } int y = sump / 2; for (int i = v; i; i -= 1) if (bs[i - 1].test(y)) D[i] = 1; else y -= p[i]; cout << "Yes\n"; vector<int> sp, sz; { int cntp = 0, cntz = 0; for (int i = 1; i <= h; i += 1) if (R[i]) ++cntp; for (int i = 1; i <= v; i += 1) if (D[i]) ++cntz; if (cntp > h - cntp) for (int i = 1; i <= h; i += 1) R[i] ^= 1; if (cntz < v - cntz) for (int i = 1; i <= v; i += 1) D[i] ^= 1; } for (int i = 1; i <= h; i += 1) if (R[i]) sp.push_back(i); for (int i = 1; i <= h; i += 1) if (not R[i]) sp.push_back(i); for (int i = 1; i <= v; i += 1) if (D[i]) sz.push_back(i); for (int i = 1; i <= v; i += 1) if (not D[i]) sz.push_back(i); x = 0, y = 0; for (int i = 0; i < h; i += 1) { cout << x << " " << y << "\n"; if (R[sp[i]]) x += l[sp[i]]; else x -= l[sp[i]]; cout << x << " " << y << "\n"; if (D[sz[i]]) y -= p[sz[i]]; else y += p[sz[i]]; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1003, K = N * N >> 1; template <typename T> void read(T &x) { int ch = getchar(); x = 0; bool f = false; for (; ch < '0' || ch > '9'; ch = getchar()) f |= ch == '-'; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; if (f) x = -x; } int t, n, m, x[N], y[N], suma, sumb; bitset<K> f[N]; vector<int> xa, xb, ya, yb; bool work(int *x, vector<int> &x1, vector<int> &x2, int sum) { f[0].set(0); x1.resize(0); x2.resize(0); for (int i = 1; i <= n; ++i) f[i] = f[i - 1] | f[i - 1] << x[i]; if (!f[n][sum]) return true; for (int i = n; i; --i) if (sum >= x[i] && f[i - 1][sum - x[i]]) { x1.emplace_back(x[i]); sum -= x[i]; } else x2.emplace_back(x[i]); sort(x1.begin(), x1.end()); sort(x2.begin(), x2.end()); return false; } void solve() { read(n); suma = sumb = 0; for (int i = 1; i <= n; ++i) { read(x[i]); suma += x[i]; } read(m); for (int i = 1; i <= m; ++i) { read(y[i]); sumb += y[i]; } if (n != m || (suma & 1) || (sumb & 1) || work(x, xa, xb, suma >> 1) || work(y, ya, yb, sumb >> 1)) { puts("No"); return; } puts("Yes"); if (xa.size() > xb.size()) swap(xa, xb); if (ya.size() < yb.size()) swap(ya, yb); auto it1 = xa.end(), it2 = ya.begin(); int dir1 = 1, dir2 = 1, nowx = 0, nowy = 0; for (int _ = 0; _ < n; ++_) { --it1; nowx += (*it1) * dir1; printf("%d %d\n", nowx, nowy); nowy += (*it2) * dir2; ++it2; printf("%d %d\n", nowx, nowy); if (it1 == xa.begin()) { it1 = xb.end(); dir1 = -1; } if (it2 == ya.end()) { it2 = yb.begin(); dir2 = -1; } } } int main() { read(t); while (t--) solve(); }
### 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 = 1003, K = N * N >> 1; template <typename T> void read(T &x) { int ch = getchar(); x = 0; bool f = false; for (; ch < '0' || ch > '9'; ch = getchar()) f |= ch == '-'; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; if (f) x = -x; } int t, n, m, x[N], y[N], suma, sumb; bitset<K> f[N]; vector<int> xa, xb, ya, yb; bool work(int *x, vector<int> &x1, vector<int> &x2, int sum) { f[0].set(0); x1.resize(0); x2.resize(0); for (int i = 1; i <= n; ++i) f[i] = f[i - 1] | f[i - 1] << x[i]; if (!f[n][sum]) return true; for (int i = n; i; --i) if (sum >= x[i] && f[i - 1][sum - x[i]]) { x1.emplace_back(x[i]); sum -= x[i]; } else x2.emplace_back(x[i]); sort(x1.begin(), x1.end()); sort(x2.begin(), x2.end()); return false; } void solve() { read(n); suma = sumb = 0; for (int i = 1; i <= n; ++i) { read(x[i]); suma += x[i]; } read(m); for (int i = 1; i <= m; ++i) { read(y[i]); sumb += y[i]; } if (n != m || (suma & 1) || (sumb & 1) || work(x, xa, xb, suma >> 1) || work(y, ya, yb, sumb >> 1)) { puts("No"); return; } puts("Yes"); if (xa.size() > xb.size()) swap(xa, xb); if (ya.size() < yb.size()) swap(ya, yb); auto it1 = xa.end(), it2 = ya.begin(); int dir1 = 1, dir2 = 1, nowx = 0, nowy = 0; for (int _ = 0; _ < n; ++_) { --it1; nowx += (*it1) * dir1; printf("%d %d\n", nowx, nowy); nowy += (*it2) * dir2; ++it2; printf("%d %d\n", nowx, nowy); if (it1 == xa.begin()) { it1 = xb.end(); dir1 = -1; } if (it2 == ya.end()) { it2 = yb.begin(); dir2 = -1; } } } int main() { read(t); while (t--) solve(); } ```
#include <bits/stdc++.h> using namespace std; const int Maxn = 1000; int a[Maxn + 5], b[Maxn + 5]; int n, m; int sum_1, sum_2; bitset<Maxn * Maxn + 5> f[Maxn + 5]; int lis[2][2][Maxn + 5], lis_len[2][2]; bool work(int *a, int sum, int d) { f[0][0] = 1; for (int i = 1; i <= n; i++) { f[i] = (f[i - 1] | (f[i - 1] << a[i])); } if (f[n][sum] == 0) { return 0; } for (int i = n; i > 0; i--) { if (sum >= a[i] && f[i - 1][sum - a[i]]) { sum -= a[i]; lis[d][0][++lis_len[d][0]] = a[i]; } else { lis[d][1][++lis_len[d][1]] = a[i]; } } return 1; } int d[2][Maxn + 5]; int d_len[2]; int main() { int T; scanf("%d", &T); while (T--) { memset(lis_len, 0, sizeof lis_len); memset(d_len, 0, sizeof d_len); sum_1 = sum_2 = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum_1 += a[i]; } scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%d", &b[i]); sum_2 += b[i]; } sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + m); if ((sum_1 & 1) || (sum_2 & 1) || n != m) { puts("No"); continue; } sum_1 >>= 1; sum_2 >>= 1; if (!work(a, sum_1, 0)) { puts("No"); continue; } if (!work(b, sum_2, 1)) { puts("No"); continue; } puts("Yes"); if (lis_len[0][0] <= lis_len[1][0]) { for (int i = 1; i <= lis_len[0][0]; i++) { d[0][++d_len[0]] = lis[0][0][i]; } for (int i = 1; i <= lis_len[0][1]; i++) { d[0][++d_len[0]] = -lis[0][1][i]; } for (int i = lis_len[1][0]; i > 0; i--) { d[1][++d_len[1]] = lis[1][0][i]; } for (int i = lis_len[1][1]; i > 0; i--) { d[1][++d_len[1]] = -lis[1][1][i]; } int x = 0, y = 0; for (int i = 1; i <= n; i++) { x += d[0][i]; printf("%d %d\n", x, y); y += d[1][i]; printf("%d %d\n", x, y); } } else { for (int i = 1; i <= lis_len[1][0]; i++) { d[0][++d_len[0]] = lis[1][0][i]; } for (int i = 1; i <= lis_len[1][1]; i++) { d[0][++d_len[0]] = -lis[1][1][i]; } for (int i = lis_len[0][0]; i > 0; i--) { d[1][++d_len[1]] = lis[0][0][i]; } for (int i = lis_len[0][1]; i > 0; i--) { d[1][++d_len[1]] = -lis[0][1][i]; } int x = 0, y = 0; for (int i = 1; i <= n; i++) { x += d[0][i]; printf("%d %d\n", y, x); y += d[1][i]; printf("%d %d\n", y, x); } } } 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 Maxn = 1000; int a[Maxn + 5], b[Maxn + 5]; int n, m; int sum_1, sum_2; bitset<Maxn * Maxn + 5> f[Maxn + 5]; int lis[2][2][Maxn + 5], lis_len[2][2]; bool work(int *a, int sum, int d) { f[0][0] = 1; for (int i = 1; i <= n; i++) { f[i] = (f[i - 1] | (f[i - 1] << a[i])); } if (f[n][sum] == 0) { return 0; } for (int i = n; i > 0; i--) { if (sum >= a[i] && f[i - 1][sum - a[i]]) { sum -= a[i]; lis[d][0][++lis_len[d][0]] = a[i]; } else { lis[d][1][++lis_len[d][1]] = a[i]; } } return 1; } int d[2][Maxn + 5]; int d_len[2]; int main() { int T; scanf("%d", &T); while (T--) { memset(lis_len, 0, sizeof lis_len); memset(d_len, 0, sizeof d_len); sum_1 = sum_2 = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum_1 += a[i]; } scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%d", &b[i]); sum_2 += b[i]; } sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + m); if ((sum_1 & 1) || (sum_2 & 1) || n != m) { puts("No"); continue; } sum_1 >>= 1; sum_2 >>= 1; if (!work(a, sum_1, 0)) { puts("No"); continue; } if (!work(b, sum_2, 1)) { puts("No"); continue; } puts("Yes"); if (lis_len[0][0] <= lis_len[1][0]) { for (int i = 1; i <= lis_len[0][0]; i++) { d[0][++d_len[0]] = lis[0][0][i]; } for (int i = 1; i <= lis_len[0][1]; i++) { d[0][++d_len[0]] = -lis[0][1][i]; } for (int i = lis_len[1][0]; i > 0; i--) { d[1][++d_len[1]] = lis[1][0][i]; } for (int i = lis_len[1][1]; i > 0; i--) { d[1][++d_len[1]] = -lis[1][1][i]; } int x = 0, y = 0; for (int i = 1; i <= n; i++) { x += d[0][i]; printf("%d %d\n", x, y); y += d[1][i]; printf("%d %d\n", x, y); } } else { for (int i = 1; i <= lis_len[1][0]; i++) { d[0][++d_len[0]] = lis[1][0][i]; } for (int i = 1; i <= lis_len[1][1]; i++) { d[0][++d_len[0]] = -lis[1][1][i]; } for (int i = lis_len[0][0]; i > 0; i--) { d[1][++d_len[1]] = lis[0][0][i]; } for (int i = lis_len[0][1]; i > 0; i--) { d[1][++d_len[1]] = -lis[0][1][i]; } int x = 0, y = 0; for (int i = 1; i <= n; i++) { x += d[0][i]; printf("%d %d\n", y, x); y += d[1][i]; printf("%d %d\n", y, x); } } } return 0; } ```
#include <bits/stdc++.h> using namespace std; int j, k, n, t, i, m, a[1050], b[1050], l1, l2, mx, sb1, sb2; vector<int> sb, x, y, v[1005000], resx1, resx2, resy1, resy2; bool dp[1005000], yes, tmp[1050]; queue<int> q; bool cmp(int x, int y) { return x > y; } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); l1 = l2 = 0; yes = 0; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); l1 += a[i]; } scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d", &b[i]); l2 += b[i]; } if (n != m || l2 % 2 || l1 % 2) { puts("nO"); continue; } l1 /= 2; l2 /= 2; queue<int> clr; swap(q, clr); memset(dp, 0, sizeof(bool) * (l1 + 5)); sb.clear(); sb.push_back(0); for (i = 1; i <= n; i++) { for (j = 0; j < sb.size(); j++) { if (sb[j] + a[i] <= l1 && !dp[sb[j] + a[i]]) { dp[sb[j] + a[i]] = 1; q.push(sb[j] + a[i]); v[sb[j] + a[i]] = v[sb[j]]; v[sb[j] + a[i]].push_back(i); } } if (dp[l1]) { yes = 1; break; } while (!q.empty()) { sb.push_back(q.front()); q.pop(); } } if (!yes) { puts("nO"); continue; } x = v[l1]; queue<int> clr2; swap(q, clr2); yes = 0; memset(dp, 0, sizeof(bool) * (l2 + 5)); sb.clear(); sb.push_back(0); for (i = 1; i <= m; i++) { for (j = 0; j < sb.size(); j++) { if (sb[j] + b[i] <= l2 && !dp[sb[j] + b[i]]) { dp[sb[j] + b[i]] = 1; q.push(sb[j] + b[i]); v[sb[j] + b[i]] = v[sb[j]]; v[sb[j] + b[i]].push_back(i); } } if (dp[l2]) { yes = 1; break; } while (!q.empty()) { sb.push_back(q.front()); q.pop(); } } if (!yes) { puts("nO"); continue; } y = v[l2]; memset(tmp, 0, sizeof(tmp)); for (i = 0; i < x.size(); i++) { tmp[x[i]] = 1; } resx2.clear(); resx1.clear(); for (i = 1; i <= n; i++) { if (!tmp[i]) { resx2.push_back(a[i]); } else { resx1.push_back(a[i]); } } memset(tmp, 0, sizeof(tmp)); for (i = 0; i < y.size(); i++) { tmp[y[i]] = 1; } resy2.clear(); resy1.clear(); for (i = 1; i <= m; i++) { if (!tmp[i]) { resy2.push_back(b[i]); } else { resy1.push_back(b[i]); } } if (resx1.size() > resx2.size()) { swap(resx1, resx2); } if (resy1.size() < resy2.size()) { swap(resy1, resy2); } sort(resx1.begin(), resx1.end(), cmp); sort(resx2.begin(), resx2.end(), cmp); sort(resy1.begin(), resy1.end()); sort(resy2.begin(), resy2.end()); for (i = 0; i < resx2.size(); i++) { resx1.push_back(-resx2[i]); } for (i = 0; i < resy2.size(); i++) { resy1.push_back(-resy2[i]); } sb1 = sb2 = 0; puts("yES"); for (i = 0; i < n; i++) { sb1 += resx1[i]; printf("%d %d\n", sb1, sb2); sb2 += resy1[i]; printf("%d %d\n", sb1, sb2); } } }
### 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 j, k, n, t, i, m, a[1050], b[1050], l1, l2, mx, sb1, sb2; vector<int> sb, x, y, v[1005000], resx1, resx2, resy1, resy2; bool dp[1005000], yes, tmp[1050]; queue<int> q; bool cmp(int x, int y) { return x > y; } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); l1 = l2 = 0; yes = 0; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); l1 += a[i]; } scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d", &b[i]); l2 += b[i]; } if (n != m || l2 % 2 || l1 % 2) { puts("nO"); continue; } l1 /= 2; l2 /= 2; queue<int> clr; swap(q, clr); memset(dp, 0, sizeof(bool) * (l1 + 5)); sb.clear(); sb.push_back(0); for (i = 1; i <= n; i++) { for (j = 0; j < sb.size(); j++) { if (sb[j] + a[i] <= l1 && !dp[sb[j] + a[i]]) { dp[sb[j] + a[i]] = 1; q.push(sb[j] + a[i]); v[sb[j] + a[i]] = v[sb[j]]; v[sb[j] + a[i]].push_back(i); } } if (dp[l1]) { yes = 1; break; } while (!q.empty()) { sb.push_back(q.front()); q.pop(); } } if (!yes) { puts("nO"); continue; } x = v[l1]; queue<int> clr2; swap(q, clr2); yes = 0; memset(dp, 0, sizeof(bool) * (l2 + 5)); sb.clear(); sb.push_back(0); for (i = 1; i <= m; i++) { for (j = 0; j < sb.size(); j++) { if (sb[j] + b[i] <= l2 && !dp[sb[j] + b[i]]) { dp[sb[j] + b[i]] = 1; q.push(sb[j] + b[i]); v[sb[j] + b[i]] = v[sb[j]]; v[sb[j] + b[i]].push_back(i); } } if (dp[l2]) { yes = 1; break; } while (!q.empty()) { sb.push_back(q.front()); q.pop(); } } if (!yes) { puts("nO"); continue; } y = v[l2]; memset(tmp, 0, sizeof(tmp)); for (i = 0; i < x.size(); i++) { tmp[x[i]] = 1; } resx2.clear(); resx1.clear(); for (i = 1; i <= n; i++) { if (!tmp[i]) { resx2.push_back(a[i]); } else { resx1.push_back(a[i]); } } memset(tmp, 0, sizeof(tmp)); for (i = 0; i < y.size(); i++) { tmp[y[i]] = 1; } resy2.clear(); resy1.clear(); for (i = 1; i <= m; i++) { if (!tmp[i]) { resy2.push_back(b[i]); } else { resy1.push_back(b[i]); } } if (resx1.size() > resx2.size()) { swap(resx1, resx2); } if (resy1.size() < resy2.size()) { swap(resy1, resy2); } sort(resx1.begin(), resx1.end(), cmp); sort(resx2.begin(), resx2.end(), cmp); sort(resy1.begin(), resy1.end()); sort(resy2.begin(), resy2.end()); for (i = 0; i < resx2.size(); i++) { resx1.push_back(-resx2[i]); } for (i = 0; i < resy2.size(); i++) { resy1.push_back(-resy2[i]); } sb1 = sb2 = 0; puts("yES"); for (i = 0; i < n; i++) { sb1 += resx1[i]; printf("%d %d\n", sb1, sb2); sb2 += resy1[i]; printf("%d %d\n", sb1, sb2); } } } ```
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmx(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmn(T &x, T y) { return x > y ? x = y, true : false; } const int maxn = 1000; const int maxw = 5e5; int T, n, m, a[maxn + 5], b[maxn + 5]; bitset<maxw> dp[maxn + 5]; vector<int> x[2], y[2]; bool check(int a[], vector<int> &x, vector<int> &y) { int sum = 0; for (int i = (1); i <= int(n); i++) sum += a[i], dp[i].reset(); if (sum & 1) return 0; sum >>= 1, dp[0].set(0); for (int i = (1); i <= int(n); i++) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[n][sum]) return 0; for (int i = (n); i >= int(1); i--) if (!dp[i - 1][sum]) x.push_back(a[i]), sum -= a[i]; else y.push_back(a[i]); return 1; } int main() { for (scanf("%d", &T); T; T--) { scanf("%d", &n), x[0].clear(), x[1].clear(), y[0].clear(), y[1].clear(); for (int i = (1); i <= int(n); i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = (1); i <= int(m); i++) scanf("%d", &b[i]); if (n != m) { puts("NO"); continue; } if (!check(a, x[0], x[1]) || !check(b, y[0], y[1])) { puts("NO"); continue; } puts("YES"); sort(x[0].begin(), x[0].end()), sort(x[1].begin(), x[1].end()); sort(y[0].begin(), y[0].end()), sort(y[1].begin(), y[1].end()); int nwx = 0, nwy = 0; ; if ((int)(x[0].size()) > (int)(y[0].size())) swap(x[0], x[1]), swap(y[0], y[1]); for (int i = (0); i <= int((int)(x[0].size()) - 1); i++) { nwx += x[0][(int)(x[0].size()) - 1 - i]; cout << nwx << ' ' << nwy << endl; nwy -= y[0][(int)(y[0].size()) - (int)(x[0].size()) + i]; cout << nwx << ' ' << nwy << endl; } for (int i = (0); i <= int((int)(y[0].size()) - (int)(x[0].size()) - 1); i++) { nwx -= x[1][(int)(y[1].size()) + i]; cout << nwx << ' ' << nwy << endl; nwy -= y[0][(int)(y[0].size()) - (int)(x[0].size()) - i - 1]; cout << nwx << ' ' << nwy << endl; } for (int i = (0); i <= int((int)(y[1].size()) - 1); i++) { nwx -= x[1][(int)(y[1].size()) - i - 1]; cout << nwx << ' ' << nwy << endl; nwy += y[1][i]; cout << nwx << ' ' << nwy << endl; } } 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; template <typename T> bool chkmx(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmn(T &x, T y) { return x > y ? x = y, true : false; } const int maxn = 1000; const int maxw = 5e5; int T, n, m, a[maxn + 5], b[maxn + 5]; bitset<maxw> dp[maxn + 5]; vector<int> x[2], y[2]; bool check(int a[], vector<int> &x, vector<int> &y) { int sum = 0; for (int i = (1); i <= int(n); i++) sum += a[i], dp[i].reset(); if (sum & 1) return 0; sum >>= 1, dp[0].set(0); for (int i = (1); i <= int(n); i++) dp[i] = dp[i - 1] | (dp[i - 1] << a[i]); if (!dp[n][sum]) return 0; for (int i = (n); i >= int(1); i--) if (!dp[i - 1][sum]) x.push_back(a[i]), sum -= a[i]; else y.push_back(a[i]); return 1; } int main() { for (scanf("%d", &T); T; T--) { scanf("%d", &n), x[0].clear(), x[1].clear(), y[0].clear(), y[1].clear(); for (int i = (1); i <= int(n); i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = (1); i <= int(m); i++) scanf("%d", &b[i]); if (n != m) { puts("NO"); continue; } if (!check(a, x[0], x[1]) || !check(b, y[0], y[1])) { puts("NO"); continue; } puts("YES"); sort(x[0].begin(), x[0].end()), sort(x[1].begin(), x[1].end()); sort(y[0].begin(), y[0].end()), sort(y[1].begin(), y[1].end()); int nwx = 0, nwy = 0; ; if ((int)(x[0].size()) > (int)(y[0].size())) swap(x[0], x[1]), swap(y[0], y[1]); for (int i = (0); i <= int((int)(x[0].size()) - 1); i++) { nwx += x[0][(int)(x[0].size()) - 1 - i]; cout << nwx << ' ' << nwy << endl; nwy -= y[0][(int)(y[0].size()) - (int)(x[0].size()) + i]; cout << nwx << ' ' << nwy << endl; } for (int i = (0); i <= int((int)(y[0].size()) - (int)(x[0].size()) - 1); i++) { nwx -= x[1][(int)(y[1].size()) + i]; cout << nwx << ' ' << nwy << endl; nwy -= y[0][(int)(y[0].size()) - (int)(x[0].size()) - i - 1]; cout << nwx << ' ' << nwy << endl; } for (int i = (0); i <= int((int)(y[1].size()) - 1); i++) { nwx -= x[1][(int)(y[1].size()) - i - 1]; cout << nwx << ' ' << nwy << endl; nwy += y[1][i]; cout << nwx << ' ' << nwy << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &num) { T x = 0, f = 1; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') f = -1; for (; ch <= '9' && ch >= '0'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ '0'); num = x * f; } int ttt, n, m, a[2105], a1[2105], a2[2105], b[2105], b1[2105], b2[2105], t1[2105], t2[2105], suma, sumb, X, Y; int ans[2105 << 1][2], cnt; bitset<1000005> dp[1005]; bool chk(int *c, int *c1, int *c2, int sumc) { for (int i = 1; i <= n; i++) dp[i].reset(); dp[0][0] = 1; for (int i = 1; i <= n; i++) dp[i] = (dp[i - 1] << c[i]) | dp[i - 1]; if (!dp[n][sumc]) return 1; c1[0] = c2[0] = 0; for (int i = n; i; i--) { if (sumc >= c[i] && dp[i - 1][sumc - c[i]]) c1[++c1[0]] = c[i], sumc -= c[i]; else c2[++c2[0]] = c[i]; } return 0; } void solve(int *c1, int *c2, int d1, int d2) { int l = c1[0]; if (!l) return; sort(c1 + 1, c1 + l + 1, greater<int>()); sort(c2 + 1, c2 + l + 1); for (int i = 1; i <= l; i++) { X += c1[i] * d1; ans[++cnt][0] = X; ans[cnt][1] = Y; Y += c2[i] * d2; ans[++cnt][0] = X; ans[cnt][1] = Y; } } int main() { read(ttt); while (ttt--) { suma = sumb = 0; read(n); for (int i = 1; i <= n; i++) read(a[i]), suma += a[i]; read(m); for (int i = 1; i <= m; i++) read(b[i]), sumb += b[i]; if (n != m || (suma & 1) || (sumb & 1)) { puts("NO"); continue; } if (chk(a, a1, a2, suma >> 1) || chk(b, b1, b2, sumb >> 1)) { puts("NO"); continue; } puts("YES"); if (a1[0] > a2[0]) swap(a1, a2); if (b1[0] < b2[0]) swap(b1, b2); X = Y = cnt = 0; t1[0] = t2[0] = 0; for (int i = 1; i <= a1[0]; i++) { t1[++t1[0]] = a1[i]; t2[++t2[0]] = b1[i]; } solve(t1, t2, 1, 1); t1[0] = t2[0] = 0; for (int i = 1; i <= b1[0] - a1[0]; i++) { t1[++t1[0]] = a2[i]; t2[++t2[0]] = b1[i + a1[0]]; } solve(t1, t2, -1, 1); t1[0] = t2[0] = 0; for (int i = 1; i <= b2[0]; i++) { t1[++t1[0]] = a2[i + b1[0] - a1[0]]; t2[++t2[0]] = b2[i]; } solve(t1, t2, -1, -1); for (int i = 1; i <= cnt; i++) { printf("%d %d\n", ans[i][0], ans[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; template <typename T> inline void read(T &num) { T x = 0, f = 1; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) if (ch == '-') f = -1; for (; ch <= '9' && ch >= '0'; ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ '0'); num = x * f; } int ttt, n, m, a[2105], a1[2105], a2[2105], b[2105], b1[2105], b2[2105], t1[2105], t2[2105], suma, sumb, X, Y; int ans[2105 << 1][2], cnt; bitset<1000005> dp[1005]; bool chk(int *c, int *c1, int *c2, int sumc) { for (int i = 1; i <= n; i++) dp[i].reset(); dp[0][0] = 1; for (int i = 1; i <= n; i++) dp[i] = (dp[i - 1] << c[i]) | dp[i - 1]; if (!dp[n][sumc]) return 1; c1[0] = c2[0] = 0; for (int i = n; i; i--) { if (sumc >= c[i] && dp[i - 1][sumc - c[i]]) c1[++c1[0]] = c[i], sumc -= c[i]; else c2[++c2[0]] = c[i]; } return 0; } void solve(int *c1, int *c2, int d1, int d2) { int l = c1[0]; if (!l) return; sort(c1 + 1, c1 + l + 1, greater<int>()); sort(c2 + 1, c2 + l + 1); for (int i = 1; i <= l; i++) { X += c1[i] * d1; ans[++cnt][0] = X; ans[cnt][1] = Y; Y += c2[i] * d2; ans[++cnt][0] = X; ans[cnt][1] = Y; } } int main() { read(ttt); while (ttt--) { suma = sumb = 0; read(n); for (int i = 1; i <= n; i++) read(a[i]), suma += a[i]; read(m); for (int i = 1; i <= m; i++) read(b[i]), sumb += b[i]; if (n != m || (suma & 1) || (sumb & 1)) { puts("NO"); continue; } if (chk(a, a1, a2, suma >> 1) || chk(b, b1, b2, sumb >> 1)) { puts("NO"); continue; } puts("YES"); if (a1[0] > a2[0]) swap(a1, a2); if (b1[0] < b2[0]) swap(b1, b2); X = Y = cnt = 0; t1[0] = t2[0] = 0; for (int i = 1; i <= a1[0]; i++) { t1[++t1[0]] = a1[i]; t2[++t2[0]] = b1[i]; } solve(t1, t2, 1, 1); t1[0] = t2[0] = 0; for (int i = 1; i <= b1[0] - a1[0]; i++) { t1[++t1[0]] = a2[i]; t2[++t2[0]] = b1[i + a1[0]]; } solve(t1, t2, -1, 1); t1[0] = t2[0] = 0; for (int i = 1; i <= b2[0]; i++) { t1[++t1[0]] = a2[i + b1[0] - a1[0]]; t2[++t2[0]] = b2[i]; } solve(t1, t2, -1, -1); for (int i = 1; i <= cnt; i++) { printf("%d %d\n", ans[i][0], ans[i][1]); } } return 0; } ```
#include <bits/stdc++.h> int T; int n, m, a[1005], b[1005]; std::bitset<1005 * 1005> f[1005]; std::vector<int> a1, a2, b1, b2; 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; } int sum = 0; for (int i = 1; i <= n; i++) sum += a[i]; if (sum % 2 == 1) { puts("No"); continue; } f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); sum /= 2; if (!f[n][sum]) { puts("No"); continue; } a1.clear(); a2.clear(); for (int i = n; i; i--) if (a[i] <= sum && f[i - 1][sum - a[i]]) { sum -= a[i]; a1.push_back(a[i]); } else a2.push_back(a[i]); sum = 0; for (int i = 1; i <= n; i++) sum += b[i]; if (sum % 2 == 1) { puts("No"); continue; } sum /= 2; f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (!f[n][sum]) { puts("No"); continue; } b1.clear(); b2.clear(); for (int i = n; i; i--) if (b[i] <= sum && f[i - 1][sum - b[i]]) { sum -= b[i]; b1.push_back(b[i]); } else b2.push_back(b[i]); std::sort(a1.begin(), a1.end()); std::sort(a2.begin(), a2.end()); std::sort(b1.begin(), b1.end()); std::sort(b2.begin(), b2.end()); if (a1.size() > a2.size()) std::swap(a1, a2); if (b1.size() < b2.size()) std::swap(b1, b2); std::vector<int>::iterator i = a1.end(), j = b1.begin(); int x = 0, y = 0, t1 = 1, t2 = 1; puts("Yes"); while (n--) { x += t1 * (*--i); printf("%d %d\n", x, y); y += t2 * (*j++); printf("%d %d\n", x, y); if (i == a1.begin()) { t1 = -1; i = a2.end(); } if (j == b1.end()) { t2 = -1; j = b2.begin(); } } } }
### 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> int T; int n, m, a[1005], b[1005]; std::bitset<1005 * 1005> f[1005]; std::vector<int> a1, a2, b1, b2; 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; } int sum = 0; for (int i = 1; i <= n; i++) sum += a[i]; if (sum % 2 == 1) { puts("No"); continue; } f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << a[i]); sum /= 2; if (!f[n][sum]) { puts("No"); continue; } a1.clear(); a2.clear(); for (int i = n; i; i--) if (a[i] <= sum && f[i - 1][sum - a[i]]) { sum -= a[i]; a1.push_back(a[i]); } else a2.push_back(a[i]); sum = 0; for (int i = 1; i <= n; i++) sum += b[i]; if (sum % 2 == 1) { puts("No"); continue; } sum /= 2; f[0][0] = 1; for (int i = 1; i <= n; i++) f[i] = f[i - 1] | (f[i - 1] << b[i]); if (!f[n][sum]) { puts("No"); continue; } b1.clear(); b2.clear(); for (int i = n; i; i--) if (b[i] <= sum && f[i - 1][sum - b[i]]) { sum -= b[i]; b1.push_back(b[i]); } else b2.push_back(b[i]); std::sort(a1.begin(), a1.end()); std::sort(a2.begin(), a2.end()); std::sort(b1.begin(), b1.end()); std::sort(b2.begin(), b2.end()); if (a1.size() > a2.size()) std::swap(a1, a2); if (b1.size() < b2.size()) std::swap(b1, b2); std::vector<int>::iterator i = a1.end(), j = b1.begin(); int x = 0, y = 0, t1 = 1, t2 = 1; puts("Yes"); while (n--) { x += t1 * (*--i); printf("%d %d\n", x, y); y += t2 * (*j++); printf("%d %d\n", x, y); if (i == a1.begin()) { t1 = -1; i = a2.end(); } if (j == b1.end()) { t2 = -1; j = b2.begin(); } } } } ```
#include <bits/stdc++.h> 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; } 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; const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[8] = {0, 1, 0, -1, -1, 1, -1, 1}; 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); 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 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...); } const int mxN = 3e5 + 5; int q = 1, n, m; vector<int> a, b; pair<vector<int>, vector<int>> partition(vector<int>& v) { int sum = accumulate(begin(v), end(v), 0); if (sum % 2) return {vector<int>(), vector<int>()}; sum /= 2; vector<bitset<mxN>> pos(v.size() + 5); pos[0][0] = 1; for (int i = (0); i < (v.size()); i++) pos[i + 1] = pos[i] | pos[i] << v[i]; if (!pos[v.size()][sum]) return {vector<int>(), vector<int>()}; int idx = v.size(); for (int i = (v.size()) - 1; i >= (0); i--) { if (sum >= v[i] && pos[i][sum - v[i]]) sum -= v[i]; else swap(v[i], v[--idx]); } return {vector<int>(v.begin(), v.begin() + idx), vector<int>(v.begin() + idx, v.end())}; } void solve() { cin >> n; a.resize(n); for (int i = (0); i < (n); i++) cin >> a[i]; cin >> m; b.resize(m); for (int i = (0); i < (m); i++) cin >> b[i]; pair<vector<int>, vector<int>> h = partition(a), v = partition(b); if (n != m || (!h.first.size() && !h.second.size()) || (!v.first.size() && !v.second.size())) cout << "No\n"; else { cout << "Yes\n"; vector<int> r = h.first, l = h.second, u = v.first, d = v.second; assert(l.size() + r.size() == d.size() + u.size()); assert(accumulate(begin(l), end(l), 0) == accumulate(begin(r), end(r), 0) && accumulate(begin(d), end(d), 0) == accumulate(begin(u), end(u), 0)); if (r.size() > l.size()) swap(l, r); if (d.size() > u.size()) swap(d, u); sort(begin(r), end(r)), sort(begin(l), end(l)), sort(begin(u), end(u)), sort(begin(d), end(d)); reverse(begin(r), end(r)), reverse(begin(d), end(d)); pair<int, int> p; int cl = 0, cr = 0, cd = 0, cu = 0; for (int i = (0); i < (r.size()); i++) { p.first += r[i]; cout << p.first << " " << p.second << "\n"; p.second += u[i]; cout << p.first << " " << p.second << "\n"; cr++, cu++; } for (int i = (r.size()); i < (u.size()); i++) { p.first -= l[i - r.size() + d.size()]; cout << p.first << " " << p.second << "\n"; p.second += u[i]; cout << p.first << " " << p.second << "\n"; cu++, cl++; } for (int i = (d.size()) - 1; i >= (0); i--) { p.first -= l[i]; cout << p.first << " " << p.second << "\n"; p.second -= d[i]; cout << p.first << " " << p.second << "\n"; cl++, cd++; } } a.clear(), b.clear(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> q; while (q--) { solve(); } }
### 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; 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; const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[8] = {0, 1, 0, -1, -1, 1, -1, 1}; 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); 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 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...); } const int mxN = 3e5 + 5; int q = 1, n, m; vector<int> a, b; pair<vector<int>, vector<int>> partition(vector<int>& v) { int sum = accumulate(begin(v), end(v), 0); if (sum % 2) return {vector<int>(), vector<int>()}; sum /= 2; vector<bitset<mxN>> pos(v.size() + 5); pos[0][0] = 1; for (int i = (0); i < (v.size()); i++) pos[i + 1] = pos[i] | pos[i] << v[i]; if (!pos[v.size()][sum]) return {vector<int>(), vector<int>()}; int idx = v.size(); for (int i = (v.size()) - 1; i >= (0); i--) { if (sum >= v[i] && pos[i][sum - v[i]]) sum -= v[i]; else swap(v[i], v[--idx]); } return {vector<int>(v.begin(), v.begin() + idx), vector<int>(v.begin() + idx, v.end())}; } void solve() { cin >> n; a.resize(n); for (int i = (0); i < (n); i++) cin >> a[i]; cin >> m; b.resize(m); for (int i = (0); i < (m); i++) cin >> b[i]; pair<vector<int>, vector<int>> h = partition(a), v = partition(b); if (n != m || (!h.first.size() && !h.second.size()) || (!v.first.size() && !v.second.size())) cout << "No\n"; else { cout << "Yes\n"; vector<int> r = h.first, l = h.second, u = v.first, d = v.second; assert(l.size() + r.size() == d.size() + u.size()); assert(accumulate(begin(l), end(l), 0) == accumulate(begin(r), end(r), 0) && accumulate(begin(d), end(d), 0) == accumulate(begin(u), end(u), 0)); if (r.size() > l.size()) swap(l, r); if (d.size() > u.size()) swap(d, u); sort(begin(r), end(r)), sort(begin(l), end(l)), sort(begin(u), end(u)), sort(begin(d), end(d)); reverse(begin(r), end(r)), reverse(begin(d), end(d)); pair<int, int> p; int cl = 0, cr = 0, cd = 0, cu = 0; for (int i = (0); i < (r.size()); i++) { p.first += r[i]; cout << p.first << " " << p.second << "\n"; p.second += u[i]; cout << p.first << " " << p.second << "\n"; cr++, cu++; } for (int i = (r.size()); i < (u.size()); i++) { p.first -= l[i - r.size() + d.size()]; cout << p.first << " " << p.second << "\n"; p.second += u[i]; cout << p.first << " " << p.second << "\n"; cu++, cl++; } for (int i = (d.size()) - 1; i >= (0); i--) { p.first -= l[i]; cout << p.first << " " << p.second << "\n"; p.second -= d[i]; cout << p.first << " " << p.second << "\n"; cl++, cd++; } } a.clear(), b.clear(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> q; while (q--) { solve(); } } ```
#include <bits/stdc++.h> using namespace std; int T, n, m, a[1005], b[1005], A[1005], cn1 = 0, B[1005], cn2 = 0, C[1005], cn3 = 0, D[1005], cn4 = 0, su = 0, f1 = 1, f2 = 1; bool fl; bitset<1000002> f[1005]; inline bool cmp1(int x, int y) { return (x > y); } inline bool cmp2(int x, int y) { return (x < y); } pair<int, int> all[1005 << 1]; bool vi[1005 << 1]; int main() { scanf("%d", &T); f[0][0] = 1; while (T--) { scanf("%d", &n); int i, X = 0, Y = 0; fl = true; su = 0; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); su += a[i]; f[i] = f[i - 1] | (f[i - 1] << a[i]); } if ((su & 1) || (!f[n][su >> 1])) fl = false; if (fl) { su >>= 1; cn1 = cn3 = 0; for (i = n; i >= 1; i--) if ((su >= a[i]) && f[i - 1][su - a[i]]) su -= (A[++cn1] = a[i]); else C[++cn3] = a[i]; } scanf("%d", &m); su = 0; for (i = 1; i <= m; i++) { scanf("%d", &b[i]); su += b[i]; f[i] = f[i - 1] | (f[i - 1] << b[i]); } if ((su & 1) || (!f[m][su >> 1])) fl = false; if (fl) { su >>= 1; cn2 = cn4 = 0; for (i = m; i >= 1; i--) if ((su >= b[i]) && f[i - 1][su - b[i]]) su -= (B[++cn2] = b[i]); else D[++cn4] = b[i]; } if ((n != m) || (!fl)) { printf("No\n"); continue; } if (cn1 > cn3) swap(cn1, cn3), swap(A, C); if (cn2 < cn4) swap(cn2, cn4), swap(B, D); printf("Yes\n"); sort(A + 1, A + cn1 + 1, cmp1); sort(C + 1, C + cn3 + 1, cmp1); sort(B + 1, B + cn2 + 1, cmp2); sort(D + 1, D + cn4 + 1, cmp2); int o1 = 1, o2 = 2; memset(vi, 0, sizeof(vi)); for (i = 1; i <= cn1; i++) all[o1] = make_pair(A[i], 0), vi[o1] = 1, o1 += 2; for (i = 1; i <= cn2; i++) all[o2] = make_pair(0, B[i]), vi[o2] = 1, o2 += 2; for (i = 1; i <= cn3; i++) all[o1] = make_pair(-C[i], 0), vi[o1] = 1, o1 += 2; for (i = 1; i <= cn4; i++) all[o2] = make_pair(0, -D[i]), vi[o2] = 1, o2 += 2; for (i = 1; i <= max(o1, o2); i++) if (vi[i]) printf("%d %d\n", X, Y), X += all[i].first, Y += all[i].second; } 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; int T, n, m, a[1005], b[1005], A[1005], cn1 = 0, B[1005], cn2 = 0, C[1005], cn3 = 0, D[1005], cn4 = 0, su = 0, f1 = 1, f2 = 1; bool fl; bitset<1000002> f[1005]; inline bool cmp1(int x, int y) { return (x > y); } inline bool cmp2(int x, int y) { return (x < y); } pair<int, int> all[1005 << 1]; bool vi[1005 << 1]; int main() { scanf("%d", &T); f[0][0] = 1; while (T--) { scanf("%d", &n); int i, X = 0, Y = 0; fl = true; su = 0; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); su += a[i]; f[i] = f[i - 1] | (f[i - 1] << a[i]); } if ((su & 1) || (!f[n][su >> 1])) fl = false; if (fl) { su >>= 1; cn1 = cn3 = 0; for (i = n; i >= 1; i--) if ((su >= a[i]) && f[i - 1][su - a[i]]) su -= (A[++cn1] = a[i]); else C[++cn3] = a[i]; } scanf("%d", &m); su = 0; for (i = 1; i <= m; i++) { scanf("%d", &b[i]); su += b[i]; f[i] = f[i - 1] | (f[i - 1] << b[i]); } if ((su & 1) || (!f[m][su >> 1])) fl = false; if (fl) { su >>= 1; cn2 = cn4 = 0; for (i = m; i >= 1; i--) if ((su >= b[i]) && f[i - 1][su - b[i]]) su -= (B[++cn2] = b[i]); else D[++cn4] = b[i]; } if ((n != m) || (!fl)) { printf("No\n"); continue; } if (cn1 > cn3) swap(cn1, cn3), swap(A, C); if (cn2 < cn4) swap(cn2, cn4), swap(B, D); printf("Yes\n"); sort(A + 1, A + cn1 + 1, cmp1); sort(C + 1, C + cn3 + 1, cmp1); sort(B + 1, B + cn2 + 1, cmp2); sort(D + 1, D + cn4 + 1, cmp2); int o1 = 1, o2 = 2; memset(vi, 0, sizeof(vi)); for (i = 1; i <= cn1; i++) all[o1] = make_pair(A[i], 0), vi[o1] = 1, o1 += 2; for (i = 1; i <= cn2; i++) all[o2] = make_pair(0, B[i]), vi[o2] = 1, o2 += 2; for (i = 1; i <= cn3; i++) all[o1] = make_pair(-C[i], 0), vi[o1] = 1, o1 += 2; for (i = 1; i <= cn4; i++) all[o2] = make_pair(0, -D[i]), vi[o2] = 1, o2 += 2; for (i = 1; i <= max(o1, o2); i++) if (vi[i]) printf("%d %d\n", X, Y), X += all[i].first, Y += all[i].second; } 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 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 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; 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 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; 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; const int MAXN = 1010; const int MAXL = 250010; int x[MAXN], y[MAXN]; int f[MAXL]; bool GetValue(int n, int* x, vector<int>& x1, vector<int>& x2) { int sumx = 0; for (int i = 0; i < n; ++i) sumx += x[i]; if (sumx & 1) return false; sort(x, x + n); memset(f, -1, sizeof(f)); f[0] = 0; int sum = 0; for (int i = 0; i < n; ++i) { sum += x[i]; if (sum > sumx / 2) sum = sumx / 2; for (int j = sum; j >= x[i]; --j) { if (f[j] != -1) continue; if (f[j - x[i]] != -1) f[j] = i; } } if (f[sum] == -1) { return false; } while (sum > 0) { x1.push_back(f[sum]); sum -= x[f[sum]]; } sort(x1.begin(), x1.end()); int k = 0; for (int i = 0; i < n; ++i) { if (k < x1.size() && x1[k] == i) { ++k; continue; } x2.push_back(i); } return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); int times; cin >> times; while (times--) { int n, m; cin >> n; for (int i = 0; i < n; ++i) cin >> x[i]; cin >> m; for (int i = 0; i < m; ++i) cin >> y[i]; if (n != m) { cout << "No" << endl; continue; } vector<int> x1, x2, y1, y2; if (!GetValue(n, x, x1, x2) || !GetValue(m, y, y1, y2)) { cout << "No" << endl; continue; } if (x1.size() > x2.size()) swap(x1, x2); if (y1.size() < y2.size()) swap(y1, y2); reverse(x1.begin(), x1.end()); reverse(y2.begin(), y2.end()); vector<pair<int, int>> ansx, ansy; int tx = 0, ty = 0; for (int i = 0; i < x1.size(); ++i) { tx += x[x1[i]]; ansx.push_back(make_pair(tx, ty)); ty += y[y1[i]]; ansx.push_back(make_pair(tx, ty)); } tx = 0, ty = 0; for (int i = 0; i < y2.size(); ++i) { ty += y[y2[i]]; ansy.push_back(make_pair(tx, ty)); tx += x[x2[i]]; ansy.push_back(make_pair(tx, ty)); } int base = y1.size() - x1.size(); for (int i = 0; i < base; ++i) { ty -= y[y1[i + x1.size()]]; ansy.push_back(make_pair(tx, ty)); tx += x[x2[i + y2.size()]]; ansy.push_back(make_pair(tx, ty)); } cout << "Yes" << endl; for (auto& [a, b] : ansx) cout << a << " " << b << endl; for (int i = (int)ansy.size() - 2; i >= 0; --i) { cout << ansy[i].first << " " << ansy[i].second << endl; } cout << "0 0" << endl; } 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 = 1010; const int MAXL = 250010; int x[MAXN], y[MAXN]; int f[MAXL]; bool GetValue(int n, int* x, vector<int>& x1, vector<int>& x2) { int sumx = 0; for (int i = 0; i < n; ++i) sumx += x[i]; if (sumx & 1) return false; sort(x, x + n); memset(f, -1, sizeof(f)); f[0] = 0; int sum = 0; for (int i = 0; i < n; ++i) { sum += x[i]; if (sum > sumx / 2) sum = sumx / 2; for (int j = sum; j >= x[i]; --j) { if (f[j] != -1) continue; if (f[j - x[i]] != -1) f[j] = i; } } if (f[sum] == -1) { return false; } while (sum > 0) { x1.push_back(f[sum]); sum -= x[f[sum]]; } sort(x1.begin(), x1.end()); int k = 0; for (int i = 0; i < n; ++i) { if (k < x1.size() && x1[k] == i) { ++k; continue; } x2.push_back(i); } return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); int times; cin >> times; while (times--) { int n, m; cin >> n; for (int i = 0; i < n; ++i) cin >> x[i]; cin >> m; for (int i = 0; i < m; ++i) cin >> y[i]; if (n != m) { cout << "No" << endl; continue; } vector<int> x1, x2, y1, y2; if (!GetValue(n, x, x1, x2) || !GetValue(m, y, y1, y2)) { cout << "No" << endl; continue; } if (x1.size() > x2.size()) swap(x1, x2); if (y1.size() < y2.size()) swap(y1, y2); reverse(x1.begin(), x1.end()); reverse(y2.begin(), y2.end()); vector<pair<int, int>> ansx, ansy; int tx = 0, ty = 0; for (int i = 0; i < x1.size(); ++i) { tx += x[x1[i]]; ansx.push_back(make_pair(tx, ty)); ty += y[y1[i]]; ansx.push_back(make_pair(tx, ty)); } tx = 0, ty = 0; for (int i = 0; i < y2.size(); ++i) { ty += y[y2[i]]; ansy.push_back(make_pair(tx, ty)); tx += x[x2[i]]; ansy.push_back(make_pair(tx, ty)); } int base = y1.size() - x1.size(); for (int i = 0; i < base; ++i) { ty -= y[y1[i + x1.size()]]; ansy.push_back(make_pair(tx, ty)); tx += x[x2[i + y2.size()]]; ansy.push_back(make_pair(tx, ty)); } cout << "Yes" << endl; for (auto& [a, b] : ansx) cout << a << " " << b << endl; for (int i = (int)ansy.size() - 2; i >= 0; --i) { cout << ansy[i].first << " " << ansy[i].second << endl; } cout << "0 0" << endl; } return 0; } ```
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define F first #define S second #define _IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); const ll N=1e6+9,mod=1e9+7,inf=1e18; string s; ll n,k; int main() { _IO; ll t;cin>>t; while(t--) { cin>>n>>k; cin>>s; ll cnt[n]; memset(cnt,0,sizeof cnt); cnt[0]=s[0]-48; for(int i=1;i<n;i++)cnt[i]=cnt[i-1]+s[i]-48; set<ll>st; ll m=min(20LL,k); ll msk=0; for(int i=k-m;i<k-1;i++) { msk=(msk<<1); msk =msk | (s[i]-48); } //st.insert(msk); //cout<<msk<<" "; for(int i=k-1;i<n;i++) { if( (msk&(1<<(m-1))) )msk=(msk^(1<<(m-1))); msk=(msk<<1); msk =msk | (s[i]-48); ll l=0; if(i-m>=0)l=cnt[i-m]; if(i-k>=0)l-=cnt[i-k]; if(l==k-m)st.insert(msk); //cout<<msk<<" "; } ll ans=(1<<m); msk=(1<<m)-1; while(msk>=0) { if(st.find(msk)==st.end()) { ans=(((1<<m)-1)^msk); break; } msk--; } stack<ll>sc; while(ans) { sc.push(ans%2); ans/=2; } while(sc.size()<k)sc.push(0); if(sc.size()>k)cout<<"NO\n"; else { cout<<"YES\n"; while(!sc.empty()) { cout<<sc.top(); sc.pop(); } cout<<"\n"; } } }
### Prompt Develop a solution in Cpp to the problem described below: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define F first #define S second #define _IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); const ll N=1e6+9,mod=1e9+7,inf=1e18; string s; ll n,k; int main() { _IO; ll t;cin>>t; while(t--) { cin>>n>>k; cin>>s; ll cnt[n]; memset(cnt,0,sizeof cnt); cnt[0]=s[0]-48; for(int i=1;i<n;i++)cnt[i]=cnt[i-1]+s[i]-48; set<ll>st; ll m=min(20LL,k); ll msk=0; for(int i=k-m;i<k-1;i++) { msk=(msk<<1); msk =msk | (s[i]-48); } //st.insert(msk); //cout<<msk<<" "; for(int i=k-1;i<n;i++) { if( (msk&(1<<(m-1))) )msk=(msk^(1<<(m-1))); msk=(msk<<1); msk =msk | (s[i]-48); ll l=0; if(i-m>=0)l=cnt[i-m]; if(i-k>=0)l-=cnt[i-k]; if(l==k-m)st.insert(msk); //cout<<msk<<" "; } ll ans=(1<<m); msk=(1<<m)-1; while(msk>=0) { if(st.find(msk)==st.end()) { ans=(((1<<m)-1)^msk); break; } msk--; } stack<ll>sc; while(ans) { sc.push(ans%2); ans/=2; } while(sc.size()<k)sc.push(0); if(sc.size()>k)cout<<"NO\n"; else { cout<<"YES\n"; while(!sc.empty()) { cout<<sc.top(); sc.pop(); } cout<<"\n"; } } } ```
#include<cstdio> #include<queue> #include<iostream> #include<cstring> #include<stack> #include<list> #include<set> #include<map> #include<string> #include<vector> #include<cmath> #include<algorithm> #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; typedef long long ll; const int maxn = 1e6 + 8; const int mod = 1e9+7; char str[maxn],str1[maxn],ans[maxn]; set<string> se; int main(){ // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int T;scanf("%d",&T); while(T--){ int n,k;scanf("%d%d",&n,&k); scanf("%s",str+1); se.clear(); int m=1,val=n-k+1; while(m<=k && (1<<m)<val+1) m++; m = min(m,k); int t = 0; for(int i=1;i<=k;i++){ if(str[i]=='1') str1[i]='0'; else str1[i]='1'; if(str[i]=='0'&&i<=k-m) t--; } if(t==0) se.insert(str1+(k-m+1)); for(int i=k+1;i<=n;i++){ if(str[i]=='1') str1[i]='0'; else str1[i]='1'; if(str[i-m]=='0') t--; if(str[i-k]=='0') t++; if(t==0) se.insert(str1+(i-m+1)); } // for(auto i=se.begin();i!=se.end();i++){ // cout<<*i<<endl; // } for(int i=1;i<=n;i++) str1[i]='\0'; ans[m+1]='\0'; int flag = 0; for(int i=0;i<(1<<m);i++){ for(int j=0;j<m;j++){ if(i&(1<<j)) ans[m-j]='1'; else ans[m-j]='0'; } // printf("%d:%s\n",i,ans+1); if(se.find(ans+1)==se.end()){ flag = 1; break; } } if(flag==1){ printf("YES\n"); for(int i=1;i<=k-m;i++) printf("0"); for(int i=1;i<=m;i++) printf("%c",ans[i]); printf("\n"); } else printf("NO\n"); } }
### Prompt Generate a CPP solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include<cstdio> #include<queue> #include<iostream> #include<cstring> #include<stack> #include<list> #include<set> #include<map> #include<string> #include<vector> #include<cmath> #include<algorithm> #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; typedef long long ll; const int maxn = 1e6 + 8; const int mod = 1e9+7; char str[maxn],str1[maxn],ans[maxn]; set<string> se; int main(){ // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int T;scanf("%d",&T); while(T--){ int n,k;scanf("%d%d",&n,&k); scanf("%s",str+1); se.clear(); int m=1,val=n-k+1; while(m<=k && (1<<m)<val+1) m++; m = min(m,k); int t = 0; for(int i=1;i<=k;i++){ if(str[i]=='1') str1[i]='0'; else str1[i]='1'; if(str[i]=='0'&&i<=k-m) t--; } if(t==0) se.insert(str1+(k-m+1)); for(int i=k+1;i<=n;i++){ if(str[i]=='1') str1[i]='0'; else str1[i]='1'; if(str[i-m]=='0') t--; if(str[i-k]=='0') t++; if(t==0) se.insert(str1+(i-m+1)); } // for(auto i=se.begin();i!=se.end();i++){ // cout<<*i<<endl; // } for(int i=1;i<=n;i++) str1[i]='\0'; ans[m+1]='\0'; int flag = 0; for(int i=0;i<(1<<m);i++){ for(int j=0;j<m;j++){ if(i&(1<<j)) ans[m-j]='1'; else ans[m-j]='0'; } // printf("%d:%s\n",i,ans+1); if(se.find(ans+1)==se.end()){ flag = 1; break; } } if(flag==1){ printf("YES\n"); for(int i=1;i<=k-m;i++) printf("0"); for(int i=1;i<=m;i++) printf("%c",ans[i]); printf("\n"); } else printf("NO\n"); } } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=1<<20; int N,K; string s; bool Vis[MAXN]; inline void work(){ cin>>N>>K>>s; int len=min(20,K),U=(1<<len)-1,num=0,Cnt[2]={0}; for(int i=0;i<K-len;i++) ++Cnt[s[i]&1]; for(int i=K-len;i<K;i++) num=(num<<1)|(s[i]+1&1); if(!Cnt[0]) Vis[num]=1; for(int i=K;i<s.size();i++){ num=((num<<1)|(s[i]+1&1))&U; --Cnt[s[i-K]&1]; ++Cnt[s[i-len]&1]; if(!Cnt[0]) Vis[num]=1; } int res=-1; for(int i=0;i<=U&&res==-1;i++) if(!Vis[i]) res=i; if(res==-1) cout<<"NO\n"; else{ cout<<"YES\n"; for(int i=K-len;i>=1;i--) cout<<"0"; for(int i=len-1;i>=0;i--) cout<<((res>>i)&1); cout<<"\n"; } num=0; for(int i=K-len;i<K-1;i++) num=(num<<1)|(s[i]+1&1); for(int i=K-1;i<s.size();i++){ num=((num<<1)|(s[i]+1&1))&U; Vis[num]=0; } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int Q; cin>>Q; while(Q--) work(); cout.flush(); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=1<<20; int N,K; string s; bool Vis[MAXN]; inline void work(){ cin>>N>>K>>s; int len=min(20,K),U=(1<<len)-1,num=0,Cnt[2]={0}; for(int i=0;i<K-len;i++) ++Cnt[s[i]&1]; for(int i=K-len;i<K;i++) num=(num<<1)|(s[i]+1&1); if(!Cnt[0]) Vis[num]=1; for(int i=K;i<s.size();i++){ num=((num<<1)|(s[i]+1&1))&U; --Cnt[s[i-K]&1]; ++Cnt[s[i-len]&1]; if(!Cnt[0]) Vis[num]=1; } int res=-1; for(int i=0;i<=U&&res==-1;i++) if(!Vis[i]) res=i; if(res==-1) cout<<"NO\n"; else{ cout<<"YES\n"; for(int i=K-len;i>=1;i--) cout<<"0"; for(int i=len-1;i>=0;i--) cout<<((res>>i)&1); cout<<"\n"; } num=0; for(int i=K-len;i<K-1;i++) num=(num<<1)|(s[i]+1&1); for(int i=K-1;i<s.size();i++){ num=((num<<1)|(s[i]+1&1))&U; Vis[num]=0; } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int Q; cin>>Q; while(Q--) work(); cout.flush(); return 0; } ```
/*** ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ ***/ #include <bits/stdc++.h> #define rep(i, x, y) for(int i = (x); i <= (y); ++i) #define repd(i, x, y) for(int i = (x); i >= (y); --i) #define eb emplace_back #define endl "\n" using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 1e6 + 7; const int inf = 1e9 + 7; const ll inf64 = 1e18 + 17; int n, k; bool a[N]; bool check[N * 2]; int next0[N]; void inp() { cin >> n >> k; rep(i, 1, n){ char x; cin >> x; a[i] = x - '0'; //cout << a[i]; } } void special() /// k <= 20 { int temp = (1 << k) - 1; int x = 0; rep(i, 1, k) x <<= 1, x ^= a[i]; check[temp - x] = true; rep(i, k + 1, n){ if(a[i - k]) x ^= (1 << (k - 1)); x <<= 1; x ^= a[i]; check[temp - x] = true; } rep(d, 0, temp) if(!check[d]){ cout << "YES\n"; repd(i, k, 1) cout << ((d >> (i - 1)) & 1); cout << endl; return; } cout << "NO\n"; } int ceilLog(int x) { int ans = 0; while((1 << ans) < x) ++ans; return ans; } void solve() { next0[n] = (!a[n] ? n : n + 1); repd(i, n - 1, 1) next0[i] = (!a[i] ? i : next0[i + 1]); int d = min(ceilLog(n - k + 2), k); int temp = (1 << d) - 1; fill(check, check + temp + 1, false); int x = 0; rep(i, k - d + 1, k) x <<= 1, x ^= a[i]; if(next0[1] > k - d) check[temp ^ x] = true; rep(i, k + 1, n){ if(a[i - d]) x ^= (1 << (d - 1)); x <<= 1; x ^= a[i]; if(next0[i - k + 1] > i - d) check[temp ^ x] = true; } rep(m, 0, temp) if(!check[m]){ cout << "YES\n"; rep(i, 1, k - d) cout << 0; repd(i, d, 1) cout << ((m >> (i - 1)) & 1); cout << endl; return; } cout << "NO\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("file.inp", "r", stdin); int t = 1; cin >> t; while(t--){ inp(); solve(); } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp /*** ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣇⠸⣿⣿⣆⠹⣿⣿⢾⣟⣯⣿⣿⣿⣿⣿⣿⣽⣻⣿⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣻⣽⡿⣿⣎⠙⣿⣞⣷⡌⢻⣟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⡄⠹⣿⣿⡆⠻⣿⣟⣯⡿⣽⡿⣿⣿⣿⣿⣽⡷⣯⣿⣿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣷⣿⣿⣿⡀⠹⣟⣾⣟⣆⠹⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢠⡘⣿⣿⡄⠉⢿⣿⣽⡷⣿⣻⣿⣿⣿⣿⡝⣷⣯⢿⣿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣯⢿⣾⢿⣿⡄⢄⠘⢿⣞⡿⣧⡈⢷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⢸⣧⠘⣿⣷⠈⣦⠙⢿⣽⣷⣻⣽⣿⣿⣿⣿⣌⢿⣯⢿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⣟⣯⣿⢿⣿⡆⢸⡷⡈⢻⡽⣷⡷⡄⠻⣽⣿⣿⡿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣏⢰⣯⢷⠈⣿⡆⢹⢷⡌⠻⡾⢋⣱⣯⣿⣿⣿⣿⡆⢻⡿⣿⣿⣿ ⣿⣿⣿⣿⣿⣿⡎⣿⢾⡿⣿⡆⢸⣽⢻⣄⠹⣷⣟⣿⣄⠹⣟⣿⣿⣟⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⢸⣯⣟⣧⠘⣷⠈⡯⠛⢀⡐⢾⣟⣷⣻⣿⣿⣿⡿⡌⢿⣻⣿⣿ ⣿⣿⣿⣿⣿⣿⣧⢸⡿⣟⣿⡇⢸⣯⣟⣮⢧⡈⢿⣞⡿⣦⠘⠏⣹⣿⣽⢿⣿⣿⣿⣿⣯⣿⣿⣿⡇⢸⣿⣿⣾⡆⠹⢀⣠⣾⣟⣷⡈⢿⣞⣯⢿⣿⣿⣿⢷⠘⣯⣿⣿ ⣿⣿⣿⣿⣿⣿⣿⡈⣿⢿⣽⡇⠘⠛⠛⠛⠓⠓⠈⠛⠛⠟⠇⢀⢿⣻⣿⣯⢿⣿⣿⣿⣷⢿⣿⣿⠁⣾⣿⣿⣿⣧⡄⠇⣹⣿⣾⣯⣿⡄⠻⣽⣯⢿⣻⣿⣿⡇⢹⣾⣿ ⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⡽⡇⢸⣿⣿⣿⣿⣿⣞⣆⠰⣶⣶⡄⢀⢻⡿⣯⣿⡽⣿⣿⣿⢯⣟⡿⢀⣿⣿⣿⣿⣿⣧⠐⣸⣿⣿⣷⣿⣿⣆⠹⣯⣿⣻⣿⣿⣿⢀⣿⢿ ⣿⣿⣿⣿⣿⣿⣿⣿⠘⣯⡿⡇⢸⣿⣿⣿⣿⣿⣿⣿⣧⡈⢿⣳⠘⡄⠻⣿⢾⣽⣟⡿⣿⢯⣿⡇⢸⣿⣿⣿⣿⣿⣿⡀⢾⣿⣿⣿⣿⣿⣿⣆⠹⣾⣷⣻⣿⡿⡇⢸⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡇⢹⣿⠇⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣄⠻⡇⢹⣆⠹⣟⣾⣽⣻⣟⣿⣽⠁⣾⣿⣿⣿⣿⣿⣿⣇⣿⣿⠿⠛⠛⠉⠙⠋⢀⠁⢘⣯⣿⣿⣧⠘⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⣿⡈⣿⡃⢼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡙⠌⣿⣆⠘⣿⣞⡿⣞⡿⡞⢠⣿⣿⣿⣿⣿⡿⠛⠉⠁⢀⣀⣠⣤⣤⣶⣶⣶⡆⢻⣽⣞⡿⣷⠈⣿ ⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠘⠁⠉⠉⠉⠉⠉⠉⠉⠉⠉⠙⠛⠛⢿⣄⢻⣿⣧⠘⢯⣟⡿⣽⠁⣾⣿⣿⣿⣿⣿⡃⢀⢀⠘⠛⠿⢿⣻⣟⣯⣽⣻⣵⡀⢿⣯⣟⣿⢀⣿ ⣿⣿⣿⣟⣿⣿⣿⣿⣶⣶⡆⢀⣿⣾⣿⣾⣷⣿⣶⠿⠚⠉⢀⢀⣤⣿⣷⣿⣿⣷⡈⢿⣻⢃⣼⣿⣿⣿⣿⣻⣿⣿⣿⡶⣦⣤⣄⣀⡀⠉⠛⠛⠷⣯⣳⠈⣾⡽⣾⢀⣿ ⣿⢿⣿⣿⣻⣿⣿⣿⣿⣿⡿⠐⣿⣿⣿⣿⠿⠋⠁⢀⢀⣤⣾⣿⣿⣿⣿⣿⣿⣿⣿⣌⣥⣾⡿⣿⣿⣷⣿⣿⢿⣷⣿⣿⣟⣾⣽⣳⢯⣟⣶⣦⣤⡾⣟⣦⠘⣿⢾⡁⢺ ⣿⣻⣿⣿⡷⣿⣿⣿⣿⣿⡗⣦⠸⡿⠋⠁⢀⢀⣠⣴⢿⣿⣽⣻⢽⣾⣟⣷⣿⣟⣿⣿⣿⣳⠿⣵⣧⣼⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣽⣳⣯⣿⣿⣿⣽⢀⢷⣻⠄⠘ ⣿⢷⣻⣿⣿⣷⣻⣿⣿⣿⡷⠛⣁⢀⣀⣤⣶⣿⣛⡿⣿⣮⣽⡻⣿⣮⣽⣻⢯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⢀⢸⣿⢀⡆ ⠸⣟⣯⣿⣿⣷⢿⣽⣿⣿⣷⣿⣷⣆⠹⣿⣶⣯⠿⣿⣶⣟⣻⢿⣷⣽⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢀⣯⣟⢀⡇ ⣇⠹⣟⣾⣻⣿⣿⢾⡽⣿⣿⣿⣿⣿⣆⢹⣶⣿⣻⣷⣯⣟⣿⣿⣽⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢀⡿⡇⢸⡇ ⣿⣆⠹⣷⡻⣽⣿⣯⢿⣽⣻⣿⣿⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⢸⣿⠇⣼⡇ ⡙⠾⣆⠹⣿⣦⠛⣿⢯⣷⢿⡽⣿⣿⣿⣿⣆⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠎⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⢀⣿⣾⣣⡿⡇ ⣿⣷⡌⢦⠙⣿⣿⣌⠻⣽⢯⣿⣽⣻⣿⣿⣿⣧⠩⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⢰⢣⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⢀⢀⢿⣞⣷⢿⡇ ⣿⣽⣆⠹⣧⠘⣿⣿⡷⣌⠙⢷⣯⡷⣟⣿⣿⣿⣷⡀⡹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣈⠃⣸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⢀⣴⡧⢀⠸⣿⡽⣿⢀ ⢻⣽⣿⡄⢻⣷⡈⢿⣿⣿⢧⢀⠙⢿⣻⡾⣽⣻⣿⣿⣄⠌⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠛⢁⣰⣾⣟⡿⢀⡄⢿⣟⣿⢀ ⡄⢿⣿⣷⢀⠹⣟⣆⠻⣿⣿⣆⢀⣀⠉⠻⣿⡽⣯⣿⣿⣷⣈⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⢀⣠⠘⣯⣷⣿⡟⢀⢆⠸⣿⡟⢸ ⣷⡈⢿⣿⣇⢱⡘⢿⣷⣬⣙⠿⣧⠘⣆⢀⠈⠻⣷⣟⣾⢿⣿⣆⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⣠⡞⢡⣿⢀⣿⣿⣿⠇⡄⢸⡄⢻⡇⣼ ⣿⣷⡈⢿⣿⡆⢣⡀⠙⢾⣟⣿⣿⣷⡈⠂⠘⣦⡈⠿⣯⣿⢾⣿⣆⠙⠻⠿⠿⠿⠿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⢋⣠⣾⡟⢠⣿⣿⢀⣿⣿⡟⢠⣿⢈⣧⠘⢠⣿ ⣿⣿⣿⣄⠻⣿⡄⢳⡄⢆⡙⠾⣽⣿⣿⣆⡀⢹⡷⣄⠙⢿⣿⡾⣿⣆⢀⡀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⢀⣀⣠⣴⡿⣯⠏⣠⣿⣿⡏⢸⣿⡿⢁⣿⣿⢀⣿⠆⢸⣿ ⣿⣿⣿⣿⣦⡙⣿⣆⢻⡌⢿⣶⢤⣉⣙⣿⣷⡀⠙⠽⠷⠄⠹⣿⣟⣿⣆⢙⣋⣤⣤⣤⣄⣀⢀⢀⢀⢀⣾⣿⣟⡷⣯⡿⢃⣼⣿⣿⣿⠇⣼⡟⣡⣿⣿⣿⢀⡿⢠⠈⣿ ⣿⣿⣿⣿⣿⣷⣮⣿⣿⣿⡌⠁⢤⣤⣤⣤⣬⣭⣴⣶⣶⣶⣆⠈⢻⣿⣿⣆⢻⣿⣿⣿⣿⣿⣿⣷⣶⣤⣌⣉⡘⠛⠻⠶⣿⣿⣿⣿⡟⣰⣫⣴⣿⣿⣿⣿⠄⣷⣿⣿⣿ ***/ #include <bits/stdc++.h> #define rep(i, x, y) for(int i = (x); i <= (y); ++i) #define repd(i, x, y) for(int i = (x); i >= (y); --i) #define eb emplace_back #define endl "\n" using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 1e6 + 7; const int inf = 1e9 + 7; const ll inf64 = 1e18 + 17; int n, k; bool a[N]; bool check[N * 2]; int next0[N]; void inp() { cin >> n >> k; rep(i, 1, n){ char x; cin >> x; a[i] = x - '0'; //cout << a[i]; } } void special() /// k <= 20 { int temp = (1 << k) - 1; int x = 0; rep(i, 1, k) x <<= 1, x ^= a[i]; check[temp - x] = true; rep(i, k + 1, n){ if(a[i - k]) x ^= (1 << (k - 1)); x <<= 1; x ^= a[i]; check[temp - x] = true; } rep(d, 0, temp) if(!check[d]){ cout << "YES\n"; repd(i, k, 1) cout << ((d >> (i - 1)) & 1); cout << endl; return; } cout << "NO\n"; } int ceilLog(int x) { int ans = 0; while((1 << ans) < x) ++ans; return ans; } void solve() { next0[n] = (!a[n] ? n : n + 1); repd(i, n - 1, 1) next0[i] = (!a[i] ? i : next0[i + 1]); int d = min(ceilLog(n - k + 2), k); int temp = (1 << d) - 1; fill(check, check + temp + 1, false); int x = 0; rep(i, k - d + 1, k) x <<= 1, x ^= a[i]; if(next0[1] > k - d) check[temp ^ x] = true; rep(i, k + 1, n){ if(a[i - d]) x ^= (1 << (d - 1)); x <<= 1; x ^= a[i]; if(next0[i - k + 1] > i - d) check[temp ^ x] = true; } rep(m, 0, temp) if(!check[m]){ cout << "YES\n"; rep(i, 1, k - d) cout << 0; repd(i, d, 1) cout << ((m >> (i - 1)) & 1); cout << endl; return; } cout << "NO\n"; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("file.inp", "r", stdin); int t = 1; cin >> t; while(t--){ inp(); solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, k, K; char str[1000010]; void solve() { scanf("%d%d%s", &n, &k, str + 1); K = min(k, 20); vector<bool> st(1 << K, 0); for (int i = 1, state = 0, num = 0; i <= n - K + 1; i ++ ) { if (num >= k - K) { state = 0; for (int j = 0; j < K; j ++ ) state = state * 2 + (str[i + j] != '1'); st[state] = 1; } num = (str[i] == '1' ? num + 1 : 0); } for (int i = 0; i < 1 << K; i ++ ) if (!st[i]) { puts("YES"); for (int j = 1; j <= k - K; j ++ ) cout << '0'; for (int j = K - 1; ~j; j -- ) cout << (i >> j & 1) ? '0' : '1'; puts(""); return; } puts("NO"); } int main() { int t; cin >> t; while (t -- ) solve(); return 0; }
### Prompt Create a solution in Cpp for the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, k, K; char str[1000010]; void solve() { scanf("%d%d%s", &n, &k, str + 1); K = min(k, 20); vector<bool> st(1 << K, 0); for (int i = 1, state = 0, num = 0; i <= n - K + 1; i ++ ) { if (num >= k - K) { state = 0; for (int j = 0; j < K; j ++ ) state = state * 2 + (str[i + j] != '1'); st[state] = 1; } num = (str[i] == '1' ? num + 1 : 0); } for (int i = 0; i < 1 << K; i ++ ) if (!st[i]) { puts("YES"); for (int j = 1; j <= k - K; j ++ ) cout << '0'; for (int j = K - 1; ~j; j -- ) cout << (i >> j & 1) ? '0' : '1'; puts(""); return; } puts("NO"); } int main() { int t; cin >> t; while (t -- ) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl "\n" const int N = 1e6 + 5; int n, k; int pref[N]; string s; bool check(string &cur, int left) { set<int> st; for(int i = 0; i + k - 1 < n; i++) { int off = 0; if(k >= 21) { if(pref[i + k - 1 - 20 + 1] - pref[i] >= 1) continue; off = k - 20; } bool skip = 0; for(int j = 0; j < cur.size(); j++) if(cur[j] == s[i + off + j]) skip = 1; if(skip) continue; int val = 0; for(int j = cur.size(); i + off + j <= i + k - 1; j++) val = val * 2 + s[i + off + j] - '0'; st.insert(val); } if(st.size() == left) return 0; return 1; } bool check2(string &cur) { for(int i = 0; i + k - 1 < n; i++) { int off = 0; if(k >= 21) { if(pref[i + k - 1 - 20 + 1] - pref[i] >= 1) continue; off = k - 20; } bool skip = 0; for(int j = 0; j < cur.size(); j++) if(cur[j] == s[i + off + j]) skip = 1; if(skip) continue; return 0; } return 1; } int32_t main() { IOS; int t; cin >> t; while(t--) { cin >> n >> k; cin >> s; string ans = ""; for(int i = 0; i < n; i++) pref[i + 1] = (s[i] == '0') + pref[i]; int left = (1 << min(20, k)); int take = min(20, k); while(ans.size() < take) { string nw = ans + '0'; if(nw.size() < k && check(nw, left / 2)) ans += '0'; else if(nw.size() == k && check2(nw)) ans += '0'; else ans += '1'; left /= 2; } if(check2(ans)) { cout << "YES" << endl; for(int i = 0; i + 20 < k; i++) cout << "0"; cout << ans << endl; } else cout << "NO" << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl "\n" const int N = 1e6 + 5; int n, k; int pref[N]; string s; bool check(string &cur, int left) { set<int> st; for(int i = 0; i + k - 1 < n; i++) { int off = 0; if(k >= 21) { if(pref[i + k - 1 - 20 + 1] - pref[i] >= 1) continue; off = k - 20; } bool skip = 0; for(int j = 0; j < cur.size(); j++) if(cur[j] == s[i + off + j]) skip = 1; if(skip) continue; int val = 0; for(int j = cur.size(); i + off + j <= i + k - 1; j++) val = val * 2 + s[i + off + j] - '0'; st.insert(val); } if(st.size() == left) return 0; return 1; } bool check2(string &cur) { for(int i = 0; i + k - 1 < n; i++) { int off = 0; if(k >= 21) { if(pref[i + k - 1 - 20 + 1] - pref[i] >= 1) continue; off = k - 20; } bool skip = 0; for(int j = 0; j < cur.size(); j++) if(cur[j] == s[i + off + j]) skip = 1; if(skip) continue; return 0; } return 1; } int32_t main() { IOS; int t; cin >> t; while(t--) { cin >> n >> k; cin >> s; string ans = ""; for(int i = 0; i < n; i++) pref[i + 1] = (s[i] == '0') + pref[i]; int left = (1 << min(20, k)); int take = min(20, k); while(ans.size() < take) { string nw = ans + '0'; if(nw.size() < k && check(nw, left / 2)) ans += '0'; else if(nw.size() == k && check2(nw)) ans += '0'; else ans += '1'; left /= 2; } if(check2(ans)) { cout << "YES" << endl; for(int i = 0; i + 20 < k; i++) cout << "0"; cout << ans << endl; } else cout << "NO" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; //#include "testlib.h" #define ff first #define ss second #define all(v) v.begin(),v.end() #define int long long #define ll long long #define M 1000000007 #define MM 998244353 #define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i] #define GCD(m,n) __gcd(m,n) #define LCM(m,n) m*(n/GCD(m,n)) #define mii map<ll ,ll > #define msi map<string,ll > #define rep(a,b) for(ll i=a;i<b;i++) #define rep0(n) for(ll i=0;i<n;i++) #define repi(i,a,b) for(ll i=a;i<b;i++) #define pb push_back #define vi vector<ll> #define vs vector<string> #define ppb pop_back #define endl '\n' #define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define r0 return 0; #define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i) #define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); #define Set(a, s) (a, s, sizeof (a)) #define FOR repi #define vii vector<pii> #define pii pair<int,int> #define REVERSE(v) reverse(all(v)) #define trav(a, x) for(auto& a : x) #define display(x) trav(a,x) cout<<a<<" ";cout<<endl #define debug cerr<<"bhau"<<endl #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ std::cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } template<typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template<typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } ll max(ll a, ll b) { return (a > b)? a : b;} int min(int a, int b) { return (a < b)? a : b;} const int N = (1<<21); int poss[N]; int solve(){ int n,k; cin>>n>>k; string s; cin>>s; int k2 = log2(n-k+1)+1; k2 = min(k,k2); int cnt[n+1]; // memset(cnt,0,sizeof(cnt)); cnt[0]=0; for(int i=0;i<n;i++){ if(i) cnt[i] = cnt[i-1]; cnt[i]+=(s[i]-'0'); } int left = k - k2; for(int i=0;i<(1<<k2);i++) poss[i]=1; for(int i=0;i<=n-k;i++){ int cur = 0; if(i+k-k2>=1) cur=cnt[i+k-k2-1]; if(i) cur-=cnt[i-1]; if(cur==k-k2 ){ int x = 0; for(int j=k-k2;j<k;j++){ x*=2; if(s[i+j]=='0') x++; } // trace(i,x); poss[x]=0; } } // trace(poss[0]); // trace(k2); for(int i=0;i<(1<<k2);i++){ if(poss[i]){ cout<<"YES"<<endl; string res=""; while(i){ if(i%2) res.pb('1'); else res.pb('0'); i/=2; } while(res.size()<k) res.pb('0'); reverse(all(res)); cout<<res<<endl; r0 } } cout<<"NO"<<endl; return 0; } signed main(){ asdf int t=1; cin>>t; while(t--){ solve(); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <bits/stdc++.h> using namespace std; //#include "testlib.h" #define ff first #define ss second #define all(v) v.begin(),v.end() #define int long long #define ll long long #define M 1000000007 #define MM 998244353 #define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i] #define GCD(m,n) __gcd(m,n) #define LCM(m,n) m*(n/GCD(m,n)) #define mii map<ll ,ll > #define msi map<string,ll > #define rep(a,b) for(ll i=a;i<b;i++) #define rep0(n) for(ll i=0;i<n;i++) #define repi(i,a,b) for(ll i=a;i<b;i++) #define pb push_back #define vi vector<ll> #define vs vector<string> #define ppb pop_back #define endl '\n' #define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define r0 return 0; #define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i) #define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout); #define Set(a, s) (a, s, sizeof (a)) #define FOR repi #define vii vector<pii> #define pii pair<int,int> #define REVERSE(v) reverse(all(v)) #define trav(a, x) for(auto& a : x) #define display(x) trav(a,x) cout<<a<<" ";cout<<endl #define debug cerr<<"bhau"<<endl #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ std::cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } template<typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template<typename T, typename U> static inline void amax(T &x, U y) { if (x < y) x = y; } ll max(ll a, ll b) { return (a > b)? a : b;} int min(int a, int b) { return (a < b)? a : b;} const int N = (1<<21); int poss[N]; int solve(){ int n,k; cin>>n>>k; string s; cin>>s; int k2 = log2(n-k+1)+1; k2 = min(k,k2); int cnt[n+1]; // memset(cnt,0,sizeof(cnt)); cnt[0]=0; for(int i=0;i<n;i++){ if(i) cnt[i] = cnt[i-1]; cnt[i]+=(s[i]-'0'); } int left = k - k2; for(int i=0;i<(1<<k2);i++) poss[i]=1; for(int i=0;i<=n-k;i++){ int cur = 0; if(i+k-k2>=1) cur=cnt[i+k-k2-1]; if(i) cur-=cnt[i-1]; if(cur==k-k2 ){ int x = 0; for(int j=k-k2;j<k;j++){ x*=2; if(s[i+j]=='0') x++; } // trace(i,x); poss[x]=0; } } // trace(poss[0]); // trace(k2); for(int i=0;i<(1<<k2);i++){ if(poss[i]){ cout<<"YES"<<endl; string res=""; while(i){ if(i%2) res.pb('1'); else res.pb('0'); i/=2; } while(res.size()<k) res.pb('0'); reverse(all(res)); cout<<res<<endl; r0 } } cout<<"NO"<<endl; return 0; } signed main(){ asdf int t=1; cin>>t; while(t--){ solve(); } return 0; } ```
/* ___ ______ __ __ / |____ __ ___________ _/ ____/___ _/ /___ ____ ______/ /____ / /| /_ / / / / / ___/ __ `/ / / __ `/ __/ / / / / / / __ / ___/ / ___ |/ /_/ /_/ (__ ) /_/ / /___/ /_/ / /_/ /_/ / /_/ / /_/ (__ ) /_/ |_/___/\__,_/____/\__,_/\____/\__,_/\__/\__, /\__, /\__,_/____/ /____//____/      />  フ      |  _  _|      /`ミ _x 彡      /      |     /  ヽ   ?  / ̄|   | | |  | ( ̄ヽ__ヽ_)_)  \二つ */ #include <queue> #include <vector> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define MP make_pair #define ll long long #define fi first #define se second using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (;!isdigit(c);c=getchar()) if (c=='-') f=1; for (;isdigit(c);c=getchar()) x=x*10+(c^48); if (f) x=-x; } template<typename F> inline void write(F x, char ed = '\n') { static short st[30];short tp=0; if(x<0) putchar('-'),x=-x; do st[++tp]=x%10,x/=10; while(x); while(tp) putchar('0'|st[tp--]); putchar(ed); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } #define ull unsigned ll const int N = 2000500; const int P = 19260817; const int bs = 13331; int T, n, k; char s[N]; ll h1[N], b1[N]; ull h2[N], b2[N]; #include <set> set<pair<ll, ull> > s1; inline ll g1(int l, int r) { return (h1[r] + (P - h1[l-1]) * b1[r - l + 1]) % P; } inline ull g2(int l, int r) { return h2[r] - h2[l-1] * b2[r - l + 1]; } int st[N], fl; void dfs(int las, ll hh1, ull hh2) { if (fl) return; if (!las) { if (s1.find(MP(hh1, hh2)) == s1.end()) fl = 1; return; } if (!fl) st[las] = 0, dfs(las - 1, (hh1 * bs + '0') % P, hh2 * bs + '0'); if (!fl) st[las] = 1, dfs(las - 1, (hh1 * bs + '1') % P, hh2 * bs + '1'); } int main() { b1[0] = b2[0] = 1; for (int i = 1;i <= 1000000; i++) b1[i] = b1[i-1] * bs % P, b2[i] = b2[i-1] * bs; for (read(T); T; T--) { read(n), read(k), fl = 0; scanf ("%s", s + 1); for (int i = 1;i <= n; i++) h1[i] = (h1[i-1] * bs + (s[i] ^ 1)) % P, h2[i] = h2[i-1] * bs + (s[i] ^ 1); s1.clear(); for (int i = k;i <= n; i++) s1.insert(MP(g1(i - k + 1, i), g2(i - k + 1, i))); dfs(k, 0, 0); if (!fl) puts("NO"); else { puts("YES"); for (int i = k;i >= 1; i--) putchar('0' | st[i]); puts(""); } } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp /* ___ ______ __ __ / |____ __ ___________ _/ ____/___ _/ /___ ____ ______/ /____ / /| /_ / / / / / ___/ __ `/ / / __ `/ __/ / / / / / / __ / ___/ / ___ |/ /_/ /_/ (__ ) /_/ / /___/ /_/ / /_/ /_/ / /_/ / /_/ (__ ) /_/ |_/___/\__,_/____/\__,_/\____/\__,_/\__/\__, /\__, /\__,_/____/ /____//____/      />  フ      |  _  _|      /`ミ _x 彡      /      |     /  ヽ   ?  / ̄|   | | |  | ( ̄ヽ__ヽ_)_)  \二つ */ #include <queue> #include <vector> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define MP make_pair #define ll long long #define fi first #define se second using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (;!isdigit(c);c=getchar()) if (c=='-') f=1; for (;isdigit(c);c=getchar()) x=x*10+(c^48); if (f) x=-x; } template<typename F> inline void write(F x, char ed = '\n') { static short st[30];short tp=0; if(x<0) putchar('-'),x=-x; do st[++tp]=x%10,x/=10; while(x); while(tp) putchar('0'|st[tp--]); putchar(ed); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } #define ull unsigned ll const int N = 2000500; const int P = 19260817; const int bs = 13331; int T, n, k; char s[N]; ll h1[N], b1[N]; ull h2[N], b2[N]; #include <set> set<pair<ll, ull> > s1; inline ll g1(int l, int r) { return (h1[r] + (P - h1[l-1]) * b1[r - l + 1]) % P; } inline ull g2(int l, int r) { return h2[r] - h2[l-1] * b2[r - l + 1]; } int st[N], fl; void dfs(int las, ll hh1, ull hh2) { if (fl) return; if (!las) { if (s1.find(MP(hh1, hh2)) == s1.end()) fl = 1; return; } if (!fl) st[las] = 0, dfs(las - 1, (hh1 * bs + '0') % P, hh2 * bs + '0'); if (!fl) st[las] = 1, dfs(las - 1, (hh1 * bs + '1') % P, hh2 * bs + '1'); } int main() { b1[0] = b2[0] = 1; for (int i = 1;i <= 1000000; i++) b1[i] = b1[i-1] * bs % P, b2[i] = b2[i-1] * bs; for (read(T); T; T--) { read(n), read(k), fl = 0; scanf ("%s", s + 1); for (int i = 1;i <= n; i++) h1[i] = (h1[i-1] * bs + (s[i] ^ 1)) % P, h2[i] = h2[i-1] * bs + (s[i] ^ 1); s1.clear(); for (int i = k;i <= n; i++) s1.insert(MP(g1(i - k + 1, i), g2(i - k + 1, i))); dfs(k, 0, 0); if (!fl) puts("NO"); else { puts("YES"); for (int i = k;i >= 1; i--) putchar('0' | st[i]); puts(""); } } return 0; } ```
#include<bits/stdc++.h> using namespace std; string doi(int a){ string sol=""; while (a>0) { int s=a%2; sol=to_string(s)+sol; a/=2; } return sol; } int main (){ ios::sync_with_stdio(0); cin.tie(0); int casi; cin>>casi; while (casi--){ int n,k; cin>>n>>k; string s; cin>>s; int r=min(20,k); bitset<(int)2e6> v; vector<int> next(n); if (s[0]=='1') next[0]=-1; for (int i=1;i<n;i++){ if (s[i]=='0') next[i]=i; else next[i]=next[i-1]; } for (int i=s.size()-1;i-k+1>=0;i--){ if (r<k&&next[i-r]>=i-k+1) continue; int tmp=0; for (int j=0;j<r;j++){ tmp+=(('1'-s[i-j])<<j); } v[tmp]=true; } int id=0; while (v[id]) id++; string tmp=doi(id); if ((int)tmp.size()>k) { cout<<"NO\n"; continue; } else cout<<"YES\n"; int temp=tmp.size(); while(temp<k){ ++temp; cout << 0; } cout<<tmp<<'\n'; } }
### Prompt Generate a CPP solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include<bits/stdc++.h> using namespace std; string doi(int a){ string sol=""; while (a>0) { int s=a%2; sol=to_string(s)+sol; a/=2; } return sol; } int main (){ ios::sync_with_stdio(0); cin.tie(0); int casi; cin>>casi; while (casi--){ int n,k; cin>>n>>k; string s; cin>>s; int r=min(20,k); bitset<(int)2e6> v; vector<int> next(n); if (s[0]=='1') next[0]=-1; for (int i=1;i<n;i++){ if (s[i]=='0') next[i]=i; else next[i]=next[i-1]; } for (int i=s.size()-1;i-k+1>=0;i--){ if (r<k&&next[i-r]>=i-k+1) continue; int tmp=0; for (int j=0;j<r;j++){ tmp+=(('1'-s[i-j])<<j); } v[tmp]=true; } int id=0; while (v[id]) id++; string tmp=doi(id); if ((int)tmp.size()>k) { cout<<"NO\n"; continue; } else cout<<"YES\n"; int temp=tmp.size(); while(temp<k){ ++temp; cout << 0; } cout<<tmp<<'\n'; } } ```
#include <bits/stdc++.h> #pragma GCC optimize ("O3", "unroll-all-loops") #pragma GCC target ("sse4.2") using namespace std; #define F first #define S second typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; 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; const int sz = 20; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, k; string s; bool viv = false; string i_to_s(int val) { string res; for (int i = sz - 1; i >= 0; i--) res += char('0' + ((val >> i) & 1)); return res; } int s_to_i(string s) { int res = 0; for (int i = 0; i < s.size(); i++) res <<= 1, res += (s[i] == '1'); return res; } void inv(string &s) { for (auto &i : s) if (i == '0') i = '1'; else i = '0'; } void solve() { cin >> n >> k; cin >> s; set<int> bad; vector<int> p = {0}; for (int i = 0; i < n; i++) p.push_back(p.back() + (s[i] == '1')); for (int i = k; i <= n; i++) { int l = i - k, r = i; int fl = max(l, r - sz); string ss; for (int j = fl; j < r; j++) ss += s[j]; inv(ss); if (viv) { cout << "at " << r << " see " << ss << endl; } if (p[fl] - p[l] == fl - l) bad.insert(s_to_i(ss)); } if (viv) { cout << "bad = "; for (auto i : bad) cout << i << ' '; cout << endl; } int val = 0; while (val < (1 << min(k, sz)) && bad.count(val)) val++; if (viv) { cout << "val = " << val << endl; } if (val == (1 << min(k, sz))) return void(cout << "NO\n\n"); cout << "YES\n"; auto ans = i_to_s(val); if (viv) { cout << "ans = " << ans << endl; } reverse(ans.begin(), ans.end()); while (ans.size() > k) ans.pop_back(); while (ans.size() < k) ans += '0'; reverse(ans.begin(), ans.end()); cout << ans << '\n'; } int main() { // viv = true; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t = 1; cin >> t; while (t--) solve(); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize ("O3", "unroll-all-loops") #pragma GCC target ("sse4.2") using namespace std; #define F first #define S second typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; 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; const int sz = 20; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, k; string s; bool viv = false; string i_to_s(int val) { string res; for (int i = sz - 1; i >= 0; i--) res += char('0' + ((val >> i) & 1)); return res; } int s_to_i(string s) { int res = 0; for (int i = 0; i < s.size(); i++) res <<= 1, res += (s[i] == '1'); return res; } void inv(string &s) { for (auto &i : s) if (i == '0') i = '1'; else i = '0'; } void solve() { cin >> n >> k; cin >> s; set<int> bad; vector<int> p = {0}; for (int i = 0; i < n; i++) p.push_back(p.back() + (s[i] == '1')); for (int i = k; i <= n; i++) { int l = i - k, r = i; int fl = max(l, r - sz); string ss; for (int j = fl; j < r; j++) ss += s[j]; inv(ss); if (viv) { cout << "at " << r << " see " << ss << endl; } if (p[fl] - p[l] == fl - l) bad.insert(s_to_i(ss)); } if (viv) { cout << "bad = "; for (auto i : bad) cout << i << ' '; cout << endl; } int val = 0; while (val < (1 << min(k, sz)) && bad.count(val)) val++; if (viv) { cout << "val = " << val << endl; } if (val == (1 << min(k, sz))) return void(cout << "NO\n\n"); cout << "YES\n"; auto ans = i_to_s(val); if (viv) { cout << "ans = " << ans << endl; } reverse(ans.begin(), ans.end()); while (ans.size() > k) ans.pop_back(); while (ans.size() < k) ans += '0'; reverse(ans.begin(), ans.end()); cout << ans << '\n'; } int main() { // viv = true; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t = 1; cin >> t; while (t--) solve(); return 0; } ```
// remove unsolved tag lol #include <algorithm> #include <array> #include <cassert> #include <chrono> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <vector> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef NEAL_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const string YES = "YES"; const string NO = "NO"; const int INF = int(1e9) + 5; const int LOG = 21; void run_case() { int N, K; string S; cin >> N >> K >> S; assert(int(S.size()) == N); for (auto &ch : S) ch = char('0' + '1' - ch); vector<int> locations; for (int i = 0; i < N; i++) if (S[i] == '1') locations.push_back(i); vector<bool> seen(N + 1, false); int L = int(locations.size()); int loc_index = 0; unsigned value = 0; for (int i = 0; i < N; i++) { int start = i - (K - 1); value = 2 * value + (S[i] - '0'); if (K < 32) value &= (1U << K) - 1; if (start >= 0) { while (loc_index < L && locations[loc_index] < start) loc_index++; if (loc_index >= L || locations[loc_index] >= i - LOG) { if (value <= unsigned(N)) seen[value] = true; } } } int limit = min(N + 1, K <= LOG ? 1 << K : INF); int ans = 0; while (ans < limit && seen[ans]) ans++; if (ans >= limit) { cout << NO << '\n'; return; } cout << YES << '\n'; for (int i = K - 1; i >= 0; i--) cout << (i > LOG ? 0 : (ans >> i & 1)); cout << '\n'; } int main() { ios::sync_with_stdio(false); #ifndef NEAL_DEBUG cin.tie(nullptr); #endif int tests; cin >> tests; while (tests-- > 0) run_case(); }
### Prompt Please provide a Cpp coded solution to the problem described below: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp // remove unsolved tag lol #include <algorithm> #include <array> #include <cassert> #include <chrono> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <vector> using namespace std; template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef NEAL_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif const string YES = "YES"; const string NO = "NO"; const int INF = int(1e9) + 5; const int LOG = 21; void run_case() { int N, K; string S; cin >> N >> K >> S; assert(int(S.size()) == N); for (auto &ch : S) ch = char('0' + '1' - ch); vector<int> locations; for (int i = 0; i < N; i++) if (S[i] == '1') locations.push_back(i); vector<bool> seen(N + 1, false); int L = int(locations.size()); int loc_index = 0; unsigned value = 0; for (int i = 0; i < N; i++) { int start = i - (K - 1); value = 2 * value + (S[i] - '0'); if (K < 32) value &= (1U << K) - 1; if (start >= 0) { while (loc_index < L && locations[loc_index] < start) loc_index++; if (loc_index >= L || locations[loc_index] >= i - LOG) { if (value <= unsigned(N)) seen[value] = true; } } } int limit = min(N + 1, K <= LOG ? 1 << K : INF); int ans = 0; while (ans < limit && seen[ans]) ans++; if (ans >= limit) { cout << NO << '\n'; return; } cout << YES << '\n'; for (int i = K - 1; i >= 0; i--) cout << (i > LOG ? 0 : (ans >> i & 1)); cout << '\n'; } int main() { ios::sync_with_stdio(false); #ifndef NEAL_DEBUG cin.tie(nullptr); #endif int tests; cin >> tests; while (tests-- > 0) run_case(); } ```
#ifdef Prateek #include "Prateek.h" #else #include <bits/stdc++.h> using namespace std; #define debug(...) 42 #endif #define F first #define S second #define pb push_back #define int ll #define f(i,x,n) for(int i=x;i<n;i++) #define all(c) c.begin(),c.end() using ll = long long; const int MOD = 1e9+7, N = 1e5 + 10; const int M = 20; int can[1 << M]; void test(){ int n, k; cin >> n >> k; string s; cin >> s; s = "#" + s; vector<int> nxt(n + 1); nxt[n] = (s[n] == '0') ? n : 1e9; for (int i = n - 1; i; --i) { if (s[i] == '0') { nxt[i] = i; } else { nxt[i] = nxt[i + 1]; } } int m = min(k, 20LL); int d = k - m; for (int i = 1; i + k - 1 <= n; ++i) { if (nxt[i] - i < d) continue; int cur = 0; for (int j = i + d; j < i + k; ++j) { cur = cur * 2 + s[j] - '0'; } can[((1 << m) - 1) ^ cur] = 1; } int r = -1; for (int i = 0; i < (1 << m); ++i) { if (can[i] == 0) { r = i; break; } } for (int i = 1; i + k - 1 <= n; ++i) { if (nxt[i] - i < d) continue; int cur = 0; for (int j = i + d; j < i + k; ++j) { cur = cur * 2 + s[j] - '0'; } can[((1 << m) - 1) ^ cur] = 0; } if (r == -1) { cout << "NO\n"; return; } cout << "YES\n" << string(d, '0'); for (int j = m - 1; j >= 0; --j) { cout << ((r >> j) & 1); } cout << '\n'; return; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(NULL); int tt = 1; cin >> tt; f(i,0,tt) test(); return 0; }
### Prompt Please formulate a CPP solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #ifdef Prateek #include "Prateek.h" #else #include <bits/stdc++.h> using namespace std; #define debug(...) 42 #endif #define F first #define S second #define pb push_back #define int ll #define f(i,x,n) for(int i=x;i<n;i++) #define all(c) c.begin(),c.end() using ll = long long; const int MOD = 1e9+7, N = 1e5 + 10; const int M = 20; int can[1 << M]; void test(){ int n, k; cin >> n >> k; string s; cin >> s; s = "#" + s; vector<int> nxt(n + 1); nxt[n] = (s[n] == '0') ? n : 1e9; for (int i = n - 1; i; --i) { if (s[i] == '0') { nxt[i] = i; } else { nxt[i] = nxt[i + 1]; } } int m = min(k, 20LL); int d = k - m; for (int i = 1; i + k - 1 <= n; ++i) { if (nxt[i] - i < d) continue; int cur = 0; for (int j = i + d; j < i + k; ++j) { cur = cur * 2 + s[j] - '0'; } can[((1 << m) - 1) ^ cur] = 1; } int r = -1; for (int i = 0; i < (1 << m); ++i) { if (can[i] == 0) { r = i; break; } } for (int i = 1; i + k - 1 <= n; ++i) { if (nxt[i] - i < d) continue; int cur = 0; for (int j = i + d; j < i + k; ++j) { cur = cur * 2 + s[j] - '0'; } can[((1 << m) - 1) ^ cur] = 0; } if (r == -1) { cout << "NO\n"; return; } cout << "YES\n" << string(d, '0'); for (int j = m - 1; j >= 0; --j) { cout << ((r >> j) & 1); } cout << '\n'; return; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(NULL); int tt = 1; cin >> tt; f(i,0,tt) test(); return 0; } ```
#include<iostream> #include<vector> #include <string> #include <unordered_map> #include <algorithm> using namespace std; string get_str(int ans, int m){ string res; string res2; for(int j = 0; j < m; j++) { res2.push_back('0' + (ans % 2)); ans /= 2; } reverse(res2.begin(), res2.end()); res += res2; return res; } void determine(string l, int n, int k){ int m = min(k, 20); unordered_map<string, bool> used; int l_len = l.size(); auto preSum = new int[l_len + 1]; preSum[0] = 0; for (int i = 0; i < l_len; i++){ if (l[i] == '0'){ preSum[i + 1] = preSum[i]; }else{ preSum[i + 1] = preSum[i] + 1; } } for(int i=0; i<n-k+1; i++){ string t; //(k-m, '0') if (preSum[i+k-m]-preSum[i] != k-m){ continue; } for (int j=i+k-m;j<i+k;j++){ if (l[j] == '1'){ t += '0'; }else{ t += '1'; } } used[t]=true; } int ok=0; for (int i=0;i<1<<m;i++) { string ke = get_str(i, m); if (!used[ke]) { string ts = string(k-m, '0'); ok=1; cout<<"YES"<<endl; cout << ts+ke <<endl; break; } } if (!ok) cout<<"NO"<<endl; } int main(){ std::ios::sync_with_stdio(false); int i; cin >>i ; // i = 1<<6; // cout << i; while(i>0){ int n, k; string l; cin >> n; cin >> k; cin >> l; determine(l, n, k); i--; } }
### Prompt Please provide a cpp coded solution to the problem described below: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include<iostream> #include<vector> #include <string> #include <unordered_map> #include <algorithm> using namespace std; string get_str(int ans, int m){ string res; string res2; for(int j = 0; j < m; j++) { res2.push_back('0' + (ans % 2)); ans /= 2; } reverse(res2.begin(), res2.end()); res += res2; return res; } void determine(string l, int n, int k){ int m = min(k, 20); unordered_map<string, bool> used; int l_len = l.size(); auto preSum = new int[l_len + 1]; preSum[0] = 0; for (int i = 0; i < l_len; i++){ if (l[i] == '0'){ preSum[i + 1] = preSum[i]; }else{ preSum[i + 1] = preSum[i] + 1; } } for(int i=0; i<n-k+1; i++){ string t; //(k-m, '0') if (preSum[i+k-m]-preSum[i] != k-m){ continue; } for (int j=i+k-m;j<i+k;j++){ if (l[j] == '1'){ t += '0'; }else{ t += '1'; } } used[t]=true; } int ok=0; for (int i=0;i<1<<m;i++) { string ke = get_str(i, m); if (!used[ke]) { string ts = string(k-m, '0'); ok=1; cout<<"YES"<<endl; cout << ts+ke <<endl; break; } } if (!ok) cout<<"NO"<<endl; } int main(){ std::ios::sync_with_stdio(false); int i; cin >>i ; // i = 1<<6; // cout << i; while(i>0){ int n, k; string l; cin >> n; cin >> k; cin >> l; determine(l, n, k); i--; } } ```
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <cstring> #include <list> #include <cassert> #include <climits> #include <bitset> #include <chrono> #include <random> using namespace std; #define PB push_back #define MP make_pair #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define REPE(i,n) FORE(i,0,n) #define FORSZ(i,a,v) FOR(i,a,SZ(v)) #define REPSZ(i,v) REP(i,SZ(v)) std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 1000000; int n, k; char s[MAXN + 1]; char ans[MAXN + 1]; int zeroes[MAXN + 1]; bool solve() { int nsubs = n - k + 1; int nbits = 0; while ((1 << nbits) <= nsubs) ++nbits; nbits = min(nbits, k); zeroes[0] = 0; REP(i, n) zeroes[i + 1] = zeroes[i] + (s[i] == '0' ? 1 : 0); vector<bool> blocked(1 << nbits, false); REPE(i, n - k) { int cur = 0; REP(j, nbits) { char c = s[i + k - nbits + j]; if (c == '0') cur |= 1 << (nbits - j - 1); } if (zeroes[i + (k - nbits)] - zeroes[i] != 0) continue; blocked[cur] = true; } REP(mask, 1 << nbits) if (!blocked[mask]) { REP(i, k - nbits) ans[i] = '0'; REP(i, nbits) ans[k - nbits + i] = '0' + ((mask >> (nbits - i - 1)) & 1); ans[k] = '\0'; return true; } return false; } void run() { scanf("%d%d", &n, &k); scanf("%s", s); assert(strlen(s) == n); if (!solve()) { printf("NO\n"); return; } printf("YES\n"); printf("%s\n", ans); } int main() { int ncase; scanf("%d", &ncase); FORE(i, 1, ncase) run(); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <cstring> #include <list> #include <cassert> #include <climits> #include <bitset> #include <chrono> #include <random> using namespace std; #define PB push_back #define MP make_pair #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define FORE(i,a,b) for(int i=(a);i<=(b);++i) #define REPE(i,n) FORE(i,0,n) #define FORSZ(i,a,v) FOR(i,a,SZ(v)) #define REPSZ(i,v) REP(i,SZ(v)) std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 1000000; int n, k; char s[MAXN + 1]; char ans[MAXN + 1]; int zeroes[MAXN + 1]; bool solve() { int nsubs = n - k + 1; int nbits = 0; while ((1 << nbits) <= nsubs) ++nbits; nbits = min(nbits, k); zeroes[0] = 0; REP(i, n) zeroes[i + 1] = zeroes[i] + (s[i] == '0' ? 1 : 0); vector<bool> blocked(1 << nbits, false); REPE(i, n - k) { int cur = 0; REP(j, nbits) { char c = s[i + k - nbits + j]; if (c == '0') cur |= 1 << (nbits - j - 1); } if (zeroes[i + (k - nbits)] - zeroes[i] != 0) continue; blocked[cur] = true; } REP(mask, 1 << nbits) if (!blocked[mask]) { REP(i, k - nbits) ans[i] = '0'; REP(i, nbits) ans[k - nbits + i] = '0' + ((mask >> (nbits - i - 1)) & 1); ans[k] = '\0'; return true; } return false; } void run() { scanf("%d%d", &n, &k); scanf("%s", s); assert(strlen(s) == n); if (!solve()) { printf("NO\n"); return; } printf("YES\n"); printf("%s\n", ans); } int main() { int ncase; scanf("%d", &ncase); FORE(i, 1, ncase) run(); return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; vector<int> pref; string s, sr; int get(int l, int r) { return pref[r + 1] - pref[l]; } int conv(int l, int r) { int res = 0; for(int i = l; i <= r; i++) { res *= 2; if(sr[i] == '1') { res += 1; } } return res; } string conv1(int a, int k) { string res = ""; while(a != 0) { res += '0' + (a % 2); a /= 2; } reverse(res.begin(), res.end()); string res1 = ""; for(int i = 0; i < k - res.size(); i++) { res1 += '0'; } res1 += res; return res1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef _FILE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int q; cin >> q; while(q--) { int n, k; cin >> n >> k; sr = ""; cin >> s; for(int i = 0; i < n; i++) { if(s[i] == '0') { sr += '1'; } else { sr += '0'; } } pref.clear(); pref.resize(n + 1); pref[0] = 0; for(int i = 1; i <= n; i++) { pref[i] = pref[i - 1]; if(sr[i - 1] == '1') { pref[i]++; } } set<int> set1; for(int i = 0; i < n - k + 1; i++) { int l = i; int r = i + k - 1; if(r - l + 1 <= 20) { set1.insert(conv(l, r)); } else if(get(l, r - 20) == 0) { set1.insert(conv(r - 19, r)); } } int max1 = 2000000; if(k < 25) { max1 = (1 << k); } bool ok = true; for(int i = 0; i < max1; i++) { if(set1.find(i) == set1.end()) { ok = false; cout << "YES\n"; cout << conv1(i, k) << '\n'; break; } } if(ok) { cout << "NO\n"; } } }
### Prompt Your challenge is to write a CPP solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; vector<int> pref; string s, sr; int get(int l, int r) { return pref[r + 1] - pref[l]; } int conv(int l, int r) { int res = 0; for(int i = l; i <= r; i++) { res *= 2; if(sr[i] == '1') { res += 1; } } return res; } string conv1(int a, int k) { string res = ""; while(a != 0) { res += '0' + (a % 2); a /= 2; } reverse(res.begin(), res.end()); string res1 = ""; for(int i = 0; i < k - res.size(); i++) { res1 += '0'; } res1 += res; return res1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef _FILE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int q; cin >> q; while(q--) { int n, k; cin >> n >> k; sr = ""; cin >> s; for(int i = 0; i < n; i++) { if(s[i] == '0') { sr += '1'; } else { sr += '0'; } } pref.clear(); pref.resize(n + 1); pref[0] = 0; for(int i = 1; i <= n; i++) { pref[i] = pref[i - 1]; if(sr[i - 1] == '1') { pref[i]++; } } set<int> set1; for(int i = 0; i < n - k + 1; i++) { int l = i; int r = i + k - 1; if(r - l + 1 <= 20) { set1.insert(conv(l, r)); } else if(get(l, r - 20) == 0) { set1.insert(conv(r - 19, r)); } } int max1 = 2000000; if(k < 25) { max1 = (1 << k); } bool ok = true; for(int i = 0; i < max1; i++) { if(set1.find(i) == set1.end()) { ok = false; cout << "YES\n"; cout << conv1(i, k) << '\n'; break; } } if(ok) { cout << "NO\n"; } } } ```
/* India defeats australia in 2 nd test, I'm very happy */ /* The right approach fot this qstn is to build it up from k=1 For k=1 if we have both 1 and 0 then no For k=2 we can have 00,11,01,10 If we take 00 then 11 must not be present If we take 11 then 00 must not be present If we take 01 then 10 must not be there So compliment must not be there For k=3 000 001 010 011 100 101 110 111 So for any of them it's compliment must not be there nd for a particular k we have 2^k If k>=20 then ans is always yes as we can have atmost 1e6(appx) strings Till k<20 we can do brute force Nd for k>=20 we can just check for last 20 bits Using hash function was givning WA on t.c 43 Some people used multiple hashing(Like 5-6 times) with large prime numbers to avoid collision nd it's difficult to hack such solutions We can avoid hasging by using a boolean array Using optimisations makes code faster by 20% */ #pragma GCC optimize("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include<bits/stdc++.h> using namespace std; #define ll int string s; vector<string> unv[21]; ll one[1000005]; int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); unv[1].push_back("0"); unv[1].push_back("1"); for(ll i=2;i<=20;i++){ for(auto it : unv[i-1]){ unv[i].push_back("0"+it); } for(auto it : unv[i-1]){ unv[i].push_back("1"+it); } } ll t; cin>>t; while(t--){ ll n,k; cin>>n>>k>>s; if(k<20){ vector<bool> vis(1<<k,0); for(ll i=0;i+k-1<s.length();i++){ ll val=0; for(ll j=i+k-1;j>=i;j--){ val+=(1-(s[j]-'0'))*(1<<(i+k-1-j)); } vis[val]=1; } string ans=""; for(ll i=0;i<unv[k].size();i++){ if(!vis[i]){ ans=unv[k][i]; break; } } if(ans==""){ cout<<"NO"<<"\n"; continue; } cout<<"YES"<<"\n"; cout<<ans<<"\n"; } else{ vector<bool> vis(1<<20,0); one[s.length()]=0; for(ll i=s.length()-1;i>=0;i--){ if(s[i]=='1'){ one[i]=1+one[i+1]; } else{ one[i]=0; } } for(ll i=0;i+k-1<s.length();i++){ if(one[i]>=k-20){ ll val=0; for(ll j=i+k-1;j>=i+k-20;j--){ val+=(1-(s[j]-'0'))*(1<<(i+k-1-j)); } vis[val]=1; } } string ans=""; for(ll i=0;i<unv[20].size();i++){ if(!vis[i]){ ans=unv[20][i]; break; } } cout<<"YES"<<"\n"; for(ll i=0;i<k-20;i++){ cout<<"0"; } cout<<ans<<"\n"; } } }
### Prompt In CPP, your task is to solve the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp /* India defeats australia in 2 nd test, I'm very happy */ /* The right approach fot this qstn is to build it up from k=1 For k=1 if we have both 1 and 0 then no For k=2 we can have 00,11,01,10 If we take 00 then 11 must not be present If we take 11 then 00 must not be present If we take 01 then 10 must not be there So compliment must not be there For k=3 000 001 010 011 100 101 110 111 So for any of them it's compliment must not be there nd for a particular k we have 2^k If k>=20 then ans is always yes as we can have atmost 1e6(appx) strings Till k<20 we can do brute force Nd for k>=20 we can just check for last 20 bits Using hash function was givning WA on t.c 43 Some people used multiple hashing(Like 5-6 times) with large prime numbers to avoid collision nd it's difficult to hack such solutions We can avoid hasging by using a boolean array Using optimisations makes code faster by 20% */ #pragma GCC optimize("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include<bits/stdc++.h> using namespace std; #define ll int string s; vector<string> unv[21]; ll one[1000005]; int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); unv[1].push_back("0"); unv[1].push_back("1"); for(ll i=2;i<=20;i++){ for(auto it : unv[i-1]){ unv[i].push_back("0"+it); } for(auto it : unv[i-1]){ unv[i].push_back("1"+it); } } ll t; cin>>t; while(t--){ ll n,k; cin>>n>>k>>s; if(k<20){ vector<bool> vis(1<<k,0); for(ll i=0;i+k-1<s.length();i++){ ll val=0; for(ll j=i+k-1;j>=i;j--){ val+=(1-(s[j]-'0'))*(1<<(i+k-1-j)); } vis[val]=1; } string ans=""; for(ll i=0;i<unv[k].size();i++){ if(!vis[i]){ ans=unv[k][i]; break; } } if(ans==""){ cout<<"NO"<<"\n"; continue; } cout<<"YES"<<"\n"; cout<<ans<<"\n"; } else{ vector<bool> vis(1<<20,0); one[s.length()]=0; for(ll i=s.length()-1;i>=0;i--){ if(s[i]=='1'){ one[i]=1+one[i+1]; } else{ one[i]=0; } } for(ll i=0;i+k-1<s.length();i++){ if(one[i]>=k-20){ ll val=0; for(ll j=i+k-1;j>=i+k-20;j--){ val+=(1-(s[j]-'0'))*(1<<(i+k-1-j)); } vis[val]=1; } } string ans=""; for(ll i=0;i<unv[20].size();i++){ if(!vis[i]){ ans=unv[20][i]; break; } } cout<<"YES"<<"\n"; for(ll i=0;i<k-20;i++){ cout<<"0"; } cout<<ans<<"\n"; } } } ```
#define _USE_MATH_DEFINES #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags #pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX #include <bits/stdc++.h> using namespace std; #define LSB(i) ((i) & -(i)) // zeroes all the bits except the least significant one #define OPEN(a) freopen(a, "r", stdin) #define F first #define S second #define PB push_back #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ALL(i) begin((i)), end((i)) #define SZ(i) ((int)i.size()) #define SORT(a) sort(ALL(a)) using LL = long long; using LD = long double;//__float128; using PII = pair<int, int>; using PLL = pair<LL, LL>; using PLD = pair<LD, LD>; using VI = vector<int>; using VLL = vector<LL>; using VLD = vector<LD>; using VPII = vector<PII>; using VPLL = vector<PLL>; using VPLD = vector<PLD>; using VVI = vector<VI>; #define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME #define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc)) #define __FOR2(i, a, n) __FOR3(i, a, n, 1) #define __FOR1(i, n) __FOR2(i, 0, n) #define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__) #define REV(a,b) for(int a= b; a >= 0; --a) #define FRU(a,b) for(const auto& a: b) #define FRUM(a,b) for(auto& a : b) const int inf = 1e9 + 7; const int MOD = inf; const LL INF = 1e18 + 7; const long double PI = acos(-1); const LD EPS = 1e-9; namespace input { template< class T> istream& operator>>(istream& st, vector<T> & container) { for (auto& u : container) st >> u; return st; } template< class T, size_t N> istream& operator>>(istream& st, array<T, N> & container) { for (auto& u : container) st >> u; return st; } template <class T, class U> istream& operator>>(istream& st, pair<T, U> & p) { st >> p.first >> p.second; return st; } void re() {} template<typename T, typename... TArgs> void re(T& x, TArgs&... rest) { cin >> x; re(rest...); } } using namespace input; namespace output { template< class T> ostream& operator<<(ostream& st, const vector<T> & container) { for (auto& u : container) st << u << ' '; return st; } template< class T> ostream& operator<<(ostream& st, const deque<T> & container) { for (auto& u : container) st << u << ' '; return st; } template< class T, size_t N> ostream& operator<<(ostream& st, const array<T, N> & container) { for (auto& u : container) st << u << ' '; return st; } template <class T, class U> ostream& operator<<(ostream& st, pair<T, U> p) { st << p.first << ' ' << p.second; return st; } void pr() {} template <typename T> void pr(const T& x) { cout << x; } template <typename T, typename... TArgs> void pr(const T& x, const TArgs&... rest) { cout << x << ' '; pr(rest...); } template <typename... TArgs> void prln(const TArgs&... args) { pr(args...); cout << '\n'; } } using namespace output; namespace pairs { template<class T, class U, class V> pair<T, U> operator* (pair<T, U>p, V val) { return{ p.first * val, p.second * val }; } template<class T, class U, class V> pair<T, U> operator/ (pair<T, U>p, V val) { return{ p.first / val, p.second / val }; } template<class T, class U> pair<T, U> operator- (pair<T, U> a, pair<T, U> b) { return{ a.first - b.first, a.second - b.second }; } template<class T, class U> pair<T, U> operator+ (pair<T, U> a, pair<T, U> b) { return{ a.first + b.first, a.second + b.second }; } } using namespace pairs; namespace triples { #define TT1T2T3 template<class T1, class T2, class T3> #define TT1T2T3T4 template<class T1, class T2, class T3, class T4> #define TRT1T2T3 triple<T1, T2, T3> TT1T2T3 struct triple { T1 x; T2 y; T3 z; triple() : x(T1()), y(T2()), z(T3()) {}; triple(T1 _x, T2 _y, T3 _z) :x(_x), y(_y), z(_z) {} }; TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x < t2.x; if (t1.y != t2.y) return t1.y < t2.y; else return t1.z < t2.z; } TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x > t2.x; if (t1.y != t2.y) return t1.y > t2.y; else return t1.z > t2.z; } TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.x == t2.x && t1.y == t2.y && t1.z == t2.z); } TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.x >> t.y >> t.z; } TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.x << " " << t.y << " " << t.z; } TT1T2T3 TRT1T2T3 operator+(TRT1T2T3 a, TRT1T2T3 b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; } TT1T2T3 TRT1T2T3 operator-(TRT1T2T3 a, TRT1T2T3 b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; } TT1T2T3T4 TRT1T2T3 operator*(TRT1T2T3 a, T4 val) { return { a.x * val, a.y * val, a.z * val }; } TT1T2T3T4 TRT1T2T3 operator/(TRT1T2T3 a, T4 val) { return { a.x / val, a.y / val, a.z / val }; } #undef TT1T2T3T4 #undef TRT1T2T3 #undef TT1T2T3 using TRII = triple<int, int, int>; using TRLL = triple<LL, LL, LL>; using TRLD = triple<LD, LD, LD>; using VTRII = vector<TRII>; using VTRLL = vector<TRLL>; using VTRLD = vector<TRLD>; } using namespace triples; namespace geo { template<class T> T dotProduct(pair<T, T> a, pair<T, T> b) { return a.first*b.first + a.second* b.second; } template<class T> T crossProduct(pair<T, T>a, pair<T, T> b) { return a.first * b.second - a.second * b.first; } template<class T> T lengthPow(pair<T, T> a) { return a.first*1ll*a.first + a.second*1ll*a.second; } template<class T> LD length(pair<T, T> a) { return sqrt(lengthPow(a)); } template<class T> T dotProduct(triple<T, T, T> a, triple<T, T, T> b) { return a.x*b.x + a.y* b.y + a.z*b.z; } template<class T> T crossProduct(triple<T, T, T> a, triple<T, T, T> b) { return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x }; } template<class T> T lengthPow(triple<T, T, T> a) { return a.x*1ll*a.x + a.y*1ll*a.y + a.z*1ll*a.z; } template<class T> LD length(triple<T, T, T> a) { return sqrt(lengthPow(a)); } } using namespace geo; template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1 a %= b; if (a == 0) return b == 1 ? 0 : -1; T x = invGeneral(b, a); return x == -1 ? -1 : ((1 - (LL)b * x) / a + b) % b; } template<class T> struct modular { T val; explicit operator T() const { return val; } modular() { val = 0; } modular(const LL& v) { val = (-MOD <= v && v <= MOD) ? v : v % MOD; if (val < 0) val += MOD; } friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; } friend void pr(const modular& a) { pr(a.val); } friend void re(modular& a) { LL x; re(x); a = modular(x); } friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; } friend bool operator!=(const modular& a, const modular& b) { return !(a == b); } friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; } modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; } modular& operator*=(const modular& m) { val = (LL)val*m.val%MOD; return *this; } friend modular pow(modular a, LL p) { modular ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend modular inv(const modular& a) { auto i = invGeneral(a.val, MOD); assert(i != -1); return i; } // equivalent to return exp(b,MOD-2) if MOD is prime modular& operator/=(const modular& m) { return (*this) *= inv(m); } friend modular operator+(modular a, const modular& b) { return a += b; } friend modular operator-(modular a, const modular& b) { return a -= b; } friend modular operator*(modular a, const modular& b) { return a *= b; } friend modular operator/(modular a, const modular& b) { return a /= b; } }; using MI = modular<int>; using PMI = pair<MI, MI>; using VMI = vector<MI>; using VPMI = vector<PMI>; namespace debug { template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); } #if 1 && defined(LOCAL) #define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) 1999 #define cerr if(0) cout #endif } using namespace debug; template <class T> bool setMax(T& v, T newV) { if (v < newV) { v = newV; return true; } return false; } template <class T> bool setMin(T& v, T newV) { if (v > newV) { v = newV; return true; } return false; } string s; string res; pair<VI,VI> split(const VI & dane, int pos) { VI zer, jed; for(auto u : dane) { if(s[u + pos] == '0') zer.push_back(u); else jed.push_back(u); } return {zer, jed}; } bool calc(int i, int k, const VI & potrzebne) { if(i == k) { return potrzebne.size() == 0; } auto [zer, jed] = split(potrzebne, i); res += '0'; if(calc(i + 1, k, jed)) { return true; } else { res.back() = '1'; if(calc(i + 1, k, zer)) { return true; } res.pop_back(); return false; } } void solve() { int n, k; re(n, k); re(s); int ile = n - k + 1; // if(k < 30 and ile >= (1 << k)) // { // prln("NO"); // return; // } ile = max(0, k - 25); res = string(ile, '0'); VI potrzebne; deque<int> zera; FOR(i, ile) { if(s[i] == '0') zera.push_back(i); } FOR(i, ile, n) { int id = i - ile; if(id + k > n) break; while(zera.size() and zera.front() < id) zera.pop_front(); if(zera.empty()) potrzebne.push_back(id); if(s[i] == '0') zera.push_back(i); } auto ok = calc(ile, k, potrzebne); if(not ok) { prln("NO"); return; } prln("YES"); prln(res); } int main() { // #ifndef LOCAL // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif BOOST; cout << fixed << setprecision(13); int t = 1; cerr << "testy!!!!\n"; cin >> t; for(int i = 1; i <= t; ++i) // string s; // while(cin >> s) { solve(); } return 0; }
### Prompt Please formulate a CPP solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #define _USE_MATH_DEFINES #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags #pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX #include <bits/stdc++.h> using namespace std; #define LSB(i) ((i) & -(i)) // zeroes all the bits except the least significant one #define OPEN(a) freopen(a, "r", stdin) #define F first #define S second #define PB push_back #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ALL(i) begin((i)), end((i)) #define SZ(i) ((int)i.size()) #define SORT(a) sort(ALL(a)) using LL = long long; using LD = long double;//__float128; using PII = pair<int, int>; using PLL = pair<LL, LL>; using PLD = pair<LD, LD>; using VI = vector<int>; using VLL = vector<LL>; using VLD = vector<LD>; using VPII = vector<PII>; using VPLL = vector<PLL>; using VPLD = vector<PLD>; using VVI = vector<VI>; #define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME #define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc)) #define __FOR2(i, a, n) __FOR3(i, a, n, 1) #define __FOR1(i, n) __FOR2(i, 0, n) #define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__) #define REV(a,b) for(int a= b; a >= 0; --a) #define FRU(a,b) for(const auto& a: b) #define FRUM(a,b) for(auto& a : b) const int inf = 1e9 + 7; const int MOD = inf; const LL INF = 1e18 + 7; const long double PI = acos(-1); const LD EPS = 1e-9; namespace input { template< class T> istream& operator>>(istream& st, vector<T> & container) { for (auto& u : container) st >> u; return st; } template< class T, size_t N> istream& operator>>(istream& st, array<T, N> & container) { for (auto& u : container) st >> u; return st; } template <class T, class U> istream& operator>>(istream& st, pair<T, U> & p) { st >> p.first >> p.second; return st; } void re() {} template<typename T, typename... TArgs> void re(T& x, TArgs&... rest) { cin >> x; re(rest...); } } using namespace input; namespace output { template< class T> ostream& operator<<(ostream& st, const vector<T> & container) { for (auto& u : container) st << u << ' '; return st; } template< class T> ostream& operator<<(ostream& st, const deque<T> & container) { for (auto& u : container) st << u << ' '; return st; } template< class T, size_t N> ostream& operator<<(ostream& st, const array<T, N> & container) { for (auto& u : container) st << u << ' '; return st; } template <class T, class U> ostream& operator<<(ostream& st, pair<T, U> p) { st << p.first << ' ' << p.second; return st; } void pr() {} template <typename T> void pr(const T& x) { cout << x; } template <typename T, typename... TArgs> void pr(const T& x, const TArgs&... rest) { cout << x << ' '; pr(rest...); } template <typename... TArgs> void prln(const TArgs&... args) { pr(args...); cout << '\n'; } } using namespace output; namespace pairs { template<class T, class U, class V> pair<T, U> operator* (pair<T, U>p, V val) { return{ p.first * val, p.second * val }; } template<class T, class U, class V> pair<T, U> operator/ (pair<T, U>p, V val) { return{ p.first / val, p.second / val }; } template<class T, class U> pair<T, U> operator- (pair<T, U> a, pair<T, U> b) { return{ a.first - b.first, a.second - b.second }; } template<class T, class U> pair<T, U> operator+ (pair<T, U> a, pair<T, U> b) { return{ a.first + b.first, a.second + b.second }; } } using namespace pairs; namespace triples { #define TT1T2T3 template<class T1, class T2, class T3> #define TT1T2T3T4 template<class T1, class T2, class T3, class T4> #define TRT1T2T3 triple<T1, T2, T3> TT1T2T3 struct triple { T1 x; T2 y; T3 z; triple() : x(T1()), y(T2()), z(T3()) {}; triple(T1 _x, T2 _y, T3 _z) :x(_x), y(_y), z(_z) {} }; TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x < t2.x; if (t1.y != t2.y) return t1.y < t2.y; else return t1.z < t2.z; } TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x > t2.x; if (t1.y != t2.y) return t1.y > t2.y; else return t1.z > t2.z; } TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.x == t2.x && t1.y == t2.y && t1.z == t2.z); } TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.x >> t.y >> t.z; } TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.x << " " << t.y << " " << t.z; } TT1T2T3 TRT1T2T3 operator+(TRT1T2T3 a, TRT1T2T3 b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; } TT1T2T3 TRT1T2T3 operator-(TRT1T2T3 a, TRT1T2T3 b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; } TT1T2T3T4 TRT1T2T3 operator*(TRT1T2T3 a, T4 val) { return { a.x * val, a.y * val, a.z * val }; } TT1T2T3T4 TRT1T2T3 operator/(TRT1T2T3 a, T4 val) { return { a.x / val, a.y / val, a.z / val }; } #undef TT1T2T3T4 #undef TRT1T2T3 #undef TT1T2T3 using TRII = triple<int, int, int>; using TRLL = triple<LL, LL, LL>; using TRLD = triple<LD, LD, LD>; using VTRII = vector<TRII>; using VTRLL = vector<TRLL>; using VTRLD = vector<TRLD>; } using namespace triples; namespace geo { template<class T> T dotProduct(pair<T, T> a, pair<T, T> b) { return a.first*b.first + a.second* b.second; } template<class T> T crossProduct(pair<T, T>a, pair<T, T> b) { return a.first * b.second - a.second * b.first; } template<class T> T lengthPow(pair<T, T> a) { return a.first*1ll*a.first + a.second*1ll*a.second; } template<class T> LD length(pair<T, T> a) { return sqrt(lengthPow(a)); } template<class T> T dotProduct(triple<T, T, T> a, triple<T, T, T> b) { return a.x*b.x + a.y* b.y + a.z*b.z; } template<class T> T crossProduct(triple<T, T, T> a, triple<T, T, T> b) { return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x }; } template<class T> T lengthPow(triple<T, T, T> a) { return a.x*1ll*a.x + a.y*1ll*a.y + a.z*1ll*a.z; } template<class T> LD length(triple<T, T, T> a) { return sqrt(lengthPow(a)); } } using namespace geo; template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1 a %= b; if (a == 0) return b == 1 ? 0 : -1; T x = invGeneral(b, a); return x == -1 ? -1 : ((1 - (LL)b * x) / a + b) % b; } template<class T> struct modular { T val; explicit operator T() const { return val; } modular() { val = 0; } modular(const LL& v) { val = (-MOD <= v && v <= MOD) ? v : v % MOD; if (val < 0) val += MOD; } friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; } friend void pr(const modular& a) { pr(a.val); } friend void re(modular& a) { LL x; re(x); a = modular(x); } friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; } friend bool operator!=(const modular& a, const modular& b) { return !(a == b); } friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; } modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; } modular& operator*=(const modular& m) { val = (LL)val*m.val%MOD; return *this; } friend modular pow(modular a, LL p) { modular ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend modular inv(const modular& a) { auto i = invGeneral(a.val, MOD); assert(i != -1); return i; } // equivalent to return exp(b,MOD-2) if MOD is prime modular& operator/=(const modular& m) { return (*this) *= inv(m); } friend modular operator+(modular a, const modular& b) { return a += b; } friend modular operator-(modular a, const modular& b) { return a -= b; } friend modular operator*(modular a, const modular& b) { return a *= b; } friend modular operator/(modular a, const modular& b) { return a /= b; } }; using MI = modular<int>; using PMI = pair<MI, MI>; using VMI = vector<MI>; using VPMI = vector<PMI>; namespace debug { template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); } #if 1 && defined(LOCAL) #define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) 1999 #define cerr if(0) cout #endif } using namespace debug; template <class T> bool setMax(T& v, T newV) { if (v < newV) { v = newV; return true; } return false; } template <class T> bool setMin(T& v, T newV) { if (v > newV) { v = newV; return true; } return false; } string s; string res; pair<VI,VI> split(const VI & dane, int pos) { VI zer, jed; for(auto u : dane) { if(s[u + pos] == '0') zer.push_back(u); else jed.push_back(u); } return {zer, jed}; } bool calc(int i, int k, const VI & potrzebne) { if(i == k) { return potrzebne.size() == 0; } auto [zer, jed] = split(potrzebne, i); res += '0'; if(calc(i + 1, k, jed)) { return true; } else { res.back() = '1'; if(calc(i + 1, k, zer)) { return true; } res.pop_back(); return false; } } void solve() { int n, k; re(n, k); re(s); int ile = n - k + 1; // if(k < 30 and ile >= (1 << k)) // { // prln("NO"); // return; // } ile = max(0, k - 25); res = string(ile, '0'); VI potrzebne; deque<int> zera; FOR(i, ile) { if(s[i] == '0') zera.push_back(i); } FOR(i, ile, n) { int id = i - ile; if(id + k > n) break; while(zera.size() and zera.front() < id) zera.pop_front(); if(zera.empty()) potrzebne.push_back(id); if(s[i] == '0') zera.push_back(i); } auto ok = calc(ile, k, potrzebne); if(not ok) { prln("NO"); return; } prln("YES"); prln(res); } int main() { // #ifndef LOCAL // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif BOOST; cout << fixed << setprecision(13); int t = 1; cerr << "testy!!!!\n"; cin >> t; for(int i = 1; i <= t; ++i) // string s; // while(cin >> s) { solve(); } return 0; } ```
#include <bits/stdc++.h> #define MAX_SIZE 1000111 using namespace std; int prefix[MAX_SIZE]; char st[MAX_SIZE], pt[MAX_SIZE]; int main() { int T; scanf("%d", &T); while (T-- > 0) { int N, K, up = 20, i, j; scanf("%d%d", &N, &K); up = min(up, K); scanf("%s", st + 1); for (i = 1; i <= N; ++i) { if (st[i] == '0') { pt[i] = '1'; } else { pt[i] = '0'; } } for (i = 1; i <= N; ++i) { prefix[i] = prefix[i-1] + st[i] - '0'; } vector<int> bad; for (i = 1; i <= N - K + 1; ++i) { if (prefix[i + K - up - 1] - prefix[i-1] == K - up) { int curr = 0; for (j = i + K - up; j <= i + K - 1; ++j) { curr = curr * 2 + (pt[j] - '0'); } bad.push_back(curr); } } sort(bad.begin(), bad.end()); vector<int>::iterator it; it = unique (bad.begin(), bad.end()); bad.resize(distance(bad.begin(),it)); int ans = 0; for (auto v : bad) { if (ans != v) break; ans = v + 1; } if (ans >= (1 << up)) { printf("NO\n"); } else { printf("YES\n"); for (i = 1; i <= K - up; ++i) { printf("0"); } for (i = up - 1; i >= 0; --i) { if (ans & (1 << i)) { printf("1"); } else { printf("0"); } } printf("\n"); } } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <bits/stdc++.h> #define MAX_SIZE 1000111 using namespace std; int prefix[MAX_SIZE]; char st[MAX_SIZE], pt[MAX_SIZE]; int main() { int T; scanf("%d", &T); while (T-- > 0) { int N, K, up = 20, i, j; scanf("%d%d", &N, &K); up = min(up, K); scanf("%s", st + 1); for (i = 1; i <= N; ++i) { if (st[i] == '0') { pt[i] = '1'; } else { pt[i] = '0'; } } for (i = 1; i <= N; ++i) { prefix[i] = prefix[i-1] + st[i] - '0'; } vector<int> bad; for (i = 1; i <= N - K + 1; ++i) { if (prefix[i + K - up - 1] - prefix[i-1] == K - up) { int curr = 0; for (j = i + K - up; j <= i + K - 1; ++j) { curr = curr * 2 + (pt[j] - '0'); } bad.push_back(curr); } } sort(bad.begin(), bad.end()); vector<int>::iterator it; it = unique (bad.begin(), bad.end()); bad.resize(distance(bad.begin(),it)); int ans = 0; for (auto v : bad) { if (ans != v) break; ans = v + 1; } if (ans >= (1 << up)) { printf("NO\n"); } else { printf("YES\n"); for (i = 1; i <= K - up; ++i) { printf("0"); } for (i = up - 1; i >= 0; --i) { if (ans & (1 << i)) { printf("1"); } else { printf("0"); } } printf("\n"); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<vvvi> vvvvi; typedef long long ll; typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<vvll> vvvll; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<vvc> vvvc; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<vvd> vvvd; typedef pair<int,int> pi; typedef pair<ll,ll> pll; typedef vector<pi> vpi; typedef vector<vpi> vvpi; typedef vector<vvpi> vvvpi; typedef vector<pll> vpll; typedef vector<vpll> vvpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef pair<double,double> pd; typedef vector<pd> vpd; typedef complex<double> cd; typedef vector<cd> vcd; typedef unsigned int uint; template<class C> void mini(C&a, C b){ a=min(a, b);} template<class C> void maxi(C&a, C b){a=max(a,b);} #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define forall(it,s) for(auto it = s.begin(); it != s.end(); ++it) #define F0(i,n) for(int i = 0; i < n; i++) #define F1(i,n) for(int (i) = 1; i <= n; i++) #define F0R(i,n) for(int (i) = n-1; i >= 0; i--) #define F1R(i,n) for(int (i) = n; i >= 1; i--) #define REP(i,a,b) for(int i = a; i <= b; i++) #define REPR(i,a,b) for(int i = a; i >= b; i--) #define INF 1e18 #define todo(v) v.begin(),v.end() #define eps 0.000000000001 #define mod 1000000007 void e(){ int n,k; cin>>n>>k; string s; cin>>s; vi ceros(n); F0(i,n) if(s[i] == '0') ceros[i]++; F1(i,n-1) ceros[i] += ceros[i-1]; F0(i,n) { if(s[i] == '1') s[i] = '0'; else s[i] = '1'; } int k1 = min(21,k); while(k1 > 0 and (1<<(k1-1)) > n-k+1) k1--; vb dp((1<<k1),false); int val = 0; REP(i,k-k1,k-1){ val *= 2; if(s[i] == '1') val++; } if(k-k1-1 < 0 or ceros[k-k1-1] == 0) dp[val] = true; int r = k; while(r < n){ val *= 2; val %= (1<<k1); if(s[r] == '1') val++; r++; if(ceros[r-k1-1] - ceros[r-k-1] == 0) dp[val] = true; } F0(val,(1<<k1)){ if(!dp[val]){ vc res(k,'0'); F0(j,k1) { if((val & (1<<j)) != 0) res[j] = '1'; } cout<<"YES\n"; F0R(i,k) cout<<res[i]; cout<<'\n'; return; } } cout<<"NO\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //freopen("../input.txt","r",stdin); //freopen("../output.txt","w",stdout); int t; cin>>t; while(t--) e(); return 0; }
### Prompt Please create a solution in Cpp to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<vvvi> vvvvi; typedef long long ll; typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<vvll> vvvll; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<vvc> vvvc; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<vvd> vvvd; typedef pair<int,int> pi; typedef pair<ll,ll> pll; typedef vector<pi> vpi; typedef vector<vpi> vvpi; typedef vector<vvpi> vvvpi; typedef vector<pll> vpll; typedef vector<vpll> vvpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef pair<double,double> pd; typedef vector<pd> vpd; typedef complex<double> cd; typedef vector<cd> vcd; typedef unsigned int uint; template<class C> void mini(C&a, C b){ a=min(a, b);} template<class C> void maxi(C&a, C b){a=max(a,b);} #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define forall(it,s) for(auto it = s.begin(); it != s.end(); ++it) #define F0(i,n) for(int i = 0; i < n; i++) #define F1(i,n) for(int (i) = 1; i <= n; i++) #define F0R(i,n) for(int (i) = n-1; i >= 0; i--) #define F1R(i,n) for(int (i) = n; i >= 1; i--) #define REP(i,a,b) for(int i = a; i <= b; i++) #define REPR(i,a,b) for(int i = a; i >= b; i--) #define INF 1e18 #define todo(v) v.begin(),v.end() #define eps 0.000000000001 #define mod 1000000007 void e(){ int n,k; cin>>n>>k; string s; cin>>s; vi ceros(n); F0(i,n) if(s[i] == '0') ceros[i]++; F1(i,n-1) ceros[i] += ceros[i-1]; F0(i,n) { if(s[i] == '1') s[i] = '0'; else s[i] = '1'; } int k1 = min(21,k); while(k1 > 0 and (1<<(k1-1)) > n-k+1) k1--; vb dp((1<<k1),false); int val = 0; REP(i,k-k1,k-1){ val *= 2; if(s[i] == '1') val++; } if(k-k1-1 < 0 or ceros[k-k1-1] == 0) dp[val] = true; int r = k; while(r < n){ val *= 2; val %= (1<<k1); if(s[r] == '1') val++; r++; if(ceros[r-k1-1] - ceros[r-k-1] == 0) dp[val] = true; } F0(val,(1<<k1)){ if(!dp[val]){ vc res(k,'0'); F0(j,k1) { if((val & (1<<j)) != 0) res[j] = '1'; } cout<<"YES\n"; F0R(i,k) cout<<res[i]; cout<<'\n'; return; } } cout<<"NO\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //freopen("../input.txt","r",stdin); //freopen("../output.txt","w",stdout); int t; cin>>t; while(t--) e(); return 0; } ```
#include <bits/stdc++.h> #define fr(i, n, m) for(int i = (n); i < (m); i ++) #define rfr(i, n, m) for(int i = (n); i >= (m); i --) #define pb push_back #define st first #define nd second #define pq priority_queue #define all(x) begin(x), end(x) #include <time.h> #include <cmath> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll,int> pii; const int i_inf = 1e9; const ll inf = 1e18; const ll mod = 1e9+7; const ld eps = 1e-13; const ld pi = 3.14159265359; const int mxn = 3e5+5; mt19937 _rand(time(NULL)); clock_t z; bool used[(1<<22)]; void solve(){ int n, k; cin >> n >> k; string s; cin >> s; int zer[n+1]; zer[0] = 0; fr(i, 1, n+1){ zer[i] = zer[i-1]; if(s[i-1] == '0') zer[i] = i; } fr(i, k, n+1){ if(zer[i] <= i-k){ used[0] = true; } int mask = 0; for(int j = i, pos = 0; j >= 1 && pos < 22; j --, pos++){ if(s[j-1] == '0') mask |= (1<<pos); if(zer[j-1] <= i-k){ used[mask] = true; } } } int ans = -1; int bo = n+10; if(k < 25) bo = min(bo, (1<<k)); fr(i, 0, bo){ if(used[i]) continue; ans = i; break; } fr(i, k, n+1){ if(zer[i] <= i-k){ used[0] = false; } int mask = 0; for(int j = i, pos = 0; j >= 1 && pos < 22; j --, pos++){ if(s[j-1] == '0') mask |= (1<<pos); if(zer[j-1] <= i-k){ used[mask] = false; } } } if(ans == -1){ cout<<"NO"<<endl; return; } else{ cout<<"YES"<<endl; string sol = ""; int lst = 0; fr(i, 0, 22){ if(ans & (1<<i)){ lst = i+1; } } fr(i, 0, lst){ if(ans & (1<<i)){ sol += '1'; } else{ sol += '0'; } } fr(i, lst, k){ sol += '0'; } reverse(sol.begin(), sol.end()); cout<<sol<<endl; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int tc = 1; cin >> tc; while(tc--){ solve(); } }
### Prompt Your task is to create a CPP solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <bits/stdc++.h> #define fr(i, n, m) for(int i = (n); i < (m); i ++) #define rfr(i, n, m) for(int i = (n); i >= (m); i --) #define pb push_back #define st first #define nd second #define pq priority_queue #define all(x) begin(x), end(x) #include <time.h> #include <cmath> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll,int> pii; const int i_inf = 1e9; const ll inf = 1e18; const ll mod = 1e9+7; const ld eps = 1e-13; const ld pi = 3.14159265359; const int mxn = 3e5+5; mt19937 _rand(time(NULL)); clock_t z; bool used[(1<<22)]; void solve(){ int n, k; cin >> n >> k; string s; cin >> s; int zer[n+1]; zer[0] = 0; fr(i, 1, n+1){ zer[i] = zer[i-1]; if(s[i-1] == '0') zer[i] = i; } fr(i, k, n+1){ if(zer[i] <= i-k){ used[0] = true; } int mask = 0; for(int j = i, pos = 0; j >= 1 && pos < 22; j --, pos++){ if(s[j-1] == '0') mask |= (1<<pos); if(zer[j-1] <= i-k){ used[mask] = true; } } } int ans = -1; int bo = n+10; if(k < 25) bo = min(bo, (1<<k)); fr(i, 0, bo){ if(used[i]) continue; ans = i; break; } fr(i, k, n+1){ if(zer[i] <= i-k){ used[0] = false; } int mask = 0; for(int j = i, pos = 0; j >= 1 && pos < 22; j --, pos++){ if(s[j-1] == '0') mask |= (1<<pos); if(zer[j-1] <= i-k){ used[mask] = false; } } } if(ans == -1){ cout<<"NO"<<endl; return; } else{ cout<<"YES"<<endl; string sol = ""; int lst = 0; fr(i, 0, 22){ if(ans & (1<<i)){ lst = i+1; } } fr(i, 0, lst){ if(ans & (1<<i)){ sol += '1'; } else{ sol += '0'; } } fr(i, lst, k){ sol += '0'; } reverse(sol.begin(), sol.end()); cout<<sol<<endl; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int tc = 1; cin >> tc; while(tc--){ solve(); } } ```
#include <bits/stdc++.h> #define fi first #define se second #define DB double #define U unsigned #define P std::pair #define LL long long #define LD long double #define pb push_back #define MP std::make_pair #define SZ(x) ((int)x.size()) #define all(x) x.begin(),x.end() #define CLR(i,a) memset(i,a,sizeof(i)) #define FOR(i,a,b) for(int i = a;i <= b;++i) #define ROF(i,a,b) for(int i = a;i >= b;--i) #define DEBUG(x) std::cerr << #x << '=' << x << std::endl const int MAXN = 1e6 + 5; char str[MAXN]; int a[MAXN],ans[MAXN]; int n,k; int suf[MAXN]; inline void Solve(){ scanf("%d%d",&n,&k);scanf("%s",str+1);FOR(i,1,n) a[i] = (str[i]=='0'); suf[n+1] = 0; FOR(i,1,k-20) ans[i] = 0; int l = std::max(1,k-19),len = l-1; std::vector<int> S; ROF(i,n,1) suf[i] = a[i]?0:suf[i+1]+1; FOR(i,1,n-k+1){ if(suf[i] < len) continue; int t = 0; FOR(j,i+len,i+k-1) t = (t<<1)|a[j]; S.pb(t); } std::sort(all(S));S.erase(std::unique(all(S)),S.end()); FOR(i,l,k){ int bit = k-i; int sm = 0; for(auto x:S){ if(x != -1) if(!((x>>bit)&1)) sm++; } if(sm != (1<<(k-i))) ans[i] = 0; else{ sm = 0; for(auto x:S){ if(x != -1) if((((x>>bit)&1))) sm++; } if(sm == ((1<<(k-i)))){ puts("NO"); return; } else ans[i] = 1; } for(auto &x:S) if(((x>>bit)&1) != ans[i]) x = -1; } puts("YES"); FOR(i,1,k) printf("%d",ans[i]);puts(""); } int main(){ int T;scanf("%d",&T);while(T--) Solve(); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <bits/stdc++.h> #define fi first #define se second #define DB double #define U unsigned #define P std::pair #define LL long long #define LD long double #define pb push_back #define MP std::make_pair #define SZ(x) ((int)x.size()) #define all(x) x.begin(),x.end() #define CLR(i,a) memset(i,a,sizeof(i)) #define FOR(i,a,b) for(int i = a;i <= b;++i) #define ROF(i,a,b) for(int i = a;i >= b;--i) #define DEBUG(x) std::cerr << #x << '=' << x << std::endl const int MAXN = 1e6 + 5; char str[MAXN]; int a[MAXN],ans[MAXN]; int n,k; int suf[MAXN]; inline void Solve(){ scanf("%d%d",&n,&k);scanf("%s",str+1);FOR(i,1,n) a[i] = (str[i]=='0'); suf[n+1] = 0; FOR(i,1,k-20) ans[i] = 0; int l = std::max(1,k-19),len = l-1; std::vector<int> S; ROF(i,n,1) suf[i] = a[i]?0:suf[i+1]+1; FOR(i,1,n-k+1){ if(suf[i] < len) continue; int t = 0; FOR(j,i+len,i+k-1) t = (t<<1)|a[j]; S.pb(t); } std::sort(all(S));S.erase(std::unique(all(S)),S.end()); FOR(i,l,k){ int bit = k-i; int sm = 0; for(auto x:S){ if(x != -1) if(!((x>>bit)&1)) sm++; } if(sm != (1<<(k-i))) ans[i] = 0; else{ sm = 0; for(auto x:S){ if(x != -1) if((((x>>bit)&1))) sm++; } if(sm == ((1<<(k-i)))){ puts("NO"); return; } else ans[i] = 1; } for(auto &x:S) if(((x>>bit)&1) != ans[i]) x = -1; } puts("YES"); FOR(i,1,k) printf("%d",ans[i]);puts(""); } int main(){ int T;scanf("%d",&T);while(T--) Solve(); return 0; } ```
#include <iostream> #include <cstdio> using namespace std; // <|°_°|> const int MAX_LOG = 20; const int MAX_VALEURS = (1 << MAX_LOG); char Entree[MAX_VALEURS]; int Val[MAX_VALEURS]; bool Atteint[MAX_VALEURS]; bool PasZero[MAX_VALEURS]; int nbValeurs, k; void Read() { scanf("%d%d", &nbValeurs, &k); scanf("%s", Entree); for (int i = 0; i < nbValeurs; i ++) { Val[i] = Entree[i] - '0'; PasZero[i] = false; } int curLog = 1; while ((1 << curLog) < nbValeurs) { curLog ++; } curLog = min(curLog, k); int lastZ = -1; if (0 >= k - curLog) { PasZero[curLog - 1] = true; } for (int i = 0; i < nbValeurs; i ++) { if (Val[i] == 0) { lastZ = i; } if (i - lastZ >= k - curLog) { PasZero[i + curLog] = true; } } for (int i = 0; i < (1 << curLog); i ++) { Atteint[i] = false; } int cur = 0; for (int i = 0; i < nbValeurs; i ++) { cur <<= 1; cur += Val[i]; if (i >= curLog) { cur -= (Val[i - curLog] << curLog); } if (PasZero[i]) { Atteint[cur] = true; } } int maximum = (1 << curLog) - 1; while (maximum >= 0 && Atteint[maximum]) { maximum --; } if (maximum < 0) { printf("NO\n"); return; } int ans = (1 << curLog) - 1 - maximum; printf("YES\n"); for (int i = 0; i < k - curLog; i ++) { printf("0"); } for (int i = curLog - 1; i >= 0; i --) { printf("%d", (ans & (1 << i)) >> i); } printf("\n"); return; } int main() { int nbTests; scanf("%d", &nbTests); for (int i = 0; i < nbTests; i ++) { Read(); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include <iostream> #include <cstdio> using namespace std; // <|°_°|> const int MAX_LOG = 20; const int MAX_VALEURS = (1 << MAX_LOG); char Entree[MAX_VALEURS]; int Val[MAX_VALEURS]; bool Atteint[MAX_VALEURS]; bool PasZero[MAX_VALEURS]; int nbValeurs, k; void Read() { scanf("%d%d", &nbValeurs, &k); scanf("%s", Entree); for (int i = 0; i < nbValeurs; i ++) { Val[i] = Entree[i] - '0'; PasZero[i] = false; } int curLog = 1; while ((1 << curLog) < nbValeurs) { curLog ++; } curLog = min(curLog, k); int lastZ = -1; if (0 >= k - curLog) { PasZero[curLog - 1] = true; } for (int i = 0; i < nbValeurs; i ++) { if (Val[i] == 0) { lastZ = i; } if (i - lastZ >= k - curLog) { PasZero[i + curLog] = true; } } for (int i = 0; i < (1 << curLog); i ++) { Atteint[i] = false; } int cur = 0; for (int i = 0; i < nbValeurs; i ++) { cur <<= 1; cur += Val[i]; if (i >= curLog) { cur -= (Val[i - curLog] << curLog); } if (PasZero[i]) { Atteint[cur] = true; } } int maximum = (1 << curLog) - 1; while (maximum >= 0 && Atteint[maximum]) { maximum --; } if (maximum < 0) { printf("NO\n"); return; } int ans = (1 << curLog) - 1 - maximum; printf("YES\n"); for (int i = 0; i < k - curLog; i ++) { printf("0"); } for (int i = curLog - 1; i >= 0; i --) { printf("%d", (ans & (1 << i)) >> i); } printf("\n"); return; } int main() { int nbTests; scanf("%d", &nbTests); for (int i = 0; i < nbTests; i ++) { Read(); } return 0; } ```
#include<bits/stdc++.h> #include<algorithm> using namespace std; #define ll long long #define ld long double #define vi vector<int> #define pii pair<int,int> #define mkp make_pair #define all(A) A.begin(),A.end() #define allr(A) A.rbegin(),A.rend() #define F first #define S second const ll logN = 20; const ll M = 1000000007; const ll INF = 1e18; #define PI 3.14159265 #define pb push_back using namespace std; int ceillog(int x) { int y=0; while((1<<y)<x) y++; return y; } int main() { int t; cin>>t; while(t--) { int n,k; cin>>n>>k; string s; cin>>s; int m=min(k,ceillog(n-k+2)); vector<int> next0(n,int(1e9)); int d=k-m; if(s[n-1]=='0') { next0[n-1]=n-1; } for(int i=n-2;i>=0;i--) { if(s[i]=='0') { next0[i]=i; } else { next0[i]=next0[i+1]; } } vector<bool> used((1<<m),0); for(int i=0;i<n-k+1;i++) { if(next0[i]-i<d) continue; else { int temp=0; for(int j=i+d;j<i+k;j++) { temp=(temp*2)+(s[j]-'0'); } used[((1<<(m))-1)^temp]=1; } } int ans=-1; for(int i=0;i<(1<<m);i++) { if(used[i]!=1) { ans=i; break; } } if(ans==-1) { cout<<"NO"<<endl; } else { cout<<"YES"<<endl; string t1,t2(d,'0'); for(int i=0;i<m;i++) { t1.push_back('0'+(ans%2)); ans/=2; } reverse(t1.begin(),t1.end()); t2+=t1; cout<<t2<<endl; } } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Let's call two strings a and b (both of length k) a bit similar if they have the same character in some position, i. e. there exists at least one i ∈ [1, k] such that a_i = b_i. You are given a binary string s of length n (a string of n characters 0 and/or 1) and an integer k. Let's denote the string s[i..j] as the substring of s starting from the i-th character and ending with the j-th character (that is, s[i..j] = s_i s_{i + 1} s_{i + 2} ... s_{j - 1} s_j). Let's call a binary string t of length k beautiful if it is a bit similar to all substrings of s having length exactly k; that is, it is a bit similar to s[1..k], s[2..k+1], ..., s[n-k+1..n]. Your goal is to find the lexicographically smallest string t that is beautiful, or report that no such string exists. String x is lexicographically less than string y if either x is a prefix of y (and x ≠ y), or there exists such i (1 ≤ i ≤ min(|x|, |y|)), that x_i < y_i, and for any j (1 ≤ j < i) x_j = y_j. Input The first line contains one integer q (1 ≤ q ≤ 10000) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers n and k (1 ≤ k ≤ n ≤ 10^6). The second line contains the string s, consisting of n characters (each character is either 0 or 1). It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output For each test case, print the answer as follows: * if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); * otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line — the lexicographically smallest beautiful string, consisting of k characters 0 and/or 1. Example Input 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 Output YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010 ### Response ```cpp #include<bits/stdc++.h> #include<algorithm> using namespace std; #define ll long long #define ld long double #define vi vector<int> #define pii pair<int,int> #define mkp make_pair #define all(A) A.begin(),A.end() #define allr(A) A.rbegin(),A.rend() #define F first #define S second const ll logN = 20; const ll M = 1000000007; const ll INF = 1e18; #define PI 3.14159265 #define pb push_back using namespace std; int ceillog(int x) { int y=0; while((1<<y)<x) y++; return y; } int main() { int t; cin>>t; while(t--) { int n,k; cin>>n>>k; string s; cin>>s; int m=min(k,ceillog(n-k+2)); vector<int> next0(n,int(1e9)); int d=k-m; if(s[n-1]=='0') { next0[n-1]=n-1; } for(int i=n-2;i>=0;i--) { if(s[i]=='0') { next0[i]=i; } else { next0[i]=next0[i+1]; } } vector<bool> used((1<<m),0); for(int i=0;i<n-k+1;i++) { if(next0[i]-i<d) continue; else { int temp=0; for(int j=i+d;j<i+k;j++) { temp=(temp*2)+(s[j]-'0'); } used[((1<<(m))-1)^temp]=1; } } int ans=-1; for(int i=0;i<(1<<m);i++) { if(used[i]!=1) { ans=i; break; } } if(ans==-1) { cout<<"NO"<<endl; } else { cout<<"YES"<<endl; string t1,t2(d,'0'); for(int i=0;i<m;i++) { t1.push_back('0'+(ans%2)); ans/=2; } reverse(t1.begin(),t1.end()); t2+=t1; cout<<t2<<endl; } } return 0; } ```