output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable : 4018) #define ll long long int d[101]; vector<int> v; int main(void) { int N; scanf("%d", &N); for (int i = 0; i < N; i++) { int a; scanf("%d", &a); if (v.size() >= a - 1) { if(a-1 == v.size()) v.push_back(a); else v.insert(v.begin() + a - 1, a); } } if (v.size() < N) { printf("-1\n"); return 0; } for (int i = 0; i < N; i++) { printf("%d\n", v[i]); } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define _CRT_SECURE_NO_WARNINGS #pragma warning(disable : 4018) #define ll long long int d[101]; vector<int> v; int main(void) { int N; scanf("%d", &N); for (int i = 0; i < N; i++) { int a; scanf("%d", &a); if (v.size() >= a - 1) { if(a-1 == v.size()) v.push_back(a); else v.insert(v.begin() + a - 1, a); } } if (v.size() < N) { printf("-1\n"); return 0; } for (int i = 0; i < N; i++) { printf("%d\n", v[i]); } return 0; } ```
#include <iostream> #include <list> using namespace std; int main(void) { int num, i, a, n; cin >> num; list<int> d, ans; for (i = 0; i < num; i++) { scanf("%d", &a); d.push_front(a); } n = num; for (auto itr = d.begin(); itr != d.end();) { if (*itr == n) { ans.push_front(n); d.erase(itr); itr = d.begin(); n = d.size(); continue; } n--; itr++; } if (d.size()) printf("-1\n"); else { for (auto itr = ans.begin(); itr != ans.end(); itr++) printf("%d\n", *itr); } return 0; }
### Prompt Generate a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> #include <list> using namespace std; int main(void) { int num, i, a, n; cin >> num; list<int> d, ans; for (i = 0; i < num; i++) { scanf("%d", &a); d.push_front(a); } n = num; for (auto itr = d.begin(); itr != d.end();) { if (*itr == n) { ans.push_front(n); d.erase(itr); itr = d.begin(); n = d.size(); continue; } n--; itr++; } if (d.size()) printf("-1\n"); else { for (auto itr = ans.begin(); itr != ans.end(); itr++) printf("%d\n", *itr); } return 0; } ```
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; ++i) #define ALL(v) v.begin(), v.end() typedef long long ll; typedef pair<int, int> P; const int INF = 1000000007; int main() { int n; cin >> n; vector<int> b(n); rep(i,n)cin >> b[i]; rep(i,n) { if(b[i]>i+1) { cout<<-1<<endl; return 0; } } vector<int> ans; //ans.push_back(0); rep(i,n) { ans.insert(ans.begin()+b[i]-1,b[i]); } rep(i,n)cout<<ans[i]<<endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; ++i) #define ALL(v) v.begin(), v.end() typedef long long ll; typedef pair<int, int> P; const int INF = 1000000007; int main() { int n; cin >> n; vector<int> b(n); rep(i,n)cin >> b[i]; rep(i,n) { if(b[i]>i+1) { cout<<-1<<endl; return 0; } } vector<int> ans; //ans.push_back(0); rep(i,n) { ans.insert(ans.begin()+b[i]-1,b[i]); } rep(i,n)cout<<ans[i]<<endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int a[n+1],b[n+1]; for(int i=1;i<=n;i++) { cin>>a[i]; } int index=1; int N=n; for(int i=n;i>=1;) { if(a[i]==i) { b[index]=a[i]; index++; for(int j=i;j<=n;j++) { a[j]=a[j+1]; } n--; i=n; } else { i--; } } if(index-1==N) { for(int i=N;i>=1;i--) { cout<<b[i]<<endl; } } else { cout<<"-1"<<endl; } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; int a[n+1],b[n+1]; for(int i=1;i<=n;i++) { cin>>a[i]; } int index=1; int N=n; for(int i=n;i>=1;) { if(a[i]==i) { b[index]=a[i]; index++; for(int j=i;j<=n;j++) { a[j]=a[j+1]; } n--; i=n; } else { i--; } } if(index-1==N) { for(int i=N;i>=1;i--) { cout<<b[i]<<endl; } } else { cout<<"-1"<<endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n,p[105],ans[105],tot,x; int main(){ cin>>n; for(int i=1;i<=n;i++) cin>>p[i]; for(int i=n;i>0;i--){ x=0; for(int j=i;j>0;j--){ swap(x,p[j]); if(x==j){ans[++tot]=j;break;} if(j==1){cout<<-1<<endl;return 0;} } } while(tot) cout<<ans[tot--]<<endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n,p[105],ans[105],tot,x; int main(){ cin>>n; for(int i=1;i<=n;i++) cin>>p[i]; for(int i=n;i>0;i--){ x=0; for(int j=i;j>0;j--){ swap(x,p[j]); if(x==j){ans[++tot]=j;break;} if(j==1){cout<<-1<<endl;return 0;} } } while(tot) cout<<ans[tot--]<<endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin>>N; vector<int> ans(110); int a; bool fl=false; for(int i=0;i<N;i++){ cin>>a; if(a>i+1){ fl=true; break; } for(int j=105;j>=a;j--){ ans[j]=ans[j-1]; } ans[a-1]=a; } if(fl) cout<<"-1"; else{ for(int i=0;i<N;i++){ cout<<ans[i]<<endl; } } }
### Prompt Your challenge is to write a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int N; cin>>N; vector<int> ans(110); int a; bool fl=false; for(int i=0;i<N;i++){ cin>>a; if(a>i+1){ fl=true; break; } for(int j=105;j>=a;j--){ ans[j]=ans[j-1]; } ans[a-1]=a; } if(fl) cout<<"-1"; else{ for(int i=0;i<N;i++){ cout<<ans[i]<<endl; } } } ```
#include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef pair<int,int>P; int main(){ int n;cin>>n; vector<int>b(n); rep(i,n)cin>>b[i]; vector<int>v; while(!b.empty()){ bool ok=false; for(int i=b.size()-1;i>=0;i--){ if(b[i]==i+1){ ok=true; v.push_back(b[i]); b.erase(b.begin()+i); break; } } if(!ok){ puts("-1");return 0; } } reverse(v.begin(),v.end()); for(int i:v){ cout<<i<<endl; } }
### Prompt Please formulate a Cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef pair<int,int>P; int main(){ int n;cin>>n; vector<int>b(n); rep(i,n)cin>>b[i]; vector<int>v; while(!b.empty()){ bool ok=false; for(int i=b.size()-1;i>=0;i--){ if(b[i]==i+1){ ok=true; v.push_back(b[i]); b.erase(b.begin()+i); break; } } if(!ok){ puts("-1");return 0; } } reverse(v.begin(),v.end()); for(int i:v){ cout<<i<<endl; } } ```
#include<cstdio> int n,a[105],ans[105]; int main(){ scanf("%d",&n); for (int i=1;i<=n;i++)scanf("%d",&a[i]); for (int i=n;i>=1;i--){ int j=-1; for (j=i;j>=1;j--)if (a[j]==j)break; if (!j){printf("-1\n");return 0;} ans[i]=j; for (int k=j;k<i;k++)a[k]=a[k+1]; } for (int i=1;i<=n;i++)printf("%d\n",ans[i]); }
### Prompt Your challenge is to write a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<cstdio> int n,a[105],ans[105]; int main(){ scanf("%d",&n); for (int i=1;i<=n;i++)scanf("%d",&a[i]); for (int i=n;i>=1;i--){ int j=-1; for (j=i;j>=1;j--)if (a[j]==j)break; if (!j){printf("-1\n");return 0;} ans[i]=j; for (int k=j;k<i;k++)a[k]=a[k+1]; } for (int i=1;i<=n;i++)printf("%d\n",ans[i]); } ```
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> int n; std::vector<int>a,ans; int main(){ scanf("%d",&n),a.push_back(0); for(int i=1,x;i<=n;++i) scanf("%d",&x),a.push_back(x); for(int i=n;i;--i){ bool flag=false; for(int j=i;j;--j) if(a[j]==j){ a.erase(a.begin()+j); flag=true,ans.push_back(j); break; } if(!flag)return!puts("-1"); } std::reverse(ans.begin(),ans.end()); for(size_t i=0;i<ans.size();++i) printf("%d\n",ans[i]); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> int n; std::vector<int>a,ans; int main(){ scanf("%d",&n),a.push_back(0); for(int i=1,x;i<=n;++i) scanf("%d",&x),a.push_back(x); for(int i=n;i;--i){ bool flag=false; for(int j=i;j;--j) if(a[j]==j){ a.erase(a.begin()+j); flag=true,ans.push_back(j); break; } if(!flag)return!puts("-1"); } std::reverse(ans.begin(),ans.end()); for(size_t i=0;i<ans.size();++i) printf("%d\n",ans[i]); return 0; } ```
#include<bits/stdc++.h> using namespace std; const int N=210; int n; int a[N],p[N]; int main() { int i,j,x; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=n;i>=1;i--) { x=0; for(j=1;j<=i;j++) { if(a[j]==j) x=j; } if(x==0) { puts("-1"); return 0; } p[i]=x; for(j=x;j<i;j++) a[j]=a[j+1]; } for(i=1;i<=n;i++) printf("%d\n",p[i]); }
### Prompt Construct a Cpp code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int N=210; int n; int a[N],p[N]; int main() { int i,j,x; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=n;i>=1;i--) { x=0; for(j=1;j<=i;j++) { if(a[j]==j) x=j; } if(x==0) { puts("-1"); return 0; } p[i]=x; for(j=x;j<i;j++) a[j]=a[j+1]; } for(i=1;i<=n;i++) printf("%d\n",p[i]); } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[110]; bool f[110]; int main(){ ll n; vector<ll>ans; cin>>n; for(int i=0;i<n;i++)cin>>a[i]; for(int i=1;i<=n;i++){ ll cnt=0; bool ok=0; for(int j=n-1;j>=0;j--){ if(f[j]){ cnt++; continue; } if(a[j]==i-cnt){ ok=1; ans.push_back(a[j]); f[j]=1; break; } } if(!ok){ cout<<-1<<endl; return 0; } } for(auto x:ans){ cout<<x<<endl; } return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[110]; bool f[110]; int main(){ ll n; vector<ll>ans; cin>>n; for(int i=0;i<n;i++)cin>>a[i]; for(int i=1;i<=n;i++){ ll cnt=0; bool ok=0; for(int j=n-1;j>=0;j--){ if(f[j]){ cnt++; continue; } if(a[j]==i-cnt){ ok=1; ans.push_back(a[j]); f[j]=1; break; } } if(!ok){ cout<<-1<<endl; return 0; } } for(auto x:ans){ cout<<x<<endl; } return 0; } ```
#include <iostream> using namespace std; int arr[101]; int result[101]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> arr[i]; if (arr[i] > i) {cout << -1 << endl; return 0;} } for (int i = 1; i <= n; i++) { for (int j = i; j >= arr[i]; j--) { result[j + 1] = result[j]; } result[arr[i]] = arr[i]; } for (int i = 1; i <= n; i++) { cout << result[i] << endl; } }
### Prompt Please create a solution in CPP to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> using namespace std; int arr[101]; int result[101]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> arr[i]; if (arr[i] > i) {cout << -1 << endl; return 0;} } for (int i = 1; i <= n; i++) { for (int j = i; j >= arr[i]; j--) { result[j + 1] = result[j]; } result[arr[i]] = arr[i]; } for (int i = 1; i <= n; i++) { cout << result[i] << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >>N; vector<int> b(N); vector<int> c(0); for (int i=0; i<N; i++) cin >>b.at(i); while (b.size()>0) { int i; for (i=b.size()-1; i>=0; i--) { if (i+1==b.at(i)) { b.erase(b.begin()+i); c.push_back(i+1); break; } } if (-1==i) { cout <<-1 <<endl; return 0; } } for (int i=c.size()-1; i>=0; i--) cout <<c.at(i) <<endl; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N; cin >>N; vector<int> b(N); vector<int> c(0); for (int i=0; i<N; i++) cin >>b.at(i); while (b.size()>0) { int i; for (i=b.size()-1; i>=0; i--) { if (i+1==b.at(i)) { b.erase(b.begin()+i); c.push_back(i+1); break; } } if (-1==i) { cout <<-1 <<endl; return 0; } } for (int i=c.size()-1; i>=0; i--) cout <<c.at(i) <<endl; return 0; } ```
#include<bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) #define pb push_back using namespace std; typedef long long ll; int main() { int n; cin>>n; vector<int> b(n); rep(i,n){ cin>>b[i]; b[i]--; } vector<int> ans; rep(k,n){ for(int i=b.size()-1;i>=0;i--){ if(b[i]==i){ ans.pb(b[i]+1); b.erase(b.begin()+b[i]); break; } } } if(int(ans.size())!=n) puts("-1"); else{ rep(i,n) cout << ans[n-i-1] << endl; } }
### Prompt Please create a solution in cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) #define pb push_back using namespace std; typedef long long ll; int main() { int n; cin>>n; vector<int> b(n); rep(i,n){ cin>>b[i]; b[i]--; } vector<int> ans; rep(k,n){ for(int i=b.size()-1;i>=0;i--){ if(b[i]==i){ ans.pb(b[i]+1); b.erase(b.begin()+b[i]); break; } } } if(int(ans.size())!=n) puts("-1"); else{ rep(i,n) cout << ans[n-i-1] << endl; } } ```
#include <bits/stdc++.h> typedef long long ll; using namespace std; #define all(a) (a).begin(),(a).end() #define rep(i,j) for(ll i=0;i<j;i++) #define repf(i,m,j) for(ll i=m;i<j;i++) #define pb push_back #define sort(a) sort(all(a)) int main() { vector<int> a(100); vector<int> b(100); int N; int num; cin>>N; rep(i,N)scanf("%d",&b[i]); rep(i,N){ if(i+1>=b[i]) a.insert(a.begin()+b[i]-1,b[i]); else{ printf("%d\n",-1); return EXIT_SUCCESS; } } rep(i,N) printf("%d\n",a[i]); }
### Prompt Your task is to create a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> typedef long long ll; using namespace std; #define all(a) (a).begin(),(a).end() #define rep(i,j) for(ll i=0;i<j;i++) #define repf(i,m,j) for(ll i=m;i<j;i++) #define pb push_back #define sort(a) sort(all(a)) int main() { vector<int> a(100); vector<int> b(100); int N; int num; cin>>N; rep(i,N)scanf("%d",&b[i]); rep(i,N){ if(i+1>=b[i]) a.insert(a.begin()+b[i]-1,b[i]); else{ printf("%d\n",-1); return EXIT_SUCCESS; } } rep(i,N) printf("%d\n",a[i]); } ```
#include <iostream> #include <vector> using namespace std; int n,b[200],i; vector<int> ans; int main(){ cin>>n; for(i=1;i<=n;i++)cin>>b[i]; while(n){ for(i=n;i&&b[i]!=i;i--){} if(!i){ cout<<-1<<endl; return 0; } ans.push_back(i); for(;i<=n;i++)b[i]=b[i+1]; n--; } for(i=ans.size()-1;i>=0;i--)cout<<ans[i]<<endl; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> #include <vector> using namespace std; int n,b[200],i; vector<int> ans; int main(){ cin>>n; for(i=1;i<=n;i++)cin>>b[i]; while(n){ for(i=n;i&&b[i]!=i;i--){} if(!i){ cout<<-1<<endl; return 0; } ans.push_back(i); for(;i<=n;i++)b[i]=b[i+1]; n--; } for(i=ans.size()-1;i>=0;i--)cout<<ans[i]<<endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> b( N ); for( int i = 0; i < N; i++ ) { cin >> b[i]; } vector<int> a; while( b.size() ) { int flag = 0; for( int i = b.size() - 1; i >= 0; i-- ) { if( b[i] == i + 1 ) { a.push_back( i + 1 ); b.erase( b.begin() + i ); flag = 1; break; } } if( flag == 0 ) { cout << -1 << endl; return 0; } } reverse( a.begin(), a.end() ); for( int i = 0; i < N; i++ ) { cout << a[i] << endl; } }
### Prompt Develop a solution in cpp to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> b( N ); for( int i = 0; i < N; i++ ) { cin >> b[i]; } vector<int> a; while( b.size() ) { int flag = 0; for( int i = b.size() - 1; i >= 0; i-- ) { if( b[i] == i + 1 ) { a.push_back( i + 1 ); b.erase( b.begin() + i ); flag = 1; break; } } if( flag == 0 ) { cout << -1 << endl; return 0; } } reverse( a.begin(), a.end() ); for( int i = 0; i < N; i++ ) { cout << a[i] << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin>>N; vector<int> b(N); for(int i=0;i<N;i++){ cin>>b[i]; } vector<int> ans; for(int i=0;i<N;i++){ if(i+1<b[i]){ cout<<-1<<endl; return 0; } } while(b.size()!=0){ for(int i=b.size()-1;i>=0;i--){ if(i+1==b[i]){ ans.push_back(b[i]); b.erase(b.begin()+i); break; } } } reverse(ans.begin(),ans.end()); for(int i=0;i<N;i++){ if(i!=0)cout<<' '; cout<<ans[i]; } cout<<endl; return 0; }
### Prompt Develop a solution in CPP to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int N; cin>>N; vector<int> b(N); for(int i=0;i<N;i++){ cin>>b[i]; } vector<int> ans; for(int i=0;i<N;i++){ if(i+1<b[i]){ cout<<-1<<endl; return 0; } } while(b.size()!=0){ for(int i=b.size()-1;i>=0;i--){ if(i+1==b[i]){ ans.push_back(b[i]); b.erase(b.begin()+i); break; } } } reverse(ans.begin(),ans.end()); for(int i=0;i<N;i++){ if(i!=0)cout<<' '; cout<<ans[i]; } cout<<endl; return 0; } ```
#include <iostream> using namespace std; int main() { int a[105]; for (int i = 0; i <= 100; i++) { a[i] = 0; } int N; int b[100]; int count = 0; cin >> N; for (int i = 1; i <= N; i++) { cin >> b[i]; if (b[i] > i) { count++; break; } for (int j = N; j >= b[i] + 1; j--) { a[j] = a[j - 1]; } a[b[i]] = b[i]; } if (count != 0) cout << -1; else { for (int i = 1; i <= N; i++) { cout << a[i] << '\n'; } } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> using namespace std; int main() { int a[105]; for (int i = 0; i <= 100; i++) { a[i] = 0; } int N; int b[100]; int count = 0; cin >> N; for (int i = 1; i <= N; i++) { cin >> b[i]; if (b[i] > i) { count++; break; } for (int j = N; j >= b[i] + 1; j--) { a[j] = a[j - 1]; } a[b[i]] = b[i]; } if (count != 0) cout << -1; else { for (int i = 1; i <= N; i++) { cout << a[i] << '\n'; } } return 0; } ```
#include<iostream> #include<list> #include<vector> #include<algorithm> using namespace std; int n; list<int>a; vector<int>ans; int main() { cin>>n; for(int i=0;i<n;i++) { int x;cin>>x;a.push_back(x); } for(int ccc=0;ccc<n;ccc++) { list<int>::iterator id,it=a.begin(); bool flag=0; for(int i=0;it!=a.end();it++) { i++; if(*it==i) { id=it; flag=1; } } if(!flag) { cout<<-1<<endl; return 0; } ans.push_back(*id); a.erase(id); } for(int i=n;i--;)cout<<ans[i]<<endl; }
### Prompt Construct a cpp code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<iostream> #include<list> #include<vector> #include<algorithm> using namespace std; int n; list<int>a; vector<int>ans; int main() { cin>>n; for(int i=0;i<n;i++) { int x;cin>>x;a.push_back(x); } for(int ccc=0;ccc<n;ccc++) { list<int>::iterator id,it=a.begin(); bool flag=0; for(int i=0;it!=a.end();it++) { i++; if(*it==i) { id=it; flag=1; } } if(!flag) { cout<<-1<<endl; return 0; } ans.push_back(*id); a.erase(id); } for(int i=n;i--;)cout<<ans[i]<<endl; } ```
#include<bits/stdc++.h> using namespace std; int n, a; vector<int> ans; int main(){ scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%d", &a); a--; if(a <= i) ans.insert(ans.begin() + a, a+1); else return puts("-1")*0; } for(int it: ans) printf("%d\n", it); }
### Prompt Please provide a CPP coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int n, a; vector<int> ans; int main(){ scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%d", &a); a--; if(a <= i) ans.insert(ans.begin() + a, a+1); else return puts("-1")*0; } for(int it: ans) printf("%d\n", it); } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> b(n), ans; for (int i = 0; i < n; i++) { cin >> b.at(i); b.at(i)--; } for (int i = 0; i < n; i++) { int bmax = -1; for (int j = 0; j < b.size(); j++) { if (b.at(j) == j) bmax = j; } if (bmax == -1) { cout << -1 << endl; return 0; } ans.push_back(bmax + 1); b.erase(b.begin() + bmax); } for (int i = n - 1; i >= 0; i--) { cout << ans.at(i) << endl; } }
### Prompt Generate a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> b(n), ans; for (int i = 0; i < n; i++) { cin >> b.at(i); b.at(i)--; } for (int i = 0; i < n; i++) { int bmax = -1; for (int j = 0; j < b.size(); j++) { if (b.at(j) == j) bmax = j; } if (bmax == -1) { cout << -1 << endl; return 0; } ans.push_back(bmax + 1); b.erase(b.begin() + bmax); } for (int i = n - 1; i >= 0; i--) { cout << ans.at(i) << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> b(n); for(int i=0; i<n; ++i) cin >> b[i]; stack<int> buf; int index = n-1; while(b.size() > 0){ if(index < 0){ cout << -1 << endl; return 0; } if(b[index] == index+1){ buf.push(b[index]); b.erase(b.begin() + index); index = b.size() - 1; continue; } --index; } for(int i=0; i<n; ++i){ cout << buf.top() << endl; buf.pop(); } return 0; }
### Prompt Create a solution in Cpp for the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> b(n); for(int i=0; i<n; ++i) cin >> b[i]; stack<int> buf; int index = n-1; while(b.size() > 0){ if(index < 0){ cout << -1 << endl; return 0; } if(b[index] == index+1){ buf.push(b[index]); b.erase(b.begin() + index); index = b.size() - 1; continue; } --index; } for(int i=0; i<n; ++i){ cout << buf.top() << endl; buf.pop(); } return 0; } ```
#include <iostream> #include <cstdio> using namespace std; int n, a[105], b[105]; int main() { int i, j; cin >> n; for(i=1; i<=n; i++) cin >> a[i]; for(i=1; i<=n; i++) { for(j=n; a[j]!=j; j--); if(j==0) return puts("-1"), 0; b[i] = j; for(; j<=n-i; j++) a[j] = a[j+1]; a[j] = -1; } for(i=n; i; i--) cout << b[i] << endl; return 0; }
### Prompt Please create a solution in cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> #include <cstdio> using namespace std; int n, a[105], b[105]; int main() { int i, j; cin >> n; for(i=1; i<=n; i++) cin >> a[i]; for(i=1; i<=n; i++) { for(j=n; a[j]!=j; j--); if(j==0) return puts("-1"), 0; b[i] = j; for(; j<=n-i; j++) a[j] = a[j+1]; a[j] = -1; } for(i=n; i; i--) cout << b[i] << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL<<62 #define inf 1000000007 int main() { vector<ll>a; ll n; cin>>n; for(int i=0;i<n;i++){ ll x; cin>>x; a.push_back(x); } vector<ll>ans; for(int i=n;i>=1;i--){ ll j=i; while(j!=a[j-1]){ j--; if(j==0){ cout << -1; return 0; } } ans.push_back(a[j-1]); a.erase(a.begin()+j-1); } reverse(ans.begin(),ans.end()); for(int i=0;i<n;i++){ cout << ans[i]<<endl; } }
### Prompt Please provide a Cpp coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL<<62 #define inf 1000000007 int main() { vector<ll>a; ll n; cin>>n; for(int i=0;i<n;i++){ ll x; cin>>x; a.push_back(x); } vector<ll>ans; for(int i=n;i>=1;i--){ ll j=i; while(j!=a[j-1]){ j--; if(j==0){ cout << -1; return 0; } } ans.push_back(a[j-1]); a.erase(a.begin()+j-1); } reverse(ans.begin(),ans.end()); for(int i=0;i<n;i++){ cout << ans[i]<<endl; } } ```
// https://atcoder.jp/contests/agc032/tasks/agc032_a #include <bits/stdc++.h> using namespace std; using vi = vector<int>; int main(){ int n; cin >> n; vi a, b(n); for (int i = 0; i < n; i++) cin >> b[i], b[i]--; for (int i = 0; i < n; i++) { if (b[i] > a.size()) { cout << "-1\n"; return 0; } a.insert(a.begin() + b[i], b[i] + 1); } for(int x : a) cout << x << '\n'; }
### Prompt In cpp, your task is to solve the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp // https://atcoder.jp/contests/agc032/tasks/agc032_a #include <bits/stdc++.h> using namespace std; using vi = vector<int>; int main(){ int n; cin >> n; vi a, b(n); for (int i = 0; i < n; i++) cin >> b[i], b[i]--; for (int i = 0; i < n; i++) { if (b[i] > a.size()) { cout << "-1\n"; return 0; } a.insert(a.begin() + b[i], b[i] + 1); } for(int x : a) cout << x << '\n'; } ```
#include <iostream> using namespace std; int main() { int n,k; cin>>n; int *a=new int[n+1](); int *b=new int[n+1](); for(int i=1; i<=n; i++) { cin>>a[i]; } int l=1; for(int i=n; i>=1;) { if(a[i]==i) { b[l]=a[i]; l++; k++; for(int j=i; j<=n; j++) { a[j]=a[j+1]; } i=n; } i--; } if(k==n) { for(int i=n; i>=1; i--) { cout<<b[i]<<endl; } } else { cout<<-1<<endl; } return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> using namespace std; int main() { int n,k; cin>>n; int *a=new int[n+1](); int *b=new int[n+1](); for(int i=1; i<=n; i++) { cin>>a[i]; } int l=1; for(int i=n; i>=1;) { if(a[i]==i) { b[l]=a[i]; l++; k++; for(int j=i; j<=n; j++) { a[j]=a[j+1]; } i=n; } i--; } if(k==n) { for(int i=n; i>=1; i--) { cout<<b[i]<<endl; } } else { cout<<-1<<endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); vector<int> b(N); for (int i=0;i<N;i++) cin >> b.at(i); for(int i=0;i<N;i++){ if((i+1)>=b.at(i)){ a.insert(a.begin()+(b.at(i)-1),b.at(i)); } else{ cout << "-1" << endl; return 0; } } for(int i=0;i<N;i++){ cout << a.at(i) << endl; } return 0; }
### Prompt Create a solution in CPP for the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); vector<int> b(N); for (int i=0;i<N;i++) cin >> b.at(i); for(int i=0;i<N;i++){ if((i+1)>=b.at(i)){ a.insert(a.begin()+(b.at(i)-1),b.at(i)); } else{ cout << "-1" << endl; return 0; } } for(int i=0;i<N;i++){ cout << a.at(i) << endl; } return 0; } ```
#include<cstdio> using namespace std; int a[101]; int b[101]; int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=n;i;i--){ int o=0; for(int j=1;j<=i;j++) if(a[j]==j)o=j; if(o==0){ printf("-1\n"); return 0; } b[++b[0]]=o; for(int j=o;j<i;j++) a[j]=a[j+1]; } for(int i=b[0];i;i--) printf("%d\n",b[i]); }
### Prompt Construct a Cpp code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<cstdio> using namespace std; int a[101]; int b[101]; int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=n;i;i--){ int o=0; for(int j=1;j<=i;j++) if(a[j]==j)o=j; if(o==0){ printf("-1\n"); return 0; } b[++b[0]]=o; for(int j=o;j<i;j++) a[j]=a[j+1]; } for(int i=b[0];i;i--) printf("%d\n",b[i]); } ```
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; vector<int> a(n); for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=0;i<n;i++){ if(i-a[i]<-1){ cout<<-1; return 0; } } deque<int> deq; for(int i=0;i<n;i++){ for(int j=a.size()-1;j>=0;j--){ if(j+1==a[j]){ deq.push_front(a[j]); a.erase(a.begin()+j); break; } } } for(int e:deq){ cout<<e<<endl; } }
### Prompt Please formulate a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; vector<int> a(n); for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=0;i<n;i++){ if(i-a[i]<-1){ cout<<-1; return 0; } } deque<int> deq; for(int i=0;i<n;i++){ for(int j=a.size()-1;j>=0;j--){ if(j+1==a[j]){ deq.push_front(a[j]); a.erase(a.begin()+j); break; } } } for(int e:deq){ cout<<e<<endl; } } ```
#include <bits/stdc++.h> using namespace std; int a[105],b[105]; int N; int main() { scanf("%d",&N); int cnt=N; for (int i=1;i<=N;i++) scanf("%d",&a[i]); for (int i=cnt;i>=1;i--) { for (int j=N;j>=1;j--) //cout<<j<<endl; if (a[j]==j) { for (int k=j;k<=N-1;k++) a[k]=a[k+1]; a[N]=0; b[i]=j; N--; break; } } if (N!=0) printf("-1"); else for (int i=1;i<=cnt;i++) printf("%d\n",b[i]); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[105],b[105]; int N; int main() { scanf("%d",&N); int cnt=N; for (int i=1;i<=N;i++) scanf("%d",&a[i]); for (int i=cnt;i>=1;i--) { for (int j=N;j>=1;j--) //cout<<j<<endl; if (a[j]==j) { for (int k=j;k<=N-1;k++) a[k]=a[k+1]; a[N]=0; b[i]=j; N--; break; } } if (N!=0) printf("-1"); else for (int i=1;i<=cnt;i++) printf("%d\n",b[i]); return 0; } ```
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; vector<int>b(n); vector<int>a; for(int i=0;i<n;i++)cin>>b[i]; for(int i=0;i<n;i++) { int x=0,back=1,c; for(int i=0;i<n;i++) { if(back==b[i]) { x=back; c=i; } if(b[i]!=-1)back++; } if(!x) { cout<<-1<<endl; return 0; } b[c]=-1; a.push_back(x); } for(int i=a.size()-1;i>=0;i--)cout<<a[i]<<endl; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; vector<int>b(n); vector<int>a; for(int i=0;i<n;i++)cin>>b[i]; for(int i=0;i<n;i++) { int x=0,back=1,c; for(int i=0;i<n;i++) { if(back==b[i]) { x=back; c=i; } if(b[i]!=-1)back++; } if(!x) { cout<<-1<<endl; return 0; } b[c]=-1; a.push_back(x); } for(int i=a.size()-1;i>=0;i--)cout<<a[i]<<endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int i,j,k,n,m,x,y,t; int a[111],ans[111]; int main(){ scanf("%d",&n);for (i=1;i<=n;i++)scanf("%d",&a[i]); for (i=1;i<=n;i++){ bool flag=0; for (j=n-i+1;j>=1;j--) if (a[j]==j){ flag=1;x=j;break; } if (!flag){printf("-1\n");return 0;} ans[i]=a[x]; for (j=x;j<=n-i;j++)a[j]=a[j+1]; } for (i=n;i>=1;i--)printf("%d\n",ans[i]); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int i,j,k,n,m,x,y,t; int a[111],ans[111]; int main(){ scanf("%d",&n);for (i=1;i<=n;i++)scanf("%d",&a[i]); for (i=1;i<=n;i++){ bool flag=0; for (j=n-i+1;j>=1;j--) if (a[j]==j){ flag=1;x=j;break; } if (!flag){printf("-1\n");return 0;} ans[i]=a[x]; for (j=x;j<=n-i;j++)a[j]=a[j+1]; } for (i=n;i>=1;i--)printf("%d\n",ans[i]); return 0; } ```
#include<cstdio> #include<cstring> #include<algorithm> #define SF scanf #define PF printf #define MAXN 100010 using namespace std; int n; int a[MAXN]; int ans[MAXN],cnt; bool solve(int tot){ if(tot==0) return 1; for(int i=tot;i>=1;i--) if(a[i]==i){ ans[++cnt]=i; for(int j=i;j<tot;j++) a[j]=a[j+1]; return solve(tot-1); } return 0; } int main(){ SF("%d",&n); for(int i=1;i<=n;i++) SF("%d",&a[i]); if(solve(n)){ for(int i=n;i>=1;i--) PF("%d\n",ans[i]); } else PF("-1"); }
### Prompt Create a solution in cpp for the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<cstdio> #include<cstring> #include<algorithm> #define SF scanf #define PF printf #define MAXN 100010 using namespace std; int n; int a[MAXN]; int ans[MAXN],cnt; bool solve(int tot){ if(tot==0) return 1; for(int i=tot;i>=1;i--) if(a[i]==i){ ans[++cnt]=i; for(int j=i;j<tot;j++) a[j]=a[j+1]; return solve(tot-1); } return 0; } int main(){ SF("%d",&n); for(int i=1;i<=n;i++) SF("%d",&a[i]); if(solve(n)){ for(int i=n;i>=1;i--) PF("%d\n",ans[i]); } else PF("-1"); } ```
#include<iostream> using namespace std; int main () { int N; cin >> N; int b[102]; for (int i = 1; i <= N; i ++) { cin >> b[i]; if (b[i] > i) { cout << "-1" << endl; return 0; } } int ans[101]; for (int i = N; i > 0; i --) { for (int j = i; j > 0; j --) { if (b[j] == j) { ans[i] = j; for (int k = j; k < i; k ++) b[k] = b[k + 1]; break; } } } for (int i = 1; i <= N; i ++) cout << ans[i] << endl; }
### Prompt Please formulate a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<iostream> using namespace std; int main () { int N; cin >> N; int b[102]; for (int i = 1; i <= N; i ++) { cin >> b[i]; if (b[i] > i) { cout << "-1" << endl; return 0; } } int ans[101]; for (int i = N; i > 0; i --) { for (int j = i; j > 0; j --) { if (b[j] == j) { ans[i] = j; for (int k = j; k < i; k ++) b[k] = b[k + 1]; break; } } } for (int i = 1; i <= N; i ++) cout << ans[i] << endl; } ```
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int b[n]; bool ok=true; for(int i=0;i<n;i++){ cin>>b[i]; if(b[i]>i+1) ok=false; } if(!ok) {cout<<-1<<endl;return 0;} vector<int> ans; for(int i=0;i<n;i++){ auto it=ans.begin(); it +=b[i]-1; ans.insert(it,b[i]); } for(int i=0;i<n;i++){ cout<<ans[i]<<endl; } }
### Prompt Develop a solution in cpp to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int b[n]; bool ok=true; for(int i=0;i<n;i++){ cin>>b[i]; if(b[i]>i+1) ok=false; } if(!ok) {cout<<-1<<endl;return 0;} vector<int> ans; for(int i=0;i<n;i++){ auto it=ans.begin(); it +=b[i]-1; ans.insert(it,b[i]); } for(int i=0;i<n;i++){ cout<<ans[i]<<endl; } } ```
#include <iostream> #include <vector> #include <algorithm> #include <math.h> using namespace std; int main() { int N,i,temp; bool flag=1; vector<int> b,ans; cin>>N; for(i=0;i<N;i++){ cin>>temp; if(temp>i+1){ flag=0; break; } b.push_back(temp); } if(flag){ while(b.size()!=0){ for(i=b.size();i>0;i--){ if(b.at(i-1)==i){ ans.push_back(i); b.erase(b.begin()+i-1); break; } } } for(i=N;i>0;i--){ cout<<ans.at(i-1)<<endl; } }else{ cout<<-1; } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> #include <vector> #include <algorithm> #include <math.h> using namespace std; int main() { int N,i,temp; bool flag=1; vector<int> b,ans; cin>>N; for(i=0;i<N;i++){ cin>>temp; if(temp>i+1){ flag=0; break; } b.push_back(temp); } if(flag){ while(b.size()!=0){ for(i=b.size();i>0;i--){ if(b.at(i-1)==i){ ans.push_back(i); b.erase(b.begin()+i-1); break; } } } for(i=N;i>0;i--){ cout<<ans.at(i-1)<<endl; } }else{ cout<<-1; } return 0; } ```
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (n); i++) using namespace std; signed main(void) { int n; cin >> n; vector<int> b(n), out; rep(i, n) cin >> b[i]; rep(i, n) { for(int j = b.size() - 1; j >= 0; j--) { if (b[j] == j + 1) { out.push_back(b[j]); b.erase(b.begin() + j); break; } } } reverse(out.begin(), out.end()); if (out.size() == n) { rep(i, out.size()) cout << out[i] << endl; } else cout << -1 << endl; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (n); i++) using namespace std; signed main(void) { int n; cin >> n; vector<int> b(n), out; rep(i, n) cin >> b[i]; rep(i, n) { for(int j = b.size() - 1; j >= 0; j--) { if (b[j] == j + 1) { out.push_back(b[j]); b.erase(b.begin() + j); break; } } } reverse(out.begin(), out.end()); if (out.size() == n) { rep(i, out.size()) cout << out[i] << endl; } else cout << -1 << endl; return 0; } ```
#include<cstdio> #include<vector> using namespace std; int a[105]; vector<int> v; int main(){ int n,i,j,x; bool ok; scanf("%d",&n); for(i=1;i<=n;++i){ scanf("%d",&x); v.push_back(x); } for(i=1;i<=n;++i){ ok=0; for(j=v.size()-1;j>=0;--j)if(v[j]==j+1){ a[i]=j+1; v.erase(v.begin()+j); ok=1; break; } if(!ok){ puts("-1"); return 0; } } for(i=n;i;--i)printf("%d\n",a[i]); return 0; }
### Prompt Generate a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<cstdio> #include<vector> using namespace std; int a[105]; vector<int> v; int main(){ int n,i,j,x; bool ok; scanf("%d",&n); for(i=1;i<=n;++i){ scanf("%d",&x); v.push_back(x); } for(i=1;i<=n;++i){ ok=0; for(j=v.size()-1;j>=0;--j)if(v[j]==j+1){ a[i]=j+1; v.erase(v.begin()+j); ok=1; break; } if(!ok){ puts("-1"); return 0; } } for(i=n;i;--i)printf("%d\n",a[i]); return 0; } ```
#include <algorithm> #include <iostream> #include <cstdio> using namespace std ; int ans[200] ; int a[200] ; int n , m ; int main () { int i , j ; scanf("%d",&n),m=n; for ( i=1 ; i<=n ; i++ ) scanf("%d",a+i); for ( i=1 ; i<=n ; i++ ) { int flag=0 ; for ( j=n ; j>=1 ; j-- ) if ( j==a[j] ) { flag=1; break; } if ( !flag ) { puts("-1"); return 0 ; } ans[i]=j; for ( j=j ; j<=n ; j++ ) a[j]=a[j+1]; } for ( i=m ; i>=1 ; i-- ) printf("%d\n",ans[i]); return 0 ; }
### Prompt Develop a solution in cpp to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <algorithm> #include <iostream> #include <cstdio> using namespace std ; int ans[200] ; int a[200] ; int n , m ; int main () { int i , j ; scanf("%d",&n),m=n; for ( i=1 ; i<=n ; i++ ) scanf("%d",a+i); for ( i=1 ; i<=n ; i++ ) { int flag=0 ; for ( j=n ; j>=1 ; j-- ) if ( j==a[j] ) { flag=1; break; } if ( !flag ) { puts("-1"); return 0 ; } ans[i]=j; for ( j=j ; j<=n ; j++ ) a[j]=a[j+1]; } for ( i=m ; i>=1 ; i-- ) printf("%d\n",ans[i]); return 0 ; } ```
#include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> b(n), a(10, 3); for(int i = 0; i < n; i++) { cin >> b.at(i); if(b.at(i) > i+1) { cout << -1; exit(0); } } for(int i = 0; i < n; i++) { a.insert(a.begin() + b.at(i), b.at(i)); } for(int i = 1; i < n+1; i++) { cout << a.at(i) << endl; } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> b(n), a(10, 3); for(int i = 0; i < n; i++) { cin >> b.at(i); if(b.at(i) > i+1) { cout << -1; exit(0); } } for(int i = 0; i < n; i++) { a.insert(a.begin() + b.at(i), b.at(i)); } for(int i = 1; i < n+1; i++) { cout << a.at(i) << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0);cin.tie(0); int n; cin >> n; vector<int> arr; for(int i=0; i<n; i++) { int t; cin >> t; if(t-1 > arr.size()) return !(cout << -1); arr.insert(arr.begin()+t-1, t); } for(int i: arr) cout << i << '\n'; }
### Prompt Please create a solution in CPP to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### 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> arr; for(int i=0; i<n; i++) { int t; cin >> t; if(t-1 > arr.size()) return !(cout << -1); arr.insert(arr.begin()+t-1, t); } for(int i: arr) cout << i << '\n'; } ```
#include<bits/stdc++.h> using namespace std; int D[123]; vector<int>ans; int main() { int n;scanf("%d",&n); for(int i=0;i<n;i++)scanf("%d",&D[i]); for(int i=0;i<n;i++){ int res=-1; for(int j=0;j<n-i;j++) if(j+1==D[j])res=max(j,res); if(res==-1)return cout<<-1<<'\n',0; ans.push_back(D[res]); for(int j=res+1;j<n-i;j++)D[j-1]=D[j]; }for(int k=ans.size()-1;k>-1;k--)cout<<ans[k]<<'\n'; }
### Prompt Your task is to create a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int D[123]; vector<int>ans; int main() { int n;scanf("%d",&n); for(int i=0;i<n;i++)scanf("%d",&D[i]); for(int i=0;i<n;i++){ int res=-1; for(int j=0;j<n-i;j++) if(j+1==D[j])res=max(j,res); if(res==-1)return cout<<-1<<'\n',0; ans.push_back(D[res]); for(int j=res+1;j<n-i;j++)D[j-1]=D[j]; }for(int k=ans.size()-1;k>-1;k--)cout<<ans[k]<<'\n'; } ```
#include<bits/stdc++.h> using namespace std; int main(){ int n; stack<int> a; cin >> n; vector<int> b(n); for(int i=0;i<n;i++) cin >> b[i]; while(!b.empty()){ bool e = true; for(int i=b.size()-1;i>=0;i--){ if(i+1 == b[i]){ a.push(b[i]); b.erase(b.begin()+i); e = false; break; } } if(e){ cout << -1 << endl; return 0; } } while(!a.empty()){ cout << a.top() << endl; a.pop(); } }
### Prompt Please create a solution in cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int n; stack<int> a; cin >> n; vector<int> b(n); for(int i=0;i<n;i++) cin >> b[i]; while(!b.empty()){ bool e = true; for(int i=b.size()-1;i>=0;i--){ if(i+1 == b[i]){ a.push(b[i]); b.erase(b.begin()+i); e = false; break; } } if(e){ cout << -1 << endl; return 0; } } while(!a.empty()){ cout << a.top() << endl; a.pop(); } } ```
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=105; int n; int a[MAXN]; int b[MAXN],c[MAXN]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n;i++) { if(a[i]>i) { printf("-1\n"); return 0; } for(int j=1;j<i;j++) { if(a[i]<=j+b[j]&&j+b[j]<i) { b[i]--; b[j]++; } } } for(int i=1;i<=n;i++) { c[i+b[i]]=a[i]; } for(int i=1;i<=n;i++) printf("%d\n",c[i]); }
### Prompt Please create a solution in Cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=105; int n; int a[MAXN]; int b[MAXN],c[MAXN]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n;i++) { if(a[i]>i) { printf("-1\n"); return 0; } for(int j=1;j<i;j++) { if(a[i]<=j+b[j]&&j+b[j]<i) { b[i]--; b[j]++; } } } for(int i=1;i<=n;i++) { c[i+b[i]]=a[i]; } for(int i=1;i<=n;i++) printf("%d\n",c[i]); } ```
#include <bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for (int i = x; i < (int)(n); ++i) int main(){ int n; scanf("%d", &n); vector<int> x(n); f(i, 0, n)scanf("%d", &x[i]); vector<int> an; bool ok; do { ok = false; for (int i = x.size() - 1; i >= 0; --i)if (x[i] == i + 1){ an.push_back(x[i]); x.erase(x.begin() + i); ok = true; break; } }while (ok); if (!x.empty())printf("-1\n"); else for (int i = an.size() - 1; i >= 0; --i)printf("%d\n", an[i]); }
### Prompt In Cpp, your task is to solve the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for (int i = x; i < (int)(n); ++i) int main(){ int n; scanf("%d", &n); vector<int> x(n); f(i, 0, n)scanf("%d", &x[i]); vector<int> an; bool ok; do { ok = false; for (int i = x.size() - 1; i >= 0; --i)if (x[i] == i + 1){ an.push_back(x[i]); x.erase(x.begin() + i); ok = true; break; } }while (ok); if (!x.empty())printf("-1\n"); else for (int i = an.size() - 1; i >= 0; --i)printf("%d\n", an[i]); } ```
#include<bits/stdc++.h> using namespace std; int n,a[100010],b[100010],nn,cnt; int main() { cin>>n;nn=n; for(int i=1;i<=n;i++) cin>>b[i]; for(int i=1;i<=n;i++) { int j=nn; for(;j>=1;j--) if(b[j]==j) break; if(!j){puts("-1");return 0;} a[++cnt]=j; for(int k=j+1;k<=nn;k++) b[k-1]=b[k]; nn--; } for(int i=cnt;i>=1;i--) cout<<a[i]<<'\n'; }
### Prompt Please provide a Cpp coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int n,a[100010],b[100010],nn,cnt; int main() { cin>>n;nn=n; for(int i=1;i<=n;i++) cin>>b[i]; for(int i=1;i<=n;i++) { int j=nn; for(;j>=1;j--) if(b[j]==j) break; if(!j){puts("-1");return 0;} a[++cnt]=j; for(int k=j+1;k<=nn;k++) b[k-1]=b[k]; nn--; } for(int i=cnt;i>=1;i--) cout<<a[i]<<'\n'; } ```
#include <cstdio> const int MN = 105; int N; int A[MN], Ans[MN]; int main() { scanf("%d", &N); for (int i = 1; i <= N; ++i) scanf("%d", &A[i]); for (int i = 1; i <= N; ++i) if (A[i] > i) return puts("-1"), 0; for (int i = N; i >= 1; --i) { int p = 0; for (int j = 1; j <= i; ++j) if (A[j] == j) p = j; Ans[i] = A[p]; for (int j = p; j < i; ++j) A[j] = A[j + 1]; } for (int i = 1; i <= N; ++i) printf("%d\n", Ans[i]); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <cstdio> const int MN = 105; int N; int A[MN], Ans[MN]; int main() { scanf("%d", &N); for (int i = 1; i <= N; ++i) scanf("%d", &A[i]); for (int i = 1; i <= N; ++i) if (A[i] > i) return puts("-1"), 0; for (int i = N; i >= 1; --i) { int p = 0; for (int j = 1; j <= i; ++j) if (A[j] == j) p = j; Ans[i] = A[p]; for (int j = p; j < i; ++j) A[j] = A[j + 1]; } for (int i = 1; i <= N; ++i) printf("%d\n", Ans[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; using lint = long long; signed main(){ lint N; cin >> N; vector<lint> b(N); for(lint i = 0; i < N; i++) cin >> b[i]; vector<lint> a; for (lint i = 0; i < N; i++) { for (lint j = b.size() - 1; j >= 0; j--) { if (j + 1 == b[j]) { a.push_back(j + 1); b.erase(b.begin() + j); break; } } } if(a.size() != N) cout << -1 << endl; else{ for(lint i = N - 1; i >= 0; i--) cout << a[i] << " "; cout << endl; } }
### Prompt Please provide a cpp coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; using lint = long long; signed main(){ lint N; cin >> N; vector<lint> b(N); for(lint i = 0; i < N; i++) cin >> b[i]; vector<lint> a; for (lint i = 0; i < N; i++) { for (lint j = b.size() - 1; j >= 0; j--) { if (j + 1 == b[j]) { a.push_back(j + 1); b.erase(b.begin() + j); break; } } } if(a.size() != N) cout << -1 << endl; else{ for(lint i = N - 1; i >= 0; i--) cout << a[i] << " "; cout << endl; } } ```
#include <iostream> using namespace std; int main(){ int N; cin >> N; int b[N]; for(int i=0;i<N;i++)cin >> b[i]; for(int i=0;i<N;i++){ if(i-b[i]+1<0){ cout << -1 << endl; return 0; } if(b[i]!=1){ int date = b[i]; for(int j=0;j<date-1;j++){b[i-j]=b[i-j-1];} b[i-date+1]=date; } } for(int i=N-1;i>=0;i--)cout << b[i] <<endl; }
### Prompt Please create a solution in CPP to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> using namespace std; int main(){ int N; cin >> N; int b[N]; for(int i=0;i<N;i++)cin >> b[i]; for(int i=0;i<N;i++){ if(i-b[i]+1<0){ cout << -1 << endl; return 0; } if(b[i]!=1){ int date = b[i]; for(int j=0;j<date-1;j++){b[i-j]=b[i-j-1];} b[i-date+1]=date; } } for(int i=N-1;i>=0;i--)cout << b[i] <<endl; } ```
#include<bits/stdc++.h> using namespace std; #define ll long long typedef pair<ll,ll> P; #define M 1000000007 int main(){ ll n,d; cin>>n; d=n; vector<ll> b(n),a; for(int i=0;i<n;i++){ cin>>b[i]; } while(n!=0){ bool f=true; for(int i=n;i>0;i--){ if(i==b[i-1]){ a.push_back(i); b.erase(b.begin()+i-1); f=false; break; } } if(f){ cout<<-1; return 0; } n--; } for(int i=d-1;i>=0;i--) cout<<a[i]<<endl; }
### Prompt Your task is to create a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define ll long long typedef pair<ll,ll> P; #define M 1000000007 int main(){ ll n,d; cin>>n; d=n; vector<ll> b(n),a; for(int i=0;i<n;i++){ cin>>b[i]; } while(n!=0){ bool f=true; for(int i=n;i>0;i--){ if(i==b[i-1]){ a.push_back(i); b.erase(b.begin()+i-1); f=false; break; } } if(f){ cout<<-1; return 0; } n--; } for(int i=d-1;i>=0;i--) cout<<a[i]<<endl; } ```
#include<bits/stdc++.h> using namespace std; typedef long long int uli; int main(){ int n; cin>>n; vector<int>a(n); for(int i=0;i<n;i++)cin>>a[i]; vector<int>ans(n); for(int i=n-1;i>=0;i--){ int idx=-1; for(int j=i;j>=0;j--){ if(a[j]==j+1){ idx=j; break; } } if(idx==-1){ puts("-1"); return 0; } ans[i]=idx+1; a.erase(a.begin()+idx); } for(int i=0;i<n;i++)printf("%d\n",ans[i]); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long int uli; int main(){ int n; cin>>n; vector<int>a(n); for(int i=0;i<n;i++)cin>>a[i]; vector<int>ans(n); for(int i=n-1;i>=0;i--){ int idx=-1; for(int j=i;j>=0;j--){ if(a[j]==j+1){ idx=j; break; } } if(idx==-1){ puts("-1"); return 0; } ans[i]=idx+1; a.erase(a.begin()+idx); } for(int i=0;i<n;i++)printf("%d\n",ans[i]); return 0; } ```
//ksun48 - just to mark ac #include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> b(n); for(int i = 0; i < n; i++){ cin >> b[i]; b[i]--; } vector<int> ans; while(b.size() > 0){ int z = -1; for(int i = 0; i < b.size(); i++){ if(b[i] == i) z = i; } if(z == -1){ cout << -1 << endl; exit(0); } ans.push_back(z); b.erase(b.begin() + z); } reverse(ans.begin(), ans.end()); for(int x : ans){ cout << x + 1 << '\n'; } }
### Prompt Please create a solution in Cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp //ksun48 - just to mark ac #include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> b(n); for(int i = 0; i < n; i++){ cin >> b[i]; b[i]--; } vector<int> ans; while(b.size() > 0){ int z = -1; for(int i = 0; i < b.size(); i++){ if(b[i] == i) z = i; } if(z == -1){ cout << -1 << endl; exit(0); } ans.push_back(z); b.erase(b.begin() + z); } reverse(ans.begin(), ans.end()); for(int x : ans){ cout << x + 1 << '\n'; } } ```
#include<bits/stdc++.h> using namespace std; int a[1200],tot,n,ans[1200]; int solve(){ for(int i=tot;i>=1;i--){ if(a[i]==i){ ans[tot]=i; for(int j=i;j<tot;j++)a[j]=a[j+1]; return 0; } } printf("-1\n"); exit(0); } int main(){ scanf("%d",&n); tot=n; for(int i=1;i<=n;i++)scanf("%d",&a[i]); while(tot){ solve(); tot--; } for(int i=1;i<=n;i++)printf("%d\n",ans[i]); }
### Prompt Please formulate a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int a[1200],tot,n,ans[1200]; int solve(){ for(int i=tot;i>=1;i--){ if(a[i]==i){ ans[tot]=i; for(int j=i;j<tot;j++)a[j]=a[j+1]; return 0; } } printf("-1\n"); exit(0); } int main(){ scanf("%d",&n); tot=n; for(int i=1;i<=n;i++)scanf("%d",&a[i]); while(tot){ solve(); tot--; } for(int i=1;i<=n;i++)printf("%d\n",ans[i]); } ```
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<(n); ++i) using namespace std; using ll=long long; int main(){ int n; cin>>n; vector<int>x,b(n); rep(i,n)cin>>b[i]; rep(i,n){ if(b[i]-1>i){cout<<-1<<endl; return 0;} x.insert(x.begin()+b[i]-1,b[i]); } rep(i,n){ cout<<x[i]<<endl; } }
### Prompt Your task is to create a Cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<(n); ++i) using namespace std; using ll=long long; int main(){ int n; cin>>n; vector<int>x,b(n); rep(i,n)cin>>b[i]; rep(i,n){ if(b[i]-1>i){cout<<-1<<endl; return 0;} x.insert(x.begin()+b[i]-1,b[i]); } rep(i,n){ cout<<x[i]<<endl; } } ```
#include<bits/stdc++.h> #define REP(i,n) for(int i=0;i<(n);++i) using namespace std; int main(){ int n; cin>>n; vector<int> b(n); REP(i,n) cin>>b[i]; vector<int> a; while(b.size()>0){ bool jud=true;//不可かどうか. for(int i=b.size()-1;i>=0;--i){ if(i+1==b[i]){ a.push_back(b[i]); b.erase(b.begin()+i); jud=false; break;//for. } } if(jud){ cout<<-1<<endl; return 0; } } for(int i=n-1;i>=0;--i){ cout<<a[i]<<endl; } return 0; }//agc032_a.復習.
### Prompt Construct a Cpp code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> #define REP(i,n) for(int i=0;i<(n);++i) using namespace std; int main(){ int n; cin>>n; vector<int> b(n); REP(i,n) cin>>b[i]; vector<int> a; while(b.size()>0){ bool jud=true;//不可かどうか. for(int i=b.size()-1;i>=0;--i){ if(i+1==b[i]){ a.push_back(b[i]); b.erase(b.begin()+i); jud=false; break;//for. } } if(jud){ cout<<-1<<endl; return 0; } } for(int i=n-1;i>=0;--i){ cout<<a[i]<<endl; } return 0; }//agc032_a.復習. ```
#include<bits/stdc++.h> using namespace std; int main(){ unsigned long long N; cin >> N; vector<unsigned long long> v; for(unsigned long long i = 0, a; i < N; ++i){ cin >> a; --a; if(i < a)return 0 & puts("-1"); v.insert(v.begin() + a, a + 1); } for(const auto& i : v)cout << i << endl; }
### Prompt Please provide a cpp coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ unsigned long long N; cin >> N; vector<unsigned long long> v; for(unsigned long long i = 0, a; i < N; ++i){ cin >> a; --a; if(i < a)return 0 & puts("-1"); v.insert(v.begin() + a, a + 1); } for(const auto& i : v)cout << i << endl; } ```
#include <bits/stdc++.h> using namespace std; int n, arr[200], x; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; ++i) { cin >> x; if (x > i) { cout << -1; return 0; } for (int j = i; j > x; --j) { swap(arr[j], arr[j - 1]); } arr[x] = x; } for (int i = 1; i <= n; ++i) { cout << arr[i] << '\n'; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, arr[200], x; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; ++i) { cin >> x; if (x > i) { cout << -1; return 0; } for (int j = i; j > x; --j) { swap(arr[j], arr[j - 1]); } arr[x] = x; } for (int i = 1; i <= n; ++i) { cout << arr[i] << '\n'; } return 0; } ```
#include<bits/stdc++.h> using namespace std; int main() { int n,i,j,flag=0; cin>>n; vector<int> a(n); stack<int> b; for(i=0;i<n;i++) cin>>a[i]; while(!a.empty()) { flag=0; for(j=a.size()-1;j>=0;j--) { if(a[j]==j+1) { flag=1; b.push(a[j]); a.erase(a.begin()+j); break; } } if(flag==0) { cout<<-1;return 0; } } while(!b.empty()) { cout<<b.top()<<endl; b.pop(); } return 0; }
### Prompt Create a solution in CPP for the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main() { int n,i,j,flag=0; cin>>n; vector<int> a(n); stack<int> b; for(i=0;i<n;i++) cin>>a[i]; while(!a.empty()) { flag=0; for(j=a.size()-1;j>=0;j--) { if(a[j]==j+1) { flag=1; b.push(a[j]); a.erase(a.begin()+j); break; } } if(flag==0) { cout<<-1;return 0; } } while(!b.empty()) { cout<<b.top()<<endl; b.pop(); } return 0; } ```
#include<cstdio> using namespace std; int n,a[105],ans[105],m; int main() { scanf("%d",&n);m=n;for(int i=1;i<=n;++i) scanf("%d",&a[i]); for(int i=n;i;--i) { for(int j=n;j;--j) if(a[j]==j) {for(int k=j;k<n;++k) a[k]=a[k+1];--n;ans[i]=j;goto loop;} else if(a[j]>j) {printf("-1");return 0;} printf("-1");return 0; loop:; } for(int i=1;i<=m;++i) printf("%d\n",ans[i]); return 0; }
### Prompt Please create a solution in Cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<cstdio> using namespace std; int n,a[105],ans[105],m; int main() { scanf("%d",&n);m=n;for(int i=1;i<=n;++i) scanf("%d",&a[i]); for(int i=n;i;--i) { for(int j=n;j;--j) if(a[j]==j) {for(int k=j;k<n;++k) a[k]=a[k+1];--n;ans[i]=j;goto loop;} else if(a[j]>j) {printf("-1");return 0;} printf("-1");return 0; loop:; } for(int i=1;i<=m;++i) printf("%d\n",ans[i]); return 0; } ```
#include <iostream> #include <cstdio> #define N 105 using namespace std; int n, a[N], ans[N]; int main() { int i, j, k; cin >> n; for (i = 1; i <= n; i++) scanf("%d", &a[i]); for (i = n; i > 0; i--) { for (j = n, k = i; j > 0; j--) { if (a[j]) { if (a[j] == k) {ans[i] = k; a[j] = 0; break;} k--; } } if (j == 0) {puts("-1"); return 0;} } for (i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> #include <cstdio> #define N 105 using namespace std; int n, a[N], ans[N]; int main() { int i, j, k; cin >> n; for (i = 1; i <= n; i++) scanf("%d", &a[i]); for (i = n; i > 0; i--) { for (j = n, k = i; j > 0; j--) { if (a[j]) { if (a[j] == k) {ans[i] = k; a[j] = 0; break;} k--; } } if (j == 0) {puts("-1"); return 0;} } for (i = 1; i <= n; i++) printf("%d\n", ans[i]); return 0; } ```
#include<bits/stdc++.h> using namespace std; int main(){ int N,brk; cin>>N; vector<int> b(N),ans(N); for(auto&& c:b)cin>>c; for(int i=N-1;i>=0;--i){ brk=0; for(int j=i;j>=0;--j){ if(b[j]==j+1){ ans[i]=j+1; b.erase(b.begin()+j); brk=1; break; } } if(brk)continue; cout<<-1; return 0; } for(auto&& c:ans)cout<<c<<"\n"; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ int N,brk; cin>>N; vector<int> b(N),ans(N); for(auto&& c:b)cin>>c; for(int i=N-1;i>=0;--i){ brk=0; for(int j=i;j>=0;--j){ if(b[j]==j+1){ ans[i]=j+1; b.erase(b.begin()+j); brk=1; break; } } if(brk)continue; cout<<-1; return 0; } for(auto&& c:ans)cout<<c<<"\n"; return 0; } ```
#include <bits/stdc++.h> typedef long long ll; using namespace std; #define all(a) (a).begin(),(a).end() #define rep(i,j) for(ll i=0;i<j;i++) #define repf(i,m,j) for(ll i=m;i<j;i++) #define pb push_back #define sort(a) sort(all(a)) int main() { vector<int> a(100); vector<int> b(100); ll N; ll num; cin>>N; rep(i,N)cin>>b.at(i); rep(i,N){ if(i+1>=b.at(i)) a.insert(a.begin()+(b.at(i)-1),b.at(i)); else{ cout<<-1<<endl; return EXIT_SUCCESS; } } rep(i,N) cout<<a.at(i)<<endl; }
### Prompt Construct a cpp code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> typedef long long ll; using namespace std; #define all(a) (a).begin(),(a).end() #define rep(i,j) for(ll i=0;i<j;i++) #define repf(i,m,j) for(ll i=m;i<j;i++) #define pb push_back #define sort(a) sort(all(a)) int main() { vector<int> a(100); vector<int> b(100); ll N; ll num; cin>>N; rep(i,N)cin>>b.at(i); rep(i,N){ if(i+1>=b.at(i)) a.insert(a.begin()+(b.at(i)-1),b.at(i)); else{ cout<<-1<<endl; return EXIT_SUCCESS; } } rep(i,N) cout<<a.at(i)<<endl; } ```
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; if (a[i] > i) { cout << -1 << endl; return 0; } } vector<int> ans(n); for (int i = n - 1; i >= 0; i--) { for (int j = i; j >= 0; j--) { if (a[j] == j) { ans[i] = j + 1; for (int k = j; k < i; k++) { a[k] = a[k + 1]; } break; } } } for (int i = 0; i < n; i++) { cout << ans[i] << endl; } return 0; }
### Prompt Please formulate a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; if (a[i] > i) { cout << -1 << endl; return 0; } } vector<int> ans(n); for (int i = n - 1; i >= 0; i--) { for (int j = i; j >= 0; j--) { if (a[j] == j) { ans[i] = j + 1; for (int k = j; k < i; k++) { a[k] = a[k + 1]; } break; } } } for (int i = 0; i < n; i++) { cout << ans[i] << endl; } return 0; } ```
#include <bits/stdc++.h> #define MAXN 300000 #define ll long long using namespace std; int melhor[MAXN], power[MAXN], escola[MAXN]; int main() { int n; vector <int> arr; int num; bool ruim = false; scanf("%d", &n); for (int i=0; i<n; i++){ scanf("%d", &num); if (arr.size() < num-1){ ruim = true; } else { arr.insert(arr.begin()+num-1, num); } } if (ruim){ printf("%d\n", -1); } else { for (int i=0; i<n; i++){ printf("%d\n", arr[i]); } } return 0; }
### Prompt Create a solution in Cpp for the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> #define MAXN 300000 #define ll long long using namespace std; int melhor[MAXN], power[MAXN], escola[MAXN]; int main() { int n; vector <int> arr; int num; bool ruim = false; scanf("%d", &n); for (int i=0; i<n; i++){ scanf("%d", &num); if (arr.size() < num-1){ ruim = true; } else { arr.insert(arr.begin()+num-1, num); } } if (ruim){ printf("%d\n", -1); } else { for (int i=0; i<n; i++){ printf("%d\n", arr[i]); } } return 0; } ```
#include<bits/stdc++.h> using namespace std; int main(){ long N; cin >> N; vector<long> v; for(long i = 1, a; i <= N; ++i){ cin >> a; if(i < a)return 0 & puts("-1"); v.insert(v.begin() + a - 1, a); } for(const auto& i : v)cout << i << endl; }
### Prompt Please formulate a Cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int main(){ long N; cin >> N; vector<long> v; for(long i = 1, a; i <= N; ++i){ cin >> a; if(i < a)return 0 & puts("-1"); v.insert(v.begin() + a - 1, a); } for(const auto& i : v)cout << i << endl; } ```
#include<iostream> #include<cstdio> using namespace std; int n,b[105],op[105],flag; int main(){ ios::sync_with_stdio(false); int i,j,cnt; cin>>n; for(i=1;i<=n;i++) cin>>b[i]; for(i=n;i;i--){ cnt=0; for(j=1;j<cnt+b[i]+(bool)op[j];j++) cnt+=(bool)op[j]; op[j]=b[i]; } for(i=1;i<=n;i++) flag|=op[i]==0; if(flag) cout<<-1; else for(i=1;i<=n;i++) cout<<op[i]<<endl; return 0; }
### Prompt Please formulate a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<iostream> #include<cstdio> using namespace std; int n,b[105],op[105],flag; int main(){ ios::sync_with_stdio(false); int i,j,cnt; cin>>n; for(i=1;i<=n;i++) cin>>b[i]; for(i=n;i;i--){ cnt=0; for(j=1;j<cnt+b[i]+(bool)op[j];j++) cnt+=(bool)op[j]; op[j]=b[i]; } for(i=1;i<=n;i++) flag|=op[i]==0; if(flag) cout<<-1; else for(i=1;i<=n;i++) cout<<op[i]<<endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vector<int> a,b(n); for(int i=0; i<n; i++) cin>>b[i]; for(int i=n-1; i>=0; i--){ if(b[i]==i+1) { a.push_back(b[i]); b.erase(b.begin()+i); i = b.size(); } } if(b.size() != 0) { cout<<-1; return 0; } reverse(a.begin(),a.end()); for(int i=0; i<a.size(); i++) cout<<a[i]<<endl; }
### Prompt Create a solution in cpp for the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vector<int> a,b(n); for(int i=0; i<n; i++) cin>>b[i]; for(int i=n-1; i>=0; i--){ if(b[i]==i+1) { a.push_back(b[i]); b.erase(b.begin()+i); i = b.size(); } } if(b.size() != 0) { cout<<-1; return 0; } reverse(a.begin(),a.end()); for(int i=0; i<a.size(); i++) cout<<a[i]<<endl; } ```
#include<bits/stdc++.h> using namespace std; int N; bool rec(int n, list<int>&l){ if(n == 0)return true; auto it = l.begin(); for(int i = n; i > 0; --i){ if(*it == i){ l.erase(it); if(!rec(n - 1, l))return false; cout << i << endl; return true; } it++; } return false; } int main(){ cin >> N; list<int> l; for(int i = 0 ; i < N; ++i){ int x; cin >> x; l.push_front(x); } if(!rec(N, l))cout << -1 << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int N; bool rec(int n, list<int>&l){ if(n == 0)return true; auto it = l.begin(); for(int i = n; i > 0; --i){ if(*it == i){ l.erase(it); if(!rec(n - 1, l))return false; cout << i << endl; return true; } it++; } return false; } int main(){ cin >> N; list<int> l; for(int i = 0 ; i < N; ++i){ int x; cin >> x; l.push_front(x); } if(!rec(N, l))cout << -1 << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> b; for (int i = 1; i <= n; i++) { int t; cin >> t; if (t > i) { cout << -1 << endl; return 0; } b.insert(b.begin() + t - 1, t); } for (auto x:b) cout << x << endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> b; for (int i = 1; i <= n; i++) { int t; cin >> t; if (t > i) { cout << -1 << endl; return 0; } b.insert(b.begin() + t - 1, t); } for (auto x:b) cout << x << endl; return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; int main(){ int n;cin>>n; vector<int> v(n);for(int i=0;i<n;i++)cin>>v[i]; vector<int> ans; for(int i=0;i<n;i++){ int c=0; for(int j=v.size()-1;j>=0;j--){ if(v[j]==j+1){ c=1; ans.push_back(j+1); v.erase(v.begin()+j); break; } } if(c==0){ cout<<-1<<endl; return 0; } } for(int i=ans.size()-1;i>=0;i--)cout<<ans[i]<<endl; }
### Prompt Your challenge is to write a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; int main(){ int n;cin>>n; vector<int> v(n);for(int i=0;i<n;i++)cin>>v[i]; vector<int> ans; for(int i=0;i<n;i++){ int c=0; for(int j=v.size()-1;j>=0;j--){ if(v[j]==j+1){ c=1; ans.push_back(j+1); v.erase(v.begin()+j); break; } } if(c==0){ cout<<-1<<endl; return 0; } } for(int i=ans.size()-1;i>=0;i--)cout<<ans[i]<<endl; } ```
#include<cstdio> #define RI register int #define CI const int& using namespace std; const int N=105; int n,tn,a[N],ans[N]; int main() { RI i,j; for (scanf("%d",&n),i=1;i<=n;++i) scanf("%d",&a[i]); tn=n; while (n) { int pos=0; for (i=n;i;--i) if (a[i]==i) { pos=i; break; } if (!pos) return puts("-1"),0; ans[n]=pos; for (i=pos;i<n;++i) a[i]=a[i+1]; --n; } for (i=1;i<=tn;++i) printf("%d\n",ans[i]); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<cstdio> #define RI register int #define CI const int& using namespace std; const int N=105; int n,tn,a[N],ans[N]; int main() { RI i,j; for (scanf("%d",&n),i=1;i<=n;++i) scanf("%d",&a[i]); tn=n; while (n) { int pos=0; for (i=n;i;--i) if (a[i]==i) { pos=i; break; } if (!pos) return puts("-1"),0; ans[n]=pos; for (i=pos;i<n;++i) a[i]=a[i+1]; --n; } for (i=1;i<=tn;++i) printf("%d\n",ans[i]); return 0; } ```
#include<bits/stdc++.h> using namespace std; int a[1001]; int b[1001]; int main() { int n,cnt=0,flag=0; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); if(a[i]>i) flag=1; if(!b[a[i]]) b[a[i]]=a[i]; else { for(int j=i-1; j>=a[i]; j--) b[j+1]=b[j]; b[a[i]]=a[i]; } } if(flag) puts("-1"),exit(0); for(int i=1; i<=n; i++) printf("%d\n",b[i]); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int a[1001]; int b[1001]; int main() { int n,cnt=0,flag=0; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); if(a[i]>i) flag=1; if(!b[a[i]]) b[a[i]]=a[i]; else { for(int j=i-1; j>=a[i]; j--) b[j+1]=b[j]; b[a[i]]=a[i]; } } if(flag) puts("-1"),exit(0); for(int i=1; i<=n; i++) printf("%d\n",b[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; vector<int> V; int main() { scanf("%d", &n); for (int i = 1, x; i <= n; i++) { scanf("%d", &x); if (x > i) printf("-1\n"), exit(0); V.insert(V.begin() + x - 1, x); } for (int x : V) printf("%d\n", x); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; vector<int> V; int main() { scanf("%d", &n); for (int i = 1, x; i <= n; i++) { scanf("%d", &x); if (x > i) printf("-1\n"), exit(0); V.insert(V.begin() + x - 1, x); } for (int x : V) printf("%d\n", x); return 0; } ```
#include<cstdio> #include<algorithm> using namespace std; int n,i,j,k,a[105],seq[105]; int main() { scanf("%d",&n); for(i=1;i<=n;i++)scanf("%d",&a[i]); for(i=1;i<=n;i++) { for(j=n-i+1;j>0;j--) { if(a[j]==j) { seq[i]=j; for(k=j;k<=n-i;k++)a[k]=a[k+1]; goto tag; } } printf("-1\n"); return 0; tag:; } for(i=n;i>0;i--)printf("%d\n",seq[i]); return 0; }
### Prompt Please formulate a CPP solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<cstdio> #include<algorithm> using namespace std; int n,i,j,k,a[105],seq[105]; int main() { scanf("%d",&n); for(i=1;i<=n;i++)scanf("%d",&a[i]); for(i=1;i<=n;i++) { for(j=n-i+1;j>0;j--) { if(a[j]==j) { seq[i]=j; for(k=j;k<=n-i;k++)a[k]=a[k+1]; goto tag; } } printf("-1\n"); return 0; tag:; } for(i=n;i>0;i--)printf("%d\n",seq[i]); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> p(N); vector<int> b(N); bool can=true; for(int i=0;i<N;i++)cin >> b[i]; for(int i=0;i<N && can;i++){ for(int j=b.size()-1;j>=0;j--){ if(b[j]==j+1){ b.erase(b.begin()+j); p[i]=j+1; break; } else if(j==0){ can=false; break; } } } if(can){ for(int i=N-1;i>=0;i--)cout << p[i] << endl; } else cout << -1; }
### Prompt Construct a CPP code solution to the problem outlined: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> p(N); vector<int> b(N); bool can=true; for(int i=0;i<N;i++)cin >> b[i]; for(int i=0;i<N && can;i++){ for(int j=b.size()-1;j>=0;j--){ if(b[j]==j+1){ b.erase(b.begin()+j); p[i]=j+1; break; } else if(j==0){ can=false; break; } } } if(can){ for(int i=N-1;i>=0;i--)cout << p[i] << endl; } else cout << -1; } ```
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<int> b(N); for(int i=0;i<N;i++){ cin >> b.at(i); } vector<int> a; for(int i=0;i<N;i++){ for(int j=b.size()-1;j>=0;j--){ if(b.at(j)==j+1){ a.push_back(j+1); b.erase(b.begin()+j); break; } } } reverse(a.begin(),a.end()); if(a.size()==N){ for(int k=0;k<N;k++){ cout << a.at(k) << endl; } }else{ cout << -1 << endl; } }
### Prompt Please create a solution in cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<int> b(N); for(int i=0;i<N;i++){ cin >> b.at(i); } vector<int> a; for(int i=0;i<N;i++){ for(int j=b.size()-1;j>=0;j--){ if(b.at(j)==j+1){ a.push_back(j+1); b.erase(b.begin()+j); break; } } } reverse(a.begin(),a.end()); if(a.size()==N){ for(int k=0;k<N;k++){ cout << a.at(k) << endl; } }else{ cout << -1 << endl; } } ```
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) lli n; int main(void){ cin >> n; vector<lli> b(n+1); for(int i = 1; i <= n; i++){ cin >> b[i]; } stack<lli> st; while(n > 0 && b[1] == 1){ for(int i = n; i >= 1; i--){ if(i == b[i]){ st.push(b[i]); b.erase(b.begin()+i); n--; break; } } } if(n != 0) cout << -1 << endl; else{ while(!st.empty()){ cout << st.top() << endl; st.pop(); } } return 0; }
### Prompt Please create a solution in cpp to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) lli n; int main(void){ cin >> n; vector<lli> b(n+1); for(int i = 1; i <= n; i++){ cin >> b[i]; } stack<lli> st; while(n > 0 && b[1] == 1){ for(int i = n; i >= 1; i--){ if(i == b[i]){ st.push(b[i]); b.erase(b.begin()+i); n--; break; } } } if(n != 0) cout << -1 << endl; else{ while(!st.empty()){ cout << st.top() << endl; st.pop(); } } return 0; } ```
#include<bits/stdc++.h> using namespace std; int N; int main(){ vector < int > arr; cin >> N; for(int i = 1 ; i <= N ; ++i){int x; cin >> x; arr.push_back(x);} vector < int > op; while(!arr.empty()){ int t = -1; for(int i = 0 ; i < arr.size() ; ++i) if(arr[i] == i + 1) t = i; if(t == -1){puts("-1"); return 0;} op.push_back(t + 1); arr.erase(arr.begin() + t); } reverse(op.begin() , op.end()); for(auto t : op) cout << t << endl; return 0; }
### Prompt Generate a cpp solution to the following problem: Snuke has an empty sequence a. He will perform N operations on this sequence. In the i-th operation, he chooses an integer j satisfying 1 \leq j \leq i, and insert j at position j in a (the beginning is position 1). You are given a sequence b of length N. Determine if it is possible that a is equal to b after N operations. If it is, show one possible sequence of operations that achieves it. Constraints * All values in input are integers. * 1 \leq N \leq 100 * 1 \leq b_i \leq N Input Input is given from Standard Input in the following format: N b_1 \dots b_N Output If there is no sequence of N operations after which a would be equal to b, print `-1`. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted. Examples Input 3 1 2 1 Output 1 1 2 Input 2 2 2 Output -1 Input 9 1 1 1 2 2 1 2 3 2 Output 1 2 2 3 1 2 2 1 1 ### Response ```cpp #include<bits/stdc++.h> using namespace std; int N; int main(){ vector < int > arr; cin >> N; for(int i = 1 ; i <= N ; ++i){int x; cin >> x; arr.push_back(x);} vector < int > op; while(!arr.empty()){ int t = -1; for(int i = 0 ; i < arr.size() ; ++i) if(arr[i] == i + 1) t = i; if(t == -1){puts("-1"); return 0;} op.push_back(t + 1); arr.erase(arr.begin() + t); } reverse(op.begin() , op.end()); for(auto t : op) cout << t << endl; return 0; } ```
using namespace std; #include <cstdio> #include <cstring> #include <algorithm> #define N 610 #define ll long long #define mo 1000000007 int n,K; int to[N],c[N]; bool bz[N][N]; int f[N][N],g[N]; int main(){ scanf("%d%d",&n,&K); for (int i=1;i<=K;++i){ int a,b; scanf("%d%d",&a,&b); if (a>b) swap(a,b); to[a]=b,to[b]=a; for (int i=a+1;i<=b;++i) for (int j=b;j<=2*n;++j) bz[i][j]=1; for (int j=a;j<=b-1;++j) for (int i=1;i<=a;++i) bz[i][j]=1; } for (int i=1;i<=2*n;++i) c[i]=c[i-1]+(to[i]==0); g[0]=1; for (int i=2;i<=2*n;i+=2) g[i]=(ll)g[i-2]*(i-1)%mo; for (int l=2*n;l>=1;--l){ for (int r=l+1;r<=2*n;r+=2){ if (bz[l][r]) continue; ll s=0; for (int i=l+1;i<r;i+=2) (s+=(ll)f[l][i]*g[c[r]-c[i]])%=mo; f[l][r]=(g[c[r]-c[l-1]]-s+mo)%mo; } } ll ans=0; for (int i=1;i<=2*n;++i) for (int j=i+1;j<=2*n;j+=2) if (f[i][j]) (ans+=(ll)f[i][j]*g[c[2*n]-(c[j]-c[i-1])])%=mo; printf("%lld\n",ans); return 0; }
### Prompt Generate a CPP solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp using namespace std; #include <cstdio> #include <cstring> #include <algorithm> #define N 610 #define ll long long #define mo 1000000007 int n,K; int to[N],c[N]; bool bz[N][N]; int f[N][N],g[N]; int main(){ scanf("%d%d",&n,&K); for (int i=1;i<=K;++i){ int a,b; scanf("%d%d",&a,&b); if (a>b) swap(a,b); to[a]=b,to[b]=a; for (int i=a+1;i<=b;++i) for (int j=b;j<=2*n;++j) bz[i][j]=1; for (int j=a;j<=b-1;++j) for (int i=1;i<=a;++i) bz[i][j]=1; } for (int i=1;i<=2*n;++i) c[i]=c[i-1]+(to[i]==0); g[0]=1; for (int i=2;i<=2*n;i+=2) g[i]=(ll)g[i-2]*(i-1)%mo; for (int l=2*n;l>=1;--l){ for (int r=l+1;r<=2*n;r+=2){ if (bz[l][r]) continue; ll s=0; for (int i=l+1;i<r;i+=2) (s+=(ll)f[l][i]*g[c[r]-c[i]])%=mo; f[l][r]=(g[c[r]-c[l-1]]-s+mo)%mo; } } ll ans=0; for (int i=1;i<=2*n;++i) for (int j=i+1;j<=2*n;j+=2) if (f[i][j]) (ans+=(ll)f[i][j]*g[c[2*n]-(c[j]-c[i-1])])%=mo; printf("%lld\n",ans); return 0; } ```
#include<bits/stdc++.h> #define ll long long using namespace std; struct aaa{ ll x,y; }a[1010]; const ll p=1e9+7; ll ans,n,m,i,j,l,r,flag,sz,in,out,b[1010],f[602][602],g[602][602]; int main(){ scanf("%lld%lld",&n,&m); for(i=1;i<=m;i++)scanf("%lld%lld",&a[i].x,&a[i].y); b[0]=1; for(i=1;i<=n;i++)b[i]=b[i-1]*(i*2-1)%p; for(l=n*2;l;l--) for(r=l;r<=n*2;r++)if((r-l+1)%2==0){ flag=in=out=0; for(i=1;i<=m;i++){ sz=0; if(a[i].x>=l&&a[i].x<=r)sz++; if(a[i].y>=l&&a[i].y<=r)sz++; if(sz==1)flag=1; if(sz==0)out++; else in++; } if(flag)continue; f[l][r]=g[l][r]=b[(r-l+1)/2-in]; for(i=l;i<r;i++)f[l][r]=(f[l][r]-f[l][i]*g[i+1][r]%p+p)%p; ans=(ans+f[l][r]*b[n-(r-l+1)/2-out])%p; } printf("%lld",ans); }
### Prompt Your task is to create a CPP solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> #define ll long long using namespace std; struct aaa{ ll x,y; }a[1010]; const ll p=1e9+7; ll ans,n,m,i,j,l,r,flag,sz,in,out,b[1010],f[602][602],g[602][602]; int main(){ scanf("%lld%lld",&n,&m); for(i=1;i<=m;i++)scanf("%lld%lld",&a[i].x,&a[i].y); b[0]=1; for(i=1;i<=n;i++)b[i]=b[i-1]*(i*2-1)%p; for(l=n*2;l;l--) for(r=l;r<=n*2;r++)if((r-l+1)%2==0){ flag=in=out=0; for(i=1;i<=m;i++){ sz=0; if(a[i].x>=l&&a[i].x<=r)sz++; if(a[i].y>=l&&a[i].y<=r)sz++; if(sz==1)flag=1; if(sz==0)out++; else in++; } if(flag)continue; f[l][r]=g[l][r]=b[(r-l+1)/2-in]; for(i=l;i<r;i++)f[l][r]=(f[l][r]-f[l][i]*g[i+1][r]%p+p)%p; ans=(ans+f[l][r]*b[n-(r-l+1)/2-out])%p; } printf("%lld",ans); } ```
#include <bits/stdc++.h> using namespace std; const int zzy = 1000000007; int dp[610][610], n, k; int g[610], sum[610], to[610]; int main() { scanf("%d%d", &n, &k); g[0] = 1; for (int i = 2; i <= 2 * n; i += 2) g[i] = 1ll * (i - 1) * g[i - 2] % zzy; for (int i = 1; i <= k; i++) { int a, b; scanf("%d%d", &a, &b); sum[a] = sum[b] = 1; to[a] = b, to[b] = a; } for (int i = 1; i <= 2 * n; i++) sum[i] += sum[i - 1]; for (int i = 2 * n; i --> 1; ) { for (int j = i + 1; j <= 2 * n; j++) { int flag = 0; for (int k = i; k <= j; k++) if (to[k] && to[k] < i || to[k] > j) flag = 1; if (flag) continue; dp[i][j] = g[(j - i + 1) - (sum[j] - sum[i - 1])]; for (int k = i; k < j; k++) dp[i][j] = (dp[i][j] - 1ll * dp[i][k] * g[(j - k) - (sum[j] - sum[k])] % zzy + zzy) % zzy; } } int ans = 0; for (int i = 1; i < 2 * n; i++) for (int j = i + 1; j <= 2 * n; j++) ans = (ans + 1ll * dp[i][j] * g[2 * n - (j - i + 1) - sum[i - 1] - (sum[2 * n] - sum[j])]) % zzy; return cout << ans << endl, 0; }
### Prompt In CPP, your task is to solve the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int zzy = 1000000007; int dp[610][610], n, k; int g[610], sum[610], to[610]; int main() { scanf("%d%d", &n, &k); g[0] = 1; for (int i = 2; i <= 2 * n; i += 2) g[i] = 1ll * (i - 1) * g[i - 2] % zzy; for (int i = 1; i <= k; i++) { int a, b; scanf("%d%d", &a, &b); sum[a] = sum[b] = 1; to[a] = b, to[b] = a; } for (int i = 1; i <= 2 * n; i++) sum[i] += sum[i - 1]; for (int i = 2 * n; i --> 1; ) { for (int j = i + 1; j <= 2 * n; j++) { int flag = 0; for (int k = i; k <= j; k++) if (to[k] && to[k] < i || to[k] > j) flag = 1; if (flag) continue; dp[i][j] = g[(j - i + 1) - (sum[j] - sum[i - 1])]; for (int k = i; k < j; k++) dp[i][j] = (dp[i][j] - 1ll * dp[i][k] * g[(j - k) - (sum[j] - sum[k])] % zzy + zzy) % zzy; } } int ans = 0; for (int i = 1; i < 2 * n; i++) for (int j = i + 1; j <= 2 * n; j++) ans = (ans + 1ll * dp[i][j] * g[2 * n - (j - i + 1) - sum[i - 1] - (sum[2 * n] - sum[j])]) % zzy; return cout << ans << endl, 0; } ```
#include<bits/stdc++.h> #define mp make_pair #define pb push_back #define fi first #define se second #define sz(a) int(a.size()) #define clr(a) memset(a,0,sizeof(a)) #define all(a) a.begin(),a.end() using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,int> pli; const int inf=1e9; const ll Inf=1e18; const int N=610; const int mod=1e9+7; int gi() { int x=0,o=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar(); if(ch=='-') o=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*o; } template<typename T> bool chkmax(T &a,T b) { return a<b?a=b,1:0; }; template<typename T> bool chkmin(T &a,T b) { return a>b?a=b,1:0; }; int add(int a,int b) { return a+b>=mod?a+b-mod:a+b; } int sub(int a,int b) { return a-b<0?a-b+mod:a-b; } void inc(int &a,int b) { a=(a+b>=mod?a+b-mod:a+b); } void dec(int &a,int b) { a=(a-b<0?a-b+mod:a-b); } int n,m,a[N],b[N],f[N][N],g[N][N],h[N],ans=0; int main() { n=gi(),m=gi(); for(int i=1;i<=m;i++) a[i]=gi(),b[i]=gi(); h[0]=1; for(int i=1;i<=n;i++) h[i]=1ll*h[i-1]*(2*i-1)%mod; for(int l=2*n;l;l--) for(int r=l+1;r<=2*n;r+=2) { int fl=1,in=0,out=0; for(int i=1;i<=m;i++) { int op=(l<=a[i]&&a[i]<=r)+(l<=b[i]&&b[i]<=r); if(op==1) { fl=0;break; } if(op==2) ++in; } if(fl) { in=(r-l+1)/2-in,out=n-m-in; f[l][r]=g[l][r]=h[in]; for(int i=l+1;i<r;i+=2) dec(f[l][r],1ll*f[l][i]*g[i+1][r]%mod); inc(ans,1ll*f[l][r]*h[out]%mod); } } printf("%d\n",ans); return 0; }
### Prompt Construct a CPP code solution to the problem outlined: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> #define mp make_pair #define pb push_back #define fi first #define se second #define sz(a) int(a.size()) #define clr(a) memset(a,0,sizeof(a)) #define all(a) a.begin(),a.end() using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,int> pli; const int inf=1e9; const ll Inf=1e18; const int N=610; const int mod=1e9+7; int gi() { int x=0,o=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar(); if(ch=='-') o=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*o; } template<typename T> bool chkmax(T &a,T b) { return a<b?a=b,1:0; }; template<typename T> bool chkmin(T &a,T b) { return a>b?a=b,1:0; }; int add(int a,int b) { return a+b>=mod?a+b-mod:a+b; } int sub(int a,int b) { return a-b<0?a-b+mod:a-b; } void inc(int &a,int b) { a=(a+b>=mod?a+b-mod:a+b); } void dec(int &a,int b) { a=(a-b<0?a-b+mod:a-b); } int n,m,a[N],b[N],f[N][N],g[N][N],h[N],ans=0; int main() { n=gi(),m=gi(); for(int i=1;i<=m;i++) a[i]=gi(),b[i]=gi(); h[0]=1; for(int i=1;i<=n;i++) h[i]=1ll*h[i-1]*(2*i-1)%mod; for(int l=2*n;l;l--) for(int r=l+1;r<=2*n;r+=2) { int fl=1,in=0,out=0; for(int i=1;i<=m;i++) { int op=(l<=a[i]&&a[i]<=r)+(l<=b[i]&&b[i]<=r); if(op==1) { fl=0;break; } if(op==2) ++in; } if(fl) { in=(r-l+1)/2-in,out=n-m-in; f[l][r]=g[l][r]=h[in]; for(int i=l+1;i<r;i+=2) dec(f[l][r],1ll*f[l][i]*g[i+1][r]%mod); inc(ans,1ll*f[l][r]*h[out]%mod); } } printf("%d\n",ans); return 0; } ```
#include<bits/stdc++.h> using namespace std; #define rep(i,s,t) for(int i=(s);i<(t);++i) #define per(i,s,t) for(int i=((t)-1);i>=s;--i) #define repb(i,s,t) for(int i=(s);i<=(t);++i) #define lepb(i,s,t) for(int i=(s);i>=(t);--i) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define sz(x) ((int)x.size()) #define mst(a,b) memset(a,b,sizeof(a)) #define dd(x) cout<<#x<<'='<<x<<' ' #define de(x) cout<<#x<<'='<<x<<'\n' #define fi first #define se second #define sq(x) ((x)*(x)) typedef long long ll; typedef pair<int,int> pii; typedef double db; const int inf = 0x3f3f3f3f; const ll inff = 4557430888798830399ll; const db eps = 1e-10; const db pi = acos(-1.0); const ll mod = 1e9+7; int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n,k,a,b,o,s,G[700],cnt[700][700]; ll z[700],dp[700][700]; mst(z,0);z[0]=1; for(int i=2;i<=700;i+=2)z[i]=z[i-2]*(i-1)%mod; mst(G,0);s=0; cin>>n>>k; rep(i,0,k) { cin>>a>>b; G[a]=b;G[b]=a; } n*=2;k*=2;mst(dp,0);mst(cnt,0); repb(i,1,n)repb(j,i,n)cnt[i][j]=cnt[i][j-1]+(!G[j]); repb(i,1,n)repb(j,i,n) { o=1;repb(k,i,j)if(G[k]&&(G[k]<i||j<G[k]))o=0; if(!o)continue; dp[i][j]=z[cnt[i][j]]; repb(k,i+1,j-1)(dp[i][j]-=dp[i][k]*z[cnt[k+1][j]]%mod)%=mod; } repb(i,1,n)repb(j,i,n)(s+=dp[i][j]*z[n-k-cnt[i][j]]%mod)%=mod; cout<<(s+mod)%mod<<endl; }
### Prompt Your task is to create a CPP solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define rep(i,s,t) for(int i=(s);i<(t);++i) #define per(i,s,t) for(int i=((t)-1);i>=s;--i) #define repb(i,s,t) for(int i=(s);i<=(t);++i) #define lepb(i,s,t) for(int i=(s);i>=(t);--i) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define sz(x) ((int)x.size()) #define mst(a,b) memset(a,b,sizeof(a)) #define dd(x) cout<<#x<<'='<<x<<' ' #define de(x) cout<<#x<<'='<<x<<'\n' #define fi first #define se second #define sq(x) ((x)*(x)) typedef long long ll; typedef pair<int,int> pii; typedef double db; const int inf = 0x3f3f3f3f; const ll inff = 4557430888798830399ll; const db eps = 1e-10; const db pi = acos(-1.0); const ll mod = 1e9+7; int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); int n,k,a,b,o,s,G[700],cnt[700][700]; ll z[700],dp[700][700]; mst(z,0);z[0]=1; for(int i=2;i<=700;i+=2)z[i]=z[i-2]*(i-1)%mod; mst(G,0);s=0; cin>>n>>k; rep(i,0,k) { cin>>a>>b; G[a]=b;G[b]=a; } n*=2;k*=2;mst(dp,0);mst(cnt,0); repb(i,1,n)repb(j,i,n)cnt[i][j]=cnt[i][j-1]+(!G[j]); repb(i,1,n)repb(j,i,n) { o=1;repb(k,i,j)if(G[k]&&(G[k]<i||j<G[k]))o=0; if(!o)continue; dp[i][j]=z[cnt[i][j]]; repb(k,i+1,j-1)(dp[i][j]-=dp[i][k]*z[cnt[k+1][j]]%mod)%=mod; } repb(i,1,n)repb(j,i,n)(s+=dp[i][j]*z[n-k-cnt[i][j]]%mod)%=mod; cout<<(s+mod)%mod<<endl; } ```
#include<cstdio> #include<algorithm> using namespace std; const int N=602,M=1000000007; typedef long long ll; int n,k,i,j,l,r,x,y,L[N],R[N],cnt[N],num[N][N]; ll g[N],f[N][N],Ans; bool ok; void init(){ scanf("%d%d",&n,&k); n*=2; for(i=1;i<=k;i++){ scanf("%d%d",L+i,R+i); if(L[i]>R[i]) swap(L[i],R[i]); cnt[L[i]]++; cnt[R[i]]++; } for(i=1;i<=n;i++) cnt[i]=1-cnt[i]+cnt[i-1]; for(l=1;l<=n;l++) for(r=l;r<=n;r++) num[l][r]=cnt[r]-cnt[l-1]; } void work(){ g[0]=1; for(i=2;i<=n;i++) g[i]=(i-1)*g[i-2]%M; for(j=1;j<=n;j+=2) for(x=1;x+j<=n;x++){ y=x+j; ok=1; for(i=1;i<=k&&ok;i++){ if(L[i]<x&&x<=R[i]&&R[i]<=y) ok=0; if(x<=L[i]&&L[i]<=y&&y<R[i]) ok=0; } if(ok){ f[x][y]=g[num[x][y]]; for(i=x+1;i<y;i+=2) f[x][y]=(f[x][y]-f[x][i]*g[num[i+1][y]])%M; } Ans=(Ans+f[x][y]*g[num[1][x-1]+num[y+1][n]])%M; } printf("%lld",(Ans+M)%M); } int main(){ init(); work(); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<cstdio> #include<algorithm> using namespace std; const int N=602,M=1000000007; typedef long long ll; int n,k,i,j,l,r,x,y,L[N],R[N],cnt[N],num[N][N]; ll g[N],f[N][N],Ans; bool ok; void init(){ scanf("%d%d",&n,&k); n*=2; for(i=1;i<=k;i++){ scanf("%d%d",L+i,R+i); if(L[i]>R[i]) swap(L[i],R[i]); cnt[L[i]]++; cnt[R[i]]++; } for(i=1;i<=n;i++) cnt[i]=1-cnt[i]+cnt[i-1]; for(l=1;l<=n;l++) for(r=l;r<=n;r++) num[l][r]=cnt[r]-cnt[l-1]; } void work(){ g[0]=1; for(i=2;i<=n;i++) g[i]=(i-1)*g[i-2]%M; for(j=1;j<=n;j+=2) for(x=1;x+j<=n;x++){ y=x+j; ok=1; for(i=1;i<=k&&ok;i++){ if(L[i]<x&&x<=R[i]&&R[i]<=y) ok=0; if(x<=L[i]&&L[i]<=y&&y<R[i]) ok=0; } if(ok){ f[x][y]=g[num[x][y]]; for(i=x+1;i<y;i+=2) f[x][y]=(f[x][y]-f[x][i]*g[num[i+1][y]])%M; } Ans=(Ans+f[x][y]*g[num[1][x-1]+num[y+1][n]])%M; } printf("%lld",(Ans+M)%M); } int main(){ init(); work(); return 0; } ```
#include<cstdio> #include<cctype> #include<algorithm> #include<iostream> #include<cstring> #define X first #define Y second #define mp make_pair #define pb push_back #define debug(...) fprintf(stderr,__VA_ARGS__) using namespace std; inline char nc(){ static char buf[100000] , *p1,*p2; return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,100000,stdin),p1 == p2) ? EOF : *p1++;} template<class T>inline void rd(T & x){ x = 0;char ch = nc();for(;!isdigit(ch);ch = nc()); for(;isdigit(ch);ch = nc()) x = x * 10 - 48 + ch;} typedef long long ll; const int maxn = 600 + 10; const int mod = 1e9 + 7; int f[maxn][maxn] , g[maxn][maxn] , h[maxn] , a[maxn] , b[maxn]; template<class T>inline T add(T a,T b){ a += b; if(a > mod) a -= mod; return a;} template<class T>inline T sub(T a,T b){ a -= b; if(a < 0) a += mod; return a;} template<class T>inline T mul(T a,T b){ return (ll)a * b % mod;} int main(){ int n,m; rd(n); rd(m); for(int i = 1;i <= m;i++){ rd(a[i]); rd(b[i]); } h[0] = 1; for(int i = 1;i <= n;i++) h[i] = mul(h[i - 1],(i << 1) - 1); int ans = 0; for(int l = n * 2;l;l--){ for(int r = l + 1;r <= n * 2;r += 2){ bool flag = 1; int in = 0; for(int i = 1;i <= m;i++){ int t = (a[i] >= l && a[i] <= r) + (b[i] >= l && b[i] <= r); if(t == 1){ flag = 0; break; } if(t) in++; } if(flag){ in = (r - l + 1 >> 1) - in; int out = n - m - in; f[l][r] = g[l][r] = h[in]; for(int i = l + 1;i < r;i += 2){ f[l][r] = sub(f[l][r],mul(f[l][i],g[i + 1][r])); } ans = add(ans,mul(f[l][r],h[out])); } } } printf("%d\n",ans); }
### Prompt Please formulate a CPP solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<cstdio> #include<cctype> #include<algorithm> #include<iostream> #include<cstring> #define X first #define Y second #define mp make_pair #define pb push_back #define debug(...) fprintf(stderr,__VA_ARGS__) using namespace std; inline char nc(){ static char buf[100000] , *p1,*p2; return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,100000,stdin),p1 == p2) ? EOF : *p1++;} template<class T>inline void rd(T & x){ x = 0;char ch = nc();for(;!isdigit(ch);ch = nc()); for(;isdigit(ch);ch = nc()) x = x * 10 - 48 + ch;} typedef long long ll; const int maxn = 600 + 10; const int mod = 1e9 + 7; int f[maxn][maxn] , g[maxn][maxn] , h[maxn] , a[maxn] , b[maxn]; template<class T>inline T add(T a,T b){ a += b; if(a > mod) a -= mod; return a;} template<class T>inline T sub(T a,T b){ a -= b; if(a < 0) a += mod; return a;} template<class T>inline T mul(T a,T b){ return (ll)a * b % mod;} int main(){ int n,m; rd(n); rd(m); for(int i = 1;i <= m;i++){ rd(a[i]); rd(b[i]); } h[0] = 1; for(int i = 1;i <= n;i++) h[i] = mul(h[i - 1],(i << 1) - 1); int ans = 0; for(int l = n * 2;l;l--){ for(int r = l + 1;r <= n * 2;r += 2){ bool flag = 1; int in = 0; for(int i = 1;i <= m;i++){ int t = (a[i] >= l && a[i] <= r) + (b[i] >= l && b[i] <= r); if(t == 1){ flag = 0; break; } if(t) in++; } if(flag){ in = (r - l + 1 >> 1) - in; int out = n - m - in; f[l][r] = g[l][r] = h[in]; for(int i = l + 1;i < r;i += 2){ f[l][r] = sub(f[l][r],mul(f[l][i],g[i + 1][r])); } ans = add(ans,mul(f[l][r],h[out])); } } } printf("%d\n",ans); } ```
#include<bits/stdc++.h> using namespace std; #define int long long const int N=605,M=1e9+7; int g[N],n,k,x,y,to[N],cnt[N][N],ans,dp[N][N]; signed main(){ scanf("%lld%lld",&n,&k); while (k--){ scanf("%d%d",&x,&y); to[x]=y;to[y]=x; } n<<=1; for (int i=1;i<=n;i++) for (int j=i;j<=n;j++) cnt[i][j]=cnt[i][j-1]+(to[j]==0); g[0]=1; for (int i=2;i<=n;i++)g[i]=g[i-2]*(i-1)%M; for (int i=n;i;i--) for (int j=i;j<=n;j++){ int flag=cnt[i][j]%2==0; for (int k=i;k<=j;k++) if ((to[k]<i||to[k]>j)&&to[k]!=0)flag=0; if (!flag)continue; dp[i][j]=g[cnt[i][j]]; for (int k=i+1;k<j;k++)(dp[i][j]+=M-dp[i][k]*g[cnt[k+1][j]]%M)%=M; } for (int i=1;i<=n;i++) for (int j=i;j<=n;j++) (ans+=dp[i][j]*g[cnt[1][i-1]+cnt[j+1][n]])%=M; printf("%lld\n",ans); }
### Prompt Please create a solution in CPP to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define int long long const int N=605,M=1e9+7; int g[N],n,k,x,y,to[N],cnt[N][N],ans,dp[N][N]; signed main(){ scanf("%lld%lld",&n,&k); while (k--){ scanf("%d%d",&x,&y); to[x]=y;to[y]=x; } n<<=1; for (int i=1;i<=n;i++) for (int j=i;j<=n;j++) cnt[i][j]=cnt[i][j-1]+(to[j]==0); g[0]=1; for (int i=2;i<=n;i++)g[i]=g[i-2]*(i-1)%M; for (int i=n;i;i--) for (int j=i;j<=n;j++){ int flag=cnt[i][j]%2==0; for (int k=i;k<=j;k++) if ((to[k]<i||to[k]>j)&&to[k]!=0)flag=0; if (!flag)continue; dp[i][j]=g[cnt[i][j]]; for (int k=i+1;k<j;k++)(dp[i][j]+=M-dp[i][k]*g[cnt[k+1][j]]%M)%=M; } for (int i=1;i<=n;i++) for (int j=i;j<=n;j++) (ans+=dp[i][j]*g[cnt[1][i-1]+cnt[j+1][n]])%=M; printf("%lld\n",ans); } ```
#include<cstdio> #define mod 1000000007 #define int long long int n,m,a[610],b[610],nxt[610],sum[610],cnt[610][610],dp[610][610],ans; signed main(){ scanf("%lld%lld",&n,&m); for(int i=1;i<=m;i++) scanf("%lld%lld",&a[i],&b[i]), nxt[a[i]]=b[i], nxt[b[i]]=a[i]; n*=2; sum[0]=1; for(int i=2;i<=n;i+=2) sum[i]=(sum[i-2]*(i-1))%mod; for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) cnt[i][j]=cnt[i][j-1]+(!nxt[j]); for(int i=1;i<=n;i++) for(int j=i;j<=n;j++){ bool bo=false; for(int k=i;k<=j;k++) if(nxt[k]&&(nxt[k]>j||nxt[k]<i)) bo=true; if(bo)continue; dp[i][j]=sum[cnt[i][j]]; for(int k=i+1;k<=j-1;k++) dp[i][j]=(dp[i][j]-dp[i][k]*sum[cnt[k+1][j]]%mod+mod)%mod; } for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) (ans+=dp[i][j]*sum[(n/2-m)*2-cnt[i][j]]%mod)%=mod; printf("%lld\n",ans); }
### Prompt Create a solution in cpp for the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<cstdio> #define mod 1000000007 #define int long long int n,m,a[610],b[610],nxt[610],sum[610],cnt[610][610],dp[610][610],ans; signed main(){ scanf("%lld%lld",&n,&m); for(int i=1;i<=m;i++) scanf("%lld%lld",&a[i],&b[i]), nxt[a[i]]=b[i], nxt[b[i]]=a[i]; n*=2; sum[0]=1; for(int i=2;i<=n;i+=2) sum[i]=(sum[i-2]*(i-1))%mod; for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) cnt[i][j]=cnt[i][j-1]+(!nxt[j]); for(int i=1;i<=n;i++) for(int j=i;j<=n;j++){ bool bo=false; for(int k=i;k<=j;k++) if(nxt[k]&&(nxt[k]>j||nxt[k]<i)) bo=true; if(bo)continue; dp[i][j]=sum[cnt[i][j]]; for(int k=i+1;k<=j-1;k++) dp[i][j]=(dp[i][j]-dp[i][k]*sum[cnt[k+1][j]]%mod+mod)%mod; } for(int i=1;i<=n;i++) for(int j=i;j<=n;j++) (ans+=dp[i][j]*sum[(n/2-m)*2-cnt[i][j]]%mod)%=mod; printf("%lld\n",ans); } ```
#include<cstdio> #include<algorithm> using namespace std; int n, K, P[610]; long long C[610], Mod = 1000000007, D[610][610], T[610][610], res; int main() { int i, a, b, j, k; C[0] = 1; for (i = 1; i <= 300; i++)C[i] = C[i - 1] * (2 * i - 1) % Mod; scanf("%d%d", &n, &K); n *= 2; for (i = 0; i < K; i++) { scanf("%d%d", &a, &b); P[a] = b, P[b] = a; } for (i = 1; i <= n; i++) { int c = 0, c2 = 0; for (j = i; j <= n; j++) { if (P[j] < j && P[j] >= i)c++; if (P[j])c2++; if ((j - i) & 1) { if (c * 2 == c2) { T[i][j] = C[(j - i + 1) / 2 - c]; T[j][i] = C[(n - (j-i+1)) / 2 - (K - c)]; } } } } for (i = 2; i <= n; i += 2) { for (j = 1; j <= n-i+1; j++) { int b = j, e = j + i - 1; D[b][e] = T[b][e]; long long s = 0; for (k = b + 1; k < e; k += 2) { s += D[b][k] * T[k + 1][e] % Mod; } D[b][e] = (D[b][e] - s%Mod + Mod) % Mod; res += D[b][e] * T[e][b] % Mod; } } printf("%lld\n", res%Mod); }
### Prompt Generate a cpp solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<cstdio> #include<algorithm> using namespace std; int n, K, P[610]; long long C[610], Mod = 1000000007, D[610][610], T[610][610], res; int main() { int i, a, b, j, k; C[0] = 1; for (i = 1; i <= 300; i++)C[i] = C[i - 1] * (2 * i - 1) % Mod; scanf("%d%d", &n, &K); n *= 2; for (i = 0; i < K; i++) { scanf("%d%d", &a, &b); P[a] = b, P[b] = a; } for (i = 1; i <= n; i++) { int c = 0, c2 = 0; for (j = i; j <= n; j++) { if (P[j] < j && P[j] >= i)c++; if (P[j])c2++; if ((j - i) & 1) { if (c * 2 == c2) { T[i][j] = C[(j - i + 1) / 2 - c]; T[j][i] = C[(n - (j-i+1)) / 2 - (K - c)]; } } } } for (i = 2; i <= n; i += 2) { for (j = 1; j <= n-i+1; j++) { int b = j, e = j + i - 1; D[b][e] = T[b][e]; long long s = 0; for (k = b + 1; k < e; k += 2) { s += D[b][k] * T[k + 1][e] % Mod; } D[b][e] = (D[b][e] - s%Mod + Mod) % Mod; res += D[b][e] * T[e][b] % Mod; } } printf("%lld\n", res%Mod); } ```
#include <bits/stdc++.h> using namespace std; const int mod=1000000007; const int N=700; int i,j,k,n,m,x,y,t,ans,dp[N][N],cant[N][N],w[N][N],R[N][N],Y[N][N],g[N],p[N]; int main(){ scanf("%d%d",&n,&m); for (k=1;k<=n*2;k+=2)for (i=1;i<=n*2;i++)if (i+k-1<=n*2)cant[i][i+k-1]=1; for (k=1;k<=m;k++){ scanf("%d%d",&x,&y); p[x]=y;p[y]=x; } g[0]=1;for (i=2;i<=n*2;i+=2)g[i]=1ll*(i-1)*g[i-2]%mod; for (i=1;i<=n*2;i++) for (j=i+1;j<=n*2;j++) if (j-i&1){ for (k=i;k<=j;k++) if (p[k]&&(p[k]<i||p[k]>j)){ cant[i][j]=1;break; } else if (p[k])w[i][j]++; } for (i=1;i<=n*2;i++) for (j=i+1;j<=n*2;j++) if (!cant[i][j]) R[i][j]=j-i+1-w[i][j], Y[i][j]=n*2-(2*m-w[i][j])-(j-i+1); for (t=2;t<=n*2;t+=2) for (i=1;i<=n*2-t+1;i++){ j=i+t-1; if (!cant[i][j]){ dp[i][j]=1ll*g[R[i][j]]%mod; for (k=i+1;k<j;k++)dp[i][j]=(dp[i][j]-1ll*dp[i][k]*g[R[k+1][j]]%mod+mod)%mod; ans=(ans+1ll*dp[i][j]*g[Y[i][j]]%mod)%mod; // printf("%d %d %d %d\n",i,j,dp[i][j],w[i][j]); } } printf("%d\n",ans); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod=1000000007; const int N=700; int i,j,k,n,m,x,y,t,ans,dp[N][N],cant[N][N],w[N][N],R[N][N],Y[N][N],g[N],p[N]; int main(){ scanf("%d%d",&n,&m); for (k=1;k<=n*2;k+=2)for (i=1;i<=n*2;i++)if (i+k-1<=n*2)cant[i][i+k-1]=1; for (k=1;k<=m;k++){ scanf("%d%d",&x,&y); p[x]=y;p[y]=x; } g[0]=1;for (i=2;i<=n*2;i+=2)g[i]=1ll*(i-1)*g[i-2]%mod; for (i=1;i<=n*2;i++) for (j=i+1;j<=n*2;j++) if (j-i&1){ for (k=i;k<=j;k++) if (p[k]&&(p[k]<i||p[k]>j)){ cant[i][j]=1;break; } else if (p[k])w[i][j]++; } for (i=1;i<=n*2;i++) for (j=i+1;j<=n*2;j++) if (!cant[i][j]) R[i][j]=j-i+1-w[i][j], Y[i][j]=n*2-(2*m-w[i][j])-(j-i+1); for (t=2;t<=n*2;t+=2) for (i=1;i<=n*2-t+1;i++){ j=i+t-1; if (!cant[i][j]){ dp[i][j]=1ll*g[R[i][j]]%mod; for (k=i+1;k<j;k++)dp[i][j]=(dp[i][j]-1ll*dp[i][k]*g[R[k+1][j]]%mod+mod)%mod; ans=(ans+1ll*dp[i][j]*g[Y[i][j]]%mod)%mod; // printf("%d %d %d %d\n",i,j,dp[i][j],w[i][j]); } } printf("%d\n",ans); return 0; } ```
#include<bits/stdc++.h> using namespace std; #define maxn 620 #define rep(i,l,r) for(register int i = l ; i <= r ; i++) #define repd(i,r,l) for(register int i = r ; i >= l ; i--) #define rvc(i,S) for(register int i = 0 ; i < (int)S.size() ; i++) #define rvcd(i,S) for(register int i = ((int)S.size()) - 1 ; i >= 0 ; i--) #define fore(i,x)for (register int i = head[x] ; i ; i = e[i].next) #define pb push_back #define prev prev_ #define stack stack_ #define mp make_pair #define fi first #define se second #define inf 0x3f3f3f3f typedef long long ll; typedef pair<int,int> pr; const ll mod = 1e9 + 7; ll f[maxn][maxn],g[maxn * 2]; int tag[maxn],n,k,cnt[maxn][maxn]; int main(){ scanf("%d %d",&n,&k); rep(i,1,n * 2) tag[i] = -1; rep(i,1,k){ int x,y; scanf("%d %d",&x,&y); tag[x] = y; tag[y] = x; } n *= 2 , k *= 2; g[0] = 1; rep(i,2,n) if ( !(i & 1) ) g[i] = g[i - 2] * (i - 1) % mod; ll ans = 0; rep(i,1,n){ rep(j,i,n){ cnt[i][j] = cnt[i][j - 1] + (tag[j] == -1); } } rep(i,1,n){ int mn = n + 1 , mx = 0; if ( ~tag[i] ) mn = mx = tag[i]; rep(j,i + 1,n){ if ( ~tag[j] ) mn = min(mn,tag[j]) , mx = max(mx,tag[j]); if ( mn < i ) break; if ( mx > j || (cnt[i][j] & 1) ) continue; f[i][j] = g[cnt[i][j]]; rep(k,i + 1,j - 1) f[i][j] = (f[i][j] - f[i][k] * g[cnt[i][j] - cnt[i][k]] % mod + mod) % mod; ans = (ans + f[i][j] * g[n - k - cnt[i][j]]) % mod; } } printf("%lld\n",ans); }
### Prompt Please formulate a CPP solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define maxn 620 #define rep(i,l,r) for(register int i = l ; i <= r ; i++) #define repd(i,r,l) for(register int i = r ; i >= l ; i--) #define rvc(i,S) for(register int i = 0 ; i < (int)S.size() ; i++) #define rvcd(i,S) for(register int i = ((int)S.size()) - 1 ; i >= 0 ; i--) #define fore(i,x)for (register int i = head[x] ; i ; i = e[i].next) #define pb push_back #define prev prev_ #define stack stack_ #define mp make_pair #define fi first #define se second #define inf 0x3f3f3f3f typedef long long ll; typedef pair<int,int> pr; const ll mod = 1e9 + 7; ll f[maxn][maxn],g[maxn * 2]; int tag[maxn],n,k,cnt[maxn][maxn]; int main(){ scanf("%d %d",&n,&k); rep(i,1,n * 2) tag[i] = -1; rep(i,1,k){ int x,y; scanf("%d %d",&x,&y); tag[x] = y; tag[y] = x; } n *= 2 , k *= 2; g[0] = 1; rep(i,2,n) if ( !(i & 1) ) g[i] = g[i - 2] * (i - 1) % mod; ll ans = 0; rep(i,1,n){ rep(j,i,n){ cnt[i][j] = cnt[i][j - 1] + (tag[j] == -1); } } rep(i,1,n){ int mn = n + 1 , mx = 0; if ( ~tag[i] ) mn = mx = tag[i]; rep(j,i + 1,n){ if ( ~tag[j] ) mn = min(mn,tag[j]) , mx = max(mx,tag[j]); if ( mn < i ) break; if ( mx > j || (cnt[i][j] & 1) ) continue; f[i][j] = g[cnt[i][j]]; rep(k,i + 1,j - 1) f[i][j] = (f[i][j] - f[i][k] * g[cnt[i][j] - cnt[i][k]] % mod + mod) % mod; ans = (ans + f[i][j] * g[n - k - cnt[i][j]]) % mod; } } printf("%lld\n",ans); } ```
#include <bits/stdc++.h> using namespace std; const int N = 610; const int mod = 1e9 + 7; typedef long long LL; int f[N][N], g[N], G[N], cnt[N][N]; int main() { int n, k; scanf("%d%d", &n, &k), n *= 2; for (int i = 1; i <= k; i++) { int a, b; scanf("%d%d", &a, &b), G[a] = b, G[b] = a; } for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) cnt[i][j] = cnt[i][j - 1] + !G[j]; g[0] = 1; for (int i = 2; i <= n; i += 2) g[i] = (LL)g[i - 2] * (i - 1) % mod; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) { bool flag = true; for (int k = i; k <= j && flag; k++) if (G[k] && (G[k] < i || G[k] > j)) flag = false; if (!flag) continue; f[i][j] = g[cnt[i][j]]; for (int k = i; k < j; k++) f[i][j] = (f[i][j] - (LL)f[i][k] * g[cnt[k + 1][j]] % mod + mod) % mod; } int res = 0; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) res = (res + (LL)f[i][j] * g[n - k * 2 - cnt[i][j]]) % mod; printf("%d\n", res); }
### Prompt Generate a CPP solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 610; const int mod = 1e9 + 7; typedef long long LL; int f[N][N], g[N], G[N], cnt[N][N]; int main() { int n, k; scanf("%d%d", &n, &k), n *= 2; for (int i = 1; i <= k; i++) { int a, b; scanf("%d%d", &a, &b), G[a] = b, G[b] = a; } for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) cnt[i][j] = cnt[i][j - 1] + !G[j]; g[0] = 1; for (int i = 2; i <= n; i += 2) g[i] = (LL)g[i - 2] * (i - 1) % mod; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) { bool flag = true; for (int k = i; k <= j && flag; k++) if (G[k] && (G[k] < i || G[k] > j)) flag = false; if (!flag) continue; f[i][j] = g[cnt[i][j]]; for (int k = i; k < j; k++) f[i][j] = (f[i][j] - (LL)f[i][k] * g[cnt[k + 1][j]] % mod + mod) % mod; } int res = 0; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) res = (res + (LL)f[i][j] * g[n - k * 2 - cnt[i][j]]) % mod; printf("%d\n", res); } ```
#include<bits/stdc++.h> using namespace std; #define ll long long #define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++) #define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--) #define EREP(i,u) for(int i=start[u];i;i=e[i].next) #define fi first #define se second #define mkr(a,b) make_pair(a,b) #define SZ(A) ((int)A.size()) template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;} template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;} inline int read() { int s=0,f=1;char ch=getchar(); while(!isdigit(ch) && ch!='-')ch=getchar(); if(ch=='-')ch=getchar(),f=-1; while(isdigit(ch))s=s*10+ch-'0',ch=getchar(); return ~f?s:-s; } const int maxn=620; const int mod=1e9+7; int n,k; int to[maxn]; int g[maxn]; int s[maxn]; inline void init() { n=read();k=read(); REP(i,1,k) { int a=read(),b=read(); to[a]=b; to[b]=a; } REP(i,1,2*n)s[i]=s[i-1]+(!to[i]); g[0]=1; REP(i,2,2*n)g[i]=(ll)g[i-2]*(i-1)%mod; } int f[maxn][maxn]; int ans; inline void doing() { REP(i,1,2*n-1) { int l=i,r=i+1; if((!to[i] && !to[i+1]) || (to[i]==i+1))f[i][i+1]=1; ans=(ans+(ll)f[l][r]*g[s[2*n]-(s[r]-s[l-1])])%mod; } REP(len,3,2*n) { REP(l,1,2*n-len+1) { int r=l+len-1; if(s[r]-s[l-1]&1)continue; int flg=0; REP(j,l,r)if(to[j] && (to[j]<l || to[j]>r)){flg=1; break;} if(flg)continue; int num=g[(s[r]-s[l-1])]; REP(k,l+1,r-1)num=(num-(ll)f[l][k]*g[s[r]-s[k]])%mod; f[l][r]=(num+mod)%mod; ans=(ans+(ll)f[l][r]*g[s[2*n]-(s[r]-s[l-1])])%mod; } } printf("%d\n",ans); } int main() { init(); doing(); return 0; }
### Prompt Create a solution in cpp for the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define ll long long #define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++) #define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--) #define EREP(i,u) for(int i=start[u];i;i=e[i].next) #define fi first #define se second #define mkr(a,b) make_pair(a,b) #define SZ(A) ((int)A.size()) template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;} template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;} inline int read() { int s=0,f=1;char ch=getchar(); while(!isdigit(ch) && ch!='-')ch=getchar(); if(ch=='-')ch=getchar(),f=-1; while(isdigit(ch))s=s*10+ch-'0',ch=getchar(); return ~f?s:-s; } const int maxn=620; const int mod=1e9+7; int n,k; int to[maxn]; int g[maxn]; int s[maxn]; inline void init() { n=read();k=read(); REP(i,1,k) { int a=read(),b=read(); to[a]=b; to[b]=a; } REP(i,1,2*n)s[i]=s[i-1]+(!to[i]); g[0]=1; REP(i,2,2*n)g[i]=(ll)g[i-2]*(i-1)%mod; } int f[maxn][maxn]; int ans; inline void doing() { REP(i,1,2*n-1) { int l=i,r=i+1; if((!to[i] && !to[i+1]) || (to[i]==i+1))f[i][i+1]=1; ans=(ans+(ll)f[l][r]*g[s[2*n]-(s[r]-s[l-1])])%mod; } REP(len,3,2*n) { REP(l,1,2*n-len+1) { int r=l+len-1; if(s[r]-s[l-1]&1)continue; int flg=0; REP(j,l,r)if(to[j] && (to[j]<l || to[j]>r)){flg=1; break;} if(flg)continue; int num=g[(s[r]-s[l-1])]; REP(k,l+1,r-1)num=(num-(ll)f[l][k]*g[s[r]-s[k]])%mod; f[l][r]=(num+mod)%mod; ans=(ans+(ll)f[l][r]*g[s[2*n]-(s[r]-s[l-1])])%mod; } } printf("%d\n",ans); } int main() { init(); doing(); return 0; } ```
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define FOR(a, b, c) for(int a = b; a <= c; ++a) #define pb push_back #define int long long typedef pair<int, int> ii; const int N = 605; const int mod = 1e9 + 7; int n, k; int sum[N], g[N], dp[N][N], ok[N][N]; ii eg[N]; bool inside(int l, int r, int p1, int p2) { return ((l <= p1 && p1 <= r && l <= p2 && p2 <= r) || (!(l <= p1 && p1 <= r) && !(l <= p2 && p2 <= r))); } void add(int &x, int y) { x = (x + y) % mod; } void mul(int &x, int y) { x = x * y % mod; } void sub(int &x, int y) { x = (x % mod - y % mod); if(x < 0) x += mod; } signed main() { //freopen("test.inp", "r", stdin); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; n <<= 1; FOR(i, 1, k) { int u, v; cin >> u >> v; sum[u] ++; sum[v] ++; eg[i] = {u, v}; } FOR(i, 1, n) sum[i] += sum[i - 1]; FOR(i, 1, n) FOR(j, i, n) { ok[i][j] = true; FOR(t, 1, k) if(!inside(i, j, eg[t].fi, eg[t].se)) ok[i][j] = false; } k <<= 1; g[0] = 1; FOR(i, 2, N - 1) g[i] = g[i - 2] * (i - 1) % mod; for(int len = 2; len <= n; len += 2) FOR(l, 1, n) if(l + len - 1 <= n && ok[l][l + len - 1]) { int r = l + len - 1; int x = (r - l + 1) - sum[r] + sum[l - 1]; dp[l][r] = g[x]; FOR(mid, l + 1, r - 2) sub(dp[l][r], dp[l][mid] * g[r - mid - sum[r] + sum[mid]]); } int ans = 0; FOR(i, 1, n) FOR(j, i + 1, n) { //if((j - i + 1) % 2 == 0) cout << i << ' ' << j << ' ' << dp[i][j] << '\n'; add(ans, dp[i][j] * g[n - k - (j - i + 1 - sum[j] + sum[i - 1])]); } cout << ans ; }
### Prompt Develop a solution in Cpp to the problem described below: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define FOR(a, b, c) for(int a = b; a <= c; ++a) #define pb push_back #define int long long typedef pair<int, int> ii; const int N = 605; const int mod = 1e9 + 7; int n, k; int sum[N], g[N], dp[N][N], ok[N][N]; ii eg[N]; bool inside(int l, int r, int p1, int p2) { return ((l <= p1 && p1 <= r && l <= p2 && p2 <= r) || (!(l <= p1 && p1 <= r) && !(l <= p2 && p2 <= r))); } void add(int &x, int y) { x = (x + y) % mod; } void mul(int &x, int y) { x = x * y % mod; } void sub(int &x, int y) { x = (x % mod - y % mod); if(x < 0) x += mod; } signed main() { //freopen("test.inp", "r", stdin); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; n <<= 1; FOR(i, 1, k) { int u, v; cin >> u >> v; sum[u] ++; sum[v] ++; eg[i] = {u, v}; } FOR(i, 1, n) sum[i] += sum[i - 1]; FOR(i, 1, n) FOR(j, i, n) { ok[i][j] = true; FOR(t, 1, k) if(!inside(i, j, eg[t].fi, eg[t].se)) ok[i][j] = false; } k <<= 1; g[0] = 1; FOR(i, 2, N - 1) g[i] = g[i - 2] * (i - 1) % mod; for(int len = 2; len <= n; len += 2) FOR(l, 1, n) if(l + len - 1 <= n && ok[l][l + len - 1]) { int r = l + len - 1; int x = (r - l + 1) - sum[r] + sum[l - 1]; dp[l][r] = g[x]; FOR(mid, l + 1, r - 2) sub(dp[l][r], dp[l][mid] * g[r - mid - sum[r] + sum[mid]]); } int ans = 0; FOR(i, 1, n) FOR(j, i + 1, n) { //if((j - i + 1) % 2 == 0) cout << i << ' ' << j << ' ' << dp[i][j] << '\n'; add(ans, dp[i][j] * g[n - k - (j - i + 1 - sum[j] + sum[i - 1])]); } cout << ans ; } ```
#include<bits/stdc++.h> #define ll long long using namespace std; const int mod=1e9+7; ll f[1010],dp[1010][1010]; int s[1010],vis[1010],lk[1010]; inline int read() { char c=getchar();int x=0,flag=1; while(!isdigit(c)){if(c=='-') flag=-1;c=getchar();} while(isdigit(c)) x=x*10+c-'0',c=getchar(); return x*flag; } int dfs(int l,int r) { //cout<<l<<r; if(l>=r||(s[r]-s[l-1])&1) return 0; if(~dp[l][r]) return dp[l][r]; for(int i=l;i<=r;i++) if(vis[i]&&(lk[i]>r||lk[i]<l)) return dp[l][r]=0; ll ans=f[s[r]-s[l-1]]; for(int k=l;k<r;k++) { ans=(ans-dfs(l,k)*f[s[r]-s[k]]%mod+mod)%mod; } return dp[l][r]=ans; } int main() { int n=read()<<1,k=read(); for(int i=1;i<=k;i++) { int a=read(),b=read(); lk[a]=b; lk[b]=a; vis[a]=vis[b]=1; } f[0]=1; for(int i=2;i<=1000;i+=2) f[i]=f[i-2]*(i-1)%mod; for(int i=1;i<=n;i++) s[i]=s[i-1]+1-vis[i]; ll ans=0; memset(dp,-1,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) ans=(ans+dfs(i,j)*f[s[n]-s[j]+s[i-1]])%mod; cout<<ans; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> #define ll long long using namespace std; const int mod=1e9+7; ll f[1010],dp[1010][1010]; int s[1010],vis[1010],lk[1010]; inline int read() { char c=getchar();int x=0,flag=1; while(!isdigit(c)){if(c=='-') flag=-1;c=getchar();} while(isdigit(c)) x=x*10+c-'0',c=getchar(); return x*flag; } int dfs(int l,int r) { //cout<<l<<r; if(l>=r||(s[r]-s[l-1])&1) return 0; if(~dp[l][r]) return dp[l][r]; for(int i=l;i<=r;i++) if(vis[i]&&(lk[i]>r||lk[i]<l)) return dp[l][r]=0; ll ans=f[s[r]-s[l-1]]; for(int k=l;k<r;k++) { ans=(ans-dfs(l,k)*f[s[r]-s[k]]%mod+mod)%mod; } return dp[l][r]=ans; } int main() { int n=read()<<1,k=read(); for(int i=1;i<=k;i++) { int a=read(),b=read(); lk[a]=b; lk[b]=a; vis[a]=vis[b]=1; } f[0]=1; for(int i=2;i<=1000;i+=2) f[i]=f[i-2]*(i-1)%mod; for(int i=1;i<=n;i++) s[i]=s[i-1]+1-vis[i]; ll ans=0; memset(dp,-1,sizeof(dp)); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) ans=(ans+dfs(i,j)*f[s[n]-s[j]+s[i-1]])%mod; cout<<ans; return 0; } ```
#include <vector> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int N=6e2+5; const int mod=1e9+7; int n,m,lim,ans,g[N],to[N],sum[N],dp[N][N]; template <typename _Tp> inline void IN(_Tp&x) { char ch;bool flag=0;x=0; while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1; while(isdigit(ch)) x=x*10+ch-'0',ch=getchar(); if(flag) x=-x; } inline void addedge(int u=0,int v=0) {IN(u),IN(v),to[u]=v,to[v]=u;} inline bool check(int a,int b) { if((b-a+1)&1) return false; for(int i=a;i<=b;++i) if(to[i]&&(to[i]<a||to[i]>b)) return false; return true; } int main() { IN(n),IN(m),lim=n<<1,g[0]=1; for(int i=1;i<=m;++i) addedge(); for(int i=1;i<=lim;++i) g[i]=(i&1)?0:(1ll*g[i-2]*(i-1)%mod); for(int i=1;i<=lim;++i) sum[i]=sum[i-1]+(bool)(!to[i]); for(int a=1;a<=lim;++a) for(int b=a+1;b<=lim;++b) if(check(a,b)) { dp[a][b]=g[sum[b]-sum[a-1]]; for(int k=a+1;k<b;++k) if(dp[a][k]&&g[sum[b]-sum[k]]) (dp[a][b]+=mod-1ll*dp[a][k]*g[sum[b]-sum[k]]%mod)%=mod; (ans+=1ll*dp[a][b]*g[((n-m)<<1)-sum[b]+sum[a-1]]%mod)%=mod; } printf("%d\n",ans); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int N=6e2+5; const int mod=1e9+7; int n,m,lim,ans,g[N],to[N],sum[N],dp[N][N]; template <typename _Tp> inline void IN(_Tp&x) { char ch;bool flag=0;x=0; while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=1; while(isdigit(ch)) x=x*10+ch-'0',ch=getchar(); if(flag) x=-x; } inline void addedge(int u=0,int v=0) {IN(u),IN(v),to[u]=v,to[v]=u;} inline bool check(int a,int b) { if((b-a+1)&1) return false; for(int i=a;i<=b;++i) if(to[i]&&(to[i]<a||to[i]>b)) return false; return true; } int main() { IN(n),IN(m),lim=n<<1,g[0]=1; for(int i=1;i<=m;++i) addedge(); for(int i=1;i<=lim;++i) g[i]=(i&1)?0:(1ll*g[i-2]*(i-1)%mod); for(int i=1;i<=lim;++i) sum[i]=sum[i-1]+(bool)(!to[i]); for(int a=1;a<=lim;++a) for(int b=a+1;b<=lim;++b) if(check(a,b)) { dp[a][b]=g[sum[b]-sum[a-1]]; for(int k=a+1;k<b;++k) if(dp[a][k]&&g[sum[b]-sum[k]]) (dp[a][b]+=mod-1ll*dp[a][k]*g[sum[b]-sum[k]]%mod)%=mod; (ans+=1ll*dp[a][b]*g[((n-m)<<1)-sum[b]+sum[a-1]]%mod)%=mod; } printf("%d\n",ans); return 0; } ```
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2*3e2+10,mod=1e9+7; int n,k,a[maxn],sum[maxn]; ll fac[maxn],ans,powinv2[maxn],finv[maxn]; inline ll fpow(ll a,ll n){ ll res=1; for(;n;n>>=1,a=a*a%mod) if(n&1ll) res=res*a%mod; return res; } inline void init(){ fac[0]=1; for(int i=1;i<maxn;++i) fac[i]=fac[i-1]*i%mod; powinv2[0]=1; for(int i=1;i<maxn;++i) powinv2[i]=powinv2[i-1]*(mod+1>>1)%mod; for(int i=0;i<maxn;++i) finv[i]=fpow(fac[i],mod-2)%mod; } int main(){ init(); cin>>n>>k; for(int i=1,x,y;i<=k;++i){ cin>>x>>y; a[x]=y; a[y]=x; } for(int i=1;i<=n<<1;++i) sum[i]=sum[i-1]+!a[i]; for(int i=1;i<=n<<1;++i){ static int dp[2][maxn]; memset(dp,0,sizeof(dp)); dp[i&1^1][0]=1; for(int j=i,most=0;j<=n<<1;++j){ memset(dp[j&1],0,sizeof(dp[j&1])); if(a[j]&&a[j]<i) break; if(a[j]) for(int k=0;k<=n<<1;++k) dp[j&1][k]=dp[j&1^1][k]; else{ for(int k=1;k<=n<<1;++k) dp[j&1][k-1]=(ll)k*dp[j&1^1][k]%mod; for(int k=0;k<=n<<1;++k) (dp[j&1][k+1]+=dp[j&1^1][k])%=mod; } if(a[j]) most=max(most,a[j]); if(most<=j){ int rest=sum[i-1]+sum[n<<1]-sum[j]; (ans+=dp[j&1][0]*fac[rest]%mod*finv[rest/2]%mod*powinv2[rest/2])%=mod; dp[j&1][0]=0; } } } printf("%lld\n",ans); return 0; }
### Prompt Create a solution in Cpp for the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2*3e2+10,mod=1e9+7; int n,k,a[maxn],sum[maxn]; ll fac[maxn],ans,powinv2[maxn],finv[maxn]; inline ll fpow(ll a,ll n){ ll res=1; for(;n;n>>=1,a=a*a%mod) if(n&1ll) res=res*a%mod; return res; } inline void init(){ fac[0]=1; for(int i=1;i<maxn;++i) fac[i]=fac[i-1]*i%mod; powinv2[0]=1; for(int i=1;i<maxn;++i) powinv2[i]=powinv2[i-1]*(mod+1>>1)%mod; for(int i=0;i<maxn;++i) finv[i]=fpow(fac[i],mod-2)%mod; } int main(){ init(); cin>>n>>k; for(int i=1,x,y;i<=k;++i){ cin>>x>>y; a[x]=y; a[y]=x; } for(int i=1;i<=n<<1;++i) sum[i]=sum[i-1]+!a[i]; for(int i=1;i<=n<<1;++i){ static int dp[2][maxn]; memset(dp,0,sizeof(dp)); dp[i&1^1][0]=1; for(int j=i,most=0;j<=n<<1;++j){ memset(dp[j&1],0,sizeof(dp[j&1])); if(a[j]&&a[j]<i) break; if(a[j]) for(int k=0;k<=n<<1;++k) dp[j&1][k]=dp[j&1^1][k]; else{ for(int k=1;k<=n<<1;++k) dp[j&1][k-1]=(ll)k*dp[j&1^1][k]%mod; for(int k=0;k<=n<<1;++k) (dp[j&1][k+1]+=dp[j&1^1][k])%=mod; } if(a[j]) most=max(most,a[j]); if(most<=j){ int rest=sum[i-1]+sum[n<<1]-sum[j]; (ans+=dp[j&1][0]*fac[rest]%mod*finv[rest/2]%mod*powinv2[rest/2])%=mod; dp[j&1][0]=0; } } } printf("%lld\n",ans); return 0; } ```
#include <bits/stdc++.h> #define N 1010 using namespace std; typedef long long ll; const int mod = 1000000007 ; char *p1, *p2, buf[100000]; #define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ ) int rd() { int x = 0, f = 1; char c = nc(); while (c < 48) { if (c == '-') f = -1; c = nc(); } while (c > 47) { x = (((x << 2) + x) << 1) + (c ^ 48), c = nc(); } return x * f; } int to[N], sum[N], dp[N][N], g[N]; int main() { int n = rd(), k = rd(); for (int i = 1; i <= k; i ++ ) { int x = rd(), y = rd(); to[x] = y, to[y] = x; sum[x] ++ , sum[y] ++ ; } n *= 2; for (int i = 1; i <= n; i ++ ) { sum[i] += sum[i - 1]; } // init_g g[0] = 1; for (int i = 2; i <= n; i += 2) { g[i] = (ll)g[i - 2] * (i - 1) % mod; } for (int i = 1; i <= n; i ++ ) { for (int j = i + 1; j <= n; j += 2) { bool flag = false; for (int k = i; k <= j; k ++ ) { if (to[k] && (to[k] < i || to[k] > j)) { flag = true; break; } } if (flag) { continue; } dp[i][j] = g[j - i + 1 - sum[j] + sum[i - 1]]; for (int l = i + 1; l < j; l += 2) { dp[i][j] = ((ll)dp[i][j] + mod - (ll)dp[i][l] * g[j - l - sum[j] + sum[l]] % mod) % mod; } } } k <<= 1; int ans = 0; for (int i = 1; i <= n; i ++ ) { for (int j = i + 1; j <= n; j += 2) { ans = (ans + (ll)dp[i][j] * g[n - (j - i + 1) - (k - (sum[j] - sum[i - 1]))] % mod) % mod; } } cout << ans << endl ; return 0; }
### Prompt Construct a cpp code solution to the problem outlined: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include <bits/stdc++.h> #define N 1010 using namespace std; typedef long long ll; const int mod = 1000000007 ; char *p1, *p2, buf[100000]; #define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ ) int rd() { int x = 0, f = 1; char c = nc(); while (c < 48) { if (c == '-') f = -1; c = nc(); } while (c > 47) { x = (((x << 2) + x) << 1) + (c ^ 48), c = nc(); } return x * f; } int to[N], sum[N], dp[N][N], g[N]; int main() { int n = rd(), k = rd(); for (int i = 1; i <= k; i ++ ) { int x = rd(), y = rd(); to[x] = y, to[y] = x; sum[x] ++ , sum[y] ++ ; } n *= 2; for (int i = 1; i <= n; i ++ ) { sum[i] += sum[i - 1]; } // init_g g[0] = 1; for (int i = 2; i <= n; i += 2) { g[i] = (ll)g[i - 2] * (i - 1) % mod; } for (int i = 1; i <= n; i ++ ) { for (int j = i + 1; j <= n; j += 2) { bool flag = false; for (int k = i; k <= j; k ++ ) { if (to[k] && (to[k] < i || to[k] > j)) { flag = true; break; } } if (flag) { continue; } dp[i][j] = g[j - i + 1 - sum[j] + sum[i - 1]]; for (int l = i + 1; l < j; l += 2) { dp[i][j] = ((ll)dp[i][j] + mod - (ll)dp[i][l] * g[j - l - sum[j] + sum[l]] % mod) % mod; } } } k <<= 1; int ans = 0; for (int i = 1; i <= n; i ++ ) { for (int j = i + 1; j <= n; j += 2) { ans = (ans + (ll)dp[i][j] * g[n - (j - i + 1) - (k - (sum[j] - sum[i - 1]))] % mod) % mod; } } cout << ans << endl ; return 0; } ```
#pragma GCC optimize (2) #pragma G++ optimize (2) #include<bits/stdc++.h> #define INF 0x3f3f3f3f #define mod 1000000007 #define MAX 605 using namespace std; //char nc() //{ // static char buf[100000],*p1=buf,*p2=buf; // return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; //} char nc(){return getchar();} int read() { int x=0,y=1; char c=nc(); while(!isdigit(c)) { if(c=='-')y=-1; c=nc(); } while(isdigit(c)) { x=(x<<1)+(x<<3)+(c^48); c=nc(); } return x*y; } int n,K,p[MAX],sm[MAX]; long long f[MAX][MAX],g[MAX],ANS=0; int in(int l,int r) { return r-l+1-(sm[r]-sm[l-1]); } int out(int l,int r) { return n-(r-l+1)-(K-(sm[r]-sm[l-1])); } int main() { n=read();K=read(); int x,y; for(int i=1;i<=K;i++) x=read(),y=read(),sm[x]++,sm[y]++,p[x]=y,p[y]=x; n<<=1;K<<=1; for(int i=1;i<=n;i++) sm[i]+=sm[i-1]; g[0]=1;g[1]=0; for(int i=2;i<=n;i++) g[i]=g[i-2]*(i-1)%mod; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if((j-i)&1) { bool flag=true; for(int k=i;k<=j;k++) if(p[k]&&(p[k]<i||p[k]>j)) { flag=false; break; } if(!flag) { f[i][j]=0; continue; } f[i][j]=g[in(i,j)]; for(int l=i+1;l<j;l++) f[i][j]=(f[i][j]-f[i][l]*g[in(l+1,j)])%mod; if(f[i][j]<0) f[i][j]+=mod; ANS=(ANS+f[i][j]*g[out(i,j)])%mod; } printf("%lld",ANS); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #pragma GCC optimize (2) #pragma G++ optimize (2) #include<bits/stdc++.h> #define INF 0x3f3f3f3f #define mod 1000000007 #define MAX 605 using namespace std; //char nc() //{ // static char buf[100000],*p1=buf,*p2=buf; // return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; //} char nc(){return getchar();} int read() { int x=0,y=1; char c=nc(); while(!isdigit(c)) { if(c=='-')y=-1; c=nc(); } while(isdigit(c)) { x=(x<<1)+(x<<3)+(c^48); c=nc(); } return x*y; } int n,K,p[MAX],sm[MAX]; long long f[MAX][MAX],g[MAX],ANS=0; int in(int l,int r) { return r-l+1-(sm[r]-sm[l-1]); } int out(int l,int r) { return n-(r-l+1)-(K-(sm[r]-sm[l-1])); } int main() { n=read();K=read(); int x,y; for(int i=1;i<=K;i++) x=read(),y=read(),sm[x]++,sm[y]++,p[x]=y,p[y]=x; n<<=1;K<<=1; for(int i=1;i<=n;i++) sm[i]+=sm[i-1]; g[0]=1;g[1]=0; for(int i=2;i<=n;i++) g[i]=g[i-2]*(i-1)%mod; for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if((j-i)&1) { bool flag=true; for(int k=i;k<=j;k++) if(p[k]&&(p[k]<i||p[k]>j)) { flag=false; break; } if(!flag) { f[i][j]=0; continue; } f[i][j]=g[in(i,j)]; for(int l=i+1;l<j;l++) f[i][j]=(f[i][j]-f[i][l]*g[in(l+1,j)])%mod; if(f[i][j]<0) f[i][j]+=mod; ANS=(ANS+f[i][j]*g[out(i,j)])%mod; } printf("%lld",ANS); return 0; } ```
#include<bits/stdc++.h> using namespace std; const int N=605,Mod=1e9+7; int n,k,s[N],f[N][N],g[N],t[N],ans; #define mul(x,y) (1ll*(x)*(y)%Mod) inline int add(int x, int y) { return (x+y>=Mod?x+y-Mod:x+y); } inline int sub(int x, int y) { return (x-y<0?x-y+Mod:x-y); } bool check(int l, int r) { for(int i=l;i<=r;++i) if(t[i]&&(t[i]<l||t[i]>r)) return false; return true; } int main() { scanf("%d%d",&n,&k),n<<=1; for(int i=1,a,b;i<=k;++i) { scanf("%d%d",&a,&b); t[a]=b,t[b]=a,--s[a],--s[b]; } for(int i=1;i<=n;++i) s[i]+=s[i-1]+1; g[0]=1; for(int i=2;i<=n;++i) g[i]=mul(i-1,g[i-2]); for(int i=1;i<=n;++i) for(int j=i+1;j<=n;j+=2) { if(!g[s[j]-s[i-1]]||!check(i,j)) continue; f[i][j]=g[s[j]-s[i-1]]; for(int k=i;k<j;++k) f[i][j]=sub(f[i][j],mul(f[i][k],g[s[j]-s[k]])); ans=add(ans,mul(f[i][j],g[s[n]-(s[j]-s[i-1])])); } printf("%d\n",ans); }
### Prompt Develop a solution in cpp to the problem described below: There are 2N points evenly spaced on the circumference of a circle. These points are numbered 1 to 2N in clockwise order, starting from some of them. Snuke will divide these points into N pairs, then for each pair, he will draw a line segment connecting the two points. After the line segments are drawn, two points are connected when one can reach from one of those points to the other by traveling only on the line segments. The number of the connected parts here is the number of the connected components in the graph with 2N vertices, corresponding to the 2N points, where every pair of vertices corresponding to two connected points is connected with an edge. Snuke has already decided K of the pairs, and the i-th of them is a pair of Point A_i and Point B_i. He is thinking of trying all possible ways to make the remaining N-K pairs and counting the number of the connected parts for each of those ways. Find the sum of those numbers of the connected parts. As the answer can be extremely large, compute the sum modulo 10^9+7. Constraints * 1 \leq N \leq 300 * 0 \leq K \leq N * 1 \leq A_i,B_i \leq 2N * A_1,\ A_2,\ ...\ A_K,\ B_1,\ B_2,\ ...\ B_K are all distinct. * All values in input are integers. Input Input is given from Standard Input in the following format: N K A_1 B_1 A_2 B_2 : A_K B_K Output Print the sum of the numbers of the connected parts for all possible ways to make the remaining N-K pairs. Examples Input 2 0 Output 5 Input 4 2 5 2 6 1 Output 6 Input 20 10 10 18 11 17 14 7 4 6 30 28 19 24 29 22 25 32 38 34 36 39 Output 27087418 ### Response ```cpp #include<bits/stdc++.h> using namespace std; const int N=605,Mod=1e9+7; int n,k,s[N],f[N][N],g[N],t[N],ans; #define mul(x,y) (1ll*(x)*(y)%Mod) inline int add(int x, int y) { return (x+y>=Mod?x+y-Mod:x+y); } inline int sub(int x, int y) { return (x-y<0?x-y+Mod:x-y); } bool check(int l, int r) { for(int i=l;i<=r;++i) if(t[i]&&(t[i]<l||t[i]>r)) return false; return true; } int main() { scanf("%d%d",&n,&k),n<<=1; for(int i=1,a,b;i<=k;++i) { scanf("%d%d",&a,&b); t[a]=b,t[b]=a,--s[a],--s[b]; } for(int i=1;i<=n;++i) s[i]+=s[i-1]+1; g[0]=1; for(int i=2;i<=n;++i) g[i]=mul(i-1,g[i-2]); for(int i=1;i<=n;++i) for(int j=i+1;j<=n;j+=2) { if(!g[s[j]-s[i-1]]||!check(i,j)) continue; f[i][j]=g[s[j]-s[i-1]]; for(int k=i;k<j;++k) f[i][j]=sub(f[i][j],mul(f[i][k],g[s[j]-s[k]])); ans=add(ans,mul(f[i][j],g[s[n]-(s[j]-s[i-1])])); } printf("%d\n",ans); } ```