题目链接:点击打开链接

题意:

给定n个点的树。

以下n个数表示点权。

以下n-1行给出树。

找一条链,然后找出这条链中的点权组成的最长上升子序列。

求:最长上升子序列的长度。

思路:

首先是维护一条链然后求答案。可是假设直接树形dp(记录每一个点u,u往下递增和u往下递减的长度)会使序列是来回的,即递增和递减都在同一条链上。

枚举每一个点作为子序列的开头,然后维护一条链进行LIS的nlogn做法。

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet; public class Main {
int max(int x, int y) {
return x > y ? x : y;
}
static int N = 6050;
int[] a = new int[N], len = new int[N];
int n;
int ans;
ArrayList<Integer>[] G = new ArrayList[N];
int[] Stack = new int[N];
int top;
void find(int u, int fa) {
int pos = -1, val = -1;
if(a[u]>Stack[top-1]){
pos = -2;//-2表示新加了一个元素
Stack[top++] = a[u];
ans = max(ans, top);
}
else
{
int l = 0, r = top-1, siz = 0;
while(l <= r){
int mid = (l+r)>>1;
if(Stack[mid] < a[u])
l = mid+1;
else
{
r = mid-1;
siz = mid;
}
}
pos = siz; val = Stack[siz];
Stack[pos] = a[u];
}
for(int i = 0; i < G[u].size(); i++){
int v = G[u].get(i); if(v == fa)continue;
find(v, u);
}
if(pos != -1){
if(pos == -2)top--;
else {
Stack[pos] = val;
}
}
}
void solve(int u) {
for(int i = 0; i < G[u].size(); i++){
int v = G[u].get(i);
top = 0;
Stack[top++] = a[u];
find(v, u);
}
} void input() {
n = cin.nextInt();
for (int i = 1; i <= n; i++) {
G[i] = new ArrayList();
a[i] = cin.nextInt();
}
for (int i = 1, u, v; i < n; i++) {
u = cin.nextInt();
v = cin.nextInt();
G[u].add(v);
G[v].add(u);
}
} public void work() {
input();
ans = 1;
for (int i = 1; i <= n; i++)
solve(i);
out.println(ans);
} Main() {
cin = new Scanner(System.in);
out = new PrintWriter(System.out);
} public static void main(String[] args) {
Main e = new Main();
e.work();
out.close();
} public Scanner cin;
public static PrintWriter out;
}

Codeforces 490F Treeland Tour 树上的最长上升子序列的更多相关文章

  1. Codeforces 490F Treeland Tour(离散化 + 线段树合并)

    题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using name ...

  2. Codeforces 490F. Treeland Tour 暴力+LIS

    枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...

  3. Codeforces 490F Treeland Tour 树形dp

    Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...

  4. cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1

    F. Treeland Tour time limit per test 5 seconds memory limit per test 256 megabytes input standard in ...

  5. codeforces mysterious present 最长上升子序列+倒序打印路径

    link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...

  6. Codeforces 667D World Tour 最短路

    链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...

  7. 【UOJ#33】【UR #2】树上GCD(长链剖分,分块)

    [UOJ#33][UR #2]树上GCD(长链剖分,分块) 题面 UOJ 题解 首先不求恰好,改为求\(i\)的倍数的个数,最后容斥一下就可以解决了. 那么我们考虑枚举一个\(LCA\)位置,在其两棵 ...

  8. codeforces E. The Contest(最长上升子序列)

    题目链接:https://codeforces.com/contest/1257/problem/E 题意:给三个序列k1,k2,k3,每个序列有一堆数,k1是前缀,k3是后缀,k2是中间,现可以从任 ...

  9. Codeforces Global Round 11 C. The Hard Work of Paparazzi(dp/最长上升子序列)

    题目链接:https://codeforces.com/contest/1427/problem/C 题意 \(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者 ...

随机推荐

  1. 【bzoj3252】攻略 贪心+DFS序+线段树

    题目描述 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>,这款游戏有n个场景(scene),某 ...

  2. [LOJ#530]「LibreOJ β Round #5」最小倍数

    [LOJ#530]「LibreOJ β Round #5」最小倍数 试题描述 第二天,LCR 终于启动了备份存储器,准备上传数据时,却没有找到熟悉的文件资源,取而代之的是而屏幕上显示的一段话: 您的文 ...

  3. xp局域网共享访问没权限处理

    参考: http://www.360doc.com/content/11/0114/00/905007_86380468.shtml

  4. vim的复制粘贴(包括系统剪贴板)

    1.vim常用复制粘贴命令 Vim的复制粘贴命令无疑是y (yank),p(paster),加上yy,P PS: vim有个很有意思的约定(我觉得是一种约定),就是某个命令的大小写都是实现某种功能,只 ...

  5. hdu 1979 剪枝暴搜

    Fill the blanks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  6. 22深入理解C指针之---通过指针传递函数

    一.通过指针传递函数与通过指针传递数据的本质是一样的,区别就是此时的数据是函数指针(函数的开始的地址) 1.定义:通过函数指针将函数传入函数:通过函数返回函数指针实现函数返回函数的目标 2.特征: 1 ...

  7. configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+.

    configure: error: Building GCC requires GMP 4.2+, MPFR 2.4.0+ and MPC 0.8.0+. 一.错误发生情景: 在安装gcc时,执行.c ...

  8. Linux 之 LNMP服务器搭建-PHP

    LNMP服务器搭建-PHP 参考教程:[千峰教育] 安装: (1)解压源码包 cd /lnmp/srctar -jxvf php-7.3.2.tar.bz2cd php-7.3.2 (2)配置选项 . ...

  9. dedecms--二次开发之会员帐号过期无法登录

    最近在二次开发织梦系统的会员功能:要求会员帐号有期限,开始时间以会员添加时间为开始,这样登录的时候需要判断帐号是否过期,原本的织梦系统中是通过$rs = $cfg_ml->CheckUser($ ...

  10. hdu 2078(DFS)

    Matrix Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3845   Accepted: 1993 Descriptio ...