1. 程式人生 > 其它 >B. Special Permutation(思維)

B. Special Permutation(思維)

B. Special Permutation time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output

A permutation of lengthnnis an arrayp=[p1,p2,,pn]p=[p1,p2,…,pn]which contains every integer from11tonn(inclusive) exactly once. For example,p=[4,2,6,5,3,1]p=[4,2,6,5,3,1]is a permutation of length

66.

You are given three integersnn,aaandbb, wherennis an even number. Print any permutation of lengthnnthat the minimum amongall its elements of the left halfequalsaaand the maximum amongall its elements of the right halfequalsbb. Print-1if no such permutation exists.

Input

The first line of the input contains one integer

tt(1t10001≤t≤1000), the number of test cases in the test. The followingttlines contain test case descriptions.

Each test case description contains three integersnn,aa,bb(2n1002≤n≤100;1a,bn1≤a,b≤n;aba≠b), wherennis an even number (i.e.nmod2=0nmod2=0).

Output

For each test case, print a single line containing any suitable permutation. Print-1no 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,a,b,n保證為偶數,輸出使得a是左半邊的最小值,b是右半邊的最大值。

感覺難度主要體現在判斷錯誤上,直接附上程式碼

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int t,n,a,b,cnta = 0,cntb = 0;
 5 bool vis[109];
 6 int main()
 7 {
 8     scanf("%d",&t);
 9     while(t--)
10     {
11         scanf("%d%d%d",&n,&a,&b);
12         memset(vis,0,sizeof(vis));
13         vis[a] = 1,vis[b] = 1,cnta = 0,cntb = 0;
14         for(int i = a;i <= n;i++) if(!vis[i]) cnta++;
15         for(int i = b;i > 0;i--) if(!vis[i]) cntb++;
16         if(cnta >= n/2-1 && cntb >= n/2-1)
17         {
18             for(int i = n,j = 0;j < n/2-1;i--)
19             {
20                 if(!vis[i])
21                 {
22                     printf("%d ",i);
23                     vis[i] = 1;
24                     j++;
25                 }
26             }
27             printf("%d ",a);
28             for(int i = 1,j = 0;j < n/2-1;i++)
29             {
30                 if(!vis[i])
31                 {
32                     printf("%d ",i);
33                     vis[i] = 1;
34                     j++;
35                 }
36             }
37             printf("%d",b);
38             printf("\n");
39         } 
40         else printf("-1\n");
41     }
42 }