B. Best Permutation
题意
Let’s define the value of the permutation pp of nn integers 11, 22, …, nn (a permutation is an array where each element from 11 to nn occurs exactly once) as follows:
- initially, an integer variable xx is equal to 00;
- if x<p1x<p1, then add p1p1 to xx (set x=x+p1x=x+p1), otherwise assign 00 to xx;
- if x<p2x<p2, then add p2p2 to xx (set x=x+p2x=x+p2), otherwise assign 00 to xx;
- …
- if x<pnx<pn, then add pnpn to xx (set x=x+pnx=x+pn), otherwise assign 00 to xx;
- the value of the permutation is xx at the end of this process.
For example, for p=[4,5,1,2,3,6]p=[4,5,1,2,3,6], the value of xx changes as follows: 0,4,9,0,2,5,110,4,9,0,2,5,11, so the value of the permutation is 1111.
You are given an integer nn. Find a permutation pp of size nn with the maximum possible value among all permutations of size nn. If there are several such permutations, you can print any of them.
Input
The first line contains one integer tt (1≤t≤971≤t≤97) — the number of test cases.
The only line of each test case contains one integer nn (4≤n≤1004≤n≤100).
Output
For each test case, print nn integers — the permutation pp of size nn with the maximum possible value among all permutations of size nn.
题解
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
void solve()
{
int n;
cin>>n;
if(n%2 == 0)
{
for(int i = n - 2 ;i >= 1 ; i --)
{
cout<<i<<" ";
}
cout<<n - 1 <<" "<<n<<endl;
return;
} else{
cout<<1<<" ";
for (int i = n - 2; i >=2 ; i -- ) {
cout<<i<<" ";
}
cout<<n - 1 <<" "<<n<<endl;
return;
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
}