POJ 1836 Alignment 最长递增子序列(LIS)的变形
大致题意:给出一队士兵的身高,一开始不是按身高排序的。要求最少的人出列,使原序列的士兵的身高先递增后递减。
求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多。
1 2 3 4 5 4 3 2 1
这个序列从左至右看前半部分是递增,从右至左看前半部分也是递增。所以我们先把从左只右和从右至左的LIS分别求出来。
如果结果是这样的:
A[i]={1.86 1.86 1.30621 2 1.4 1 1.97 2.2} //原队列
a[i]={1 1 1 2 2 1 3 4}
b[i]={3 3 2 3 2 1 1 1}
如果是A[1]~A[i]递增,A[i+1]~A[8]递减。此时就是求:a[1]~a[i]之间的一个值与b[i+1]~b[8]之间的一个值的和的最大值。
O(n^2)和O(nlogn)算法都可以过。
O(n^2)算法:
- #include <iostream>
- #include <cstdio>
- using namespace std;
- const int Max=1e3+;
- int main()
- {
- //freopen("in.txt","r",stdin);
- int n;
- scanf("%d",&n);
- double a[Max]={};
- for(int i=; i<n; i++)
- scanf("%f",a+i);
- int l[Max]= {},r[Max]= {};
- l[]=r[n-]=;
- for(int i = ; i < n; i++)
- {
- int maxLen = ;
- for(int j = ; j < i; j++)
- if(a[j]<a[i])
- maxLen = max(maxLen,l[j]);
- l[i] = maxLen + ;
- }
- for(int i=n-; i>=; i--)
- {
- int maxLen=;
- for(int j=n-; j>i; j--)
- if(a[j]<a[i])
- maxLen=max(maxLen,r[j]);
- r[i]=maxLen+;
- }
- int maxlen=;
- for(int i=;i<n-;i++)
- for(int j=i+;j<n;j++)
- maxlen=max(maxlen,l[i]+r[j]);
- printf("%d\n",n-maxlen);
- return ;
- }
O(nlogn)算法
- #include <iostream>
- #include <cstdio>
- using namespace std;
- const int Max=1e3+;
- int l[Max]= {},r[Max]= {};
- double B[Max];
- int BinarySearch(double *a, double value, int n)
- {
- int low = ;
- int high = n - ;
- while(low <= high)
- {
- int mid = (high + low) / ;
- if(a[mid] == value)
- return mid;
- else if(value<a[mid])
- high = mid - ;
- else
- low = mid + ;
- }
- return low;
- }
- int LIS_DP_NlogN(double *a, int n,int *Len)
- {
- int nLISLen = ;
- B[] = a[];
- for(int i = ; i < n; i++)
- {
- if(a[i] > B[nLISLen - ])
- {
- B[nLISLen] = a[i];
- nLISLen++;
- Len[i]=nLISLen;
- }
- else
- {
- int pos = BinarySearch(B, a[i], nLISLen);
- B[pos] = a[i];
- Len[i]=pos+;
- }
- }
- return nLISLen;
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- int n;
- scanf("%d",&n);
- double a[Max]={};
- double b[Max]={};
- l[]=r[]=;
- for(int i=; i<n; i++)
- {
- scanf("%f",a+i);
- b[n-i-]=a[i];
- }
- LIS_DP_NlogN(a,n,l);
- LIS_DP_NlogN(b,n,r);
- int maxlen=;
- for(int i=;i<n-;i++)
- for(int j=n-i-;j>=;j--)
- maxlen=max(maxlen,l[i]+r[j]);
- printf("%d\n",n-maxlen);
- return ;
- }
POJ 1836 Alignment 最长递增子序列(LIS)的变形的更多相关文章
- 动态规划(DP),最长递增子序列(LIS)
题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...
- 2.16 最长递增子序列 LIS
[本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...
- 最长回文子序列LCS,最长递增子序列LIS及相互联系
最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...
- 一个数组求其最长递增子序列(LIS)
一个数组求其最长递增子序列(LIS) 例如数组{3, 1, 4, 2, 3, 9, 4, 6}的LIS是{1, 2, 3, 4, 6},长度为5,假设数组长度为N,求数组的LIS的长度, 需要一个额外 ...
- 算法面试题 之 最长递增子序列 LIS
找出最长递增序列 O(NlogN)(不一定连续!) 参考 http://www.felix021.com/blog/read.php?1587%E5%8F%AF%E6%98%AF%E8%BF%9E%E ...
- 算法之动态规划(最长递增子序列——LIS)
最长递增子序列是动态规划中最经典的问题之一,我们从讨论这个问题开始,循序渐进的了解动态规划的相关知识要点. 在一个已知的序列 {a1, a 2,...an}中,取出若干数组成新的序列{ai1, ai ...
- 最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现
关于最长递增子序列时间复杂度O(n^2)的实现方法在博客http://blog.csdn.net/iniegang/article/details/47379873(最长递增子序列 Java实现)中已 ...
- 动态规划 - 最长递增子序列(LIS)
最长递增子序列是动态规划中经典的问题,详细如下: 在一个已知的序列{a1,a2,...,an}中,取出若干数组组成新的序列{ai1,ai2,...,aim},其中下标i1,i2,...,im保持递增, ...
- 最长递增子序列LIS再谈
DP模型: d(i) 以第 i 个元素结尾的最长递增子序列的长度. 那么就有 d(i) = max(d(j)) + 1;(j<i&&a[j]<a[i]),答案 max(d( ...
随机推荐
- 物料分类账 [COML] PART 2 - 总体流程
核心流程概要: [1]. 分类账在物料主数据的影响 首先描述下SAP中物料价格的 物料主数据相关的几个点: q价格控制(Price Control): 决定物料计价方式. S 标准价格(Standar ...
- 架构模式之REST架构
直至今日,分布式系统(Distributed System)已经取得了大规模的应用,特别是Web的发展,已经给软件开发带来了翻天覆地的变化,这一点已经毋庸置疑了. 构建分布式系统常用的技术通常就是使用 ...
- Visual Studio 2015 开发大量 JavaScript 代码项目程序崩溃的解决方案
最近公司做新项目,基于 Bootstrap.AngularJS 和 kendo 开发一套后台的管理系统,在项目中使用了大量的 JavaScript 文件,这两天 Visual Studio 2015 ...
- BugFixed
- A successful Git branching model
这个模型比较全,收藏一下,原文: http://nvie.com/posts/a-successful-git-branching-model/ 关于这个模型中的hotfix只适应最新的Release ...
- java.lang.NullPointerException
你妹的这是什么错误啊? Errors occurred during the build. Errors running builder 'Android Resource Manager' on p ...
- 译:在C#中使用LINQ To SQL
译文出处:http://www.codeproject.com/Tips/871938/LINQ-To-SQL-Using-Csharp 今天在这个话题中,我给大家分享一个在c#编程中非常有趣和十分有 ...
- 用Razor語法寫範本-RazorEngine組件介紹【转——非常好,可以用它来代替NVelocity】
RazorEngine 官網網址:http://razorengine.codeplex.com 在找到RazorEngine之前曾經想過其他的方案,如T4與V8 Engine載jquery.temp ...
- Careercup 论坛上较有意思的题目整理
# 数据结构类 ### 线段树 segment tree http://www.careercup.com/question?id=5165570324430848 找区间内的value的个数 二维线 ...
- Android样式的开发:Style篇
前面铺垫了那么多,终于要讲到本系列的终篇,整合所有资源,定义成统一的样式.哪些该定义成统一的样式呢?举几个例子吧: 每个页面标题栏的标题基本会有一样的字体大小.颜色.对齐方式.内间距.外间距等,这就可 ...