题目链接:Problem - 1612B - Codeforces
A permutation of length nn is an array p=[p1,p2,…,pn] which contains every integer from 1 to n (inclusive) exactly once. For example, p=[4,2,6,5,3,1] is a permutation of length 6.
You are given three integers n, a and b, where nn is an even number. Print any permutation of length nn that the minimum among all its elements of the left half equals a and the maximum among all its elements of the right half equals b. Print -1 if no such permutation exists.
Input
The first line of the input contains one integer tt (1≤t≤1000), the number of test cases in the test. The following tt lines contain test case descriptions.
Each test case description contains three integers n, a, b (2≤n≤100; 1≤a,b≤n; a≠b), where nn is an even number (i.e. nmod2=0).
Output
For each test case, print a single line containing any suitable permutation. Print -1 no such permutation exists. If there are multiple answers, print any of them.
Example
input
7 6 2 5 6 1 3 6 4 3 4 2 4 10 5 3 2 1 2 2 2 1
output
4 2 6 5 3 1 -1 6 4 5 1 3 2 3 2 4 1 -1 1 2 2 1
题意:有一个长度为n的数组(n一定为偶数),ai 为1 - n的不充分的数字,左半边的最小值为a,右半边的最大值为b,问能否构造
思路:先判断必须放在左半边的数量的是否大于n/2,以及判断必须放在右半边的值是否小于n/2
,如果都满足就可以构造了
#include<bits/stdc++.h>
using namespace std;
int arr[105];
bool vis[105];
int main(){
int t;
cin >> t;
while(t--){
int n, a, b;
cin >> n >> a >> b;
int ans = n - b;
if(a < b){
ans++;
}
if(ans > n / 2){
cout << -1 << endl;
continue;
}
ans = n - a + 1;
if(b >= a){
ans--;
}
if(ans < n / 2){
cout << -1 << endl;
continue;
}
memset(vis, 0, sizeof(vis));
arr[n / 2 - 1] = a;
arr[n - 1] = b;
vis[a] = 1;
vis[b] = 1;
for(int i = 0; i < n / 2 - 1; i++){
for(int l = n; l >= 1; l--){
if(l >= a && l != b && vis[l] == 0){
arr[i] = l;
vis[l] = 1;
break;
}
}
}
for(int i = n / 2; i < n - 1; i++){
for(int l = 1; l <= n; l++){
if(l <= b && vis[l] == 0){
arr[i] = l;
vis[l] = 1;
break;
}
}
}
for(int i = 0; i < n; i++){
if(i != 0){
cout << " ";
}
cout << arr[i];
}
cout << "\n";
}
return 0;
}