First, let's review some definitions. Feel free to skip this part if you are familiar with them.
A sequence aaa is an increasing (decreasing) subsequence of a sequence bbb if aaa can be obtained from bbb by deletion of several (possibly, zero or all) elements and all elements are in increasing (decreasing) order from the beginning to the end.
A permutation is an array consisting of nnn distinct integers from 111 to nnn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2][1,2,2] is not a permutation (222 appears twice in the array) and [1,3,4][1,3,4][1,3,4] is also not a permutation (n=3n=3n=3 but there is 444 in the array).
The problem starts from here.
Link has an array. He is currently learning the longest increasing subsequence algorithm. So he comes up with the following question.
Let the value of a permutation ppp be max(lis(p),lds(p))\max({\rm lis}(p),{\rm lds}(p))max(lis(p),lds(p)), where lis(p){\rm lis}(p)lis(p) is the length of the longest increasing subsequence of ppp and lds(p){\rm lds}(p)lds(p) is the length of the longest decreasing subsequence of ppp. For all permutations of length nnn, which one has the minimum value?
输入描述:
Each test contains multiple test cases. The first line contains the number of test cases T(1≤T≤1000)T(1\le T \le1000)T(1≤T≤1000).
For each test case, there is only one line, containing an integer n(1≤n≤106)n(1 \leq n \leq 10^6)n(1≤n≤106).
It is guaranteed that the sum of nnn over all test cases does not exceed 10610^6106.
输出描述:
For each test case, output a single line containing a permutation of length nnn.
If there are multiple answers, print any of them.
示例1
输入
复制3 1 2 3
3 1 2 3
输出
复制1 1 2 1 3 2
1 1 2 1 3 2
思路:
1,打表,找到符合题意的解,发现value和sqrt(n)有关
2,3 2 1 6 5 4 9 8 7,这样的话,分sqrt(n)块,块内减,块间递增,value是sqrt(n)。
代码:
void solve(){
int n;cin>>n;
int cnt=ceil(sqrt(n));
vector<int>ve;
int now=cnt,pre=1;
rep(i,1,n){
ve.emplace_back(now--);
if(ve.size()==cnt){
for(auto j:ve)cout<<j<<' ';
ve.clear();
now=min(++pre*cnt,n);
}
}
for(auto i:ve)cout<<i<<' ';
cout<<'\n';
}
标签:nnn,permutation,Monotonic,lds,Subsequence,Link,lis,test,rm
From: https://blog.csdn.net/m0_63054077/article/details/125961619