hdu_4377 Sub Sequence (数学推导)

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;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值