uva11456——Trainsorting(LIS)
来源:互联网 发布:人工蜂群算法流程图 编辑:程序博客网 时间:2024/05/29 18:37
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?
输入
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 a single integer giving the number of cars in the longest train that can be made with the given restrictions.
样例输入
3
1
2
3
样例输出
3
一队重量不同的车要排列在火车上,只能从最前面或者最后面上,并且列车长要求必须是重量递减排列
我还是网上找了找代码,说下自己的想法。从后面上的一定是重量越来越小,从前面上的一定是重量越来越高,因为允许不让某辆车上,所以可以求出这个序列的最长递增子序列和最长递减,这两个序列相加就是答案
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 2005#define Mod 10001using namespace std;int dp1[MAXN],dp2[MAXN],n,a[MAXN];int main(){ while(~scanf("%d",&n)) { 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[i]>a[j]) dp1[i]=max(dp1[i],dp1[j]+1); else if(a[i]<a[j]) dp2[i]=max(dp2[i],dp2[j]+1); } ans=max(ans,dp1[i]+dp2[i]-1); } printf("%d\n",ans); } return 0;}
- uva11456——Trainsorting(LIS)
- UVA 11456 - Trainsorting(LIS)
- uva 11456 - Trainsorting(dp,LIS)
- uva 11456 - Trainsorting(LIS)
- UVA 11456 - Trainsorting(DP + LIS)
- HDU 3165 Trainsorting (DP, LIS)
- 动态规划 — LIS
- hdu5141——LIS again
- HDU3564——Another LIS(线段树)
- hdu(1069)——Monkey and Banana(LIS变形)
- zjnu(1299)——零件分组(LIS变形)
- 算法——最长递增子序列(LIS)
- 动态规划 —— 最长递增子序列(LIS)
- DP—最长上升子序列(LIS)
- 动态规划——LIS问题
- hdu5087——Revenge of LIS II
- LIS问题——n*logn解法
- 动态规划——LIS问题
- 新的篇章
- 模块化编程 自顶向下编程 自底向上编程 结构化编程 面向对象编程 面向过程编程
- javascript网页特效——window.opener
- 琪露诺的算数教室——解题报告
- python实现字符串全排列(注:每个字符写在了列表里)
- uva11456——Trainsorting(LIS)
- Linux下配置 Keepalived(心跳检测部署)
- 添加阴影
- javascript自定义事件应用实例
- Qt之QComboBox
- 创建HelloWorld项目
- 【IOS学习】NSString copy or not (strong)?
- 九度 OJ 1162 I Wanna Go Home
- 浅析MySQL中exists与in的使用