【UVa】11456 - Trainsorting
2016-04-07 23:26
417 查看
Problem here
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?
of each test case is the following:
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.
restrictions.
3
1
2
3
這裡-1因為LIS和LDS的起點會重複
Problem
Erin is an engineer. She drives trains. She also arranges the cars within each train. She prefers toput 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 is the number of test cases to follow. The test cases follow, one after another; the formatof each test case is the following:
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 givenrestrictions.
SAMPLE
input
13
1
2
3
output
3Solution
LIS + LDS -1這裡-1因為LIS和LDS的起點會重複
#include <iostream> #include <vector> #include "memory.h" using namespace std; int train[2000]; int len[2000]; int len2[2000]; int tn; int main(){ int t; while(cin >> t){ while(t--){ cin >> tn; for(int i = tn-1; i >= 0; i--){ len[i] = 1; len2[i] = 1; int tmp; cin >> tmp; train[i] = tmp; } for(int i = 0; i < tn; i++){ for(int j = i+1; j < tn; j++){ if(train[i] < train[j]){ len[j] = max(len[j], len[i]+1); } if(train[i] > train[j]){ len2[j] = max(len2[j], len2[i]+1); } } } int result = 0; for(int i = 0; i < tn; i++){ result = max(result, len[i]+len2[i]-1); } cout << result << endl; } } return 0; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析