output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } reverse(b.begin(), b.end()); int start = -1, fin = -1; for (int i = 0; i < n; ++i) { if (b[i] == a[i]) { if (start == -1) start = i; fin = i; } } bool bad = 0; if (start != -1) { for (int i = 0, j = start; j <= fin; ++i) { if (i == start) i = fin + 1; if (i >= n) { bad = 1; break; } if (a[i] != b[j] && b[i] != b[j]) { swap(b[i], b[j++]); } } for (int i = 0; i < n; ++i) bad |= (a[i] == b[i]); } if (bad) { cout << "No" << endl; } else { cout << "Yes" << endl; for (int i = 0; i < n; ++i) { if (i) cout << " "; cout << b[i]; } cout << endl; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } reverse(b.begin(), b.end()); int start = -1, fin = -1; for (int i = 0; i < n; ++i) { if (b[i] == a[i]) { if (start == -1) start = i; fin = i; } } bool bad = 0; if (start != -1) { for (int i = 0, j = start; j <= fin; ++i) { if (i == start) i = fin + 1; if (i >= n) { bad = 1; break; } if (a[i] != b[j] && b[i] != b[j]) { swap(b[i], b[j++]); } } for (int i = 0; i < n; ++i) bad |= (a[i] == b[i]); } if (bad) { cout << "No" << endl; } else { cout << "Yes" << endl; for (int i = 0; i < n; ++i) { if (i) cout << " "; cout << b[i]; } cout << endl; } return 0; } ```
#include<bits/stdc++.h> using namespace std; int s=200005,t,l,r,n,a[200005],b[200005]; int main() { scanf("%d",&n);l=1,r=n; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[n-i]); for(int i=1;i<=n;i++) if(a[i]==b[i]) { if(s==200005) s=i; t=i; } for(int i=s;i<=t;i++) if(a[l]!=b[i]&&b[l]!=b[i]){swap(b[l],b[i]);l++;} else if(a[r]!=b[i]&&b[r]!=b[i]){swap(b[r],b[i]);r--;} else{puts("No");return 0;} puts("Yes"); for(int i=1;i<=n;i++) printf("%d ",b[i]); printf("\n"); return 0; }
### Prompt Please create a solution in Cpp to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int s=200005,t,l,r,n,a[200005],b[200005]; int main() { scanf("%d",&n);l=1,r=n; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[n-i]); for(int i=1;i<=n;i++) if(a[i]==b[i]) { if(s==200005) s=i; t=i; } for(int i=s;i<=t;i++) if(a[l]!=b[i]&&b[l]!=b[i]){swap(b[l],b[i]);l++;} else if(a[r]!=b[i]&&b[r]!=b[i]){swap(b[r],b[i]);r--;} else{puts("No");return 0;} puts("Yes"); for(int i=1;i<=n;i++) printf("%d ",b[i]); printf("\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int N,Z=0; cin>>N; vector<int> p(N); vector<int> q(N); vector<int> r(N); for(int i=0;i<N;i++){ cin>>p[i]; r[p[i]-1]++; Z=max(Z,r[p[i]-1]); } for(int i=0;i<N;i++){ cin>>q[i]; r[q[i]-1]++; Z=max(Z,r[q[i]-1]); } if(Z>N){ cout<<"No"<<endl; return 0; } cout<<"Yes"<<endl; reverse(q.begin(),q.end()); int A=0,B=N-1; for(int i=0;i<N;i++){ if(p[i]!=q[i]){ continue; } if(p[i]!=q[A]&&p[A]!=q[i]){ swap(q[A],q[i]); A++; } else{ swap(q[B],q[i]); B--; } } for(int i=0;i<N;i++){ cout<<q[i]; if(i+1==N){ cout<<endl; } else{ cout<<" "; } } }
### Prompt Generate a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N,Z=0; cin>>N; vector<int> p(N); vector<int> q(N); vector<int> r(N); for(int i=0;i<N;i++){ cin>>p[i]; r[p[i]-1]++; Z=max(Z,r[p[i]-1]); } for(int i=0;i<N;i++){ cin>>q[i]; r[q[i]-1]++; Z=max(Z,r[q[i]-1]); } if(Z>N){ cout<<"No"<<endl; return 0; } cout<<"Yes"<<endl; reverse(q.begin(),q.end()); int A=0,B=N-1; for(int i=0;i<N;i++){ if(p[i]!=q[i]){ continue; } if(p[i]!=q[A]&&p[A]!=q[i]){ swap(q[A],q[i]); A++; } else{ swap(q[B],q[i]); B--; } } for(int i=0;i<N;i++){ cout<<q[i]; if(i+1==N){ cout<<endl; } else{ cout<<" "; } } } ```
#include <iostream> using namespace std; const int maxn=2e5+10; int a[maxn],b[maxn],c[maxn]; int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n;cin>>n; for(int i=1; i<=n; i++)cin>>a[i]; for(int i=1; i<=n; i++)cin>>b[i]; int r=n,l; for(int i=1; i<=n; i++)c[i]=b[r--]; l=1;r=n; for(int i=1; i<=n; i++){ if(a[i]!=c[i])continue; if(l<i&&c[l]!=a[i]&&a[l]!=c[i]){ swap(c[l],c[i]); l++; }else if(i<r&&c[r]!=a[i]&&a[r]!=c[i]){ swap(c[r],c[i]); r--; }else return cout<<"No"<<endl,0; } cout<<"Yes"<<endl; for(int i=1; i<=n; i++)cout<<c[i]<<' '; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <iostream> using namespace std; const int maxn=2e5+10; int a[maxn],b[maxn],c[maxn]; int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n;cin>>n; for(int i=1; i<=n; i++)cin>>a[i]; for(int i=1; i<=n; i++)cin>>b[i]; int r=n,l; for(int i=1; i<=n; i++)c[i]=b[r--]; l=1;r=n; for(int i=1; i<=n; i++){ if(a[i]!=c[i])continue; if(l<i&&c[l]!=a[i]&&a[l]!=c[i]){ swap(c[l],c[i]); l++; }else if(i<r&&c[r]!=a[i]&&a[r]!=c[i]){ swap(c[r],c[i]); r--; }else return cout<<"No"<<endl,0; } cout<<"Yes"<<endl; for(int i=1; i<=n; i++)cout<<c[i]<<' '; return 0; } ```
#include<iostream> #include<cstdio> #include<queue> using namespace std; const int N=1002020; int n,a[N],b[N]; void print(){ puts("Yes"); for(int i=1;i<=n;i++) cout<<b[i]<<" ";cout<<endl; } int main() { cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=n;i>=1;i--)cin>>b[i]; int l=n,r=0; for(int i=1;i<=n;i++) if(a[i]==b[i])l=min(l,i),r=max(r,i); if(!r){print();return 0;} int nw=l; for(int i=1;i<l && nw<=r;i++) if(b[nw]!=a[i] && b[nw]!=b[i]) swap(b[nw],b[i]),++nw; for(int i=r+1;i<=n && nw<=r;i++) if(b[nw]!=a[i] && b[nw]!=b[i]) swap(b[nw],b[i]),++nw; int fl=0; for(int i=1;i<=n;i++) if(b[i]==a[i]){fl=1;break;} if(fl)puts("No"); else print(); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<iostream> #include<cstdio> #include<queue> using namespace std; const int N=1002020; int n,a[N],b[N]; void print(){ puts("Yes"); for(int i=1;i<=n;i++) cout<<b[i]<<" ";cout<<endl; } int main() { cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=n;i>=1;i--)cin>>b[i]; int l=n,r=0; for(int i=1;i<=n;i++) if(a[i]==b[i])l=min(l,i),r=max(r,i); if(!r){print();return 0;} int nw=l; for(int i=1;i<l && nw<=r;i++) if(b[nw]!=a[i] && b[nw]!=b[i]) swap(b[nw],b[i]),++nw; for(int i=r+1;i<=n && nw<=r;i++) if(b[nw]!=a[i] && b[nw]!=b[i]) swap(b[nw],b[i]),++nw; int fl=0; for(int i=1;i<=n;i++) if(b[i]==a[i]){fl=1;break;} if(fl)puts("No"); else print(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn=200005; int n,L,R,a[maxn],b[maxn]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=1;i<=n;i++) scanf("%d",&b[i]); reverse(b+1,b+1+n); for (int i=1;i<=n;i++) if (a[i]==b[i]) { if (!L) L=i; R=i; } if (L) { int head=1,x=a[L]; bool sol=true; for (int i=L;i<=R;i++) { while (head<=n && (a[head]==x || b[head]==x)) head++; if (head>n) { sol=false; break; } swap(b[head++],b[i]); } if (!sol) { puts("No"); return 0; } } puts("Yes"); for (int i=1;i<n;i++) printf("%d ",b[i]); printf("%d\n",b[n]); return 0; }
### Prompt Generate a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn=200005; int n,L,R,a[maxn],b[maxn]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=1;i<=n;i++) scanf("%d",&b[i]); reverse(b+1,b+1+n); for (int i=1;i<=n;i++) if (a[i]==b[i]) { if (!L) L=i; R=i; } if (L) { int head=1,x=a[L]; bool sol=true; for (int i=L;i<=R;i++) { while (head<=n && (a[head]==x || b[head]==x)) head++; if (head>n) { sol=false; break; } swap(b[head++],b[i]); } if (!sol) { puts("No"); return 0; } } puts("Yes"); for (int i=1;i<n;i++) printf("%d ",b[i]); printf("%d\n",b[n]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } reverse(b.begin(), b.end()); int start = -1, fin = -1; for (int i = 0; i < n; ++i) { if (b[i] == a[i]) { if (start == -1) start = i; fin = i; } } bool bad = 0; if (start != -1) { for (int i = 0, j = start; j <= fin; ++i) { if (i == start) i = fin + 1; if (i == n) { break; } if (a[i] != b[j] && b[i] != b[j]) { swap(b[i], b[j]); j++; } } for (int i = 0; i < n; ++i) bad |= (a[i] == b[i]); } if (bad) { cout << "No" << endl; } else { cout << "Yes" << endl; for (int i = 0; i < n; ++i) { if (i) cout << " "; cout << b[i]; } cout << endl; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } reverse(b.begin(), b.end()); int start = -1, fin = -1; for (int i = 0; i < n; ++i) { if (b[i] == a[i]) { if (start == -1) start = i; fin = i; } } bool bad = 0; if (start != -1) { for (int i = 0, j = start; j <= fin; ++i) { if (i == start) i = fin + 1; if (i == n) { break; } if (a[i] != b[j] && b[i] != b[j]) { swap(b[i], b[j]); j++; } } for (int i = 0; i < n; ++i) bad |= (a[i] == b[i]); } if (bad) { cout << "No" << endl; } else { cout << "Yes" << endl; for (int i = 0; i < n; ++i) { if (i) cout << " "; cout << b[i]; } cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,a,b) for (ll i = (a); i < (b); i++) #define REP(i,n) rep(i,0,n) #define mod (1000000007) void solve() { int n;cin>>n; int a[n],b[n],c[n+1],d[n+1]; REP(i,n)cin>>a[i];REP(i,n)cin>>b[i]; REP(i,n+1)c[i]=d[i]=0; REP(i,n)c[a[i]]++,d[b[i]]++; REP(i,n+1)if(c[i]+d[i]>n){cout<<"No"<<endl;return;} cout<<"Yes"<<endl; rep(i,1,n+1)c[i]+=c[i-1],d[i]+=d[i-1]; int x=0;rep(i,1,n+1)x=max(x,c[i]-d[i-1]); REP(i,n)cout<<(i?" ":"")<<b[(i+n-x)%n];cout<<endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
### Prompt Please create a solution in cpp to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,a,b) for (ll i = (a); i < (b); i++) #define REP(i,n) rep(i,0,n) #define mod (1000000007) void solve() { int n;cin>>n; int a[n],b[n],c[n+1],d[n+1]; REP(i,n)cin>>a[i];REP(i,n)cin>>b[i]; REP(i,n+1)c[i]=d[i]=0; REP(i,n)c[a[i]]++,d[b[i]]++; REP(i,n+1)if(c[i]+d[i]>n){cout<<"No"<<endl;return;} cout<<"Yes"<<endl; rep(i,1,n+1)c[i]+=c[i-1],d[i]+=d[i-1]; int x=0;rep(i,1,n+1)x=max(x,c[i]-d[i-1]); REP(i,n)cout<<(i?" ":"")<<b[(i+n-x)%n];cout<<endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; //#define int long long const int N = 2e5 + 7; int a[N], b[N]; int32_t main() { ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i <= n; i++) cin >> b[i]; reverse(b + 1, b + n + 1); vector <int> x, y; int z = -1; for(int i = 1; i <= n; i++) { if(a[i] == b[i]) { z = b[i]; x.push_back(i); } } for(int i = 1; i <= n && y.size() < x.size(); i++) if((a[i] ^ z) && (b[i] ^ z)) { y.push_back(i); } if(x.size() > y.size()) { cout << "No" << endl; return 0; } cout << "Yes" << endl; for(int i = 0; i < x.size(); i++) swap(b[x[i]], b[y[i]]); for(int i = 1; i <= n; i++) cout << b[i] << " \n"[i == n]; }
### Prompt Create a solution in CPP for the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; //#define int long long const int N = 2e5 + 7; int a[N], b[N]; int32_t main() { ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i <= n; i++) cin >> b[i]; reverse(b + 1, b + n + 1); vector <int> x, y; int z = -1; for(int i = 1; i <= n; i++) { if(a[i] == b[i]) { z = b[i]; x.push_back(i); } } for(int i = 1; i <= n && y.size() < x.size(); i++) if((a[i] ^ z) && (b[i] ^ z)) { y.push_back(i); } if(x.size() > y.size()) { cout << "No" << endl; return 0; } cout << "Yes" << endl; for(int i = 0; i < x.size(); i++) swap(b[x[i]], b[y[i]]); for(int i = 1; i <= n; i++) cout << b[i] << " \n"[i == n]; } ```
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=4e5+5; typedef long long ll; int a[maxn],b[maxn]; int cnt[maxn],n; int main(){ cin>>n; for (int i=1;i<=n;i++){ scanf("%d",&a[i]); cnt[a[i]]++; } for (int i=1;i<=n;i++){ scanf("%d",&b[i]); cnt[b[i]]++; } for (int i=1;i<=n;i++){ if (cnt[i]>n){ cout<<"No"<<endl; return 0; } } reverse(b+1,b+n+1); int l=1,r=n; for (int i=1;i<=n;i++){ if (a[i]==b[i]){ if (b[i]!=a[l]&&b[l]!=a[i]){ swap(b[i],b[l]); l++; continue; } else if (b[i]!=a[r]&&b[r]!=a[i]){ swap(b[r],b[i]); r--; continue; } cout<<"No"<<endl; return 0; } } cout<<"Yes"<<endl; for (int i=1;i<=n;i++){ printf("%d ",b[i]); } }
### Prompt Your challenge is to write a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=4e5+5; typedef long long ll; int a[maxn],b[maxn]; int cnt[maxn],n; int main(){ cin>>n; for (int i=1;i<=n;i++){ scanf("%d",&a[i]); cnt[a[i]]++; } for (int i=1;i<=n;i++){ scanf("%d",&b[i]); cnt[b[i]]++; } for (int i=1;i<=n;i++){ if (cnt[i]>n){ cout<<"No"<<endl; return 0; } } reverse(b+1,b+n+1); int l=1,r=n; for (int i=1;i<=n;i++){ if (a[i]==b[i]){ if (b[i]!=a[l]&&b[l]!=a[i]){ swap(b[i],b[l]); l++; continue; } else if (b[i]!=a[r]&&b[r]!=a[i]){ swap(b[r],b[i]); r--; continue; } cout<<"No"<<endl; return 0; } } cout<<"Yes"<<endl; for (int i=1;i<=n;i++){ printf("%d ",b[i]); } } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int a[maxn], b[maxn]; int main(){ int n; cin >> n; for(int i = 1; i <= n; i ++) cin >> a[i]; for(int i = 1; i <= n; i ++) cin >> b[n-i+1]; int ok = 0; for(int i = 1; i <= n; i ++){ if(a[i] != b[i]) continue; int f = 0; for(int j = 1; j <= n; j ++){ if(a[i] != b[j] && a[j] != b[i]){ f = 1; swap(b[i], b[j]); break; } } if(!f){ ok=1; break; } } if(ok==1){ cout << "No" << endl; }else{ cout << "Yes" << endl; for(int i = 1; i <= n; i ++){ if(i != 1)cout << " "; cout << b[i]; } cout << endl; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int a[maxn], b[maxn]; int main(){ int n; cin >> n; for(int i = 1; i <= n; i ++) cin >> a[i]; for(int i = 1; i <= n; i ++) cin >> b[n-i+1]; int ok = 0; for(int i = 1; i <= n; i ++){ if(a[i] != b[i]) continue; int f = 0; for(int j = 1; j <= n; j ++){ if(a[i] != b[j] && a[j] != b[i]){ f = 1; swap(b[i], b[j]); break; } } if(!f){ ok=1; break; } } if(ok==1){ cout << "No" << endl; }else{ cout << "Yes" << endl; for(int i = 1; i <= n; i ++){ if(i != 1)cout << " "; cout << b[i]; } cout << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using P = pair<int, int>; #define INF 1001001001 #define MAX 200005 int main() { int n; cin >> n; vector<int> a(n), b(n), c(n+1), d(n+1); rep(i,n) cin >> a[i], c[a[i]]++; rep(i,n) cin >> b[i], d[b[i]]++; rep(i,n+1) if (c[i]+d[i]>n) { cout << "No" << endl; return 0; } rep(i,n) c[i+1]+=c[i],d[i+1]+=d[i]; int x = 0; rep(i,n) x = max(x,c[i+1]-d[i]); cout << "Yes" << endl; rep(i,n) { cout << b[(i+n-x)%n]; if (i==n-1) cout << endl; else cout << " "; } return 0; }
### Prompt Develop a solution in CPP to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using P = pair<int, int>; #define INF 1001001001 #define MAX 200005 int main() { int n; cin >> n; vector<int> a(n), b(n), c(n+1), d(n+1); rep(i,n) cin >> a[i], c[a[i]]++; rep(i,n) cin >> b[i], d[b[i]]++; rep(i,n+1) if (c[i]+d[i]>n) { cout << "No" << endl; return 0; } rep(i,n) c[i+1]+=c[i],d[i+1]+=d[i]; int x = 0; rep(i,n) x = max(x,c[i+1]-d[i]); cout << "Yes" << endl; rep(i,n) { cout << b[(i+n-x)%n]; if (i==n-1) cout << endl; else cout << " "; } return 0; } ```
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; int a[N],b[N]; int main(){ int n,l=N,r=0,w=0;scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",a+i); for(int i=n;i;i--){ scanf("%d",b+i); if(b[i]==a[i]){l=i;if(!r)r=i;} } for(int i=1;i<=n;i++)if(b[i]!=a[r]&&a[i]!=a[r])w++; if(w<r-l+1)return puts("No"),0; puts("Yes"); for(int i=1;i<=n;i++){ if(l>r)break; if(b[i]!=a[r]&&a[i]!=a[r])swap(b[i],b[l++]); } for(int i=1;i<=n;i++)printf("%d ",b[i]); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int N=2e5+5; int a[N],b[N]; int main(){ int n,l=N,r=0,w=0;scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",a+i); for(int i=n;i;i--){ scanf("%d",b+i); if(b[i]==a[i]){l=i;if(!r)r=i;} } for(int i=1;i<=n;i++)if(b[i]!=a[r]&&a[i]!=a[r])w++; if(w<r-l+1)return puts("No"),0; puts("Yes"); for(int i=1;i<=n;i++){ if(l>r)break; if(b[i]!=a[r]&&a[i]!=a[r])swap(b[i],b[l++]); } for(int i=1;i<=n;i++)printf("%d ",b[i]); return 0; } ```
#include<bits/stdc++.h> #define rep(i,x,y) for(int i=x,i##end=y;i<=i##end;++i) #define _rep(i,x,y) for(int i=x,i##end=y;i>=i##end;--i) #define ll long long const int mod=1e9+7; const int inf=1e9; #define N 200005 int n,a[N],b[N],c[N]; bool vis[N]; int fyy[N],val=0; std::vector<int> same,diff; int main(){ std::ios::sync_with_stdio(0); std::cin>>n; rep(i,1,n) std::cin>>a[i],++fyy[a[i]]; rep(i,1,n) std::cin>>b[i],++fyy[b[i]]; std::sort(b+1,b+n+1,std::greater<int>()); rep(i,1,n) if(fyy[i]>n) return puts("No"),0; rep(i,1,n){ if(a[i]==b[i]) same.push_back(i),val=a[i]; } rep(i,1,n) if(a[i]!=val&&b[i]!=val) diff.push_back(i); rep(i,0,same.size()-1) std::swap(b[same[i]],b[diff[i]]); std::cout<<"Yes\n"; rep(i,1,n) std::cout<<b[i]<<' '; std::cout<<'\n'; return 0; }
### Prompt Create a solution in CPP for the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> #define rep(i,x,y) for(int i=x,i##end=y;i<=i##end;++i) #define _rep(i,x,y) for(int i=x,i##end=y;i>=i##end;--i) #define ll long long const int mod=1e9+7; const int inf=1e9; #define N 200005 int n,a[N],b[N],c[N]; bool vis[N]; int fyy[N],val=0; std::vector<int> same,diff; int main(){ std::ios::sync_with_stdio(0); std::cin>>n; rep(i,1,n) std::cin>>a[i],++fyy[a[i]]; rep(i,1,n) std::cin>>b[i],++fyy[b[i]]; std::sort(b+1,b+n+1,std::greater<int>()); rep(i,1,n) if(fyy[i]>n) return puts("No"),0; rep(i,1,n){ if(a[i]==b[i]) same.push_back(i),val=a[i]; } rep(i,1,n) if(a[i]!=val&&b[i]!=val) diff.push_back(i); rep(i,0,same.size()-1) std::swap(b[same[i]],b[diff[i]]); std::cout<<"Yes\n"; rep(i,1,n) std::cout<<b[i]<<' '; std::cout<<'\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; int acnt[200010]; int bcnt[200010]; int main() { int n; cin >> n; int a[n]; for(int i = 0; i < n; ++i) cin >> a[i]; int b[n]; for(int i = 0; i < n; ++i) cin >> b[i]; for(int i = 0; i < n; ++i) { ++acnt[a[i]]; ++bcnt[b[i]]; } for(int i = 1; i <= n; ++i) { if(acnt[i] + bcnt[i] > n) { cout << "No" << '\n'; return 0; } acnt[i] += acnt[i - 1]; bcnt[i] += bcnt[i - 1]; } int maxi = 0; for(int i = 1; i <= n; ++i) { maxi = max(maxi, acnt[i] - bcnt[i - 1]); } int ans[n]; for(int i = 0; i < n; ++i) { ans[(i + maxi) % n] = b[i]; } cout << "Yes" << '\n'; for(int i = 0; i < n; ++i) { cout << ans[i] << (i == n - 1 ? "\n" : " "); } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int acnt[200010]; int bcnt[200010]; int main() { int n; cin >> n; int a[n]; for(int i = 0; i < n; ++i) cin >> a[i]; int b[n]; for(int i = 0; i < n; ++i) cin >> b[i]; for(int i = 0; i < n; ++i) { ++acnt[a[i]]; ++bcnt[b[i]]; } for(int i = 1; i <= n; ++i) { if(acnt[i] + bcnt[i] > n) { cout << "No" << '\n'; return 0; } acnt[i] += acnt[i - 1]; bcnt[i] += bcnt[i - 1]; } int maxi = 0; for(int i = 1; i <= n; ++i) { maxi = max(maxi, acnt[i] - bcnt[i - 1]); } int ans[n]; for(int i = 0; i < n; ++i) { ans[(i + maxi) % n] = b[i]; } cout << "Yes" << '\n'; for(int i = 0; i < n; ++i) { cout << ans[i] << (i == n - 1 ? "\n" : " "); } return 0; } ```
#include <bits/stdc++.h> #define debug(...) fprintf(stderr, __VA_ARGS__) #ifndef AT_HOME #define getchar() IO::myGetchar() #define putchar(x) IO::myPutchar(x) #endif using namespace std; int n; int a[200005], b[200005]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { cin >> b[i]; } int d = 0; int pa = 1, pb = 1; for (int i = 1; i <= n; ++i) { while (pa <= n && a[pa] == i) { ++pa; } d = std::max(d, pa - pb); while (pb <= n && b[pb] == i) { ++pb; } } std::rotate(b + 1, b + n - d + 1, b + 1 + n); for (int i = 1; i <= n; ++i) { if (a[i] == b[i]) { cout << "No\n"; return 0; } } cout << "Yes\n"; for (int i = 1; i <= n; ++i) { cout << b[i] << " "; } cout << "\n"; }
### Prompt Please create a solution in CPP to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> #define debug(...) fprintf(stderr, __VA_ARGS__) #ifndef AT_HOME #define getchar() IO::myGetchar() #define putchar(x) IO::myPutchar(x) #endif using namespace std; int n; int a[200005], b[200005]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { cin >> b[i]; } int d = 0; int pa = 1, pb = 1; for (int i = 1; i <= n; ++i) { while (pa <= n && a[pa] == i) { ++pa; } d = std::max(d, pa - pb); while (pb <= n && b[pb] == i) { ++pb; } } std::rotate(b + 1, b + n - d + 1, b + 1 + n); for (int i = 1; i <= n; ++i) { if (a[i] == b[i]) { cout << "No\n"; return 0; } } cout << "Yes\n"; for (int i = 1; i <= n; ++i) { cout << b[i] << " "; } cout << "\n"; } ```
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; int main() { int N; cin >> N; vector<int> A(N), B(N), C(N + 1, 0), D(N + 1, 0), E(N + 1, 0), F(N + 1, 0); rep(i, N) { cin >> A[i]; E[A[i]]++; } rep(i, N) { cin >> B[i]; F[B[i]]++; } rep(i, N) { if (E[i + 1] + F[i + 1] > N) { cout << "No\n"; return 0; } } cout << "Yes\n"; rep(i, N) { C[i + 1] = C[i] + E[i + 1]; D[i + 1] = D[i] + F[i + 1]; } int x = -N; rep(i, N) { x = max(x, C[i + 1] - D[i]); } rep(i, N) { cout << B[(i - x + N) % N]; if (i == N - 1) cout << "\n"; else cout << " "; } }
### Prompt Please create a solution in CPP to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; int main() { int N; cin >> N; vector<int> A(N), B(N), C(N + 1, 0), D(N + 1, 0), E(N + 1, 0), F(N + 1, 0); rep(i, N) { cin >> A[i]; E[A[i]]++; } rep(i, N) { cin >> B[i]; F[B[i]]++; } rep(i, N) { if (E[i + 1] + F[i + 1] > N) { cout << "No\n"; return 0; } } cout << "Yes\n"; rep(i, N) { C[i + 1] = C[i] + E[i + 1]; D[i + 1] = D[i] + F[i + 1]; } int x = -N; rep(i, N) { x = max(x, C[i + 1] - D[i]); } rep(i, N) { cout << B[(i - x + N) % N]; if (i == N - 1) cout << "\n"; else cout << " "; } } ```
#include <bits/stdc++.h> using namespace std; #define ll long long int #define maxn 200005 int main(){ int n; cin>>n; vector<int> a(n),b(n); for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++) cin>>b[i]; reverse(b.begin(),b.end()); int el=-1; for(int i=0;i<n;i++){ if(a[i]==b[i]){ el=a[i]; } } if(el==-1){ cout<<"Yes"<<endl; for(auto x: b) cout<<x<<" "; cout<<endl; } else{ queue<int> q1; queue<int> q2; for(int i=0;i<n;i++){ if(a[i]==el && b[i]==el) q2.push(i); if(a[i]!=el && b[i]!=el){ q1.push(i); } } if(q2.size()>q1.size()){ cout<<"No"<<endl; } else{ cout<<"Yes"<<endl; while(!q2.empty()){ int i1=q2.front(); q2.pop(); int i2=q1.front(); q1.pop(); swap(b[i1],b[i2]); } for(auto x: b) cout<<x<<" "; cout<<endl; } } }
### Prompt Your challenge is to write a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define ll long long int #define maxn 200005 int main(){ int n; cin>>n; vector<int> a(n),b(n); for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<n;i++) cin>>b[i]; reverse(b.begin(),b.end()); int el=-1; for(int i=0;i<n;i++){ if(a[i]==b[i]){ el=a[i]; } } if(el==-1){ cout<<"Yes"<<endl; for(auto x: b) cout<<x<<" "; cout<<endl; } else{ queue<int> q1; queue<int> q2; for(int i=0;i<n;i++){ if(a[i]==el && b[i]==el) q2.push(i); if(a[i]!=el && b[i]!=el){ q1.push(i); } } if(q2.size()>q1.size()){ cout<<"No"<<endl; } else{ cout<<"Yes"<<endl; while(!q2.empty()){ int i1=q2.front(); q2.pop(); int i2=q1.front(); q1.pop(); swap(b[i1],b[i2]); } for(auto x: b) cout<<x<<" "; cout<<endl; } } } ```
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; int main() { int n; cin >> n; vi a(n), b(n), ind_a(n + 1, -1), sum(n + 1); int k = 0; for(int i = 0; i < n; ++i){ cin >> a[i]; ind_a[a[i]] = i; sum[a[i]]++; } for(int i = 0; i < n; ++i){ cin >> b[i]; if((!i || b[i] != b[i - 1]) && ind_a[b[i]] != -1){ k = max(k, ind_a[b[i]] - i + 1); } sum[b[i]]++; } for(int i = 0; i <= n; ++i){ if(sum[i] > n){ cout << "No"; return 0; } } cout << "Yes\n"; for(int i = n - k; i < n; ++i){ cout << b[i] << " "; } for(int i = 0; i < n - k; ++i) cout << b[i] << " "; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using vi = vector<int>; int main() { int n; cin >> n; vi a(n), b(n), ind_a(n + 1, -1), sum(n + 1); int k = 0; for(int i = 0; i < n; ++i){ cin >> a[i]; ind_a[a[i]] = i; sum[a[i]]++; } for(int i = 0; i < n; ++i){ cin >> b[i]; if((!i || b[i] != b[i - 1]) && ind_a[b[i]] != -1){ k = max(k, ind_a[b[i]] - i + 1); } sum[b[i]]++; } for(int i = 0; i <= n; ++i){ if(sum[i] > n){ cout << "No"; return 0; } } cout << "Yes\n"; for(int i = n - k; i < n; ++i){ cout << b[i] << " "; } for(int i = 0; i < n - k; ++i) cout << b[i] << " "; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int a[MAXN], b[MAXN]; int main() { int n; cin >> n; map<int, int> m; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; } for (int i = 0; i < n; i++) { cin >> b[i]; m[b[i]]++; } for (auto it : m) { if (it.second > n) { cout << "No\n"; return 0; } } reverse(b, b + n); int l = 0, r = n - 1; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { if(a[i] != b[l] and a[l] != b[i]) { swap(b[l], b[i]); l ++; } else { swap(b[r], b[i]); r --; } } } cout << "Yes\n"; for (int i = 0; i < n; i++) cout << b[i] << " "; cout << endl; }
### Prompt Please create a solution in Cpp to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int a[MAXN], b[MAXN]; int main() { int n; cin >> n; map<int, int> m; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; } for (int i = 0; i < n; i++) { cin >> b[i]; m[b[i]]++; } for (auto it : m) { if (it.second > n) { cout << "No\n"; return 0; } } reverse(b, b + n); int l = 0, r = n - 1; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { if(a[i] != b[l] and a[l] != b[i]) { swap(b[l], b[i]); l ++; } else { swap(b[r], b[i]); r --; } } } cout << "Yes\n"; for (int i = 0; i < n; i++) cout << b[i] << " "; cout << endl; } ```
#include <iostream> #include <vector> #include <algorithm> int main() { int n; std::cin >> n; std::vector<int> a(n), b(n); for (int &e : a) std::cin >> e; for (int &e : b) std::cin >> e; auto check = [&] () { bool ret = true; for (int i = 0; i < n; i++) ret &= (a[i] != b[i]); return ret; }; std::reverse(b.begin(), b.end()); if (check()) { std::cout << "Yes" << '\n'; for (int e : b) std::cout << e << '\n'; } else { std::reverse(b.begin(), b.end()); std::rotate(b.begin(), b.begin() + (n / 2), b.end()); if (check()) { std::cout << "Yes" << '\n'; for (int e : b) std::cout << e << '\n'; } else { std::cout << "No" << '\n'; } } return 0; }
### Prompt Please formulate a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> int main() { int n; std::cin >> n; std::vector<int> a(n), b(n); for (int &e : a) std::cin >> e; for (int &e : b) std::cin >> e; auto check = [&] () { bool ret = true; for (int i = 0; i < n; i++) ret &= (a[i] != b[i]); return ret; }; std::reverse(b.begin(), b.end()); if (check()) { std::cout << "Yes" << '\n'; for (int e : b) std::cout << e << '\n'; } else { std::reverse(b.begin(), b.end()); std::rotate(b.begin(), b.begin() + (n / 2), b.end()); if (check()) { std::cout << "Yes" << '\n'; for (int e : b) std::cout << e << '\n'; } else { std::cout << "No" << '\n'; } } return 0; } ```
#include<iostream> using namespace std; const int N=1002020; int n,a[N],b[N]; void print(){ puts("Yes"); for(int i=1;i<=n;i++) cout<<b[i]<<" ";cout<<endl; } int main(){ cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=n;i>=1;i--)cin>>b[i]; int l=n,r=0; for(int i=1;i<=n;i++) if(a[i]==b[i])l=min(l,i),r=max(r,i); if(!r){print();return 0;} int nw=l; for(int i=1;i<l && nw<=r;i++) if(b[nw]!=a[i] && b[nw]!=b[i]) swap(b[nw],b[i]),++nw; for(int i=r+1;i<=n && nw<=r;i++) if(b[nw]!=a[i] && b[nw]!=b[i]) swap(b[nw],b[i]),++nw; int fl=0; for(int i=1;i<=n;i++) if(b[i]==a[i]){fl=1;break;} if(fl)puts("No"); else print(); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<iostream> using namespace std; const int N=1002020; int n,a[N],b[N]; void print(){ puts("Yes"); for(int i=1;i<=n;i++) cout<<b[i]<<" ";cout<<endl; } int main(){ cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=n;i>=1;i--)cin>>b[i]; int l=n,r=0; for(int i=1;i<=n;i++) if(a[i]==b[i])l=min(l,i),r=max(r,i); if(!r){print();return 0;} int nw=l; for(int i=1;i<l && nw<=r;i++) if(b[nw]!=a[i] && b[nw]!=b[i]) swap(b[nw],b[i]),++nw; for(int i=r+1;i<=n && nw<=r;i++) if(b[nw]!=a[i] && b[nw]!=b[i]) swap(b[nw],b[i]),++nw; int fl=0; for(int i=1;i<=n;i++) if(b[i]==a[i]){fl=1;break;} if(fl)puts("No"); else print(); return 0; } ```
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 200020 inline int read(){ int 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 n,a[N],b[N],d,cnt[N]; int main(){ n=read(); for(int i=1;i<=n;++i){ a[i]=read(); ++cnt[a[i]]; } for(int i=1;i<=n;++i){ b[i]=read(); ++cnt[b[i]]; } int pa=1,pb=1; for(int i=1;i<=n;++i){ while(a[pa]==i)++pa; d=max(d,pa-pb); while(b[pb]==i)++pb; } rotate(b+1,b+n+1-d,b+n+1); for(int i=1;i<=n;++i){ if(a[i]==b[i])return !printf("No\n"); } printf("Yes\n"); for(int i=1;i<=n;++i){ printf("%d ",b[i]); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define N 200020 inline int read(){ int 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 n,a[N],b[N],d,cnt[N]; int main(){ n=read(); for(int i=1;i<=n;++i){ a[i]=read(); ++cnt[a[i]]; } for(int i=1;i<=n;++i){ b[i]=read(); ++cnt[b[i]]; } int pa=1,pb=1; for(int i=1;i<=n;++i){ while(a[pa]==i)++pa; d=max(d,pa-pb); while(b[pb]==i)++pb; } rotate(b+1,b+n+1-d,b+n+1); for(int i=1;i<=n;++i){ if(a[i]==b[i])return !printf("No\n"); } printf("Yes\n"); for(int i=1;i<=n;++i){ printf("%d ",b[i]); } return 0; } ```
#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; //using namespace atcoder; int main() { int N; cin >> N; vector<int> A(N), B(N); map<int, int> ANum, BNum; for (int i = 0; i < N; i++) { cin >> A[i]; ANum[A[i]]++; } for (int i = 0; i < N; i++) { cin >> B[i]; BNum[B[i]]++; } for (int i = 0; i <= N; i++) { if (ANum.find(i) == ANum.end() || BNum.find(i) == BNum.end()) continue; if (ANum[i] + BNum[i] > N) { cout << "No" << endl; return 0; } } reverse(B.begin(), B.end()); int j = N - 1; for (int i = 0; i < N; i++) { if (A[i] == B[i]) { while (A[i] == B[j] || B[i] == A[j]) { j--; if (j < 0) j = N - 1; } swap(B[i], B[j]); } } cout << "Yes" << endl; for (int b : B) cout << b << " "; cout << endl; }
### Prompt Please provide a Cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; //using namespace atcoder; int main() { int N; cin >> N; vector<int> A(N), B(N); map<int, int> ANum, BNum; for (int i = 0; i < N; i++) { cin >> A[i]; ANum[A[i]]++; } for (int i = 0; i < N; i++) { cin >> B[i]; BNum[B[i]]++; } for (int i = 0; i <= N; i++) { if (ANum.find(i) == ANum.end() || BNum.find(i) == BNum.end()) continue; if (ANum[i] + BNum[i] > N) { cout << "No" << endl; return 0; } } reverse(B.begin(), B.end()); int j = N - 1; for (int i = 0; i < N; i++) { if (A[i] == B[i]) { while (A[i] == B[j] || B[i] == A[j]) { j--; if (j < 0) j = N - 1; } swap(B[i], B[j]); } } cout << "Yes" << endl; for (int b : B) cout << b << " "; cout << endl; } ```
#include<iostream> #include<algorithm> using namespace std; int N_MAX = 200001; int main() { int n,i,j,c=0; int a[N_MAX],b[N_MAX],ca[N_MAX],cb[N_MAX]; cin >> n; fill(ca+1,ca+n+1,0); fill(cb+1,cb+n+1,0); for(i=0;i<n;i++){ cin >> a[i]; ca[a[i]]++; } for(i=0;i<n;i++){ cin >> b[i]; cb[b[i]]++; } for(i=1;i<=n;i++) if(ca[i]+cb[i]>n){ cout << "No" << endl; return 0; } cout << "Yes" << endl; for(i=0;i<n;i++){ if(a[i]!=b[i]) continue; if(b[i]!=c){ j=0; c=b[i]; } for(;j<n;j++) if(a[j]!=c&&b[j]!=c){ b[i]=b[j]; b[j]=c; j++; break; } } for(i=0;i<n;i++) cout << b[i] << " "; cout << endl; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<iostream> #include<algorithm> using namespace std; int N_MAX = 200001; int main() { int n,i,j,c=0; int a[N_MAX],b[N_MAX],ca[N_MAX],cb[N_MAX]; cin >> n; fill(ca+1,ca+n+1,0); fill(cb+1,cb+n+1,0); for(i=0;i<n;i++){ cin >> a[i]; ca[a[i]]++; } for(i=0;i<n;i++){ cin >> b[i]; cb[b[i]]++; } for(i=1;i<=n;i++) if(ca[i]+cb[i]>n){ cout << "No" << endl; return 0; } cout << "Yes" << endl; for(i=0;i<n;i++){ if(a[i]!=b[i]) continue; if(b[i]!=c){ j=0; c=b[i]; } for(;j<n;j++) if(a[j]!=c&&b[j]!=c){ b[i]=b[j]; b[j]=c; j++; break; } } for(i=0;i<n;i++) cout << b[i] << " "; cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; #define ll long long int #define rep(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = a; i < (b); ++i) #define vl vector<ll> int main() { ll N; cin >> N; vl A(N); vl B(N); vl a(N+1, 0); vl b(N+1, 0); rep(i, N) { cin >> A[i]; a[A[i]]++; } rep(i, N) { cin >> B[i]; b[B[i]]++; } FOR(i, 1, N+1) { if (a[i] + b[i] > N) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; FOR(i, 1, N+1) { a[i] += a[i-1]; b[i] += b[i-1]; } ll k = 0; FOR(i, 1, N+1) { k = max(k, a[i]-b[i-1]); } rep(i, N) { cout << B[(i+N-k)%N] << " "; } cout << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define ll long long int #define rep(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = a; i < (b); ++i) #define vl vector<ll> int main() { ll N; cin >> N; vl A(N); vl B(N); vl a(N+1, 0); vl b(N+1, 0); rep(i, N) { cin >> A[i]; a[A[i]]++; } rep(i, N) { cin >> B[i]; b[B[i]]++; } FOR(i, 1, N+1) { if (a[i] + b[i] > N) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; FOR(i, 1, N+1) { a[i] += a[i-1]; b[i] += b[i-1]; } ll k = 0; FOR(i, 1, N+1) { k = max(k, a[i]-b[i-1]); } rep(i, N) { cout << B[(i+N-k)%N] << " "; } cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } reverse(b.begin(), b.end()); int l = 0, r = n - 1; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { if (b[i] != a[l] && b[i] != b[l]) { swap(b[i], b[l++]); } else if (b[i] != a[r] && b[i] != b[r]) { swap(b[i], b[r--]); } else { cout << "No" << endl; return 0; } } } cout << "Yes" << endl; for (int i : b) { cout << i << " "; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } reverse(b.begin(), b.end()); int l = 0, r = n - 1; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { if (b[i] != a[l] && b[i] != b[l]) { swap(b[i], b[l++]); } else if (b[i] != a[r] && b[i] != b[r]) { swap(b[i], b[r--]); } else { cout << "No" << endl; return 0; } } } cout << "Yes" << endl; for (int i : b) { cout << i << " "; } return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; int numa[1000000], numb[1000000]; int main(){ int n; cin >> n; vector<int> a, b; int x, y; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); numa[x]++; } for (int i = 0; i < n; i++) { cin >> y; b.push_back(y); numb[y]++; } for (int i = 1; i <= n; i++) { if(numa[i]+numb[i] > n) { puts("No"); return 0; } } puts("Yes"); for (int i = 1; i <= n; i++) { numa[i] += numa[i-1]; numb[i] += numb[i-1]; } int shift=0; for (int i = 1; i <= n; i++) { shift = max(shift, numa[i] - numb[i-1]); } for(int i = 0; i < n; i++){ cout << b.at((i+n-shift)%n) << " "; } puts(""); }
### Prompt Your task is to create a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; int numa[1000000], numb[1000000]; int main(){ int n; cin >> n; vector<int> a, b; int x, y; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); numa[x]++; } for (int i = 0; i < n; i++) { cin >> y; b.push_back(y); numb[y]++; } for (int i = 1; i <= n; i++) { if(numa[i]+numb[i] > n) { puts("No"); return 0; } } puts("Yes"); for (int i = 1; i <= n; i++) { numa[i] += numa[i-1]; numb[i] += numb[i-1]; } int shift=0; for (int i = 1; i <= n; i++) { shift = max(shift, numa[i] - numb[i-1]); } for(int i = 0; i < n; i++){ cout << b.at((i+n-shift)%n) << " "; } puts(""); } ```
#include <bits/stdc++.h> using namespace std; int main(){ int N;cin>>N; vector<int>A(N+1); vector<int>B(N+1); vector<int>C(N); vector<int>D(N); for(int i=0;i<N;i++){ cin>>C[i]; A[C[i]]++; } for(int i=0;i<N;i++){ cin>>D[i]; B[D[i]]++; } for(int i=1;i<N+1;i++){ if(A[i]+B[i]>N){ puts("No");return 0; } } puts("Yes"); for(int i=0;i<N;i++){ A[i+1]+=A[i]; B[i+1]+=B[i]; } int count=0; for(int i=0;i<N;i++)count=max(count,A[i+1]-B[i]); for(int i=0;i<N;i++)cout<<D[(i+N-count)%N]<<(i!=N? " ":""); cout<<"\n"; }
### Prompt Please provide a cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int N;cin>>N; vector<int>A(N+1); vector<int>B(N+1); vector<int>C(N); vector<int>D(N); for(int i=0;i<N;i++){ cin>>C[i]; A[C[i]]++; } for(int i=0;i<N;i++){ cin>>D[i]; B[D[i]]++; } for(int i=1;i<N+1;i++){ if(A[i]+B[i]>N){ puts("No");return 0; } } puts("Yes"); for(int i=0;i<N;i++){ A[i+1]+=A[i]; B[i+1]+=B[i]; } int count=0; for(int i=0;i<N;i++)count=max(count,A[i+1]-B[i]); for(int i=0;i<N;i++)cout<<D[(i+N-count)%N]<<(i!=N? " ":""); cout<<"\n"; } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; const double eps=1e-6,pi=acos(-1); const int N=2e5+5,M=1e9+7,SEGM=4*N,OO=0x3f3f3f3f; int t,n,m,k; int a[N]; int b[N]; int main(){ //freopen("myfile.txt","w",stdout); scanf("%d",&n); for(int i=0;i<n;++i) scanf("%d",a+i); for(int i=n-1;i>-1;--i) scanf("%d",b+i); int l=n,r=-1; for(int i=0;i<n;++i){ if(a[i]==b[i]){ l=i; break; } } for(int i=n-1;i>-1;--i){ if(a[i]==b[i]){ r=i; break; } } for(int i=0;i<n;++i) if(l<=r&&b[l]!=a[i]&&b[l]!=b[i]) swap(b[i],b[l++]); if(l<=r){ printf("No\n"); }else{ printf("Yes\n"); for(int i=0;i<n;++i) printf("%d%c",b[i]," \n"[i+1==n]); } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; const double eps=1e-6,pi=acos(-1); const int N=2e5+5,M=1e9+7,SEGM=4*N,OO=0x3f3f3f3f; int t,n,m,k; int a[N]; int b[N]; int main(){ //freopen("myfile.txt","w",stdout); scanf("%d",&n); for(int i=0;i<n;++i) scanf("%d",a+i); for(int i=n-1;i>-1;--i) scanf("%d",b+i); int l=n,r=-1; for(int i=0;i<n;++i){ if(a[i]==b[i]){ l=i; break; } } for(int i=n-1;i>-1;--i){ if(a[i]==b[i]){ r=i; break; } } for(int i=0;i<n;++i) if(l<=r&&b[l]!=a[i]&&b[l]!=b[i]) swap(b[i],b[l++]); if(l<=r){ printf("No\n"); }else{ printf("Yes\n"); for(int i=0;i<n;++i) printf("%d%c",b[i]," \n"[i+1==n]); } return 0; } ```
/*input 6 1 1 1 2 2 3 1 1 1 2 2 3 */ #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> a(n), b(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[n-1-i]; vector<int> cnt(n+1); int mcnt = 0; rep(i, n) { mcnt = max({mcnt, ++cnt[a[i]], ++cnt[b[i]]}); } if (mcnt > n) { cout << "No" << endl; return 0; } else cout << "Yes" << endl; int l = 0, r = n-1; rep(i, n) { if (a[i] == b[i]) { if (a[i] != b[l] && a[l] != b[i]) { swap(b[i], b[l++]); } else swap(b[i], b[r--]); } } // rep(i, n) cout << a[i] << " "; // cout << endl; rep(i, n) cout << b[i] << " "; cout << endl; }
### Prompt Your challenge is to write a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp /*input 6 1 1 1 2 2 3 1 1 1 2 2 3 */ #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> a(n), b(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[n-1-i]; vector<int> cnt(n+1); int mcnt = 0; rep(i, n) { mcnt = max({mcnt, ++cnt[a[i]], ++cnt[b[i]]}); } if (mcnt > n) { cout << "No" << endl; return 0; } else cout << "Yes" << endl; int l = 0, r = n-1; rep(i, n) { if (a[i] == b[i]) { if (a[i] != b[l] && a[l] != b[i]) { swap(b[i], b[l++]); } else swap(b[i], b[r--]); } } // rep(i, n) cout << a[i] << " "; // cout << endl; rep(i, n) cout << b[i] << " "; cout << endl; } ```
#include<bits/stdc++.h> #define int long long #define fi first #define se second #define vll vector<int> #define pii pair<int,int> #define pb push_back #define sz(v) (int)(v).size() #define inf 1e18 #define md 1000000007 #define all(v) (v).begin(),(v).end() #define rep(i,a,b) for(int i=a;i<b;++i) using namespace std; #define M 100010 int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n;cin>>n; int a[n],b[n]; rep(i,0,n)cin>>a[i]; rep(i,0,n)cin>>b[i]; int i=0,j=0; while(i<n){ if(a[i]==b[i]){ int k=n; while((a[i]==b[j] or a[j]==b[i]) and k--)j=(j+1)%n; swap(b[i],b[j]); if(a[i]==b[i]){ cout<<"No"; return 0; } }++i; } cout<<"Yes\n"; rep(i,0,n)cout<<b[i]<<" "; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> #define int long long #define fi first #define se second #define vll vector<int> #define pii pair<int,int> #define pb push_back #define sz(v) (int)(v).size() #define inf 1e18 #define md 1000000007 #define all(v) (v).begin(),(v).end() #define rep(i,a,b) for(int i=a;i<b;++i) using namespace std; #define M 100010 int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n;cin>>n; int a[n],b[n]; rep(i,0,n)cin>>a[i]; rep(i,0,n)cin>>b[i]; int i=0,j=0; while(i<n){ if(a[i]==b[i]){ int k=n; while((a[i]==b[j] or a[j]==b[i]) and k--)j=(j+1)%n; swap(b[i],b[j]); if(a[i]==b[i]){ cout<<"No"; return 0; } }++i; } cout<<"Yes\n"; rep(i,0,n)cout<<b[i]<<" "; return 0; } ```
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int a[n], b[n]; for(auto &it : a) cin>>it; for(auto &it : b) cin>>it; reverse(b, b+n); int c, l=0, r=-1; for(int i=0; i<n; i++){ if(a[i]==b[i]){ c = a[i]; l = i; break; } } for(int i=n-1; i>=0; i--){ if(a[i]==c && b[i]==c){ r = i; break; } } for(int i=0; i<n; i++){ if(a[i]!=c && b[i]!=c && l<=r){ swap(b[i],b[l]); l++; } } if(l<=r) cout<<"No"; else{ cout<<"Yes"<<endl; for(int x : b) cout<<x<<" "; } }
### Prompt Your task is to create a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int a[n], b[n]; for(auto &it : a) cin>>it; for(auto &it : b) cin>>it; reverse(b, b+n); int c, l=0, r=-1; for(int i=0; i<n; i++){ if(a[i]==b[i]){ c = a[i]; l = i; break; } } for(int i=n-1; i>=0; i--){ if(a[i]==c && b[i]==c){ r = i; break; } } for(int i=0; i<n; i++){ if(a[i]!=c && b[i]!=c && l<=r){ swap(b[i],b[l]); l++; } } if(l<=r) cout<<"No"; else{ cout<<"Yes"<<endl; for(int x : b) cout<<x<<" "; } } ```
#include<bits/stdc++.h> using namespace std; int n,a[202020],b[202020]; int main() { cin>>n; for(int i=0;i<n;i++) { scanf("%d",&a[i]); } for(int i=0;i<n;i++) { scanf("%d",&b[i]); } reverse(b,b+n); for(int i=0;i<n;i++) { if(a[i]!=b[i]) { continue; } bool ok=false; for(int j=0;j<n;j++) { if(i==j) { continue; } if(a[i]!=b[j] && a[j]!=b[i]) { swap(b[i],b[j]); ok=true; break; } } if(!ok) { cout<<"No"<<endl; return 0; } } cout<<"Yes"<<endl; for(int i=0;i<n;i++) { printf("%d ",b[i]); } cout<<endl; return 0; }
### Prompt Please formulate a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int n,a[202020],b[202020]; int main() { cin>>n; for(int i=0;i<n;i++) { scanf("%d",&a[i]); } for(int i=0;i<n;i++) { scanf("%d",&b[i]); } reverse(b,b+n); for(int i=0;i<n;i++) { if(a[i]!=b[i]) { continue; } bool ok=false; for(int j=0;j<n;j++) { if(i==j) { continue; } if(a[i]!=b[j] && a[j]!=b[i]) { swap(b[i],b[j]); ok=true; break; } } if(!ok) { cout<<"No"<<endl; return 0; } } cout<<"Yes"<<endl; for(int i=0;i<n;i++) { printf("%d ",b[i]); } cout<<endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int> A(N), B(N), C(N + 10, 0), D(N + 10, 0); for (auto&& e : A) { cin >> e; C[e]++; } for (auto&& e : B) { cin >> e; D[e]++; } for (int i = 0; i < N + 10; i++) { if (C[i] + D[i] > N) { cout << "No" << '\n'; return 0; } } for (int i = 0; i < N; i++) { C[i + 1] += C[i], D[i + 1] += D[i]; } int d = 0; for (int i = 0; i < N; i++) { d = max(d, D[i + 1] - C[i]); } cout << "Yes" << '\n'; for (int i = 0; i < N; i++) { cout << B[(i + d) % N] << (i == N - 1 ? '\n' : ' '); } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int> A(N), B(N), C(N + 10, 0), D(N + 10, 0); for (auto&& e : A) { cin >> e; C[e]++; } for (auto&& e : B) { cin >> e; D[e]++; } for (int i = 0; i < N + 10; i++) { if (C[i] + D[i] > N) { cout << "No" << '\n'; return 0; } } for (int i = 0; i < N; i++) { C[i + 1] += C[i], D[i + 1] += D[i]; } int d = 0; for (int i = 0; i < N; i++) { d = max(d, D[i + 1] - C[i]); } cout << "Yes" << '\n'; for (int i = 0; i < N; i++) { cout << B[(i + d) % N] << (i == N - 1 ? '\n' : ' '); } return 0; } ```
#include "bits/stdc++.h" using namespace std; int main() { int N; cin >> N; vector<int>A(N); vector<int>B(N); vector<int>C(N + 1); vector<int>D(N + 1); for (int i = 0; i < N; ++i) { cin >> A[i]; C[A[i]]++; } for (int i = 0; i < N; ++i) { cin >> B[i]; D[B[i]]++; } for (int i = 1; i <= N; ++i) { if (C[i] + D[i] > N) { cout << "No" << endl; return 0; } } for (int i = 1; i <= N; i++) { C[i] += C[i - 1]; D[i] += D[i - 1]; } int x = -1; for (int i = 1; i <= N; ++i) { x = max(x, C[i] - D[i - 1]); } cout << "Yes" << endl; for (int i = 0; i < N; ++i) { if (0 != i) { cout << " "; } cout << B[(i + N - x)%N]; } cout << endl; return 0; }
### Prompt In cpp, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include "bits/stdc++.h" using namespace std; int main() { int N; cin >> N; vector<int>A(N); vector<int>B(N); vector<int>C(N + 1); vector<int>D(N + 1); for (int i = 0; i < N; ++i) { cin >> A[i]; C[A[i]]++; } for (int i = 0; i < N; ++i) { cin >> B[i]; D[B[i]]++; } for (int i = 1; i <= N; ++i) { if (C[i] + D[i] > N) { cout << "No" << endl; return 0; } } for (int i = 1; i <= N; i++) { C[i] += C[i - 1]; D[i] += D[i - 1]; } int x = -1; for (int i = 1; i <= N; ++i) { x = max(x, C[i] - D[i - 1]); } cout << "Yes" << endl; for (int i = 0; i < N; ++i) { if (0 != i) { cout << " "; } cout << B[(i + N - x)%N]; } cout << endl; return 0; } ```
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<vector> using namespace std; const int maxn = 2e5; int n, a[maxn + 5], b[maxn + 5]; int l[maxn + 5], r[maxn + 5]; int ans[maxn + 5]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i)scanf("%d", &a[i]); for (int i = 1; i <= n; ++i)scanf("%d", &b[i]); for (int i = 1; i <= n; ++i)r[a[i]] = i; for (int i = n; i > 0; --i)l[b[i]] = i; int res = 0; for (int i = 1; i <= n; ++i)if (l[i] && r[i])res = max(res, r[i] - l[i] + 1); for (int i = 1; i <= n; ++i) { int npos = i + res; if (npos > n)npos -= n; ans[npos] = b[i]; } for (int i = 1; i <= n; ++i) { if (a[i] == ans[i]) { printf("No\n"); return 0; } } printf("Yes\n"); for (int i = 1; i <= n; ++i)printf("%d%c", ans[i], " \n"[i == n]); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> #include<vector> using namespace std; const int maxn = 2e5; int n, a[maxn + 5], b[maxn + 5]; int l[maxn + 5], r[maxn + 5]; int ans[maxn + 5]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i)scanf("%d", &a[i]); for (int i = 1; i <= n; ++i)scanf("%d", &b[i]); for (int i = 1; i <= n; ++i)r[a[i]] = i; for (int i = n; i > 0; --i)l[b[i]] = i; int res = 0; for (int i = 1; i <= n; ++i)if (l[i] && r[i])res = max(res, r[i] - l[i] + 1); for (int i = 1; i <= n; ++i) { int npos = i + res; if (npos > n)npos -= n; ans[npos] = b[i]; } for (int i = 1; i <= n; ++i) { if (a[i] == ans[i]) { printf("No\n"); return 0; } } printf("Yes\n"); for (int i = 1; i <= n; ++i)printf("%d%c", ans[i], " \n"[i == n]); return 0; } ```
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i< (n); i++) using ll = long long; using namespace std; int main(void){ int N; cin >> N; int A[N]; vector<int> cnt(N+1); int maxcnt = 1; rep(i,N) { cin >> A[i]; cnt[A[i]]++; } int B[N]; rep(i,N) { cin >> B[N-1-i]; cnt[B[N-1-i]]++; } sort(cnt.begin(),cnt.end()); maxcnt = cnt[N]; if (maxcnt > N) { cout << "No" << endl; return 0; } int j = 0; rep(i,N) { while (A[i] == B[i]) { swap(B[i], B[j]); j++; } } j = N-1; rep(i,N) { while (A[i] == B[i]) { swap(B[i], B[j]); j--; } } cout << "Yes" << endl; rep(i,N) cout << B[i] << " "; cout << endl; return 0; }
### Prompt Create a solution in Cpp for the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i< (n); i++) using ll = long long; using namespace std; int main(void){ int N; cin >> N; int A[N]; vector<int> cnt(N+1); int maxcnt = 1; rep(i,N) { cin >> A[i]; cnt[A[i]]++; } int B[N]; rep(i,N) { cin >> B[N-1-i]; cnt[B[N-1-i]]++; } sort(cnt.begin(),cnt.end()); maxcnt = cnt[N]; if (maxcnt > N) { cout << "No" << endl; return 0; } int j = 0; rep(i,N) { while (A[i] == B[i]) { swap(B[i], B[j]); j++; } } j = N-1; rep(i,N) { while (A[i] == B[i]) { swap(B[i], B[j]); j--; } } cout << "Yes" << endl; rep(i,N) cout << B[i] << " "; cout << endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+5; int n,a[N],b[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=n;i>=1;i--) scanf("%d",&b[i]); for(int i=1;i<=n;i++){ if(a[i]==b[i]){ bool flag=0; for(int j=1;j<=n;j++){ if(b[j]!=a[i]&&b[i]!=a[j]){ swap(b[i],b[j]); flag=1; break; } } if(!flag){ printf("No"); return 0; } } } printf("Yes\n"); for(int i=1;i<=n;i++) printf("%d ",b[i]); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+5; int n,a[N],b[N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=n;i>=1;i--) scanf("%d",&b[i]); for(int i=1;i<=n;i++){ if(a[i]==b[i]){ bool flag=0; for(int j=1;j<=n;j++){ if(b[j]!=a[i]&&b[i]!=a[j]){ swap(b[i],b[j]); flag=1; break; } } if(!flag){ printf("No"); return 0; } } } printf("Yes\n"); for(int i=1;i<=n;i++) printf("%d ",b[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> a(n); vector<int> b(n); vector<int> cnta(n+1, 0); vector<int> cntb(n+1, 0); for(int i = 0; i < n; i++) { cin >> a[i]; cnta[a[i]]++; } for(int i = 0; i < n; i++) { cin >> b[i]; cntb[b[i]]++; } for(int i = 0; i <= n; i++) { if(cnta[i] + cntb[i] > n) { cout << "No" << endl; return 0; } } for(int i = 1; i <= n; i++) { cnta[i] += cnta[i-1]; cntb[i] += cntb[i-1]; } cout << "Yes" << endl; int x = 0; for(int i = 1; i <= n; i++) { x = max(x, cnta[i] - cntb[i-1]); } for(int i = 0; i < n; i++) { cout << b[(i-x+n)%n] << " "; } cout << endl; }
### Prompt Create a solution in Cpp for the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> a(n); vector<int> b(n); vector<int> cnta(n+1, 0); vector<int> cntb(n+1, 0); for(int i = 0; i < n; i++) { cin >> a[i]; cnta[a[i]]++; } for(int i = 0; i < n; i++) { cin >> b[i]; cntb[b[i]]++; } for(int i = 0; i <= n; i++) { if(cnta[i] + cntb[i] > n) { cout << "No" << endl; return 0; } } for(int i = 1; i <= n; i++) { cnta[i] += cnta[i-1]; cntb[i] += cntb[i-1]; } cout << "Yes" << endl; int x = 0; for(int i = 1; i <= n; i++) { x = max(x, cnta[i] - cntb[i-1]); } for(int i = 0; i < n; i++) { cout << b[(i-x+n)%n] << " "; } cout << endl; } ```
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; const int N = 200000; int n, a[N], b[N], c[N + 1], d[N + 1], ans[N]; int main() { cin >> n; rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; memset(c, 0, sizeof(int) * (n + 1)); memset(c, 0, sizeof(int) * (n + 1)); rep(i, n) c[a[i]]++; rep(i, n) d[b[i]]++; rep(i, n + 1) if (c[i] + d[i] > n) { cout << "No" << endl; return 0; } rep(i, n) c[i + 1] += c[i]; rep(i, n) d[i + 1] += d[i]; int x = 0; rep(i, n) x = max(x, c[i + 1] - d[i]); rep(i, n) ans[(i + x) % n] = b[i]; cout << "Yes" << endl; rep(i, n - 1) cout << ans[i] << " "; cout << ans[n - 1] << endl; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; const int N = 200000; int n, a[N], b[N], c[N + 1], d[N + 1], ans[N]; int main() { cin >> n; rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; memset(c, 0, sizeof(int) * (n + 1)); memset(c, 0, sizeof(int) * (n + 1)); rep(i, n) c[a[i]]++; rep(i, n) d[b[i]]++; rep(i, n + 1) if (c[i] + d[i] > n) { cout << "No" << endl; return 0; } rep(i, n) c[i + 1] += c[i]; rep(i, n) d[i + 1] += d[i]; int x = 0; rep(i, n) x = max(x, c[i + 1] - d[i]); rep(i, n) ans[(i + x) % n] = b[i]; cout << "Yes" << endl; rep(i, n - 1) cout << ans[i] << " "; cout << ans[n - 1] << endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; const int maxn = 400001; int n, a[maxn], b[maxn], ta[maxn], tb[maxn], l = 1, r = 1; int main() { cin >> n; for(int i = 1; i <= n; ++ i) { cin >> a[i]; ta[a[i]] ++; } for(int i = 1; i <= n; ++ i) { cin >> b[i]; tb[b[i]]++; } for(int i = 1; i <= n; ++ i) { if(ta[i] + tb[i] > n) { puts("No"); exit(0); } } memset(b, 0, sizeof(b)); for(int i = 1;i <= n; ++ i) { for(int j = 1; j <= tb[i]; ++ j) { while(b[l]) ++ l; while(a[r] <= a[l] && r <= n) ++ r; while(b[r]) ++ r; if(a[l] != i) b[l ++] = i; else b[r ++] = i; } } l = 1; for(int i = n + 1; b[i]; ++ i) { while(b[l]) ++ l; if(a[l] != b[i]) a[l] = b[i]; else { while(a[r = (rand() % n + 1)] == b[i] || b[r] == a[l]); b[l] = b[r]; b[r] = b[i]; } } puts("Yes"); for(int i = 1; i <= n; ++ i) printf("%d ", b[i]); return 0; }
### Prompt Develop a solution in CPP to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int maxn = 400001; int n, a[maxn], b[maxn], ta[maxn], tb[maxn], l = 1, r = 1; int main() { cin >> n; for(int i = 1; i <= n; ++ i) { cin >> a[i]; ta[a[i]] ++; } for(int i = 1; i <= n; ++ i) { cin >> b[i]; tb[b[i]]++; } for(int i = 1; i <= n; ++ i) { if(ta[i] + tb[i] > n) { puts("No"); exit(0); } } memset(b, 0, sizeof(b)); for(int i = 1;i <= n; ++ i) { for(int j = 1; j <= tb[i]; ++ j) { while(b[l]) ++ l; while(a[r] <= a[l] && r <= n) ++ r; while(b[r]) ++ r; if(a[l] != i) b[l ++] = i; else b[r ++] = i; } } l = 1; for(int i = n + 1; b[i]; ++ i) { while(b[l]) ++ l; if(a[l] != b[i]) a[l] = b[i]; else { while(a[r = (rand() % n + 1)] == b[i] || b[r] == a[l]); b[l] = b[r]; b[r] = b[i]; } } puts("Yes"); for(int i = 1; i <= n; ++ i) printf("%d ", b[i]); return 0; } ```
#include<bits/stdc++.h> using namespace std; int n,a[400001],b[400001],ta[400001],tb[400001],l=1,r=1; int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]),ta[a[i]]++; for(int i=1;i<=n;i++)scanf("%d",&b[i]),tb[b[i]]++; for(int i=1;i<=n;i++)if(ta[i]+tb[i]>n)puts("No"),exit(0); memset(b,0,sizeof(b)); for(int i=1;i<=n;i++) { for(int j=1;j<=tb[i];j++) { while(b[l])++l; while(a[r]<=a[l]&&r<=n)++r; while(b[r])++r; if(a[l]!=i)b[l++]=i; else b[r++]=i; } } l=1; for(int i=n+1;b[i];i++) { while(b[l])++l; if(a[l]!=b[i])a[l]=b[i]; else { while(a[r=(rand()%n+1)]==b[i]||b[r]==a[l]); b[l]=b[r],b[r]=b[i]; } } puts("Yes"); for(int i=1;i<=n;i++)printf("%d ",b[i]); return 0; }
### Prompt In cpp, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int n,a[400001],b[400001],ta[400001],tb[400001],l=1,r=1; int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]),ta[a[i]]++; for(int i=1;i<=n;i++)scanf("%d",&b[i]),tb[b[i]]++; for(int i=1;i<=n;i++)if(ta[i]+tb[i]>n)puts("No"),exit(0); memset(b,0,sizeof(b)); for(int i=1;i<=n;i++) { for(int j=1;j<=tb[i];j++) { while(b[l])++l; while(a[r]<=a[l]&&r<=n)++r; while(b[r])++r; if(a[l]!=i)b[l++]=i; else b[r++]=i; } } l=1; for(int i=n+1;b[i];i++) { while(b[l])++l; if(a[l]!=b[i])a[l]=b[i]; else { while(a[r=(rand()%n+1)]==b[i]||b[r]==a[l]); b[l]=b[r],b[r]=b[i]; } } puts("Yes"); for(int i=1;i<=n;i++)printf("%d ",b[i]); return 0; } ```
#include<bits/stdc++.h> using namespace std; const int N=2e5+10; int a[N],b[N]; int n; int t[N],t1[N]; inline int read() { int Num=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') {Num=(Num<<1)+(Num<<3)+ch-'0'; ch=getchar();} return Num*f; } int tot=200,flag=0; int main() { n=read();int Bit=1,_bit=n; for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=n;i++) b[n-i+1]=read(); for(int i=1;i<=n;i++) if(a[i]==b[i]) { if(b[Bit]!=a[i]&&b[i]!=a[Bit]) swap(b[Bit++],b[i]); else if(b[_bit]!=a[i]&&a[_bit]!=b[i]) swap(b[_bit--],b[i]); else { puts("No"); return 0; } } puts("Yes"); for(int i=1;i<=n;i++) printf("%d ",b[i]); return 0; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int N=2e5+10; int a[N],b[N]; int n; int t[N],t1[N]; inline int read() { int Num=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9') {Num=(Num<<1)+(Num<<3)+ch-'0'; ch=getchar();} return Num*f; } int tot=200,flag=0; int main() { n=read();int Bit=1,_bit=n; for(int i=1;i<=n;i++) a[i]=read(); for(int i=1;i<=n;i++) b[n-i+1]=read(); for(int i=1;i<=n;i++) if(a[i]==b[i]) { if(b[Bit]!=a[i]&&b[i]!=a[Bit]) swap(b[Bit++],b[i]); else if(b[_bit]!=a[i]&&a[_bit]!=b[i]) swap(b[_bit--],b[i]); else { puts("No"); return 0; } } puts("Yes"); for(int i=1;i<=n;i++) printf("%d ",b[i]); return 0; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int SIZE = 2e5+10; int n; int a[SIZE], b[SIZE], l[SIZE], r[SIZE]; inline int read() { int x=0;char ch=getchar(); while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)){x=x*10+(ch-'0'); ch=getchar();} return x; } int main() { int l = SIZE, r = 0, c = 0; n=read(); for (int i = 1; i <= n; ++i) a[i]=read(); for (int j = 1; j <= n; ++j) b[j]=read(); for (int i = 1; i <= (n>>1);++i) swap(b[i], b[n-i+1]); for (int i = 1; i <= n; ++i) if (a[i] == b[i]) { r = i; if (l > n) l = i;} for (int i = 1; i <= n; ++i) if (a[i] != a[r] && b[i] != a[r]) ++c; if (c < r - l + 1) printf("No\n"); else { printf("Yes\n"); for (int i = 1; i <= n; ++i) { if (l > r) break; if (a[i] != a[l] && b[i] != a[l]) swap(b[i], b[l++]); } for (int i = 1; i <= n; ++i) printf("%d ", b[i]); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int SIZE = 2e5+10; int n; int a[SIZE], b[SIZE], l[SIZE], r[SIZE]; inline int read() { int x=0;char ch=getchar(); while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)){x=x*10+(ch-'0'); ch=getchar();} return x; } int main() { int l = SIZE, r = 0, c = 0; n=read(); for (int i = 1; i <= n; ++i) a[i]=read(); for (int j = 1; j <= n; ++j) b[j]=read(); for (int i = 1; i <= (n>>1);++i) swap(b[i], b[n-i+1]); for (int i = 1; i <= n; ++i) if (a[i] == b[i]) { r = i; if (l > n) l = i;} for (int i = 1; i <= n; ++i) if (a[i] != a[r] && b[i] != a[r]) ++c; if (c < r - l + 1) printf("No\n"); else { printf("Yes\n"); for (int i = 1; i <= n; ++i) { if (l > r) break; if (a[i] != a[l] && b[i] != a[l]) swap(b[i], b[l++]); } for (int i = 1; i <= n; ++i) printf("%d ", b[i]); } return 0; } ```
#include<iostream> #include<bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define deb(x) cout << #x << " " << x << endl; typedef long long int ll; int main() { IOS int n; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = n-1; i >= 0; i--) cin >> b[i]; bool swapped = true; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { swapped = false; for (int j = 0; j < n; j++) { if (b[i] == b[j]) continue; if (a[j] == b[i]) continue; if (a[i] == b[j]) continue; swap(b[i], b[j]); swapped = true; break; } if (!swapped) break; } } if (swapped) { cout << "Yes" << endl; for (int i = 0; i < n; i++) cout << b[i] << " "; } else cout << "No"; return 0; }
### Prompt Please formulate a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<iostream> #include<bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define deb(x) cout << #x << " " << x << endl; typedef long long int ll; int main() { IOS int n; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = n-1; i >= 0; i--) cin >> b[i]; bool swapped = true; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { swapped = false; for (int j = 0; j < n; j++) { if (b[i] == b[j]) continue; if (a[j] == b[i]) continue; if (a[i] == b[j]) continue; swap(b[i], b[j]); swapped = true; break; } if (!swapped) break; } } if (swapped) { cout << "Yes" << endl; for (int i = 0; i < n; i++) cout << b[i] << " "; } else cout << "No"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1; int a[N], b[N], c[N]; int lb[N], rb[N]; int main () { int n; scanf("%d", &n); for (int i = 1; i <= n; i ++) scanf("%d", a + i); for (int i = 1; i <= n; i ++) scanf("%d", b + i); for (int i = 1; i <= n; i ++) rb[a[i]] = i; for (int i = n; i >= 1; i --) lb[b[i]] = i; int sh = 0; for (int i = 1; i <= n; i ++) if (lb[i] && rb[i]) sh = max(sh, rb[i] - lb[i] + 1); for (int i = 1; i <= n; i ++) c[(i + sh > n) ? (i + sh - n) : (i + sh)] = b[i]; int ok = 1; for (int i = 1; i <= n; i ++) if (a[i] == c[i]) ok = 0; if (ok) { puts("Yes"); for (int i = 1; i <= n; i ++) printf("%d%c", c[i], "\n "[i != n]); } else { puts("No"); } }
### Prompt Your task is to create a Cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1; int a[N], b[N], c[N]; int lb[N], rb[N]; int main () { int n; scanf("%d", &n); for (int i = 1; i <= n; i ++) scanf("%d", a + i); for (int i = 1; i <= n; i ++) scanf("%d", b + i); for (int i = 1; i <= n; i ++) rb[a[i]] = i; for (int i = n; i >= 1; i --) lb[b[i]] = i; int sh = 0; for (int i = 1; i <= n; i ++) if (lb[i] && rb[i]) sh = max(sh, rb[i] - lb[i] + 1); for (int i = 1; i <= n; i ++) c[(i + sh > n) ? (i + sh - n) : (i + sh)] = b[i]; int ok = 1; for (int i = 1; i <= n; i ++) if (a[i] == c[i]) ok = 0; if (ok) { puts("Yes"); for (int i = 1; i <= n; i ++) printf("%d%c", c[i], "\n "[i != n]); } else { puts("No"); } } ```
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> a, b; int main() { int n; cin >> n; a.resize(n); b.resize(n); for (auto &x : a) { cin >> x; } for (auto &x : b) { cin >> x; } reverse(b.begin(), b.end()); int l = 0, r = 0; while (l < n && a[l] != b[l]) { l++; r++; } while (r < n && a[r] == b[r]) { r++; } for (int i = 0; i < n && l != r; i++) if (a[i] != a[l] && b[i] != b[l]) swap(b[l++], b[i]); if (l != r) { cout << "No" << '\n'; return 0; } cout << "Yes" << '\n'; for (auto &x : b) cout << x << ' '; cout << '\n'; cin.ignore(2); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> a, b; int main() { int n; cin >> n; a.resize(n); b.resize(n); for (auto &x : a) { cin >> x; } for (auto &x : b) { cin >> x; } reverse(b.begin(), b.end()); int l = 0, r = 0; while (l < n && a[l] != b[l]) { l++; r++; } while (r < n && a[r] == b[r]) { r++; } for (int i = 0; i < n && l != r; i++) if (a[i] != a[l] && b[i] != b[l]) swap(b[l++], b[i]); if (l != r) { cout << "No" << '\n'; return 0; } cout << "Yes" << '\n'; for (auto &x : b) cout << x << ' '; cout << '\n'; cin.ignore(2); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; reverse(b.begin(), b.end()); int start = -1, fin = -1; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { if (start == -1) start = i; fin = i; } } bool bad = 0; if (start != -1) { for (int i = 0, j = start; j <= fin; ++i) { if (i == start) i = fin + 1; if (i == n) { bad = 1; break; } if (a[i] != b[j] && b[j] != b[i]) { swap(b[i], b[j++]); } } } if (bad) { cout << "No" << '\n'; } else { cout << "Yes" << '\n'; for (int i = 0; i < n; ++i) { if (i) cout << " "; cout << b[i]; } cout << '\n'; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; reverse(b.begin(), b.end()); int start = -1, fin = -1; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { if (start == -1) start = i; fin = i; } } bool bad = 0; if (start != -1) { for (int i = 0, j = start; j <= fin; ++i) { if (i == start) i = fin + 1; if (i == n) { bad = 1; break; } if (a[i] != b[j] && b[j] != b[i]) { swap(b[i], b[j++]); } } } if (bad) { cout << "No" << '\n'; } else { cout << "Yes" << '\n'; for (int i = 0; i < n; ++i) { if (i) cout << " "; cout << b[i]; } cout << '\n'; } return 0; } ```
#include<bits/stdc++.h> using namespace std; int n,a[200010],b[200010],cnt[200010],L,R,l,r; inline int read() { int x=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return w?-x:x; } int main() { n=read(); for(int i=1;i<=n;i++)cnt[a[i]=read()]++; for(int i=1;i<=n;i++)cnt[b[n-i+1]=read()]++; for(int i=1;i<=n;i++) if(cnt[i]>n){cout<<"No";return 0;} cout<<"Yes\n"; L=n;R=1;l=1;r=n; for(int i=1;i<=n;i++) if(a[i]==b[i]){ L=min(L,i); R=max(R,i); } for(int i=L;i<=R;i++) if(a[i]==b[i]){ if(b[i]!=a[l]&&b[i]!=b[l]){ swap(b[i],b[l]); l++; }else{ swap(b[i],b[r]); r--; } } /*for(int i=L;i<=R;i++) if(l<L&&b[l]!=b[i])swap(b[i],b[l++]); else swap(b[i],b[r--]);*/ for(int i=1;i<=n;i++) printf("%d ",b[i]); }
### Prompt Construct a Cpp code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int n,a[200010],b[200010],cnt[200010],L,R,l,r; inline int read() { int x=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return w?-x:x; } int main() { n=read(); for(int i=1;i<=n;i++)cnt[a[i]=read()]++; for(int i=1;i<=n;i++)cnt[b[n-i+1]=read()]++; for(int i=1;i<=n;i++) if(cnt[i]>n){cout<<"No";return 0;} cout<<"Yes\n"; L=n;R=1;l=1;r=n; for(int i=1;i<=n;i++) if(a[i]==b[i]){ L=min(L,i); R=max(R,i); } for(int i=L;i<=R;i++) if(a[i]==b[i]){ if(b[i]!=a[l]&&b[i]!=b[l]){ swap(b[i],b[l]); l++; }else{ swap(b[i],b[r]); r--; } } /*for(int i=L;i<=R;i++) if(l<L&&b[l]!=b[i])swap(b[i],b[l++]); else swap(b[i],b[r--]);*/ for(int i=1;i<=n;i++) printf("%d ",b[i]); } ```
#include <bits/stdc++.h> using namespace std; #define ll long long const int N=2e5+5; int n,m; int a[N], b[N], c[N]; int main(){ cin >> n; for(int i = 1; i <= n; i ++ )cin>>a[i]; for(int i = 1;i <= n; i ++ )cin>>b[i]; reverse(b + 1, b + n + 1); int l, r, flag, num; l = r = flag = num = 0; for(int i = 1; i <= n; i ++ ){ if(a[i]==b[i]&&l==0) { l=i,r=i,flag=a[i]; } else if(a[i]==b[i]&&l) { r=i; } } if(l){ for(int i = 1; i <= n && num <= r - l; i ++ ){ if(b[i] != flag && a[i] != flag){ b[l + num] = b[i]; b[i] = flag; num ++ ; } } } if(l == 0 || num == r - l + 1){ puts("Yes"); for(int i = 1;i <= n; i ++ ){ cout << b[i] << " "; } cout << endl; } else{ puts("No"); } return 0; }
### Prompt In cpp, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define ll long long const int N=2e5+5; int n,m; int a[N], b[N], c[N]; int main(){ cin >> n; for(int i = 1; i <= n; i ++ )cin>>a[i]; for(int i = 1;i <= n; i ++ )cin>>b[i]; reverse(b + 1, b + n + 1); int l, r, flag, num; l = r = flag = num = 0; for(int i = 1; i <= n; i ++ ){ if(a[i]==b[i]&&l==0) { l=i,r=i,flag=a[i]; } else if(a[i]==b[i]&&l) { r=i; } } if(l){ for(int i = 1; i <= n && num <= r - l; i ++ ){ if(b[i] != flag && a[i] != flag){ b[l + num] = b[i]; b[i] = flag; num ++ ; } } } if(l == 0 || num == r - l + 1){ puts("Yes"); for(int i = 1;i <= n; i ++ ){ cout << b[i] << " "; } cout << endl; } else{ puts("No"); } return 0; } ```
#include<bits/stdc++.h> #define ll long long int #define mod 1000000007 using namespace std; int main(){ int n; cin>>n; vector<int>a(n),b(n); for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=0;i<n;i++){ cin>>b[i]; } reverse(b.begin(),b.end()); int lpt=0,rpt=n-1; for(int i=0;i<n;i++){ if(a[i]==b[i]){ if(b[lpt]!=b[i] && a[lpt]!=b[i]){ swap(b[i],b[lpt]); ++lpt; } else if(b[rpt]!=b[i] && a[rpt]!=b[i]){ swap(b[i],b[rpt]); --rpt; } else{ cout<<"No\n"; return 0; } } } for(int i=0;i<n;i++){ if(a[i]==b[i]){ cout<<"No\n"; return 0; } } cout<<"Yes\n"; for(auto x:b){ cout<<x<<" "; } cout<<endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> #define ll long long int #define mod 1000000007 using namespace std; int main(){ int n; cin>>n; vector<int>a(n),b(n); for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=0;i<n;i++){ cin>>b[i]; } reverse(b.begin(),b.end()); int lpt=0,rpt=n-1; for(int i=0;i<n;i++){ if(a[i]==b[i]){ if(b[lpt]!=b[i] && a[lpt]!=b[i]){ swap(b[i],b[lpt]); ++lpt; } else if(b[rpt]!=b[i] && a[rpt]!=b[i]){ swap(b[i],b[rpt]); --rpt; } else{ cout<<"No\n"; return 0; } } } for(int i=0;i<n;i++){ if(a[i]==b[i]){ cout<<"No\n"; return 0; } } cout<<"Yes\n"; for(auto x:b){ cout<<x<<" "; } cout<<endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; vector<int> a, b, res; bool solve(); int main() { cin >> n; a.resize(n); for (auto &p : a) cin >> p; b.resize(n); for (auto &p : b) cin >> p; if (solve()) { cout << "Yes" << endl; for (int i = 0; i < n; ++i) cout << res[i] << " \n"[i == n - 1]; } else cout << "No" << endl; return 0; } bool solve() { vector<int> cnta(n + 1, 0), cntb(n + 1, 0); for (int i = 0; i < n; ++i) { ++cnta[a[i]]; ++cntb[b[i]]; } for (int i = 1; i <= n; ++i) { if (cnta[i] + cntb[i] > n) return 0; cnta[i] += cnta[i - 1]; cntb[i] += cntb[i - 1]; } int dif = 0; for (int i = 1; i <= n; ++i) dif = max(dif, cnta[i] - cntb[i - 1]); res.resize(n); for (int i = 0; i < n; ++i) res[(i + dif) % n] = b[i]; return 1; }
### Prompt Please provide a cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; vector<int> a, b, res; bool solve(); int main() { cin >> n; a.resize(n); for (auto &p : a) cin >> p; b.resize(n); for (auto &p : b) cin >> p; if (solve()) { cout << "Yes" << endl; for (int i = 0; i < n; ++i) cout << res[i] << " \n"[i == n - 1]; } else cout << "No" << endl; return 0; } bool solve() { vector<int> cnta(n + 1, 0), cntb(n + 1, 0); for (int i = 0; i < n; ++i) { ++cnta[a[i]]; ++cntb[b[i]]; } for (int i = 1; i <= n; ++i) { if (cnta[i] + cntb[i] > n) return 0; cnta[i] += cnta[i - 1]; cntb[i] += cntb[i - 1]; } int dif = 0; for (int i = 1; i <= n; ++i) dif = max(dif, cnta[i] - cntb[i - 1]); res.resize(n); for (int i = 0; i < n; ++i) res[(i + dif) % n] = b[i]; return 1; } ```
#include <bits/stdc++.h> using namespace std;using v=vector<int>;int n;int h(v a,v b){int r=1;for(int i=0;i<n;i++)r&=a[i]!=b[i];return r;}int f(int g,v a){cout<<(g?"Yes\n":"No\n");if(g)for(int e:a)cout<<e<<'\n';exit(0);}int main(){cin>>n;v a(n),b(n),c;for(int&e:a)cin>>e;for(int&e:b)cin>>e;c=b;reverse(begin(b),end(b));rotate(begin(c),begin(c)+n/2,end(c));if(h(a,b))f(1,b);if(h(a,c))f(1,c);f(0,a);}
### Prompt In cpp, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std;using v=vector<int>;int n;int h(v a,v b){int r=1;for(int i=0;i<n;i++)r&=a[i]!=b[i];return r;}int f(int g,v a){cout<<(g?"Yes\n":"No\n");if(g)for(int e:a)cout<<e<<'\n';exit(0);}int main(){cin>>n;v a(n),b(n),c;for(int&e:a)cin>>e;for(int&e:b)cin>>e;c=b;reverse(begin(b),end(b));rotate(begin(c),begin(c)+n/2,end(c));if(h(a,b))f(1,b);if(h(a,c))f(1,c);f(0,a);} ```
#include <iostream> #include <string> #include <vector> #include <set> #include <queue> #include <stack> #include <map> #include <algorithm> #include <math.h> #include <cassert> #define rep(i,n) for(int i = 0; i < n; ++i ) using namespace std; using ll = long long; using P = pair<int,int>; int main() { int n; cin >> n; vector<int> a(n),b(n); rep(i,n) cin >> a[i]; rep(i,n) cin >> b[i]; vector<int> s(n+1),t(n+1); rep(i,n) s[a[i]]++, t[b[i]]++; rep(i,n+1){ if(s[i]+t[i]>n){ cout << "No" << endl; return 0; } } rep(i,n) s[i+1]+=s[i], t[i+1]+=t[i]; int x=0; rep(i,n) x = max(x,s[i+1]-t[i]); cout << "Yes\n"; rep(i,n) cout << b[(i+n-x)%n] << (i==n-1?"\n":" "); }
### Prompt Develop a solution in cpp to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <iostream> #include <string> #include <vector> #include <set> #include <queue> #include <stack> #include <map> #include <algorithm> #include <math.h> #include <cassert> #define rep(i,n) for(int i = 0; i < n; ++i ) using namespace std; using ll = long long; using P = pair<int,int>; int main() { int n; cin >> n; vector<int> a(n),b(n); rep(i,n) cin >> a[i]; rep(i,n) cin >> b[i]; vector<int> s(n+1),t(n+1); rep(i,n) s[a[i]]++, t[b[i]]++; rep(i,n+1){ if(s[i]+t[i]>n){ cout << "No" << endl; return 0; } } rep(i,n) s[i+1]+=s[i], t[i+1]+=t[i]; int x=0; rep(i,n) x = max(x,s[i+1]-t[i]); cout << "Yes\n"; rep(i,n) cout << b[(i+n-x)%n] << (i==n-1?"\n":" "); } ```
#include<bits/stdc++.h> using namespace std; int n,a[200010],b[200010],cnt[200010],L,R,l,r; inline int read() { int x=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return w?-x:x; } int main() { r=L=n=read(); for(int i=1;i<=n;i++)cnt[a[i]=read()]++; for(int i=1;i<=n;i++)cnt[b[n-i+1]=read()]++; for(int i=1;i<=n;i++)if(cnt[i]>n){cout<<"No";return 0;} cout<<"Yes\n"; l=R=1; for(int i=1;i<=n;i++) if(a[i]==b[i]){L=min(L,i);R=max(R,i);} for(int i=L;i<=R;i++) if(l<L&&b[l]!=b[i]&&b[i]!=a[l])swap(b[i],b[l++]); else swap(b[i],b[r--]); for(int i=1;i<=n;i++)printf("%d ",b[i]); }
### Prompt Please formulate a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int n,a[200010],b[200010],cnt[200010],L,R,l,r; inline int read() { int x=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return w?-x:x; } int main() { r=L=n=read(); for(int i=1;i<=n;i++)cnt[a[i]=read()]++; for(int i=1;i<=n;i++)cnt[b[n-i+1]=read()]++; for(int i=1;i<=n;i++)if(cnt[i]>n){cout<<"No";return 0;} cout<<"Yes\n"; l=R=1; for(int i=1;i<=n;i++) if(a[i]==b[i]){L=min(L,i);R=max(R,i);} for(int i=L;i<=R;i++) if(l<L&&b[l]!=b[i]&&b[i]!=a[l])swap(b[i],b[l++]); else swap(b[i],b[r--]); for(int i=1;i<=n;i++)printf("%d ",b[i]); } ```
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,n) for(int i=0;i<n;i++) const ll MOD=1000000007; const long double PI=3.14159265358979; const ll MAX=2000; int main() { ll N,c=1; cin>>N; ll a[N]; ll b[2*N]; ll ac[N+1]; ll ad[N+1]; ll bd[N+1]; ll bc[N+1]; rep(i,N+1){ ac[i]=0;bc[i]=0; } rep(i,N){ cin>>a[i]; ac[a[i]]++; ad[a[i]]=1; } rep(i,N){ cin>>b[i]; b[i+N]=b[i]; bc[b[i]]++; bd[b[i]]=1; } ll x=0; rep(i,N+1){ if(ac[i]+bc[i]>N){ c=0; } if(i){ ac[i]+=ac[i-1]; bc[i]+=bc[i-1]; } } for(int i=1;i<N+1;i++){ if(ad[i]==1&&bd[i]==1){ x=max(x,ac[i]-bc[i-1]); // cout<<ac[i]<<" "<<bc[i-1]<<"/"; } } if(c){ cout<<"Yes"<<endl; for(int i=N-x;i<2*N-x;i++){ cout<<b[i]<<" "; } } else{ cout<<"No"; } }
### Prompt Create a solution in cpp for the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,n) for(int i=0;i<n;i++) const ll MOD=1000000007; const long double PI=3.14159265358979; const ll MAX=2000; int main() { ll N,c=1; cin>>N; ll a[N]; ll b[2*N]; ll ac[N+1]; ll ad[N+1]; ll bd[N+1]; ll bc[N+1]; rep(i,N+1){ ac[i]=0;bc[i]=0; } rep(i,N){ cin>>a[i]; ac[a[i]]++; ad[a[i]]=1; } rep(i,N){ cin>>b[i]; b[i+N]=b[i]; bc[b[i]]++; bd[b[i]]=1; } ll x=0; rep(i,N+1){ if(ac[i]+bc[i]>N){ c=0; } if(i){ ac[i]+=ac[i-1]; bc[i]+=bc[i-1]; } } for(int i=1;i<N+1;i++){ if(ad[i]==1&&bd[i]==1){ x=max(x,ac[i]-bc[i-1]); // cout<<ac[i]<<" "<<bc[i-1]<<"/"; } } if(c){ cout<<"Yes"<<endl; for(int i=N-x;i<2*N-x;i++){ cout<<b[i]<<" "; } } else{ cout<<"No"; } } ```
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; signed main() { int n; cin >> n; vector<int> a(n),b(n); vector<int> cnt(n + 1); rep(i,n) { cin >> a[i]; cnt[a[i]]++; } rep(i,n) { cin >> b[i]; cnt[b[i]]++; } reverse(b.begin(),b.end()); rep(i,n + 1) { if(cnt[i] > n) { cout << "No" << '\n'; return 0; } } int x = 0; int k = 0; vector<int> overlap; for (int i = 0; i < n; i++) { if(a[i] == b[i]) { x = a[i]; overlap.push_back(i); k++; } } vector<int> idx; for (int i = 0; i < n; i++) { if(a[i] != x && b[i] != x) { idx.push_back(i); } } for (int i = 0; i < k; i++) { swap(b[overlap[i]], b[idx[i]]); } cout << "Yes" << '\n'; for (auto it : b) cout << it << " "; }
### Prompt Please create a solution in cpp to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; signed main() { int n; cin >> n; vector<int> a(n),b(n); vector<int> cnt(n + 1); rep(i,n) { cin >> a[i]; cnt[a[i]]++; } rep(i,n) { cin >> b[i]; cnt[b[i]]++; } reverse(b.begin(),b.end()); rep(i,n + 1) { if(cnt[i] > n) { cout << "No" << '\n'; return 0; } } int x = 0; int k = 0; vector<int> overlap; for (int i = 0; i < n; i++) { if(a[i] == b[i]) { x = a[i]; overlap.push_back(i); k++; } } vector<int> idx; for (int i = 0; i < n; i++) { if(a[i] != x && b[i] != x) { idx.push_back(i); } } for (int i = 0; i < k; i++) { swap(b[overlap[i]], b[idx[i]]); } cout << "Yes" << '\n'; for (auto it : b) cout << it << " "; } ```
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define N 200010 int n,a[N],b[N]; int main(){ cin>>n; rep(i,n)cin>>a[i]; rep(i,n)cin>>b[i]; int j=0,bef=-1; rep(i,n){ if(bef!=a[i])j=0; if(a[i]==b[i]){ for(;j<n;j++){ if(a[j]!=a[i]&&b[j]!=a[i]){ swap(b[i],b[j]); break; } } if(a[i]==b[i]){cout<<"No\n";return 0;} } bef=a[i]; } cout<<"Yes\n"; rep(i,n){if(i)cout<<" ";cout<<b[i];} cout<<"\n"; }
### Prompt Please formulate a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define N 200010 int n,a[N],b[N]; int main(){ cin>>n; rep(i,n)cin>>a[i]; rep(i,n)cin>>b[i]; int j=0,bef=-1; rep(i,n){ if(bef!=a[i])j=0; if(a[i]==b[i]){ for(;j<n;j++){ if(a[j]!=a[i]&&b[j]!=a[i]){ swap(b[i],b[j]); break; } } if(a[i]==b[i]){cout<<"No\n";return 0;} } bef=a[i]; } cout<<"Yes\n"; rep(i,n){if(i)cout<<" ";cout<<b[i];} cout<<"\n"; } ```
#include <bits/stdc++.h> using namespace std; int main(){ uint64_t n; cin>>n; vector<int>a(n+1,0); vector<int>b(n+1,0); vector<int>A(n); vector<int>B(n); int curr_a=1; int curr_b=1; for(int i=0;i<n;i++){ cin>>A[i]; for(int j=curr_a;j<A[i];j++){ a[j]=i; } curr_a=A[i]; } for(int i=curr_a;i<=n;i++){ a[i]=n; } for(int i=0;i<n;i++){ cin>>B[i]; for(int j=curr_b;j<B[i];j++){ b[j]=i; } curr_b=B[i]; } for(int i=curr_b;i<=n;i++){ b[i]=n; } int x=0; for(int i=1;i<=n;i++){ if(a[i]-a[i-1]+b[i]-b[i-1]>n){ cout<<"No"<<endl; return 0; }else if(a[i]-b[i-1]>x){ x=a[i]-b[i-1]; } } cout<<"Yes"<<endl; for(int i=n-x;i<n;i++){ cout<<B[i]<<" "; } for(int i=0;i<n-x;i++){ if(i<n-x-1){ cout<<B[i]<<" "; }else{ cout<<B[i]; } } cout<<endl; }
### Prompt Develop a solution in Cpp to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ uint64_t n; cin>>n; vector<int>a(n+1,0); vector<int>b(n+1,0); vector<int>A(n); vector<int>B(n); int curr_a=1; int curr_b=1; for(int i=0;i<n;i++){ cin>>A[i]; for(int j=curr_a;j<A[i];j++){ a[j]=i; } curr_a=A[i]; } for(int i=curr_a;i<=n;i++){ a[i]=n; } for(int i=0;i<n;i++){ cin>>B[i]; for(int j=curr_b;j<B[i];j++){ b[j]=i; } curr_b=B[i]; } for(int i=curr_b;i<=n;i++){ b[i]=n; } int x=0; for(int i=1;i<=n;i++){ if(a[i]-a[i-1]+b[i]-b[i-1]>n){ cout<<"No"<<endl; return 0; }else if(a[i]-b[i-1]>x){ x=a[i]-b[i-1]; } } cout<<"Yes"<<endl; for(int i=n-x;i<n;i++){ cout<<B[i]<<" "; } for(int i=0;i<n-x;i++){ if(i<n-x-1){ cout<<B[i]<<" "; }else{ cout<<B[i]; } } cout<<endl; } ```
#pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 2e5 + 10; #define fi first #define se second #define pb push_back #define wzh(x) cerr<<#x<<'='<<x<<endl; int n,a[N],b[N],L[N],R[N],f[N]; int main() { ios::sync_with_stdio(false); cin>>n; set<int>al; for(int i=1;i<=n;i++)cin>>a[i],L[a[i]]++; for(int i=1;i<=n;i++)cin>>b[i],R[b[i]]++; for(int i=1;i<=n;i++)if(L[i]+R[i]>n)return cout<<"No\n",0; reverse(b+1,b+1+n); cout<<"Yes\n"; int l=-1; vector<int>v; for(int i=1;i<=n;i++){ if(a[i]==b[i]){ l=a[i]; v.pb(i); } } // cout<<"ok"<<endl; int idx=0; for(int i=1;i<=n&&idx<v.size();i++){ if(a[i]!=b[i] && a[i]!=l && b[i]!=l){ swap(b[i],b[v[idx++]]); } } // cout<<"this"<<endl; for(int i=1;i<=n;i++)cout<<b[i]<<' '; return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 2e5 + 10; #define fi first #define se second #define pb push_back #define wzh(x) cerr<<#x<<'='<<x<<endl; int n,a[N],b[N],L[N],R[N],f[N]; int main() { ios::sync_with_stdio(false); cin>>n; set<int>al; for(int i=1;i<=n;i++)cin>>a[i],L[a[i]]++; for(int i=1;i<=n;i++)cin>>b[i],R[b[i]]++; for(int i=1;i<=n;i++)if(L[i]+R[i]>n)return cout<<"No\n",0; reverse(b+1,b+1+n); cout<<"Yes\n"; int l=-1; vector<int>v; for(int i=1;i<=n;i++){ if(a[i]==b[i]){ l=a[i]; v.pb(i); } } // cout<<"ok"<<endl; int idx=0; for(int i=1;i<=n&&idx<v.size();i++){ if(a[i]!=b[i] && a[i]!=l && b[i]!=l){ swap(b[i],b[v[idx++]]); } } // cout<<"this"<<endl; for(int i=1;i<=n;i++)cout<<b[i]<<' '; return 0; } ```
#include <cstdio> #include <cstring> #include <map> #include <vector> #include <algorithm> using namespace std; #define maxn 200010 #define pb push_back int n,A[maxn],B[maxn]; vector<int> a,b; int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&A[i]); for(int i=1;i<=n;i++)scanf("%d",&B[i]); reverse(B+1,B+n+1); for(int i=1;i<=n;i++)if(A[i]==B[i])a.pb(i); if(a.size()){ for(int i=1;i<=n;i++)if(A[i]!=A[a[0]]&&B[i]!=B[a[0]])b.pb(i); if(b.size()<a.size())return printf("No\n"),0; for(int i=0;i<a.size();i++)swap(B[b[i]],B[a[i]]); } printf("Yes\n"); for(int i=1;i<=n;i++)printf("%d ",B[i]); }
### Prompt In Cpp, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <cstdio> #include <cstring> #include <map> #include <vector> #include <algorithm> using namespace std; #define maxn 200010 #define pb push_back int n,A[maxn],B[maxn]; vector<int> a,b; int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&A[i]); for(int i=1;i<=n;i++)scanf("%d",&B[i]); reverse(B+1,B+n+1); for(int i=1;i<=n;i++)if(A[i]==B[i])a.pb(i); if(a.size()){ for(int i=1;i<=n;i++)if(A[i]!=A[a[0]]&&B[i]!=B[a[0]])b.pb(i); if(b.size()<a.size())return printf("No\n"),0; for(int i=0;i<a.size();i++)swap(B[b[i]],B[a[i]]); } printf("Yes\n"); for(int i=1;i<=n;i++)printf("%d ",B[i]); } ```
#include<bits/stdc++.h> using namespace std; const int N=200010; int a[N],b[N],n; int main(){ scanf("%d",&n); int x; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) scanf("%d",&b[i]); reverse(b+1,b+1+n); int l=0,r=-1,t=0,L=0,R=0; for(int i=1;i<=n;i++) if(a[i]==b[i]) { if(!l) l=i,r=i,t=a[i]; else r++; } for(int i=1;i<=n;i++) if(a[i]!=b[i]){ while(l<=r && b[i]!=a[l] && b[l]!=a[i]) swap(b[i],b[l]),l++; } else break; for(int i=n;i>=1;i--) if(a[i]!=b[i]){ while(l<=r && b[i]!=a[r] && b[r]!=a[i]) swap(b[i],b[r]),r--; } else break; if(l<=r) printf("No\n"); else{ printf("Yes\n"); for(int i=1;i<=n;i++) printf("%d ",b[i]); } }
### Prompt Please provide a Cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int N=200010; int a[N],b[N],n; int main(){ scanf("%d",&n); int x; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) scanf("%d",&b[i]); reverse(b+1,b+1+n); int l=0,r=-1,t=0,L=0,R=0; for(int i=1;i<=n;i++) if(a[i]==b[i]) { if(!l) l=i,r=i,t=a[i]; else r++; } for(int i=1;i<=n;i++) if(a[i]!=b[i]){ while(l<=r && b[i]!=a[l] && b[l]!=a[i]) swap(b[i],b[l]),l++; } else break; for(int i=n;i>=1;i--) if(a[i]!=b[i]){ while(l<=r && b[i]!=a[r] && b[r]!=a[i]) swap(b[i],b[r]),r--; } else break; if(l<=r) printf("No\n"); else{ printf("Yes\n"); for(int i=1;i<=n;i++) printf("%d ",b[i]); } } ```
#include<bits/stdc++.h> using namespace std; const int maxn=2e5+10; inline int qr() { int x=0,f=0; char ch=0; while(!isdigit(ch)) { f=ch=='-'; ch=getchar(); } while(isdigit(ch)) { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return f?-x:x; } int n; int a[maxn],b[maxn]; int p[maxn]; int cnt; int val; int main() { n=qr(); for(int i=1;i<=n;i++) { a[i]=qr(); } for(int i=1;i<=n;i++) { b[i]=qr(); } reverse(b+1,b+n+1); for(int i=1;i<=n;i++) { if(b[i]!=a[i])continue; cnt++; p[cnt]=i;//相等的位置; val=b[i]; } for(int i=1;i<=n;i++) { if(!cnt) break; if(a[i]!=val&&b[i]!=val)//可以交换 swap(b[p[cnt--]],b[i]); } if(cnt) //位置不够 { printf("No");return 0; } else { printf("Yes\n"); for(int i=1;i<=n;i++) { printf("%d ",b[i]); } } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int maxn=2e5+10; inline int qr() { int x=0,f=0; char ch=0; while(!isdigit(ch)) { f=ch=='-'; ch=getchar(); } while(isdigit(ch)) { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return f?-x:x; } int n; int a[maxn],b[maxn]; int p[maxn]; int cnt; int val; int main() { n=qr(); for(int i=1;i<=n;i++) { a[i]=qr(); } for(int i=1;i<=n;i++) { b[i]=qr(); } reverse(b+1,b+n+1); for(int i=1;i<=n;i++) { if(b[i]!=a[i])continue; cnt++; p[cnt]=i;//相等的位置; val=b[i]; } for(int i=1;i<=n;i++) { if(!cnt) break; if(a[i]!=val&&b[i]!=val)//可以交换 swap(b[p[cnt--]],b[i]); } if(cnt) //位置不够 { printf("No");return 0; } else { printf("Yes\n"); for(int i=1;i<=n;i++) { printf("%d ",b[i]); } } return 0; } ```
#include <algorithm> #include <cstdint> #include <iostream> #include <vector> using namespace std; int main() { int64_t N; cin >> N; vector<int64_t> As(N+1); vector<int64_t> Bs(N); vector<int64_t> Ds(N+1); for (int64_t n = 0; n < N + N; ++n) { int64_t i; cin >> i; if (n < N) { ++As[i]; } else { Bs[n % N] = i; ++Ds[i]; } } for (int64_t n = 1; n <= N; ++n) { if (As[n] + Ds[n] > N) { cout << "No" << endl; return 0; } As[n] += As[n-1]; Ds[n] += Ds[n-1]; } cout << "Yes" << endl; int64_t mx = 0; for (int64_t n = 1; n <= N; ++n) { mx = max(mx, As[n] - Ds[n-1]); } for (int64_t n = 0; n < N; ++n) { if (n) cout << " "; cout << Bs[(n + N - mx) % N]; } cout << endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <algorithm> #include <cstdint> #include <iostream> #include <vector> using namespace std; int main() { int64_t N; cin >> N; vector<int64_t> As(N+1); vector<int64_t> Bs(N); vector<int64_t> Ds(N+1); for (int64_t n = 0; n < N + N; ++n) { int64_t i; cin >> i; if (n < N) { ++As[i]; } else { Bs[n % N] = i; ++Ds[i]; } } for (int64_t n = 1; n <= N; ++n) { if (As[n] + Ds[n] > N) { cout << "No" << endl; return 0; } As[n] += As[n-1]; Ds[n] += Ds[n-1]; } cout << "Yes" << endl; int64_t mx = 0; for (int64_t n = 1; n <= N; ++n) { mx = max(mx, As[n] - Ds[n-1]); } for (int64_t n = 0; n < N; ++n) { if (n) cout << " "; cout << Bs[(n + N - mx) % N]; } cout << endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; int a[N],b[N]; int main(){ int n,si=0,l=N,r,w=0;scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",a+i); for(int i=n;i;i--){ scanf("%d",b+i); if(b[i]==a[i])si++,l=i; } r=l+si-1; for(int i=1;i<=n;i++)if(b[i]!=a[r]&&a[i]!=a[r])w++; if(w<si)return puts("No"),0; puts("Yes"); for(int i=1;i<=n;i++){ if(l>r)break; if(b[i]!=a[r]&&a[i]!=a[r])swap(b[i],b[l++]); } for(int i=1;i<=n;i++)printf("%d ",b[i]); return 0; }
### Prompt In Cpp, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int N=2e5+5; int a[N],b[N]; int main(){ int n,si=0,l=N,r,w=0;scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",a+i); for(int i=n;i;i--){ scanf("%d",b+i); if(b[i]==a[i])si++,l=i; } r=l+si-1; for(int i=1;i<=n;i++)if(b[i]!=a[r]&&a[i]!=a[r])w++; if(w<si)return puts("No"),0; puts("Yes"); for(int i=1;i<=n;i++){ if(l>r)break; if(b[i]!=a[r]&&a[i]!=a[r])swap(b[i],b[l++]); } for(int i=1;i<=n;i++)printf("%d ",b[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define REP(i,n) for(int i=1;i<=n;i++) typedef long long ll; int main(){ int n; cin >> n; vector<int> a(n); vector<int> b(n); vector<int> acount(n+1); vector<int> bcount(n+1); rep(i,n){ cin >> a[i]; acount[a[i]]++; } rep(i,n){ cin >> b[i]; bcount[b[i]]++; } REP(i,n){ if(acount[i] + bcount[i] > n){ cout << "No" << endl; return 0; } } sort(b.rbegin(),b.rend()); queue<int> que; rep(i,n){ if(a[i] == b[i]) que.push(i); } srand((unsigned int)time(NULL)); while(!que.empty()){ int p = que.front(); que.pop(); int idx = rand() % n; swap(b[p], b[idx]); if(b[p] == a[p]) que.push(p); if(b[idx] == a[idx]) que.push(idx); } cout << "Yes" << endl; rep(i,n) cout << b[i] << " "; cout << endl; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define REP(i,n) for(int i=1;i<=n;i++) typedef long long ll; int main(){ int n; cin >> n; vector<int> a(n); vector<int> b(n); vector<int> acount(n+1); vector<int> bcount(n+1); rep(i,n){ cin >> a[i]; acount[a[i]]++; } rep(i,n){ cin >> b[i]; bcount[b[i]]++; } REP(i,n){ if(acount[i] + bcount[i] > n){ cout << "No" << endl; return 0; } } sort(b.rbegin(),b.rend()); queue<int> que; rep(i,n){ if(a[i] == b[i]) que.push(i); } srand((unsigned int)time(NULL)); while(!que.empty()){ int p = que.front(); que.pop(); int idx = rand() % n; swap(b[p], b[idx]); if(b[p] == a[p]) que.push(p); if(b[idx] == a[idx]) que.push(idx); } cout << "Yes" << endl; rep(i,n) cout << b[i] << " "; cout << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int64_t MOD=1000000007; const long long INF = 1LL<<60; void YN(bool x){ if(x) cout<<"Yes"<<endl; else cout<<"No"<<endl; } int main() { bool finish=true; int N; cin>>N; vector<int> A(N),B(N),C(N,0),D(N,0); rep(i,N){ cin>>A.at(i); C.at(A.at(i)-1)++; } rep(i,N){ cin>>B.at(i); D.at(B.at(i)-1)++; } rep(i,N){ if(C.at(i)+D.at(i)>N) finish=false; } YN(finish); if(finish){ reverse(B.begin(),B.end()); int k=0,a=1; rep(i,N){ if(A.at(i)==B.at(i)){ if(A.at(i)==B.at(k)||A.at(i)==A.at(k)){ k=N-1; a*=-1; } swap(B.at(i),B.at(k)); k+=a; } } rep(i,N){ if(i==N-1) cout<<B.at(i)<<endl; else cout<<B.at(i)<<' '; } } }
### Prompt Develop a solution in cpp to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int64_t MOD=1000000007; const long long INF = 1LL<<60; void YN(bool x){ if(x) cout<<"Yes"<<endl; else cout<<"No"<<endl; } int main() { bool finish=true; int N; cin>>N; vector<int> A(N),B(N),C(N,0),D(N,0); rep(i,N){ cin>>A.at(i); C.at(A.at(i)-1)++; } rep(i,N){ cin>>B.at(i); D.at(B.at(i)-1)++; } rep(i,N){ if(C.at(i)+D.at(i)>N) finish=false; } YN(finish); if(finish){ reverse(B.begin(),B.end()); int k=0,a=1; rep(i,N){ if(A.at(i)==B.at(i)){ if(A.at(i)==B.at(k)||A.at(i)==A.at(k)){ k=N-1; a*=-1; } swap(B.at(i),B.at(k)); k+=a; } } rep(i,N){ if(i==N-1) cout<<B.at(i)<<endl; else cout<<B.at(i)<<' '; } } } ```
#include<bits/stdc++.h> using namespace std; #define Maxn 200010 int n, a[Maxn], b[Maxn], c[Maxn], P[Maxn]; set<int >S; int main() { scanf("%d", &n); for (int i=1; i<=n; i++) { scanf("%d", &a[i]); P[a[i]]++; S.insert(a[i]); } for (int i=1; i<=n; i++) { scanf("%d", &b[i]); P[b[i]]++; S.insert(b[i]); } bool flag = 0; for (auto p:S) if (P[p]>n) { flag = 1; break; } if (flag) puts("No"); else { puts("Yes"); for (int i=1; i<=n; i++) c[i]=b[n-i+1]; int cur = n, top = 1; for (int i=1; i<=n; i++) while ((a[i]==c[i]) && (cur >= top)) if(c[cur] != c[i] && a[cur] != c[i]){ swap(c[i], c[cur]);} else if(c[top] != c[i] && a[top] != c[i]) {swap(c[i], c[top]);} else cur--, top++; for (int i=1; i<=n; i++) printf("%d ", c[i]); puts(""); } }
### Prompt Please create a solution in CPP to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define Maxn 200010 int n, a[Maxn], b[Maxn], c[Maxn], P[Maxn]; set<int >S; int main() { scanf("%d", &n); for (int i=1; i<=n; i++) { scanf("%d", &a[i]); P[a[i]]++; S.insert(a[i]); } for (int i=1; i<=n; i++) { scanf("%d", &b[i]); P[b[i]]++; S.insert(b[i]); } bool flag = 0; for (auto p:S) if (P[p]>n) { flag = 1; break; } if (flag) puts("No"); else { puts("Yes"); for (int i=1; i<=n; i++) c[i]=b[n-i+1]; int cur = n, top = 1; for (int i=1; i<=n; i++) while ((a[i]==c[i]) && (cur >= top)) if(c[cur] != c[i] && a[cur] != c[i]){ swap(c[i], c[cur]);} else if(c[top] != c[i] && a[top] != c[i]) {swap(c[i], c[top]);} else cur--, top++; for (int i=1; i<=n; i++) printf("%d ", c[i]); puts(""); } } ```
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; int n,k,a[N],f[N],b[N],F,le,ri,l,r; int main(){ cin>>n; for(k=1;k<=n;k++){ cin>>a[k]; f[a[k]]++; } for(k=1;k<=n;k++){ cin>>b[k]; f[b[k]]++; if(f[b[k]]>n){ F=1; } } if(F) cout<<"No"<<endl; else { reverse(b+1,b+n+1); for(k=1;k<=n;k++){ if(a[k]==b[k])F=a[k]; } if(F){ l=n; for(k=1;k<=n;k++){ if(a[k]==F) l=min(l,k); if(b[k]==F) l=min(l,k); }le=1; ri=n; for(k=1;k<=n;k++){ if(b[k]==F && a[k]==F){ if(le<l){ swap(b[k],b[le]);le++; } else { swap(b[k],b[ri]); ri--; } } } } cout<<"Yes"<<endl; for(k=1;k<=n;k++){ cout<<b[k]<<" "; } } }
### Prompt Please create a solution in Cpp to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int N=2e5+5; int n,k,a[N],f[N],b[N],F,le,ri,l,r; int main(){ cin>>n; for(k=1;k<=n;k++){ cin>>a[k]; f[a[k]]++; } for(k=1;k<=n;k++){ cin>>b[k]; f[b[k]]++; if(f[b[k]]>n){ F=1; } } if(F) cout<<"No"<<endl; else { reverse(b+1,b+n+1); for(k=1;k<=n;k++){ if(a[k]==b[k])F=a[k]; } if(F){ l=n; for(k=1;k<=n;k++){ if(a[k]==F) l=min(l,k); if(b[k]==F) l=min(l,k); }le=1; ri=n; for(k=1;k<=n;k++){ if(b[k]==F && a[k]==F){ if(le<l){ swap(b[k],b[le]);le++; } else { swap(b[k],b[ri]); ri--; } } } } cout<<"Yes"<<endl; for(k=1;k<=n;k++){ cout<<b[k]<<" "; } } } ```
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int N = 200010; int n, a[N], b[N], cnt, tot; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[n - i + 1]; int l = 1, r = n, st = 0, ed = -1; for (int i = 1; i <= n; i++) if (a[i] == b[i]) { if (!st) st = i; ed = i; } for (int j = st; j <= ed; j++) { if (a[l] ^ b[j] && b[l] ^ b[j]) swap(b[j], b[l++]); else if (a[r] ^ b[j] && b[r] ^ b[j]) swap(b[j], b[r--]); else puts("No"), exit(0); } puts("Yes"); for (int i = 1; i <= n; i++) printf("%d%c", b[i], " \n"[i == n]); return 0; }
### Prompt In CPP, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int N = 200010; int n, a[N], b[N], cnt, tot; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[n - i + 1]; int l = 1, r = n, st = 0, ed = -1; for (int i = 1; i <= n; i++) if (a[i] == b[i]) { if (!st) st = i; ed = i; } for (int j = st; j <= ed; j++) { if (a[l] ^ b[j] && b[l] ^ b[j]) swap(b[j], b[l++]); else if (a[r] ^ b[j] && b[r] ^ b[j]) swap(b[j], b[r--]); else puts("No"), exit(0); } puts("Yes"); for (int i = 1; i <= n; i++) printf("%d%c", b[i], " \n"[i == n]); return 0; } ```
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define N 200010 int n,a[N],b[N],c[N],d[N]; const int INF = 1000000000; int main(){ cin>>n; rep(i,n)cin>>a[i]; rep(i,n)cin>>b[i]; rep(i,n+1)c[i]=d[i]=0; rep(i,n)c[a[i]]++,d[b[i]]++; rep(i,n+1){ if(c[i]+d[i]>n){ cout<<"No\n"; return 0; } } for(int i=1;i<=n;i++){ c[i]+=c[i-1]; d[i]+=d[i-1]; } int x=INF; for(int i=1;i<=n;i++)x=min(x,c[i-1]+n-d[i]); cout<<"Yes\n"; rep(i,n){ if(i)cout<<" "; cout<<b[(i+n-x)%n]; } cout<<"\n"; }
### Prompt Please formulate a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define N 200010 int n,a[N],b[N],c[N],d[N]; const int INF = 1000000000; int main(){ cin>>n; rep(i,n)cin>>a[i]; rep(i,n)cin>>b[i]; rep(i,n+1)c[i]=d[i]=0; rep(i,n)c[a[i]]++,d[b[i]]++; rep(i,n+1){ if(c[i]+d[i]>n){ cout<<"No\n"; return 0; } } for(int i=1;i<=n;i++){ c[i]+=c[i-1]; d[i]+=d[i-1]; } int x=INF; for(int i=1;i<=n;i++)x=min(x,c[i-1]+n-d[i]); cout<<"Yes\n"; rep(i,n){ if(i)cout<<" "; cout<<b[(i+n-x)%n]; } cout<<"\n"; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n; int A[maxn], B[maxn]; int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &A[i]); for(int i = n; i; --i) scanf("%d", &B[i]); int j = 1; for(int i = 1; i <= n; ++i) if(A[i] == B[i]) { bool f = 0; for(; j <= n; ++j) if(B[j] != A[i] && B[i] != A[j]) { swap(B[i], B[j]); f = 1; break; } if(!f) { puts("No"); return 0; } } puts("Yes"); for(int i = 1; i <= n; ++i) printf("%d ", B[i]); return 0; }
### Prompt In CPP, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n; int A[maxn], B[maxn]; int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &A[i]); for(int i = n; i; --i) scanf("%d", &B[i]); int j = 1; for(int i = 1; i <= n; ++i) if(A[i] == B[i]) { bool f = 0; for(; j <= n; ++j) if(B[j] != A[i] && B[i] != A[j]) { swap(B[i], B[j]); f = 1; break; } if(!f) { puts("No"); return 0; } } puts("Yes"); for(int i = 1; i <= n; ++i) printf("%d ", B[i]); return 0; } ```
#include <algorithm> #include <iostream> using namespace std; const int maxn = 2e5 + 5; int a[maxn], b[maxn]; int main(void) { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; int j = 0, last = -1; for (int i = 0; i < n; ++i) { if(last!=a[i]) j = 0; if (a[i] == b[i]) { for (; j < n; ++j) { if (a[j] != a[i] && b[j] != a[i]) { swap(b[i], b[j]); break; } } if (a[i] == b[i]) { cout << "No" << endl; return 0; } } last = a[i]; } cout << "Yes" << endl; for (int i = 0; i < n; ++i) cout << b[i] << " "; cout << endl; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <algorithm> #include <iostream> using namespace std; const int maxn = 2e5 + 5; int a[maxn], b[maxn]; int main(void) { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; int j = 0, last = -1; for (int i = 0; i < n; ++i) { if(last!=a[i]) j = 0; if (a[i] == b[i]) { for (; j < n; ++j) { if (a[j] != a[i] && b[j] != a[i]) { swap(b[i], b[j]); break; } } if (a[i] == b[i]) { cout << "No" << endl; return 0; } } last = a[i]; } cout << "Yes" << endl; for (int i = 0; i < n; ++i) cout << b[i] << " "; cout << endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+5; int a[maxn],b[maxn]; int main(){ int n; vector<int>p; cin>>n; for(int i=1;i<=n;++i)scanf("%d",&a[i]); for(int i=1;i<=n;++i)scanf("%d",&b[i]); reverse(b+1,b+n+1); int h1=n,q1=1,mx; for(int i=1;i<=n;++i){ if(a[i]==b[i]){ mx=a[i]; h1=i; while(a[i]==b[i]){ i++; } q1=i-1; break; } } for(int i=1;i<=n;++i){ if(a[i]!=mx&&b[i]!=mx&&h1<=q1){ swap(b[i],b[h1]); h1++; } } if(h1<=q1){ puts("No");return 0;} puts("Yes"); for(int i=1;i<n;++i) printf("%d ",b[i]); printf("%d\n",b[n]); }
### Prompt Please create a solution in CPP to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+5; int a[maxn],b[maxn]; int main(){ int n; vector<int>p; cin>>n; for(int i=1;i<=n;++i)scanf("%d",&a[i]); for(int i=1;i<=n;++i)scanf("%d",&b[i]); reverse(b+1,b+n+1); int h1=n,q1=1,mx; for(int i=1;i<=n;++i){ if(a[i]==b[i]){ mx=a[i]; h1=i; while(a[i]==b[i]){ i++; } q1=i-1; break; } } for(int i=1;i<=n;++i){ if(a[i]!=mx&&b[i]!=mx&&h1<=q1){ swap(b[i],b[h1]); h1++; } } if(h1<=q1){ puts("No");return 0;} puts("Yes"); for(int i=1;i<n;++i) printf("%d ",b[i]); printf("%d\n",b[n]); } ```
#include<bits/stdc++.h> using namespace std; #define ll long long int main(){ int n,i,j,l,r,x; cin >> n; vector<int> a(n),b(n),ca(n,0),cb(n,0); for(i=0;i<n;i++){ cin >> a.at(i); ca.at(a.at(i)-1)++; } for(i=0;i<n;i++){ cin >> b.at(i); cb.at(b.at(i)-1)++; } for(i=0;i<n;i++){ if(ca.at(i)+cb.at(i)>n){ cout << "No" << endl; return 0; } } i=0; j=0; while(i<n){ if(a.at(i)==b.at(i)){ l=i; x=a.at(l); while(i<n&&a.at(i)==x&&b.at(i)==x){ i++; } r=i; j=0; for(i=l;i<r;i++){ while(a.at(j)==x||b.at(j)==x){ j++; } swap(b.at(i),b.at(j)); } i=r; }else{ i++; } } cout << "Yes" << endl; for(i=0;i<n;i++){ cout << b.at(i); if(i==n-1){ cout << endl; }else{ cout << " "; } } }
### Prompt Your task is to create a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define ll long long int main(){ int n,i,j,l,r,x; cin >> n; vector<int> a(n),b(n),ca(n,0),cb(n,0); for(i=0;i<n;i++){ cin >> a.at(i); ca.at(a.at(i)-1)++; } for(i=0;i<n;i++){ cin >> b.at(i); cb.at(b.at(i)-1)++; } for(i=0;i<n;i++){ if(ca.at(i)+cb.at(i)>n){ cout << "No" << endl; return 0; } } i=0; j=0; while(i<n){ if(a.at(i)==b.at(i)){ l=i; x=a.at(l); while(i<n&&a.at(i)==x&&b.at(i)==x){ i++; } r=i; j=0; for(i=l;i<r;i++){ while(a.at(j)==x||b.at(j)==x){ j++; } swap(b.at(i),b.at(j)); } i=r; }else{ i++; } } cout << "Yes" << endl; for(i=0;i<n;i++){ cout << b.at(i); if(i==n-1){ cout << endl; }else{ cout << " "; } } } ```
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int64_t MOD=1000000007; const long long INF = 1LL<<60; void YN(bool x){ if(x) cout<<"Yes"<<endl; else cout<<"No"<<endl; } int main() { bool finish=true; int N; cin>>N; vector<int> A(N),B(N),C(N,0),D(N,0); rep(i,N){ cin>>A.at(i); C.at(A.at(i)-1)++; } rep(i,N){ cin>>B.at(i); D.at(B.at(i)-1)++; } rep(i,N){ if(C.at(i)+D.at(i)>N) finish=false; } YN(finish); if(finish){ reverse(B.begin(),B.end()); int k=0; rep(i,N){ if(A.at(i)==B.at(i)){ k=0; while(A.at(i)==B.at(k)||A.at(i)==A.at(k)){ k++; if(k==N) k=0; } swap(B.at(i),B.at(k)); } } rep(i,N){ if(i==N-1) cout<<B.at(i)<<endl; else cout<<B.at(i)<<' '; } } }
### Prompt Please provide a cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int64_t MOD=1000000007; const long long INF = 1LL<<60; void YN(bool x){ if(x) cout<<"Yes"<<endl; else cout<<"No"<<endl; } int main() { bool finish=true; int N; cin>>N; vector<int> A(N),B(N),C(N,0),D(N,0); rep(i,N){ cin>>A.at(i); C.at(A.at(i)-1)++; } rep(i,N){ cin>>B.at(i); D.at(B.at(i)-1)++; } rep(i,N){ if(C.at(i)+D.at(i)>N) finish=false; } YN(finish); if(finish){ reverse(B.begin(),B.end()); int k=0; rep(i,N){ if(A.at(i)==B.at(i)){ k=0; while(A.at(i)==B.at(k)||A.at(i)==A.at(k)){ k++; if(k==N) k=0; } swap(B.at(i),B.at(k)); } } rep(i,N){ if(i==N-1) cout<<B.at(i)<<endl; else cout<<B.at(i)<<' '; } } } ```
#include <bits/stdc++.h> int main() { int N; scanf("%d", &N); std::vector<int> A(N), B(N); std::vector<std::pair<int, int>> num(N + 1); for (int i{}; i <= N; i++) num[i].second = i; num.reserve(2 * N); for (auto& e: A) { scanf("%d", &e); num[e].first++; } for (auto& e: B) { scanf("%d", &e); num[e].first++; } std::sort(num.rbegin(), num.rend()); if (num.front().first > N) { puts("No"); return 0; } puts("Yes"); std::reverse(B.begin(), B.end()); int change{}; for (int i{}; i < N; i++) { if (A[i] != B[i]) continue; while (A[change] == A[i] || B[change] == A[i]) change++; std::swap(B[i], B[change]); change++; } for (int i{}; i < N; i++) printf("%d%c", B[i], i + 1 == N? '\n': ' '); return 0; }
### Prompt Please formulate a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> int main() { int N; scanf("%d", &N); std::vector<int> A(N), B(N); std::vector<std::pair<int, int>> num(N + 1); for (int i{}; i <= N; i++) num[i].second = i; num.reserve(2 * N); for (auto& e: A) { scanf("%d", &e); num[e].first++; } for (auto& e: B) { scanf("%d", &e); num[e].first++; } std::sort(num.rbegin(), num.rend()); if (num.front().first > N) { puts("No"); return 0; } puts("Yes"); std::reverse(B.begin(), B.end()); int change{}; for (int i{}; i < N; i++) { if (A[i] != B[i]) continue; while (A[change] == A[i] || B[change] == A[i]) change++; std::swap(B[i], B[change]); change++; } for (int i{}; i < N; i++) printf("%d%c", B[i], i + 1 == N? '\n': ' '); return 0; } ```
#include <iostream> #include <vector> #include <algorithm> bool check (std::vector<int> a, std::vector<int> b) { bool ret = true; for (std::size_t i = 0; i < a.size(); i++) ret &= (a[i] != b[i]); return ret; } void answer (bool ans, std::vector<int> a) { std::cout << (ans ? "Yes" : "No") << '\n'; if (ans) for (int e : a) std::cout << e << '\n'; } int main() { int n; std::cin >> n; std::vector<int> a(n), b(n), c(n); for (int i = 0; i < n; i++) std::cin >> a[i]; for (int i = 0; i < n; i++) { std::cin >> b[i]; c[i] = b[i]; } std::reverse(b.begin(), b.end()); std::rotate(c.begin(), c.begin() + (n / 2), c.end()); if (check(a, b)) answer(true, b); else if (check(a, c)) answer(true, c); else answer(false, a); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> bool check (std::vector<int> a, std::vector<int> b) { bool ret = true; for (std::size_t i = 0; i < a.size(); i++) ret &= (a[i] != b[i]); return ret; } void answer (bool ans, std::vector<int> a) { std::cout << (ans ? "Yes" : "No") << '\n'; if (ans) for (int e : a) std::cout << e << '\n'; } int main() { int n; std::cin >> n; std::vector<int> a(n), b(n), c(n); for (int i = 0; i < n; i++) std::cin >> a[i]; for (int i = 0; i < n; i++) { std::cin >> b[i]; c[i] = b[i]; } std::reverse(b.begin(), b.end()); std::rotate(c.begin(), c.begin() + (n / 2), c.end()); if (check(a, b)) answer(true, b); else if (check(a, c)) answer(true, c); else answer(false, a); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n; int A[maxn], B[maxn]; int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &A[i]); for(int i = n; i; --i) scanf("%d", &B[i]); for(int i = 1; i <= n; ++i) if(A[i] == B[i]) { bool f = 0; for(int j = 1; j <= n; ++j) if(B[j] != A[i] && B[i] != A[j]) { swap(B[i], B[j]); f = 1; break; } if(!f) { puts("No"); return 0; } } puts("Yes"); for(int i = 1; i <= n; ++i) printf("%d ", B[i]); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n; int A[maxn], B[maxn]; int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", &A[i]); for(int i = n; i; --i) scanf("%d", &B[i]); for(int i = 1; i <= n; ++i) if(A[i] == B[i]) { bool f = 0; for(int j = 1; j <= n; ++j) if(B[j] != A[i] && B[i] != A[j]) { swap(B[i], B[j]); f = 1; break; } if(!f) { puts("No"); return 0; } } puts("Yes"); for(int i = 1; i <= n; ++i) printf("%d ", B[i]); return 0; } ```
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); i++) #define sz(x) int(x.size()) using namespace std; typedef long long ll; const int inf = 1001001001; int main(){ int n; cin >> n; vector<int> a(n), b(n), aa(n+1,0), bb(n+1,0); rep(i,n) { cin >> a[i]; aa[a[i]]++; } rep(i,n) { cin >> b[i]; bb[b[i]]++; } rep(i,n){ if(bb[i+1]+aa[i+1] > n){ cout << "No" << endl; return 0; } } cout << "Yes" << endl; rep(i,n){ aa[i+1] += aa[i]; bb[i+1] += bb[i]; } int x = 0; rep(i,n){ x = max(x,aa[i+1]-bb[i]); } rep(i,n){ cout << b[(i-x+n)%n] << " "; } cout << endl; }
### Prompt Develop a solution in CPP to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); i++) #define sz(x) int(x.size()) using namespace std; typedef long long ll; const int inf = 1001001001; int main(){ int n; cin >> n; vector<int> a(n), b(n), aa(n+1,0), bb(n+1,0); rep(i,n) { cin >> a[i]; aa[a[i]]++; } rep(i,n) { cin >> b[i]; bb[b[i]]++; } rep(i,n){ if(bb[i+1]+aa[i+1] > n){ cout << "No" << endl; return 0; } } cout << "Yes" << endl; rep(i,n){ aa[i+1] += aa[i]; bb[i+1] += bb[i]; } int x = 0; rep(i,n){ x = max(x,aa[i+1]-bb[i]); } rep(i,n){ cout << b[(i-x+n)%n] << " "; } cout << endl; } ```
#include<bits/stdc++.h> using namespace std; const int maxn=200010; int n,c=-1,l=0,r=-1,a[maxn]={},b[maxn]={},h[maxn]={}; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++)scanf("%d",&b[i]),h[n-i+1]=b[i]; for(int i=1;i<=n;i++)b[i]=h[i]; for(int i=1;i<=n;i++)if(a[i]==b[i]){c=a[i];break;} for(int i=1;i<=n;i++)if(a[i]==c&&b[i]==c){l=i;break;} for(int i=n;i>=1;i--)if(a[i]==c&&b[i]==c){r=i;break;} for(int i=1;i<=n;i++)if(a[i]!=c&&b[i]!=c&&l<=r)swap(b[l],b[i]),l++; if(l<=r)printf("No"); else{printf("Yes\n");for(int i=1;i<=n;i++)printf("%d ",b[i]);} return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int maxn=200010; int n,c=-1,l=0,r=-1,a[maxn]={},b[maxn]={},h[maxn]={}; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++)scanf("%d",&b[i]),h[n-i+1]=b[i]; for(int i=1;i<=n;i++)b[i]=h[i]; for(int i=1;i<=n;i++)if(a[i]==b[i]){c=a[i];break;} for(int i=1;i<=n;i++)if(a[i]==c&&b[i]==c){l=i;break;} for(int i=n;i>=1;i--)if(a[i]==c&&b[i]==c){r=i;break;} for(int i=1;i<=n;i++)if(a[i]!=c&&b[i]!=c&&l<=r)swap(b[l],b[i]),l++; if(l<=r)printf("No"); else{printf("Yes\n");for(int i=1;i<=n;i++)printf("%d ",b[i]);} return 0; } ```
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); ++i) using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> cnt(n + 1), a(n), b(n); rep(i,n) { cin >> a[i]; cnt[a[i]]++; } rep(i,n) { cin >> b[i]; cnt[b[i]]++; } rep(i, n) { if (cnt[i + 1] > n) { cout << "No" << endl; return 0; } } int j = 0; rep(i, n) { if (a[i] == b[i]) { for (; j < n; j++) { if (a[j] != a[i] && b[j] != a[i]) { swap(b[i], b[j]); break; } } } if (i != n - 1) { if (a[i] != a[i + 1]) j = 0; } } cout << "Yes" << endl; rep (i, n) cout << b[i] << ' '; return 0; }
### Prompt Please create a solution in cpp to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (int)(n); ++i) using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> cnt(n + 1), a(n), b(n); rep(i,n) { cin >> a[i]; cnt[a[i]]++; } rep(i,n) { cin >> b[i]; cnt[b[i]]++; } rep(i, n) { if (cnt[i + 1] > n) { cout << "No" << endl; return 0; } } int j = 0; rep(i, n) { if (a[i] == b[i]) { for (; j < n; j++) { if (a[j] != a[i] && b[j] != a[i]) { swap(b[i], b[j]); break; } } } if (i != n - 1) { if (a[i] != a[i + 1]) j = 0; } } cout << "Yes" << endl; rep (i, n) cout << b[i] << ' '; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 2e5; int n, a[N], b[N], t, l, r; inline void solve() { for (register int i = 0; i < n; i++) if (a[i] == b[i]) { l = i; t = a[i]; break; } for (register int i = n-1; i >= 0; i--) if (a[i] == t && b[i] == t) { r = i; break; } for (register int i = 0; i < n; i++) if (a[i] != t && b[i] != t && l <= r) swap(b[i], b[l++]); if (l <= r) { puts("No"); return; } puts("Yes"); for (register int i = 0; i < n; i++) printf("%d ", b[i]); puts(""); } int main() { scanf("%d", &n); for (register int i = 0; i < n; i++) scanf("%d", a+i); for (register int i = n-1; i >= 0; i--) scanf("%d", b+i); solve(); return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 2e5; int n, a[N], b[N], t, l, r; inline void solve() { for (register int i = 0; i < n; i++) if (a[i] == b[i]) { l = i; t = a[i]; break; } for (register int i = n-1; i >= 0; i--) if (a[i] == t && b[i] == t) { r = i; break; } for (register int i = 0; i < n; i++) if (a[i] != t && b[i] != t && l <= r) swap(b[i], b[l++]); if (l <= r) { puts("No"); return; } puts("Yes"); for (register int i = 0; i < n; i++) printf("%d ", b[i]); puts(""); } int main() { scanf("%d", &n); for (register int i = 0; i < n; i++) scanf("%d", a+i); for (register int i = n-1; i >= 0; i--) scanf("%d", b+i); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; typedef pair<int, int> P; const int INF = 1001001001; const int MOD = 1e9 + 7; int main() { int n; cin >> n; vector<int> a(n); vector<int> b(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; reverse(b.begin(), b.end()); int lhr = 0; int rhr = b.size() - 1; rep(i, b.size()) { if (a[i] == b[i]) { if (a[lhr] != b[i] && b[lhr] != b[i]) { int t = b[lhr]; b[lhr] = b[i]; b[i] = t; lhr++; } else if (a[rhr] != b[i] && b[rhr] != b[i]) { int t = b[rhr]; b[rhr] = b[i]; b[i] = t; rhr--; } else { cout << "No" << endl; return 0; } } } cout << "Yes" << endl; rep(i, n - 1) cout << b[i] << " "; cout << b[n - 1] << endl; return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; typedef pair<int, int> P; const int INF = 1001001001; const int MOD = 1e9 + 7; int main() { int n; cin >> n; vector<int> a(n); vector<int> b(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; reverse(b.begin(), b.end()); int lhr = 0; int rhr = b.size() - 1; rep(i, b.size()) { if (a[i] == b[i]) { if (a[lhr] != b[i] && b[lhr] != b[i]) { int t = b[lhr]; b[lhr] = b[i]; b[i] = t; lhr++; } else if (a[rhr] != b[i] && b[rhr] != b[i]) { int t = b[rhr]; b[rhr] = b[i]; b[i] = t; rhr--; } else { cout << "No" << endl; return 0; } } } cout << "Yes" << endl; rep(i, n - 1) cout << b[i] << " "; cout << b[n - 1] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; reverse(b.begin(), b.end()); int start = -1, fin = -1; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { if (start == -1) start = i; fin = i; } } bool bad = 0; if (start != -1) { for (int i = 0, j = start; j <= fin; ++i) { if (i == start) i = fin + 1; if (i >= n) { bad = 1; break; } if (a[i] != b[j] && b[j] != b[i]) { swap(b[i], b[j]); j++; } } } for (int i = 0; i < n; ++i) { if (a[i] == b[i]) bad = 1; } if (bad) { cout << "No" << '\n'; } else { cout << "Yes" << '\n'; for (int i = 0; i < n; ++i) { if (i) cout << " "; cout << b[i]; } cout << '\n'; } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; reverse(b.begin(), b.end()); int start = -1, fin = -1; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { if (start == -1) start = i; fin = i; } } bool bad = 0; if (start != -1) { for (int i = 0, j = start; j <= fin; ++i) { if (i == start) i = fin + 1; if (i >= n) { bad = 1; break; } if (a[i] != b[j] && b[j] != b[i]) { swap(b[i], b[j]); j++; } } } for (int i = 0; i < n; ++i) { if (a[i] == b[i]) bad = 1; } if (bad) { cout << "No" << '\n'; } else { cout << "Yes" << '\n'; for (int i = 0; i < n; ++i) { if (i) cout << " "; cout << b[i]; } cout << '\n'; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N; cin >> N; vector<ll> a(N), b(N); for(ll i=0; i<N; i++) cin >> a[i]; for(ll i=0; i<N; i++) cin >> b[i]; map<ll,ll> am,bm; for(ll i=0; i<N; i++){ am[a[i]]++; bm[b[i]]++; } for(auto&& pr:am){ if(pr.second + bm[pr.first]> N){ cout << "No\n"; return 0; } } reverse(b.begin(), b.end()); ll index=0; for(ll i=0; i<N; i++){ if(a[i]==b[i]){ while(a[index]==a[i] || b[index]==a[i])index++; swap(b[index],b[i]); index++; } } cout << "Yes\n"; for(ll i=0; i<N; i++) cout << b[i] << " \n"[i==N-1]; return 0; }
### Prompt Create a solution in Cpp for the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N; cin >> N; vector<ll> a(N), b(N); for(ll i=0; i<N; i++) cin >> a[i]; for(ll i=0; i<N; i++) cin >> b[i]; map<ll,ll> am,bm; for(ll i=0; i<N; i++){ am[a[i]]++; bm[b[i]]++; } for(auto&& pr:am){ if(pr.second + bm[pr.first]> N){ cout << "No\n"; return 0; } } reverse(b.begin(), b.end()); ll index=0; for(ll i=0; i<N; i++){ if(a[i]==b[i]){ while(a[index]==a[i] || b[index]==a[i])index++; swap(b[index],b[i]); index++; } } cout << "Yes\n"; for(ll i=0; i<N; i++) cout << b[i] << " \n"[i==N-1]; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; vector<int> p(n); for (int i = 0; i < n; i++) { p[i] = upper_bound(b.begin(), b.end(), a[i]) - b.begin(); if (i - 1 >= 0) p[i] = max(p[i], p[i - 1] + 1); } for (int i = n - 2; i >= 0; i--) { p[i] = max(p[i], p[i + 1] - 1); } for (int i = 0; i < n; i++) { if (a[i] == b[p[i] % n]) { cout << "No" << "\n"; return 0; } } cout << "Yes" << "\n"; for (int i = 0; i < n; i++) { cout << b[p[i] % n] << " \n"[i == n - 1]; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; vector<int> p(n); for (int i = 0; i < n; i++) { p[i] = upper_bound(b.begin(), b.end(), a[i]) - b.begin(); if (i - 1 >= 0) p[i] = max(p[i], p[i - 1] + 1); } for (int i = n - 2; i >= 0; i--) { p[i] = max(p[i], p[i + 1] - 1); } for (int i = 0; i < n; i++) { if (a[i] == b[p[i] % n]) { cout << "No" << "\n"; return 0; } } cout << "Yes" << "\n"; for (int i = 0; i < n; i++) { cout << b[p[i] % n] << " \n"[i == n - 1]; } return 0; } ```
#include<bits/stdc++.h> #define gc() getchar() #define ri register int using std::max; inline int read() { int ret=0;char ch=gc(); while(!isdigit(ch))ch=gc(); while(isdigit(ch)) { ret=ret*10+ch-'0'; ch=gc(); } return ret; } const int MAXN=2e5+10; int n,d; int a[MAXN],b[MAXN],c[MAXN]; int l[MAXN],r[MAXN]; int main() { // freopen("in.in","r",stdin); n=read(); memset(r,-1,sizeof(r)); memset(l,-1,sizeof(l)); for(ri i=0;i<n;++i) { a[i]=read(); r[a[i]]=i; } for(ri i=0;i<n;++i) { b[i]=read(); if(l[b[i]]==-1)l[b[i]]=i; } for(ri i=1;i<=n;++i)if(l[i]!=-1 && r[i]!=-1) d=max(d,r[i]-l[i]+1); for(ri i=0;i<n;++i) { c[(i+d)%n]=b[i]; if(a[(i+d)%n]==c[(i+d)%n]) { printf("No\n"); return 0; } } printf("Yes\n"); for(ri i=0;i<n;++i)printf("%d ",c[i]); return 0; }
### Prompt Develop a solution in cpp to the problem described below: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> #define gc() getchar() #define ri register int using std::max; inline int read() { int ret=0;char ch=gc(); while(!isdigit(ch))ch=gc(); while(isdigit(ch)) { ret=ret*10+ch-'0'; ch=gc(); } return ret; } const int MAXN=2e5+10; int n,d; int a[MAXN],b[MAXN],c[MAXN]; int l[MAXN],r[MAXN]; int main() { // freopen("in.in","r",stdin); n=read(); memset(r,-1,sizeof(r)); memset(l,-1,sizeof(l)); for(ri i=0;i<n;++i) { a[i]=read(); r[a[i]]=i; } for(ri i=0;i<n;++i) { b[i]=read(); if(l[b[i]]==-1)l[b[i]]=i; } for(ri i=1;i<=n;++i)if(l[i]!=-1 && r[i]!=-1) d=max(d,r[i]-l[i]+1); for(ri i=0;i<n;++i) { c[(i+d)%n]=b[i]; if(a[(i+d)%n]==c[(i+d)%n]) { printf("No\n"); return 0; } } printf("Yes\n"); for(ri i=0;i<n;++i)printf("%d ",c[i]); return 0; } ```
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; //template #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} //end int main(){ int n; cin>>n; vector<int> a(n),b(n); rep(i,0,n)cin>>a[i]; rep(i,0,n)cin>>b[i]; int d=0; rep(i,0,n){ int idx=upper_bound(ALL(b),a[i])-b.begin(); chmax(d,idx-i); } rotate(b.begin(),b.begin()+d,b.end()); rep(i,0,n)if(a[i]==b[i]){ puts("No"); return 0; } puts("Yes"); rep(i,0,n)cout<<b[i]<<(i==n-1?'\n':' '); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; //template #define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;} template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} //end int main(){ int n; cin>>n; vector<int> a(n),b(n); rep(i,0,n)cin>>a[i]; rep(i,0,n)cin>>b[i]; int d=0; rep(i,0,n){ int idx=upper_bound(ALL(b),a[i])-b.begin(); chmax(d,idx-i); } rotate(b.begin(),b.begin()+d,b.end()); rep(i,0,n)if(a[i]==b[i]){ puts("No"); return 0; } puts("Yes"); rep(i,0,n)cout<<b[i]<<(i==n-1?'\n':' '); return 0; } ```
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main() { int n,flag=0; cin >> n; int a[n],b[n],ans[n]; rep(i,n) cin >> a[i]; rep(i,n) cin >> b[i]; rep(i,n){ ans[i]=b[n-1-i]; } int l=0,r=n-1; rep(i,n){ if(a[i]==ans[i]){ if(a[i]!=ans[l] && a[l]!=ans[i]){ swap(ans[i],ans[l]); l++; } else if(a[i]!=ans[r] && a[r]!=ans[i]){ swap(ans[i],ans[r]); r--; } else flag=1; } } if(flag==1) cout << "No" << endl; else { cout << "Yes" << endl; rep(i,n) cout << ans[i] << " "; cout << "" << endl; } }
### Prompt Your task is to create a Cpp solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main() { int n,flag=0; cin >> n; int a[n],b[n],ans[n]; rep(i,n) cin >> a[i]; rep(i,n) cin >> b[i]; rep(i,n){ ans[i]=b[n-1-i]; } int l=0,r=n-1; rep(i,n){ if(a[i]==ans[i]){ if(a[i]!=ans[l] && a[l]!=ans[i]){ swap(ans[i],ans[l]); l++; } else if(a[i]!=ans[r] && a[r]!=ans[i]){ swap(ans[i],ans[r]); r--; } else flag=1; } } if(flag==1) cout << "No" << endl; else { cout << "Yes" << endl; rep(i,n) cout << ans[i] << " "; cout << "" << endl; } } ```
#include<iostream> #include <bits/stdc++.h> #define ll long long #define ld long double #define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; const int N=2e5+2; int n, a[N], b[N]; vector<int> v; int main() { IO cin>>n; for(int i=0; i<n; i++) cin>>a[i]; for(int i=0; i<n; i++) cin>>b[i]; sort(b,b+n,greater<int>()); for(int i=0; i<n; i++) if(a[i]==b[i]) v.push_back(i); int j=0; for(auto i : v) { while(j<n && (a[j]==b[i] || b[j]==a[i])) j++; if(j==n) return cout<<"No", 0; swap(b[j],b[i]); } cout<<"Yes\n"; for(int i=0; i<n; i++) cout<<b[i]<<" "; }
### Prompt Construct a CPP code solution to the problem outlined: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<iostream> #include <bits/stdc++.h> #define ll long long #define ld long double #define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; const int N=2e5+2; int n, a[N], b[N]; vector<int> v; int main() { IO cin>>n; for(int i=0; i<n; i++) cin>>a[i]; for(int i=0; i<n; i++) cin>>b[i]; sort(b,b+n,greater<int>()); for(int i=0; i<n; i++) if(a[i]==b[i]) v.push_back(i); int j=0; for(auto i : v) { while(j<n && (a[j]==b[i] || b[j]==a[i])) j++; if(j==n) return cout<<"No", 0; swap(b[j],b[i]); } cout<<"Yes\n"; for(int i=0; i<n; i++) cout<<b[i]<<" "; } ```
#include<bits/stdc++.h> using namespace std; long long n, k=-1, j=1, a[200005], b[200005]; int main(){ std::ios::sync_with_stdio(false); cin>>n; for(long long i=1; i<=n; i++) cin>>a[i]; for(long long i=1; i<=n; i++) cin>>b[i]; for(long long i=1; i<=n; i++){ if(a[i]!=k) j=1; if(b[i]==a[i]){ for(; j<=n; j++) if(b[j]!=a[i] && a[i]!=a[j]){ swap(b[i], b[j]); break; } if(a[i]==b[i]){ cout<<"No"<<endl; return 0; } } k=a[i]; } cout<<"Yes"<<endl; for(long long i=1; i<=n; i++) cout<<b[i]<<" "; cout<<endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; long long n, k=-1, j=1, a[200005], b[200005]; int main(){ std::ios::sync_with_stdio(false); cin>>n; for(long long i=1; i<=n; i++) cin>>a[i]; for(long long i=1; i<=n; i++) cin>>b[i]; for(long long i=1; i<=n; i++){ if(a[i]!=k) j=1; if(b[i]==a[i]){ for(; j<=n; j++) if(b[j]!=a[i] && a[i]!=a[j]){ swap(b[i], b[j]); break; } if(a[i]==b[i]){ cout<<"No"<<endl; return 0; } } k=a[i]; } cout<<"Yes"<<endl; for(long long i=1; i<=n; i++) cout<<b[i]<<" "; cout<<endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; #define F(i,a,n) for(int i=a;i<n;i++) #define mod 1000000007 #define pb push_back int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int w=1; // cin>>w; while(w--){ ll n; cin>>n; vector<ll>v1(n),v2(n); F(i,0,n){ cin>>v1[i]; } F(i,0,n){ cin>>v2[i]; } vi v; for(int i=0,j=n-1;i<n;i++,j--){ if(v1[i]==v2[j]){v.pb(j);} } int x=0; for(auto i:v){ swap(v2[i],v2[x]); x++; } v.clear(); for(int i=0,j=n-1;i<n;i++,j--){ if(v1[i]==v2[j]){v.pb(j);} } x=n-1; for(auto i:v){ swap(v2[i],v2[x]); x--; } for(int i=0,j=n-1;i<n;i++,j--){ if(v1[i]==v2[j]){cout<<"No";return 0;} } cout<<"Yes\n"; for(int j=n-1;j>=0;j--){ cout<<v2[j]<<" "; } } return 0; }
### Prompt Create a solution in Cpp for the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; #define F(i,a,n) for(int i=a;i<n;i++) #define mod 1000000007 #define pb push_back int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int w=1; // cin>>w; while(w--){ ll n; cin>>n; vector<ll>v1(n),v2(n); F(i,0,n){ cin>>v1[i]; } F(i,0,n){ cin>>v2[i]; } vi v; for(int i=0,j=n-1;i<n;i++,j--){ if(v1[i]==v2[j]){v.pb(j);} } int x=0; for(auto i:v){ swap(v2[i],v2[x]); x++; } v.clear(); for(int i=0,j=n-1;i<n;i++,j--){ if(v1[i]==v2[j]){v.pb(j);} } x=n-1; for(auto i:v){ swap(v2[i],v2[x]); x--; } for(int i=0,j=n-1;i<n;i++,j--){ if(v1[i]==v2[j]){cout<<"No";return 0;} } cout<<"Yes\n"; for(int j=n-1;j>=0;j--){ cout<<v2[j]<<" "; } } return 0; } ```
#include<bits/stdc++.h> using namespace std; const int N=2e5+10; int a[N],b[N],n,val,cnt; vector<int> p,q; inline int read() { int x=0,f=1; char c=getchar(); while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); } while(c<='9'&&c>='0') { x=x*10+c-'0'; c=getchar(); } return x*f; } int main() { n=read(); for(register int i=1;i<=n;i++) a[i]=read(); for(register int i=1;i<=n;i++) b[i]=read(); reverse(b+1,b+1+n); for(register int i=1;i<=n;i++) { if(a[i]!=b[i]) continue; cnt++; p.push_back(i); val=b[i]; } for(register int i=1;i<=n;i++) { if(!cnt) break; if(a[i]==b[i]) continue; if(a[i]!=val&&b[i]!=val) { cnt--; q.push_back(i); } } if(cnt) { puts("No"); return 0; } for(register int i=0;i<q.size();i++) swap(b[p[i]],b[q[i]]); puts("Yes"); for(register int i=1;i<=n;i++) printf("%d ",b[i]); puts(""); return 0; }
### Prompt In Cpp, your task is to solve the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int N=2e5+10; int a[N],b[N],n,val,cnt; vector<int> p,q; inline int read() { int x=0,f=1; char c=getchar(); while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); } while(c<='9'&&c>='0') { x=x*10+c-'0'; c=getchar(); } return x*f; } int main() { n=read(); for(register int i=1;i<=n;i++) a[i]=read(); for(register int i=1;i<=n;i++) b[i]=read(); reverse(b+1,b+1+n); for(register int i=1;i<=n;i++) { if(a[i]!=b[i]) continue; cnt++; p.push_back(i); val=b[i]; } for(register int i=1;i<=n;i++) { if(!cnt) break; if(a[i]==b[i]) continue; if(a[i]!=val&&b[i]!=val) { cnt--; q.push_back(i); } } if(cnt) { puts("No"); return 0; } for(register int i=0;i<q.size();i++) swap(b[p[i]],b[q[i]]); puts("Yes"); for(register int i=1;i<=n;i++) printf("%d ",b[i]); puts(""); return 0; } ```
#include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; //using namespace atcoder; int main() { int N; cin >> N; vector<int> A(N), B(N); map<int, int> ANum, BNum; for (int i = 0; i < N; i++) { cin >> A[i]; ANum[A[i]]++; } for (int i = 0; i < N; i++) { cin >> B[i]; BNum[B[i]]++; } for (int i = 0; i <= N; i++) { if (ANum.find(i) == ANum.end() || BNum.find(i) == BNum.end()) continue; if (ANum[i] + BNum[i] > N) { cout << "No" << endl; return 0; } } int j = 0, bef = -1; for (int i = 0; i < N; i++) { if (bef != A[i]) j = 0; if (A[i] == B[i]) { while (j < N) { if (A[j] != A[i] && B[j] != A[i]) { swap(B[i], B[j]); break; } j++; } } bef = A[i]; } cout << "Yes" << endl; for (int b : B) cout << b << " "; cout << endl; }
### Prompt Please create a solution in CPP to the following problem: Given are two sequences A and B, both of length N. A and B are each sorted in the ascending order. Check if it is possible to reorder the terms of B so that for each i (1 \leq i \leq N) A_i \neq B_i holds, and if it is possible, output any of the reorderings that achieve it. Constraints * 1\leq N \leq 2 \times 10^5 * 1\leq A_i,B_i \leq N * A and B are each sorted in the ascending order. * All values in input are integers. Input Input is given from Standard Input in the following format: N A_1 A_2 \cdots A_N B_1 B_2 \cdots B_N Output If there exist no reorderings that satisfy the condition, print `No`. If there exists a reordering that satisfies the condition, print `Yes` on the first line. After that, print a reordering of B on the second line, separating terms with a whitespace. If there are multiple reorderings that satisfy the condition, you can print any of them. Examples Input 6 1 1 1 2 2 3 1 1 1 2 2 3 Output Yes 2 2 3 1 1 1 Input 3 1 1 2 1 1 3 Output No Input 4 1 1 2 3 1 2 3 3 Output Yes 3 3 1 2 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std; //using namespace atcoder; int main() { int N; cin >> N; vector<int> A(N), B(N); map<int, int> ANum, BNum; for (int i = 0; i < N; i++) { cin >> A[i]; ANum[A[i]]++; } for (int i = 0; i < N; i++) { cin >> B[i]; BNum[B[i]]++; } for (int i = 0; i <= N; i++) { if (ANum.find(i) == ANum.end() || BNum.find(i) == BNum.end()) continue; if (ANum[i] + BNum[i] > N) { cout << "No" << endl; return 0; } } int j = 0, bef = -1; for (int i = 0; i < N; i++) { if (bef != A[i]) j = 0; if (A[i] == B[i]) { while (j < N) { if (A[j] != A[i] && B[j] != A[i]) { swap(B[i], B[j]); break; } j++; } } bef = A[i]; } cout << "Yes" << endl; for (int b : B) cout << b << " "; cout << endl; } ```
#include <bits/stdc++.h> using namespace std; int main(void){ int n,k; cin>>n>>k; map<int,bool> m; for(int i=1;i<=k;i++){ int d; cin>>d; for(int j=1;j<=d;j++){ int c; cin>>c; if(!m[c]) n--,m[c]=true; } } cout<<n<<endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town. There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}. Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief? Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq K \leq 100 * 1 \leq d_i \leq N * 1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N Input Input is given from Standard Input in the following format: N K d_1 A_{1, 1} \cdots A_{1, d_1} \vdots d_K A_{K, 1} \cdots A_{K, d_K} Output Print the answer. Examples Input 3 2 2 1 3 1 3 Output 1 Input 3 3 1 3 1 3 1 3 Output 2 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void){ int n,k; cin>>n>>k; map<int,bool> m; for(int i=1;i<=k;i++){ int d; cin>>d; for(int j=1;j<=d;j++){ int c; cin>>c; if(!m[c]) n--,m[c]=true; } } cout<<n<<endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; #define rep(i,N) for(int i=0;i<(int)N;i++) int main() { int N,K,D,x,count=0; cin>>N>>K; vector<int> A(N,0); rep(i,K){cin>>D;rep(j,D){cin>>x;A.at(x-1)++;}} for(auto a:A)if(a==0)count++; return cout<<count<<endl,0; }
### Prompt Your challenge is to write a cpp solution to the following problem: N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town. There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}. Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief? Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq K \leq 100 * 1 \leq d_i \leq N * 1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N Input Input is given from Standard Input in the following format: N K d_1 A_{1, 1} \cdots A_{1, d_1} \vdots d_K A_{K, 1} \cdots A_{K, d_K} Output Print the answer. Examples Input 3 2 2 1 3 1 3 Output 1 Input 3 3 1 3 1 3 1 3 Output 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define rep(i,N) for(int i=0;i<(int)N;i++) int main() { int N,K,D,x,count=0; cin>>N>>K; vector<int> A(N,0); rep(i,K){cin>>D;rep(j,D){cin>>x;A.at(x-1)++;}} for(auto a:A)if(a==0)count++; return cout<<count<<endl,0; } ```
#include<bits/stdc++.h> using namespace std; int main() { int n,k,i=1,g,h; cin>>n>>k; set<int>p; while(n--) { p.insert(i); i++; } while(k--) { cin>>g; while(g--) { cin>>h; if(p.count(h)==1) p.erase(h); } } cout<<p.size(); }
### Prompt Please create a solution in cpp to the following problem: N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town. There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}. Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief? Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq K \leq 100 * 1 \leq d_i \leq N * 1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N Input Input is given from Standard Input in the following format: N K d_1 A_{1, 1} \cdots A_{1, d_1} \vdots d_K A_{K, 1} \cdots A_{K, d_K} Output Print the answer. Examples Input 3 2 2 1 3 1 3 Output 1 Input 3 3 1 3 1 3 1 3 Output 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main() { int n,k,i=1,g,h; cin>>n>>k; set<int>p; while(n--) { p.insert(i); i++; } while(k--) { cin>>g; while(g--) { cin>>h; if(p.count(h)==1) p.erase(h); } } cout<<p.size(); } ```
#include<bits/stdc++.h> using namespace std; typedef long long int ll; int main(){ ll n,k; cin>>n>>k; set<ll>a; ll d,ele; while(k--){ cin>>d; while(d--){ cin>>ele; a.insert(ele); } } cout<<n-a.size()<<"\n"; }
### Prompt Develop a solution in cpp to the problem described below: N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town. There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}. Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief? Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq K \leq 100 * 1 \leq d_i \leq N * 1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N Input Input is given from Standard Input in the following format: N K d_1 A_{1, 1} \cdots A_{1, d_1} \vdots d_K A_{K, 1} \cdots A_{K, d_K} Output Print the answer. Examples Input 3 2 2 1 3 1 3 Output 1 Input 3 3 1 3 1 3 1 3 Output 2 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long int ll; int main(){ ll n,k; cin>>n>>k; set<ll>a; ll d,ele; while(k--){ cin>>d; while(d--){ cin>>ele; a.insert(ele); } } cout<<n-a.size()<<"\n"; } ```