出自一次很失败的开学测试

LIS自然会做

可以参见:https://blog.csdn.net/Radium_1209/article/details/79704234

由于对于LIS的nlogn算法不熟悉,导致错误理解,记录的路径出现了问题,其中还用了n^2的算法记录路径(好理解),但是不出所料T了。

正确的方法应该是:由于nlogn算法中,dp[i]存的是长度为i的最后一位数字,并不是路径!!!我们可以用一个path数组来标记每一个数字位于的位置,然后通过dfs倒着来找最后结果长度的最后一个,或者不用dfs直接找也行。

例题:(UVA481)

Write a program that will select the longest strictly increasing subsequence from a sequence of integers.

Input

The input file will contain a sequence of integers (positive, negative, and/or zero). Each line of the input file will contain one integer.

Output

The output for this program will be a line indicating the length of the longest subsequence, a newline, a dash character ('-'), a newline, and then the subsequence itself printed with one integer per line. If the input contains more than one longest subsequence, the output file should print the one that occurs last in the input file.

Notice that the second 8 was not included -- the subsequence must be strictly increasing.

Sample Input

-7
10
9
2
3
8
8
1

Sample Output

4
-
-7
2
3
8

题意:求LIS并输出路径(最后一个)

dfs版:

#include <cstdio>
#include <iostream>
using namespace std;int n=1,a[1000005];
int dp[1000005];
int path[1000005];void dfs(int i,int x)
{if (i<1 || x<=0)return;while(path[i]!=x)i--;dfs(i,x-1);printf("%d\n",a[i]);
}int main()
{while(~scanf("%d",&a[n])){n++;}n--;int len=1;dp[1]=a[1]; path[1]=1;for (int i=2;i<=n;i++){if (a[i]>dp[len]){dp[++len]=a[i];path[i]=len;}else{int pos=lower_bound(dp+1,dp+len+1,a[i])-dp;dp[pos]=a[i];path[i]=pos;}}printf("%d\n-\n",len);dfs(n,len);return 0;
}

非dfs版:

#include <cstdio>
#include <iostream>
using namespace std;int n=1,a[1000005];
int dp[1000005];
int path[1000005];
int ans[1000005];int main()
{while(~scanf("%d",&a[n])){n++;}n--;int len=1;dp[1]=a[1]; path[1]=1;for (int i=2;i<=n;i++){if (a[i]>dp[len]){dp[++len]=a[i];path[i]=len;}else{int pos=lower_bound(dp+1,dp+len+1,a[i])-dp;dp[pos]=a[i];path[i]=pos;}}printf("%d\n-\n",len);int temp=n;int l=len;while(l>=1){int i;for(i=temp;i>=1;i--){if (path[i]==l){ans[l]=a[i];l--; temp=i;break;}}if (i==0)break;}for (int i=1;i<=len;i++)printf("%d\n",ans[i]);return 0;
}

n^2版本(T了,正确性未知)

#include <cstdio>
#include <iostream>
using namespace std;int n=1,a[1000005];
int dp[1000005];
int path[1000005];
int ans[1000005];int main()
{while(~scanf("%d",&a[n])){n++;}n--;int len=0;for (int i=1;i<=n;i++){dp[i]=1;for (int j=1;j<i;j++)if (a[j]<a[i]){if (dp[i]<dp[j]+1){dp[i]=dp[j]+1;path[dp[i]]=j;}}if (dp[i]>=len){len=dp[i];ans[len]=a[i];for (int i=len;i>=2;i--)ans[i-1]=a[path[i]];}}printf("%d\n-\n",len);for (int i=1;i<=len;i++)printf("%d\n",ans[i]);return 0;
}

转载于:https://www.cnblogs.com/Radium1209/p/10415345.html

LIS路径记录(UVA481)相关推荐

  1. FatMouse's Speed(LIS+路径记录)

    题目:FatMouse's Speed 题解:采用O(n^2)的LIS算法,用path记录路径 #include<bits/stdc++.h> using namespace std; t ...

  2. floyd和迪杰斯特拉算法的路径记录方法。

    先说迪杰斯特拉,先看题目吧. 题目1 这题就是迪杰斯特拉的路径记录算法题啊,还记录了最小路径个数. 代码如下 #include<iostream> #include<cstdio&g ...

  3. poj--3984--迷宫问题(bfs+路径记录)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  4. 01背包、完全背包、多重背包问题的C++实现及路径记录

    这里主要实现路径记录,只求最值问题移步 01背包.完全背包.多重背包问题的C++实现 以下均打印输出路径,即装入背包的物品序号,和最大值. 01背包问题 #include <iostream&g ...

  5. vue中的路由2-----嵌套路由跳转,传参和路径记录问题

    vue路由如何传参的问题 这篇文章分为两个部分, 第一个是怎么传过去 第二个是怎么接收信息 在学习之前我们要了解一下路由跳转的规则 (1)路由跳转相当于一个栈,你每跳一次就往栈添加一条数据,所以你可以 ...

  6. 基于STM32跑步路径记录

    基于STM32跑步路径记录   随着科技不断进步,电子化设备不断进入涌入我们的日常生活.生活水平的提高,各项健身运动应运而生,然后,健身运动不能盲目进行,科学的健身方式才能有效的提升我们自身的身体素质 ...

  7. C#部署安装,将用户安装路径记录下写入注册表,并启动

    安装部署程序,将安装目录写入注册表 (1)在"安装部署项目"上点击"注册表编辑器" (2)在HKey_LOCAL_MACHINE_SoftWare 下新建键 M ...

  8. 牛客网 短最优升级路径 【Dijkstra算法】+【路径记录】

    链接:https://www.nowcoder.com/questionTerminal/a7052c5bd8634edb9ccee711a5c1ea54 来源:牛客网 短最优升级路径 题目描述:游戏 ...

  9. 躁动的小Z 最短路+路径记录

    躁动的小Z 时间限制: 1 Sec  内存限制: 128 MB 题目描述 你猜怎么样?小Z追到Gakki了!Gakki邀请小Z去她家共进晚餐,小Z喜出望外.小Z的家和Gakki的家隔着几个街区,所以他 ...

最新文章

  1. 图解从 URL 到网页通信原理
  2. BUUCTF-misc另外一个世界 8个二进制数为一组转ASC码
  3. [转]JavaScript var obj = { id:1, name:jacky } 大括号是啥意思?
  4. Cloud for Customer UI designer模型里编写的script运行时是如何执行的
  5. 域 正在应用计算机设置,入域的时候卡在”正在应用计算机设置”持续了好几分钟...
  6. 微信支付宝是如何赚钱的?
  7. [C++] string
  8. 物联网大数据平台有哪些功能特点
  9. Squid缓存服务器方案
  10. Chapter 1 Securing Your Server and Network(7):禁用SQL Server Browse
  11. iOS / OXS LeanCloud云存储方案简单测试记录
  12. Atitit sift匹配度计算 图片连线 oepncv sift java匹配
  13. 海湾5000汉字编码app
  14. WINDOWS渗透与提权总结(2)
  15. matlab锯齿交换,MATLAB折线消除锯齿平滑
  16. 想自学软件测试?这本《软件测试》,入门必看
  17. DELL Inspiron 15 5585 AMD Ryzen™ 5 3500U 解除功耗频率限制 性能起飞(文章失效:最新方案重装系统或者删除dell全套)
  18. A. Equalize Prices Again(水题) Codeforces Round #590 (Div. 3)
  19. android 5.x—Elevation阴影
  20. maven的xml配置文件内容

热门文章

  1. E - 嗯? 51Nod - 1432(二分)
  2. 推荐系统的发展演进历史和模型的目标及优缺点
  3. 深度学习(10)TensorFlow基础操作六: 数学运算
  4. 【竞赛算法学习】学术前沿趋势分析-论文数据统计
  5. 【算法竞赛学习】数据分析达人赛3:汽车产品聚类分析
  6. 疯子的算法总结(六) 简单排序总 选择排序+插入排序+比较排序+冒泡排序
  7. 《TCP/IP详解》笔记----第四章 ARP协议
  8. [Golang] GoConvey测试框架使用指南
  9. PADS2007中的层类型(plane type) 简介
  10. Spring boot 系列 入门--配置