大致题意:给出一队士兵的身高,一开始不是按身高排序的。要求最少的人出列,使原序列的士兵的身高先递增后递减。

求递增和递减不难想到递增子序列,要求最少的人出列,也就是原队列的人要最多。

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)算法:

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. const int Max=1e3+;
  6.  
  7. int main()
  8. {
  9. //freopen("in.txt","r",stdin);
  10. int n;
  11. scanf("%d",&n);
  12. double a[Max]={};
  13. for(int i=; i<n; i++)
  14. scanf("%f",a+i);
  15. int l[Max]= {},r[Max]= {};
  16. l[]=r[n-]=;
  17. for(int i = ; i < n; i++)
  18. {
  19. int maxLen = ;
  20. for(int j = ; j < i; j++)
  21. if(a[j]<a[i])
  22. maxLen = max(maxLen,l[j]);
  23. l[i] = maxLen + ;
  24. }
  25. for(int i=n-; i>=; i--)
  26. {
  27. int maxLen=;
  28. for(int j=n-; j>i; j--)
  29. if(a[j]<a[i])
  30. maxLen=max(maxLen,r[j]);
  31. r[i]=maxLen+;
  32. }
  33. int maxlen=;
  34. for(int i=;i<n-;i++)
  35. for(int j=i+;j<n;j++)
  36. maxlen=max(maxlen,l[i]+r[j]);
  37. printf("%d\n",n-maxlen);
  38. return ;
  39. }

O(nlogn)算法

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4.  
  5. const int Max=1e3+;
  6. int l[Max]= {},r[Max]= {};
  7. double B[Max];
  8. int BinarySearch(double *a, double value, int n)
  9. {
  10. int low = ;
  11. int high = n - ;
  12. while(low <= high)
  13. {
  14. int mid = (high + low) / ;
  15. if(a[mid] == value)
  16. return mid;
  17. else if(value<a[mid])
  18. high = mid - ;
  19. else
  20. low = mid + ;
  21. }
  22. return low;
  23. }
  24. int LIS_DP_NlogN(double *a, int n,int *Len)
  25. {
  26. int nLISLen = ;
  27. B[] = a[];
  28. for(int i = ; i < n; i++)
  29. {
  30. if(a[i] > B[nLISLen - ])
  31. {
  32. B[nLISLen] = a[i];
  33. nLISLen++;
  34. Len[i]=nLISLen;
  35. }
  36. else
  37. {
  38. int pos = BinarySearch(B, a[i], nLISLen);
  39. B[pos] = a[i];
  40. Len[i]=pos+;
  41. }
  42. }
  43. return nLISLen;
  44. }
  45. int main()
  46. {
  47. //freopen("in.txt","r",stdin);
  48. int n;
  49. scanf("%d",&n);
  50. double a[Max]={};
  51. double b[Max]={};
  52. l[]=r[]=;
  53. for(int i=; i<n; i++)
  54. {
  55. scanf("%f",a+i);
  56. b[n-i-]=a[i];
  57. }
  58. LIS_DP_NlogN(a,n,l);
  59. LIS_DP_NlogN(b,n,r);
  60. int maxlen=;
  61. for(int i=;i<n-;i++)
  62. for(int j=n-i-;j>=;j--)
  63. maxlen=max(maxlen,l[i]+r[j]);
  64. printf("%d\n",n-maxlen);
  65. return ;
  66. }

POJ 1836 Alignment 最长递增子序列(LIS)的变形的更多相关文章

  1. 动态规划(DP),最长递增子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...

  2. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  3. 最长回文子序列LCS,最长递增子序列LIS及相互联系

    最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...

  4. 一个数组求其最长递增子序列(LIS)

    一个数组求其最长递增子序列(LIS) 例如数组{3, 1, 4, 2, 3, 9, 4, 6}的LIS是{1, 2, 3, 4, 6},长度为5,假设数组长度为N,求数组的LIS的长度, 需要一个额外 ...

  5. 算法面试题 之 最长递增子序列 LIS

    找出最长递增序列 O(NlogN)(不一定连续!) 参考 http://www.felix021.com/blog/read.php?1587%E5%8F%AF%E6%98%AF%E8%BF%9E%E ...

  6. 算法之动态规划(最长递增子序列——LIS)

    最长递增子序列是动态规划中最经典的问题之一,我们从讨论这个问题开始,循序渐进的了解动态规划的相关知识要点. 在一个已知的序列 {a1, a 2,...an}中,取出若干数组成新的序列{ai1, ai ...

  7. 最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现

    关于最长递增子序列时间复杂度O(n^2)的实现方法在博客http://blog.csdn.net/iniegang/article/details/47379873(最长递增子序列 Java实现)中已 ...

  8. 动态规划 - 最长递增子序列(LIS)

    最长递增子序列是动态规划中经典的问题,详细如下: 在一个已知的序列{a1,a2,...,an}中,取出若干数组组成新的序列{ai1,ai2,...,aim},其中下标i1,i2,...,im保持递增, ...

  9. 最长递增子序列LIS再谈

    DP模型: d(i) 以第 i 个元素结尾的最长递增子序列的长度. 那么就有 d(i) = max(d(j)) + 1;(j<i&&a[j]<a[i]),答案 max(d( ...

随机推荐

  1. 物料分类账 [COML] PART 2 - 总体流程

    核心流程概要: [1]. 分类账在物料主数据的影响 首先描述下SAP中物料价格的 物料主数据相关的几个点: q价格控制(Price Control): 决定物料计价方式. S 标准价格(Standar ...

  2. 架构模式之REST架构

    直至今日,分布式系统(Distributed System)已经取得了大规模的应用,特别是Web的发展,已经给软件开发带来了翻天覆地的变化,这一点已经毋庸置疑了. 构建分布式系统常用的技术通常就是使用 ...

  3. Visual Studio 2015 开发大量 JavaScript 代码项目程序崩溃的解决方案

    最近公司做新项目,基于 Bootstrap.AngularJS 和 kendo 开发一套后台的管理系统,在项目中使用了大量的 JavaScript 文件,这两天 Visual Studio 2015 ...

  4. BugFixed

  5. A successful Git branching model

    这个模型比较全,收藏一下,原文: http://nvie.com/posts/a-successful-git-branching-model/ 关于这个模型中的hotfix只适应最新的Release ...

  6. java.lang.NullPointerException

    你妹的这是什么错误啊? Errors occurred during the build. Errors running builder 'Android Resource Manager' on p ...

  7. 译:在C#中使用LINQ To SQL

    译文出处:http://www.codeproject.com/Tips/871938/LINQ-To-SQL-Using-Csharp 今天在这个话题中,我给大家分享一个在c#编程中非常有趣和十分有 ...

  8. 用Razor語法寫範本-RazorEngine組件介紹【转——非常好,可以用它来代替NVelocity】

    RazorEngine 官網網址:http://razorengine.codeplex.com 在找到RazorEngine之前曾經想過其他的方案,如T4與V8 Engine載jquery.temp ...

  9. Careercup 论坛上较有意思的题目整理

    # 数据结构类 ### 线段树 segment tree http://www.careercup.com/question?id=5165570324430848 找区间内的value的个数 二维线 ...

  10. Android样式的开发:Style篇

    前面铺垫了那么多,终于要讲到本系列的终篇,整合所有资源,定义成统一的样式.哪些该定义成统一的样式呢?举几个例子吧: 每个页面标题栏的标题基本会有一样的字体大小.颜色.对齐方式.内间距.外间距等,这就可 ...