题目链接:点击打开链接

题意:

给定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. google浏览器audits

    功能翻译 audits,审计 Audits help you identify and fix common problems that affect your site'sperformance,a ...

  2. ACM程序设计选修课——1057: Beautiful Garden(模拟+耐心调试)

    1057: Beautiful Garden Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 25  Solved: 12 [Submit][Statu ...

  3. leetcode 26 水

    class Solution { public: int removeDuplicates(vector<int>& nums) { sort(nums.begin(),nums. ...

  4. java面试题之spring aop中jdk和cglib哪个动态代理的性能更好?

    在jdk6和jdk7的时候,jdk比cglib要慢: 在jdk8的时候,jdk性能得到提升比cglib要快很多: 结论出自:https://www.cnblogs.com/xuliugen/p/104 ...

  5. webpack打包字体图标报错的解决办法

    webpack打包字体图标需要两个加载器  url-loader 和 file-loader 另外  字体图标的引入方式  本来应该是  url("....") 这样的方式,但是w ...

  6. touch event 存疑

    1.原声js与借用jquery输出来的事件列表却不一样 function touchPlay(e) { e.preventDefault(); console.log(e); } var screen ...

  7. Delphi GDI对象之绘制位图

    http://www.cnblogs.com/pchmonster/archive/2012/07/06/2579334.html 绘制位图(Drawing Bitmaps) 绘制位图听起来似乎很难, ...

  8. 理解webpack中的devTool的配置项

    2.1. eval  eval 会将每一个module模块,执行eval,执行后不会生成sourcemap文件,仅仅是在每一个模块后,增加sourceURL来关联模块处理前后对应的关系.在webpac ...

  9. java 判断请求来自手机端还是电脑端

    根据当前请求的特征,判断该请求是否来自手机终端,主要检测特殊的头信息,以及user-Agent这个header public static boolean isMobileDevice(HttpSer ...

  10. Java线程的中断

    引言 Java没有提供任何机制来安全地终止线程,但提供了中断机制,即thread.interrupt()方法.线程中断是一种协作式的机制,并不是说调用了中断方法之后目标线程一定会立即中断,而是发送了一 ...