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≤i1<i2<…<ik≤n1≤i1<i2<…<ik≤n is called increasing if ai1<ai2<ai3<…<aikai1<ai2<ai3<…<aik. If ai1>ai2>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.
The only line contains one integer nn (1≤n≤1051≤n≤105) — the length of permutation that you need to build.
Print a permutation that gives a minimum sum of lengths of LIS and LDS.
If there are multiple answers, print any.
4
3 4 1 2
2
2 1
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.
这是一道猜规律的题。开根号分块,我这样的菜鸡自然是猜不到的。。
不过还是可以证明的,面积一定的时候,周长最小的是正方形,其实也就是基本不等式嘛。。。
所以就是如果采用分块思想,可以得知,LIS是L,LDS则为ceil(n/L),要使两者相加最小,也就是开根号的时候。
挺好的题,开阔一下思路
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 using namespace std; 20 #define INF 0x3f3f3f3f 21 #define maxn 100 22 #define maxm 30000 23 #define ll long long 24 #define mod 1000000007 25 #define mem(a,b) memset(a,b,sizeof a) 26 #ifndef ONLINE_JUDGE 27 #define dbg(x) cout<<#x<<"="<<x<<endl; 28 #else 29 #define dbg(x) 30 #endif 31 inline int read() 32 { 33 int x=0,f=1; 34 char ch=getchar(); 35 while(ch<'0'||ch>'9') 36 { 37 if(ch=='-') f=-1; 38 ch=getchar(); 39 } 40 while(ch>='0'&&ch<='9') 41 { 42 x=10*x+ch-'0'; 43 ch=getchar(); 44 } 45 return x*f; 46 } 47 inline void Out(int a) 48 { 49 if(a>9) 50 Out(a/10); 51 putchar(a%10+'0'); 52 } 53 int a[100005],b[100005]; 54 int main() 55 { 56 int n; 57 while(~scanf("%d",&n)){ 58 for(int i=1;i<=n;++i) a[i]=i; 59 int hh=sqrt(n); 60 int tot=n; 61 int tt=n/hh; 62 for(int i=1;i<=tt;++i) 63 { 64 for(int j=1;j<=hh;++j) 65 { 66 cout<<a[tot-hh+j]<<" "; 67 68 } 69 tot-=hh; 70 } 71 for(int i=1;i<=tot;++i) cout<<a[i]<<" "; 72 cout<<endl; 73 } 74 }