poj 1836 Alignment(线性dp)
题目链接:http://poj.org/problem?id=1836
思路分析:假设数组为A[0, 1, …, n],求在数组中最少去掉几个数字,构成的新数组B[0, 1, …, m]满足条件B[0] < B[1] <….<B[i] 且 B[i+1] > … > B[m];
该问题实质为求A[0, …, k]的最长递增子序列和A[j, …, n]中的最长递减子序列(0 <= k <= n, 0 <= j <= n, k < j);所以求出A[0, .., k]的最长递增子序列
与A[j, …, n]中的最长递减子序列,在枚举k与j的值,求出最大的和,在用人数减去最大和即可;
代码如下:
#include <iostream>
using namespace std; const int MAX_N = + ;
double num[MAX_N];
int dp_l[MAX_N], dp_r[MAX_N]; inline int Max(int a, int b) { return a - b > ? a : b; }
int main()
{
int n; while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; ++ i)
scanf("%lf", &num[i]); for (int i = ; i < n; ++ i)
{
dp_l[i] = ;
for (int j = ; j < i; ++ j)
{
if (num[j] < num[i])
dp_l[i] = Max(dp_l[i], dp_l[j] + );
}
}
for (int i = n - ; i >= ; -- i)
{
dp_r[i] = ;
for (int j = n - ; j > i; -- j)
{
if (num[j] < num[i])
dp_r[i] = Max(dp_r[i], dp_r[j] + );
}
} int ans = ;
for (int i = ; i < n; ++ i)
{
int temp = dp_l[i];
for (int j = i + ; j < n; ++ j)
{
if (temp + dp_r[j] > ans)
ans = temp + dp_r[j];
}
}
ans = Max(ans, dp_l[n - ]);
ans = Max(ans, dp_r[]); printf("%d\n", n - ans);
}
return ;
}
poj 1836 Alignment(线性dp)的更多相关文章
- POJ 1836 Alignment 水DP
题目: http://poj.org/problem?id=1836 没读懂题,以为身高不能有相同的,没想到排中间的两个身高是可以相同的.. #include <stdio.h> #inc ...
- POJ 1836 Alignment(DP max(最长上升子序列 + 最长下降子序列))
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14486 Accepted: 4695 Descri ...
- POJ 2479-Maximum sum(线性dp)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33918 Accepted: 10504 Des ...
- poj 1836 Alignment(dp)
题目:http://poj.org/problem?id=1836 题意:最长上升子序列问题, 站队,求踢出最少的人数后,使得队列里的人都能看到 左边的无穷远处 或者 右边的无穷远处. 代码O(n^2 ...
- POJ 1836 Alignment (双向DP)
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10804 Accepted: 3464 Descri ...
- POJ 1836 Alignment
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11450 Accepted: 3647 Descriptio ...
- poj 3356 AGTC(线性dp)
题目链接:http://poj.org/problem?id=3356 思路分析:题目为经典的编辑距离问题,其实质为动态规划问题: 编辑距离问题定义:给定一个字符串source,可以对其进行复制,替换 ...
- POJ 1745 Divisibility (线性dp)
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10598 Accepted: 3787 Des ...
- POJ 1836 Alignment 最长递增子序列(LIS)的变形
大致题意:给出一队士兵的身高,一开始不是按身高排序的.要求最少的人出列,使原序列的士兵的身高先递增后递减. 求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多. 1 2 3 4 ...
随机推荐
- ASP.Net中使用Report Service
昨天研究以下Report Service,发现SQL Service自带的报表服务器也非常强大, 一,报表在不同的应用中表现形式 参考:http://www.cnitblog.com/yide/arc ...
- 块对象block小结(2)
MRC环境下 //// main.m// blcok//// Created by ios on 16/4/6.// Copyright © 2016年 ios. All rights res ...
- Oracle查询表结构的常用语句
1. 查询表结构基本信息 select * from user_tables t,user_tab_comments c where c.table_name = t.table_name and t ...
- Chain of Responsibility模式
熟悉VC/MFC的都知道,VC是“基于消息,事件驱动”,消息在VC开发中起着举足轻重的作用.MFC提供了消息的处理的链式处理策略,处理消息的请求将沿着预定好的路径依次进行处理.消息的发送者并不知道该消 ...
- mvn command is not recognized as an internal or external command
even though I have configured %m2_home% and %path% correctly, the command "mvn" is still n ...
- 一周学会Mootools 1.4中文教程:(6)动画
先看一下动画的参数设置: 参数: fps - (number:默认是50) 每秒的帧数. unit - (string:默认是 false) 单位,可为 'px','em',或 '%'. link - ...
- POSIX扩展正则表达式函数
1.ereg()函数和eregi()函数 函数语法: bool ereg/eregi ( string pattern, string string [, array regs] ) 函数功能: 在字 ...
- SublimeLinter
SublimeLinter 关于代码检查 郑重推荐这个插件 官方插件库中仅有sublime3 有此插件 (对于ST2 有个sublimeLinter for ST2 没试过) SublimeLinte ...
- “-Xmx1024m -Xms1024m -Xmn512m -Xss256k”——Java运行参数(转)
JVM的堆的内存, 是通过下面面两个参数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后 ...
- MFG 常用英文单字
Semiconductor 导体.绝缘体和半导体主要依据导电系数的大小,决定了电子的移动速度. 导体:金.银.铜.铁.人.水……导电系数大,传导容易 绝缘体:塑料.木头.皮革.纸……导电系数小.传导不 ...