Trainsorting

D. Trainsorting (100)

Time Limit: 1s   Memory Limit: 256MB


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 Data

The first line contains an integer , 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 Data

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

思路:将此题看成最长上升子序列和最长下降子序列的结合。我们首先维护数组的最长下降子序列所得到的最长长度dp_i,从小到大的ep_i,然后计算答案的时候,我们分别考虑下面三种情况,要么答案是max(dp[i],ep[i]),要么是,这个数作为最长下降子序列的开头,若后面有比他小的,就尝试“拼接”,这个数作为最长上升子序列的末尾时也同理。

#include <bits/stdc++.h>
 
using namespace std;
const int N=2010;
int dp[N];
int ep[N];
int a[N];
int main() {
    int n;
    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    fill(dp, dp + 1 + n, 1);
    fill(ep, ep + 1 + n, 1);
    for(int i = n; i >= 1; --i) {
        for (int j = n; j > i; --j) {
            if (a[j] > a[i]) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
            if(a[j] < a[i]) {
                ep[i] = max(ep[i], ep[j] + 1);
            }
        }
    }
    int ans = 0;
    for(int i = 1;i <= n; i++) {
        ans = max(ans, max(dp[i], ep[i]));
        for(int j = i + 1;j <= n; j++) {
            if(a[i] < a[j]) {
                ans = max(ans, dp[j] + ep[i]);
            }
            else if(a[i] > a[j]) {
                ans = max(ans, dp[i] + ep[j]);
            }
        }
    }
    cout << ans << '\n';

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值