codeforces 1017 problem C. The Phone Number(思维+构造)

C. The Phone Number

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number!

The only thing Mrs. Smith remembered was that any permutation of nn can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband.

The sequence of nn integers is called a permutation if it contains all integers from 11 to nn exactly once.

The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS).

A subsequence ai1,ai2,…,aikai1,ai2,…,aik where 1≤i1ai2>ai3>…>aikai1>ai2>ai3>…>aik, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences.

For example, if there is a permutation [6,4,1,7,2,3,5][6,4,1,7,2,3,5], LIS of this permutation will be [1,2,3,5][1,2,3,5], so the length of LIS is equal to 44. LDScan be [6,4,1][6,4,1], [6,4,2][6,4,2], or [6,4,3][6,4,3], so the length of LDS is 33.

Note, the lengths of LIS and LDS can be different.

So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS.

Input

The only line contains one integer nn (1≤n≤1051≤n≤105) — the length of permutation that you need to build.

Output

Print a permutation that gives a minimum sum of lengths of LIS and LDS.

If there are multiple answers, print any.

Examples

input

Copy

4

output

Copy

3 4 1 2

input

Copy

2

output

Copy

2 1

Note

In the first sample, you can build a permutation [3,4,1,2][3,4,1,2]. LIS is [3,4][3,4] (or [1,2][1,2]), so the length of LIS is equal to 22. LDS can be ony of [3,1][3,1], [4,2][4,2], [3,2][3,2], or [4,1][4,1]. The length of LDS is also equal to 22. The sum is equal to 44. Note that [3,4,1,2][3,4,1,2] is not the only permutation that is valid.

In the second sample, you can build a permutation [2,1][2,1]. LIS is [1][1] (or [2][2]), so the length of LIS is equal to 11. LDS is [2,1][2,1], so the length of LDS is equal to 22. The sum is equal to 33. Note that permutation [1,2][1,2] is also valid.

 

题意:给出一个数字n,构造一个从1~n的数列,使得这个数列的最长上升子序列加上最长下降子序列的长度之和sum最小。

 

思路:我们可以将1-n分成若干个区间,每个区间都是一个严格的上升子串,那么,1-n的最长下降字序列就是区间的个数。

所以,我们只要确定了一个合适的区间长度,就好了。假设n是可以刚好分成x个区间的,那么区间长度为n/x,区间个数为x。

sum=n/x+x。利用一些不等式的定理可得,只有当n/x=x的时候最小,那么x就等于sqrt(n)了。

 

#include
#include
using namespace std;
int main()
{
    int n,j;
    cin>>n;
    int x=floor(sqrt(n));//确定区间长度
    for(int i=n;i>=1;i-=x){
        for(j=(i-x+1)>0?i-x+1:1;j<=i;j++)//最后一段的长度可能不是x
            cout<

 

你可能感兴趣的:(水题,构造)