Codeforces 490F Treeland Tour 树上的最长上升子序列
题目链接:点击打开链接
题意:
给定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 树上的最长上升子序列的更多相关文章
- Codeforces 490F Treeland Tour(离散化 + 线段树合并)
题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using name ...
- Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...
- Codeforces 490F Treeland Tour 树形dp
Treeland Tour 离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快.. #include<bits/stdc++.h> #define LL long ...
- cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1
F. Treeland Tour time limit per test 5 seconds memory limit per test 256 megabytes input standard in ...
- codeforces mysterious present 最长上升子序列+倒序打印路径
link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...
- Codeforces 667D World Tour 最短路
链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...
- 【UOJ#33】【UR #2】树上GCD(长链剖分,分块)
[UOJ#33][UR #2]树上GCD(长链剖分,分块) 题面 UOJ 题解 首先不求恰好,改为求\(i\)的倍数的个数,最后容斥一下就可以解决了. 那么我们考虑枚举一个\(LCA\)位置,在其两棵 ...
- codeforces E. The Contest(最长上升子序列)
题目链接:https://codeforces.com/contest/1257/problem/E 题意:给三个序列k1,k2,k3,每个序列有一堆数,k1是前缀,k3是后缀,k2是中间,现可以从任 ...
- Codeforces Global Round 11 C. The Hard Work of Paparazzi(dp/最长上升子序列)
题目链接:https://codeforces.com/contest/1427/problem/C 题意 \(r\) 行与 \(r\) 列相交形成了 \(r \times r\) 个点,初始时刻记者 ...
随机推荐
- python 计算日期间隔
from datetime import date a = date(2011,11,24) b = date(2011,11,17) print(a-b)
- 设计模式(二 & 三)工厂模式:3-抽象工厂模式
什么是抽象工厂? 抽象工厂模式,引入了“产品族”的概念. 何为产品族?还是以 设计模式(二)工厂模式:2-工厂方法模式 提到的 Operation 为例. 之前讨论的都是局限于 Operation 这 ...
- WTForms 表单动态验证
class UserDetails(Form): group_id = SelectField(u'Group', coerce=int) def edit_user(request, id): us ...
- 【bzoj3513】[MUTC2013]idiots FFT
题目描述 给定n个长度分别为a_i的木棒,问随机选择3个木棒能够拼成三角形的概率. 输入 第一行T(T<=100),表示数据组数. 接下来若干行描述T组数据,每组数据第一行是n,接下来一行有n个 ...
- poj1734Sightseeing trip
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6811 Accepted: 2602 ...
- jsp实现文件下载,out = pageContext.pushBody();out.close();不用写到jsp中
测试jsp: <%@ page contentType="text/html; charset=gbk" %> <% try{ com.enfo.intrust. ...
- python中的daemon守护进程实现方法
原文参考:http://blog.csdn.net/tao_627/article/details/49532021 守护进程是生存期长的一种进程.它们独立于控制终端并且周期性的执行某种任务或等待处理 ...
- C语言实现DES算法
原文转自 http://www.cnblogs.com/imapla/archive/2012/09/07/2674788.html 用C语言实现DES(数据加密算法)的一个例子,密文和密钥都是8个字 ...
- Yii使用find findAll查找出指定字段的实现方法
Yii使用find findAll查找出指定字段的实现方法,非常实用的技巧,需要的朋友可以参考下. 用过Yii的朋友都知道,采用如下方法: 查看代码 打印 1 modelName::model() ...
- LeetCode OJ--Binary Tree Preorder Traversal
http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 二叉树的先跟遍历,写的是递归版本,也可以使用stack来进行,替代了递归 ...