POJ 1836 Alignment (简单DP)
来源:互联网 发布:linux连接数据库命令 编辑:程序博客网 时间:2024/04/30 12:35
题目大意
- 给出n个浮点数(2 <= n <= 1000 , 0.5 <= 浮点数 <=2.5),然后求一段序列长度ans,要求这个序列中从左到右递增,或者从右到左递增。输出n - ans
分析
- 求最长上升子序列和最长下降子序列的长度
- dp1[i]表示以i结尾的最长上升子序列的长度
- dp2[i]表示以i结尾的最长下降子序列的长度
- 然后枚举两段区间[0,i] [j,n-1]
- 时间复杂度O(n^2)
代码
#include <iostream>using namespace std;const int maxn = 1010;double num[maxn];int dp1[maxn] = {1}; // dp1[i]表示以i结尾的最长上升子序列的长度int dp2[maxn] = {1}; // dp2[i]表示以i结尾的最长下降子序列的长度int main(){ int n; while(cin >> n) { for(int i = 0; i < n; i++) cin >> num[i]; for(int i = 1; i < n; i++) { //LIS dp1[i] = 1; for(int j = 0; j < i; j++) if(num[j] < num[i]) dp1[i] = max(dp1[i] , dp1[j]+1); } dp2[n-1] = 1; for(int i = n-2; i >= 0; i--) { //LDS dp2[i] = 1; for(int j = n-1; j > i; j--) if(num[j] < num[i]) dp2[i] = max(dp2[i] , dp2[j]+1); } int ans = 0; for(int i = 0; i < n; i++) { //枚举起点[0,i] [j,n-1] ans = max(ans , dp1[i]); for(int j = i+1; j < n; j++) ans = max(ans , dp1[i]+dp2[j]); } cout << n - ans << endl; } return 0;}
0 0
- POJ 1836 Alignment (简单DP)
- poj 1836 Alignment DP
- POJ 1836-Alignment(dp)
- poj 1836 Alignment dp
- poj 1836 Alignment(dp,LIS)
- POJ 1836-Alignment(DP/LIS变形)
- POJ 1836 Alignment(DP | LIS)
- POJ 1836 Alignment DP(LIS)
- poj 1836 Alignment DP入门
- poj 1836 Alignment (DP LIS)
- POJ 1836 Alignment(DP LIS)
- [POJ 1836]Alignment[DP][LIS]
- POJ 1836 Alignment DP LIS
- POJ 1836Alignment(DP最长上升子序列)
- POJ 1836 Alignment 双向LIS+DP
- POJ 1836 Alignment(LIS)
- poj 1080 dp(sequence alignment)
- POJ 1836 Alignment(DP max(最长上升子序列 + 最长下降子序列))
- android系统开发中log的使用方法
- Python Linux下安装python 软件包
- [Java]collection类集中的Set接口
- Java Web 中使用ffmpeg实现视频转码、视频截图
- HDU_2448_Mining Station on the Sea(最短路 + 最小费用流)
- POJ 1836 Alignment (简单DP)
- VTK鼠标选点
- Intellij Idea 将java项目打包成jar
- UESTC 92 Journey(LCA或树剖)
- 快速排序
- POJ2777 Count Color 线段树区间更新
- 匿名
- FOJ有奖月赛-2015年10月(没有补完)
- leetcode 102 Binary Tree Level Order Traversal(难易度:Easy)