sdnu1395(双向LIS)

Problem B: Trainsorting   ( LIS)
Description
        
        
            
Problem B: Trainsorting
Erin is an engineer. She drives trains. She also arranges the cars within each train. She prefers to put the cars in decreasing order of weight, with the heaviest car at the front of the train.
Unfortunately, sorting train cars is not easy. One cannot simply pick up a car and place it somewhere else. It is impractical to insert a car within an existing train. A car may only be added to the beginning and end of the train.
Cars arrive at the train station in a predetermined order. When each car arrives, Erin can add it to the beginning or end of her train, or refuse to add it at all. The resulting train should be as long as possible, but the cars within it must be ordered by weight.
Given the weights of the cars in the order in which they arrive, what is the longest train that Erin can make?


Input

The first line contains an integer 0 <= n <= 2000, the number of cars. Each of the following n lines contains a non-negative integer giving the weight of a car. No two cars have the same weight.  


Output


Output a single integer giving the number of cars in the longest train that can be made with the given restrictions.


Sample Input

3
1
2
3


Sample Output
3

求满足题意的火车最长长度,求正序和反序的LIS,相加减1即可。

#include <bits/stdc++.h>
using namespace std;

int dp1[2005],dp2[2005],a[2005];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(!n)
        {
            cout<<0<<'\n';
            continue;
        }
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        int ans=0;
        for(int i=n;i>=1;--i)
        {
            dp1[i]=1;
            dp2[i]=1;
            for(int j=n;j>i;--j)
            {
                if(a[j]<a[i])
                    dp1[i]=max(dp1[i],dp1[j]+1);
                if(a[j]>a[i])
                    dp2[i]=max(dp2[i],dp2[j]+1);
            }
            ans=max(ans,dp1[i]+dp2[i]-1);
        }
        cout<<ans<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

2024-12-25 09:09
四川师范大学 运营
想和你交朋友的潜伏者要冲国企:先去沃尔玛亲身感受标准化流程体系,一两年后再跳槽国内任何零售行业,可以有更大选择权吧?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 听劝,这个简历怎么改 #
14099次浏览 183人参与
# 面试被问“你的缺点是什么?”怎么答 #
6405次浏览 99人参与
# 水滴春招 #
16484次浏览 349人参与
# 入职第四天,心情怎么样 #
11321次浏览 63人参与
# 租房找室友 #
8027次浏览 53人参与
# 读研or工作,哪个性价比更高? #
26163次浏览 356人参与
# 职场新人生存指南 #
199236次浏览 5510人参与
# 参加完秋招的机械人,还参加春招吗? #
27000次浏览 276人参与
# 文科生还参加今年的春招吗 #
4114次浏览 31人参与
# 简历无回复,你会继续海投还是优化再投? #
48629次浏览 561人参与
# 你见过最离谱的招聘要求是什么? #
144719次浏览 829人参与
# 如果重来一次你还会读研吗 #
155719次浏览 1706人参与
# 机械人选offer,最看重什么? #
69077次浏览 449人参与
# 选择和努力,哪个更重要? #
44310次浏览 493人参与
# 如果再来一次,你还会学硬件吗 #
103647次浏览 1245人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
20521次浏览 414人参与
# 招聘要求与实际实习内容不符怎么办 #
46753次浏览 494人参与
# 22届毕业,是读研还是拿外包offer先苟着 #
4652次浏览 27人参与
# 你们的毕业论文什么进度了 #
901291次浏览 8961人参与
# 软开人,你觉得应届生多少薪资才算合理? #
81379次浏览 496人参与
# 国企还是互联网,你怎么选? #
109198次浏览 853人参与
牛客网
牛客企业服务