Sub Sequence
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 19 Accepted Submission(s) : 9
Problem Description
Recently, YCC is studying sequence problems. And he is struggling with it!!!= = For such an ACMer like you, it is commonly believed that you must know the Longest Increasing Subsequence problem. Now Let’s define some useful functions and symbols: Let U(A) be the length of the longest increasing subsequence of the original sequence A; Let D(A) be the length of the longest decreasing subsequence of the original sequence A; Let H(A) be the maximum value of U(A) and D(A), that is H(A) = max{U(A), D(A)}. H(A)!!! Ah... Zauber anzahl, ist es nicht? Now YCC wants to study the attributes of all the permutations of integer numbers from 1 to N. He wants to get such permutation P having minimum H(P). If more than one permutation satisfies the condition, you should give the lexicographically smallest one.
Input
The first line contains an integer T which indicates the number of test cases. The following T lines each contain an integer N (1 ≤ N ≤ 100000).
Output
For each case, output N space separated integers in one line, which denote the permutation expected.
Sample Input
3 1 2 3
Sample Output
1 1 2 1 3 2
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
int a[100005];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n, b = 0, d = 0, e = 0, f = 0; double c = 0;
scanf("%d", &n);
if (n == 1) { printf("1\n"); continue; }
if (n == 2){ printf("1 2\n"); continue; }
if (n == 3){ printf("1 3 2\n"); continue; }
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++) a[i] = i;
c = sqrt(n*1.0); b = (int)sqrt(n*1.0);
if (c == b) // 平方数
{
for (int i = 1; i <= n; i++)
{
if (i%b == 1) a[i] = (i / b + 1)*b;
else a[i] = a[i - 1] - 1;
}
}
else
{
b = b + 1; // 除第一组外每组个数
if (n%b == 0) d = n / b; // 组数
else d = n / b + 1;
e = n - b*(d - 1); // 第一组个数
f = b - d; // 第一组保持升序个数
for (int i = f + 1; i <= e; i++)
{
if (i == f + 1) a[i] = e;
else a[i] = a[i - 1] - 1;
}
int k = 0;
for (int i = e + 1; i <= n; i++)
{
k += 1;
if (k%b == 1) a[i] = (k / b + 1)*b + e;
else a[i] = a[i - 1] - 1;
}
}
printf("%d", a[1]);
for (int i = 2; i <= n; i++)
printf(" %d", a[i]);
printf("\n");
}
return 0;
}