LIS路径记录(UVA481)

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/249541.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Activemq源码、编译、导入idea、源码调试总结

1、在本地下载源码 在GitHub官网搜activemq&#xff0c;找到排名第一的&#xff0c;并打开&#xff0c;如图所示&#xff0c;拷贝url地址。 activemq托管地址&#xff1a;https://github.com/apache/activemq.git 切换到git bash下&#xff0c;输入命令&#xff1a; mkdir a…

activiti 视图

1. application.properties增加如下配置 spring.activiti.database-schema-updatefalsespring.activiti.db-history-usedfalsespring.activiti.db-identity-usedfalse 2. 视图sql -- 修改表名称 ALTER TABLE act_id_user RENAME act_id_user_bak1; ALTER TABLE act_id_group RE…

ActiveMQ源码解析 建立连接

作为一个消息中间件&#xff0c;有客户端和服务端两部分代码&#xff0c;这次的源码解析系列主要从客户端的代码入手&#xff0c;分成建立连接、消息发送、消息消费三个部分。趁着我昨天弄明白了源码编译的兴奋劲头还没过去&#xff0c;今天研究一下建立连接的部分。 如果读起…

原生Js_实现广告弹窗

广告样式当页面加载后5s刷新在右下角 <!DOCTYPE html> <html><head><meta charset"utf-8" /><title>Gary图片轮播</title><style type"text/css">#ad{width:300px;height: 300px;background-color:antiquewhite…

springcloud注册中心eureka

1、前提 springcloud的注册中心是以springboot为基础搭建起来的。 开发工具&#xff1a;IDEA 项目管理工具&#xff1a;maven 2、搭建步骤 创建一个web项目&#xff08;建议使用IDEA工具构建项目&#xff09;修改pom文件 <dependency><groupId>org.springframework…

Nancy in .Net Core学习笔记 - 视图引擎

前文中我们介绍了Nancy中的路由&#xff0c;这一篇我们来介绍一下Nancy中的视图引擎。 Nancy中如何返回一个视图(View) 在ASP.NET Mvc中&#xff0c;我们使用ViewResult类来返回一个视图。Nancy中也提供了类似的功能, 在NancyModule类中&#xff0c;Nancy提供了一个ViewRendere…

设计模式之组合模式(Composite 模式)

引入composite模式 在计算机文件系统中&#xff0c;有文件夹的概念&#xff0c;文件夹里面既可以放入文件也可以放入文件夹&#xff0c;但是文件中却不能放入任何东西。文件夹和文件构成了一种递归结构和容器结构。 虽然文件夹和文件是不同的对象&#xff0c;但是他们都可以被放…

Ansible批量在远程主机执行命令

Ansible直接执行远程命令&#xff0c;不用ssh登陆交互执行。    如下&#xff1a;    ansible all -i 192.168.199.180, -m shell -a "ifconfig" -u supermap    参数解释&#xff1a;    -i 连接到远程主机“192.168.199.180&#xff0c;”&#xf…

HOJ 2651

一道二分的题目&#xff0c;但要注意不能用double&#xff0c; 并且要注意一下二分的步骤 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define pi 3.1415926535898 #define eps 0.0001 using namespace std; inl…

HierarchicalBeanFactory接口

HierarchicalBeanFactory 提供父容器的访问功能.至于父容器的设置,需要找ConfigurableBeanFactory的setParentBeanFactory(接口把设置跟获取给拆开了!). HierarchicalBeanFactory源码具体&#xff1a; 1、第一个方法返回本Bean工厂的父工厂。这个方法实现了工厂的分层。 2、第…

C++: C++函数声明的时候后面加const

C: C函数声明的时候后面加const 转自&#xff1a;http://blog.csdn.net/zhangss415/article/details/7998123 非静态成员函数后面加const&#xff08;加到非成员函数或静态成员后面会产生编译错误&#xff09;&#xff0c;表示成员函数隐含传入的this指针为const指针&#xff0…

【计蒜客习题】消除字符串

问题描述 蒜头君喜欢中心对称的字符串&#xff0c;即回文字符串。现在蒜头君手里有一个字符串 SS&#xff0c;蒜头君每次都会进行这样的操作&#xff1a;从 SS 中挑选一个回文的子序列&#xff0c;将其从字符串 SS 中去除&#xff0c;剩下的字符重组成新的字符串 SS。 蒜头君想…

HierarchicalBeanFactory

BeanFactory分层 package org.springframework.beans.factory;//分层工厂 public interface HierarchicalBeanFactory extends BeanFactory {//返回工厂的父工厂BeanFactory getParentBeanFactory();//这个工厂中是否包含这个Beanboolean containsLocalBean(String name); }测…

Training a classifier

你已经学习了如何定义神经网络&#xff0c;计算损失和执行网络权重的更新。 现在你或许在思考。 What about data? 通常当你需要处理图像&#xff0c;文本&#xff0c;音频&#xff0c;视频数据&#xff0c;你能够使用标准的python包将数据加载进numpy数组。之后你能够转换这些…

19岁白帽子通过bug悬赏赚到一百万美元--转

出处&#xff1a;https://news.cnblogs.com/n/620858/ 19 岁的 Santiago Lopez 通过 bug 悬赏平台 HackerOne 报告漏洞&#xff0c;成为第一位通过 bug 悬赏赚到一百万美元的白帽子黑客。他的白帽子生涯始于 2015 年&#xff0c;至今共报告了超过 1600 个安全漏洞。他在 16 岁时…

代码分层的设计

分层思想&#xff0c;是应用系统最常见的一种架构模式&#xff0c;我们会将系统横向切割&#xff0c;根据业务职责划分。MVC 三层架构就是非常典型架构模式&#xff0c;划分的目的是规划软件系统的逻辑结构便于开发维护。MVC&#xff1a;英文即 Model-View-Controller&#xff…

【24小时内第四更】为什么我们要坚持写博客?

前言 从2018年7月份&#xff0c;我开始了写作博客之路。开始之前&#xff0c;我打算分享下之前的经历。去年初公司来了个架构师&#xff0c;内部分享过docker原理&#xff0c;TDD单元测试驱动&#xff0c;并发并行异步编程等内容&#xff0c;让我着实惊呆了&#xff0c;因为确实…

sqoop快速入门

转自http://www.aboutyun.com/thread-22549-1-1.html 转载于:https://www.cnblogs.com/drjava/p/10473297.html

ListableBeanFactory接口

ListableBeanFactory获取bean时,Spring 鼓励使用这个接口定义的api. 还有个Beanfactory方便使用.其他的4个接口都是不鼓励使用的. 提供容器中bean迭代的功能,不再需要一个个bean地查找.比如可以一次获取全部的bean(太暴力了),根据类型获取bean.在看SpringMVC时,扫描包路径下的…

HDU 4035 Maze

Maze http://acm.hdu.edu.cn/showproblem.php?pid4035 分析&#xff1a; 在树上走来走去&#xff0c;然后在一个点可以k的概率回到1&#xff0c;可以e的概率走出去&#xff0c;可以1-k-e的概率走到其他的位置&#xff08;分为父节点和子节点讨论&#xff09;。 转移方程就是&a…