解题报告:hdu1257 LIS裸题

2017-09-02 17:28:44

writer:pprp

那个裸题练练手,讲解在之前的博客中提到了

代码如下:

/*
@theme:hdu1257
@writer:pprp
@begin:17:13
@end:17:26
@declare:LIS的裸题,温习一下
@error:注意题目中说明了是多组数据
@date:2017/9/2
*/

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1010;
int arr[maxn];
int dp[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    int N,Max = 0;
    while(cin >> N)
    {
        Max = 0;
        for(int i = 0 ; i < N; i++)
        {
            dp[i] = 1;
            cin >> arr[i];
            for(int j = 0 ; j < i ; j++)
            {
                if(arr[j] < arr[i])
                {
                    dp[i] = max(dp[j]+1,dp[i]);
                }
            }
            Max = max(Max,dp[i]);
        }
        cout << Max << endl;
    }

    return 0;
}

 

第二种不用dp的解法:

代码如下:

/*
@theme:hdu1257
@writer:pprp
@begin:17:13
@end:17:36
@declare:LIS的裸题,温习一下
@error:
@date:2017/9/2
*/

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1010;
int arr[maxn];
int tmp[maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    int N,len = 0;
    while(cin >> N)
    {
        len = 1;
        cin >> arr[0];
        tmp[len] = arr[0];
        for(int i = 1 ; i < N ; i++)
        {
            cin >> arr[i];
            if(arr[i] > tmp[len])
            {
                len++;
                tmp[len] = arr[i];
            }
            else
                *lower_bound(tmp,tmp+len,arr[i]) = arr[i];
        }
        cout << len << endl;
    }

    return 0;
}

 

转载于:https://www.cnblogs.com/pprp/p/7467222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值