http://acm.hdu.edu.cn/showproblem.php?pid=4377
题意:
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)}.
就是求n的排列中,满足最长上升子序列和最长下降子序列的最大值的最小的排列,如有多个,输出字典序小的。
思路:
这题wa到心碎啊,开始一直以为H(n)=n/2。。。
后面想通了,sqrt(n)就可以了
我把n分成t=sqrt(n)个区间,每个区间为t,不满足t的使前面区间最小。然后每个区间直接逆序输出就行了。
比如:
n=16时序列为
4 3 2 1 8 7 6 5 12 11 10 9 16 15 14 13
H(16)=4
规律很明显了吧,O(n)。
我的代码:
/*
program:hdu_4377
author:BlackAndWhite
*/
#include<stdio.h>
#include<math.h>
int T,n;
int i,j,qujian[100001],began,t,res;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
t=(int)(sqrt(n)+0.99999);
res=t*t-n;
for(i=0;i<t;i++) qujian[i]=t;
for(i=0;i<t;i++)
if(qujian[i]<res+1)
{
qujian[i]=1;
res-=(t-1);
}
else
{
qujian[i]-=res;
break;
}
began=0;
for(i=0;i<t;i++)
{
began+=qujian[i];
for(j=0;j<qujian[i];j++)
if(i==t-1&&j==qujian[i]-1) printf("%d\n",began-j);
else printf("%d ",began-j);
}
}
return 0;
}