cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1
5 seconds
256 megabytes
standard input
standard output
The "Road Accident" band is planning an unprecedented tour around Treeland. The RA fans are looking forward to the event and making bets on how many concerts their favorite group will have.
Treeland consists of n cities, some pairs of cities are connected by bidirectional roads. Overall the country has n - 1 roads. We know that it is possible to get to any city from any other one. The cities are numbered by integers from 1 to n. For every city we know its value ri — the number of people in it.
We know that the band will travel along some path, having concerts in some cities along the path. The band's path will not pass one city twice, each time they move to the city that hasn't been previously visited. Thus, the musicians will travel along some path (without visiting any city twice) and in some (not necessarily all) cities along the way they will have concerts.
The band plans to gather all the big stadiums and concert halls during the tour, so every time they will perform in a city which population is larger than the population of the previously visited with concert city. In other words, the sequence of population in the cities where the concerts will be held is strictly increasing.
In a recent interview with the leader of the "road accident" band promised to the fans that the band will give concert in the largest possible number of cities! Thus the band will travel along some chain of cities of Treeland and have concerts in some of these cities, so that the population number will increase, and the number of concerts will be the largest possible.
The fans of Treeland are frantically trying to figure out how many concerts the group will have in Treeland. Looks like they can't manage without some help from a real programmer! Help the fans find the sought number of concerts.
The first line of the input contains integer n (2 ≤ n ≤ 6000) — the number of cities in Treeland. The next line contains n integersr1, r2, ..., rn (1 ≤ ri ≤ 106), where ri is the population of the i-th city. The next n - 1 lines contain the descriptions of the roads, one road per line. Each road is defined by a pair of integers aj, bj (1 ≤ aj, bj ≤ n) — the pair of the numbers of the cities that are connected by the j-th road. All numbers in the lines are separated by spaces.
Print the number of cities where the "Road Accident" band will have concerts.
6
1 2 3 4 5 1
1 2
2 3
3 4
3 5
3 6
4
5
1 2 3 4 5
1 2
1 3
2 4
3 5
3 这道题一开始打算用树形dp,分别以每个点作为根做n次来进行,但是时间复杂度太高,后来决定用二分形式的最长上升子序列,但是如何处理同层的节点又成了新的问题,那么就保存现场,结束遍历后恢复现场
思路:
设dp[i]为长度为i+1时结尾的最小值,(初始化为inf),
1 二分形式的最长上升子序列,假设从根节点遍历到叶节点,从根节点到叶节点就有一个人口值序列,从根节点往叶节点,对每个节点,找到它可以作为dp[i]的位置,因为dp[i]的值一定是递增的(想想为什么),所以可以用二分来查找这个位置.然后更新,这个位置的最大值就是答案,这里时间复杂度是(nlogn)
2 上述说的是单链的情况,因为这个时候用来更新dp的都是位在它前面的点,所以一定是合法的,可是树上的点有同级的和不在一个枝上的,怎么办?(如果直接按照找出所有链然后计算,复杂度会高到n^3)这个时候就要利用到回溯法,把当前修改了的dp值先保存起来,遍历完这个节点再恢复,这里同前面时间复杂度结合起来(nlogn)
3 当根不同的时候,上升还是下降,还有能遍历到的链的长度都不一样,因为(nnlogn)复杂度也够,所以把每个点都当成一次根
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=6005;
const int inf=0x7fffffff;
int dp[maxn],n,r[maxn];//dp[i]i+1长度的结尾最小人口数,n 城市数,r[i] 第i个城市的人口数
int first[maxn],nxt[2*maxn],to[2*maxn],len[maxn];//邻接表相关
int ans;
void dfs(int s,int f){
int pos=lower_bound(dp,dp+n,r[s])-dp;//找到一个比s人口值小的dp[]值
int tmp=dp[pos];//保存这个dp值
ans=max(pos+1,ans);
dp[pos]=min(dp[pos],r[s]);//更新dp
for(int i=first[s];i!=-1;i=nxt[i]){//继续往下延拓
int t=to[i];
if(t==f)continue;
dfs(t,s);
}
dp[pos]=tmp;//恢复
}
void addedge(int f,int t,int ind){
nxt[ind*2]=first[f];to[ind*2]=t;first[f]=2*ind;
nxt[ind*2+1]=first[t];to[ind*2+1]=f;first[t]=2*ind+1;
}
int main(){
scanf("%d",&n);
memset(first,-1,sizeof(first));
fill(dp,dp+n,inf);//初始化
for(int i=1;i<=n;i++)scanf("%d",r+i);//输入
for(int i=1;i<n;i++){int f,t;scanf("%d%d",&f,&t);addedge(f,t,i-1);}
for(int i=1;i<=n;i++){dfs(i,-1);}//开始分别以i为根遍历
printf("%d\n",ans);//输出
return 0;
}
cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1的更多相关文章
- ACM/ICPC 之 最长公共子序列计数及其回溯算法(51Nod-1006(最长公共子序列))
这道题被51Nod定为基础题(这要求有点高啊),我感觉应该可以算作一级或者二级题目,主要原因不是动态规划的状态转移方程的问题,而是需要理解最后的回溯算法. 题目大意:找到两个字符串中最长的子序列,子序 ...
- hdu 5773 The All-purpose Zero 最长上升子序列+树状数组
题目链接:hdu 5773 The All-purpose Zero 官方题解:0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的. 因此我们可以把0拿出来,对剩下的做O(nl ...
- BZOJ 3173 最长上升子序列(树状数组+二分+线段树)
给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 由于序列是顺序插入的,所以当前插入的数字对之 ...
- bzoj3173: [Tjoi2013]最长上升子序列(树状数组+二分倒推)
3173: [Tjoi2013]最长上升子序列 题目:传送门 题解: 好题! 怎么说吧...是应该扇死自己...看错了两次题: 每次加一个数的时候,如果当前位置有数了,是要加到那个数的前面,而不是直 ...
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- 最长公共子序列(lcs)
给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为: abcicba abdkscab ab是两个串的子序列,abc也是,abca也是,其中abca是这两个字符 ...
- Codeforces 490F Treeland Tour 树上的最长上升子序列
题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
随机推荐
- 『NiFi 节点本地流与集群流不一致导致集群加入失败』问题解决
一.概述 在某些极端情况下,某些 NiFi 节点信息会由于用户强行 disconnect from cluster ,而出现 local flow 与 cluster 的 flow 不同步的问题. 此 ...
- hdu4300 Clairewd’s message
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目: Clairewd’s message Time Limit: 2000/1000 MS (J ...
- linux meta 18.0.1 系统安装nodejs
前置条件是:需要准备sudo apt-get 命令 第一步: 执行命令sudo apt-get install nodejs 即可安装, 之后可使用node -v 查看版本node 版本号 第二步: ...
- Linux下多线程下载工具myget
[root@superdba ~]# mytgetMytget 0.0.99: A download accelerator for GNU/LinuxUsage: mytget [options]. ...
- 详解Java中的clone方法 -- 原型模式
转自: http://blog.csdn.net/zhangjg_blog/article/details/18369201 Java中对象的创建 clone顾名思义就是复制, 在Java语言中, ...
- 高通camera结构(摄像头基础介绍)
摄像头基础介绍 一.摄像头结构和工作原理. 拍摄景物通过镜头,将生成的光学图像投射到传感器上,然后光学图像被转换成电信号,电信号再经过模数转换变为数字信号,数字信号经过DSP加工处理,再被送到电脑中进 ...
- GIT使用—提交的查找与变更
一.查找提交 (1)git bisect(二分搜索法) 基于任意搜索条件查找特定的错误提交.在排查某个提交版本导致的错误时非常有用. [root@localhost public_html]# git ...
- 20145313张雪纯 《Java程序设计》第6周学习总结
20145313张雪纯 <Java程序设计>第6周学习总结 教材学习内容总结 将数据从来源中取出,可以使用输入串流:将数据写入目的地,可以使用输出串流. 输入串流代表对象为java.io. ...
- 20145322何志威 《Java程序设计》课程总结
课程总结 每周读书笔记链接汇总 •第一周读书笔记 •第二周读书笔记 •第三周读书笔记 •第四周读书笔记 •第五周读书笔记 •第六周读书笔记 •第七周读书笔记 •第八周读书笔记 •第九周读书笔记 •第十 ...
- ubifs文件系统挂载时提示ubi0: MTD device 5 is write-protected, attach in read-only mode
答:笔者遇到的这种情况是由于分区表未与nor flash的物理擦除块边界对齐而导致的,因此调整分区表即可解决此问题