ZCMU-1245-Trainsorting

1245: Trainsorting

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 14   Solved: 12
[ Submit][ Status][ Web Board]

Description

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

【解析】
恩.一看题目sort就感觉应该和排序有关,但不知道是不是..这道题的意思就是说火车进站吧,大致其实就是想把车
尽可能多的放入火车当中。可以从火车的头放入也可以从火车的尾巴放入,但是呢车的质量从头到尾必须是越来越轻
的。其实就是要求最大的上升子序列和最大的下降子序列,这样的话我们肯定能放最多进去,比如说6,5,7,4,3,2,8
这样我们其实都能把这些车全部放入也就是先把6,7,8一次从火车车头进去,5,4,3,2从尾巴上去。如果是6,8,9,5,7,10
那就只能放5辆车了,6,8,9,10从头开进去,5从尾巴开进去。7不能进去,注意车是依次过来的。那么用DP求一下最大上
升子序列和最大下降子序列就好了。
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int n,i,j,sum;
    int b[2001];
    int c[2001];
    int a[2001];
    while(~scanf("%d",&n))
    {
        sum=0;
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(a,0,sizeof(a));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=n;i>=1;i--)
        {
            b[i]=1;
            c[i]=1;
            for(j=n;j>i;j--)
            {
                if(a[i]>a[j])
                {
                    b[i]=max(b[i],b[j]+1);
                }
                else if(a[i]<a[j])
                {
                    c[i]=max(c[i],c[j]+1);//动态的去写入在当前车质量下最大的上升子序列是多少。
                }
            }
            sum=max(sum,c[i]+b[i]-1);//不同质量之间的比较
        }
        printf("%d\n",sum);
    }
    return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值