Codeforces 490F Treeland Tour(dp)

题目链接:Codeforces 490F Treeland Tour

类似于nlogn的递增上升子序列算法。

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;
const int maxn = 6005;
const int inf = 0x3f3f3f3f;

int N, R[maxn], D[maxn], ans = 0;
vector<int> g[maxn];

void init () {
	scanf("%d", &N);
	for (int i = 1; i <= N; i++)
		scanf("%d", &R[i]);
	for (int i = 0; i < N; i++)
		D[i] = inf;

	int a, b;
	for (int i = 1; i < N; i++) {
		scanf("%d%d", &a, &b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
}

void dfs(int u, int f) {
	int idx = lower_bound(D, D + N, R[u]) - D;
	ans = max(ans, idx + 1);

	int tmp = D[idx];
	D[idx] = R[u];
	for (int i = 0; i < g[u].size(); i++) {
		if (g[u][i] == f) continue;
		dfs(g[u][i], u);
	}
	D[idx] = tmp;
}

int main () {
	init();
	for (int i = 1; i <= N; i++)
		dfs(i, 0);
	printf("%d\n", ans);
	return 0;
}
时间: 2024-08-14 02:51:55

Codeforces 490F Treeland Tour(dp)的相关文章

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

题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N =

Codeforces 490F Treeland Tour 树上的最长上升子序列

题目链接:点击打开链接 题意: 给定n个点的树. 下面n个数表示点权. 下面n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条链然后求答案,但是如果直接树形dp(记录每个点u,u往下递增和u往下递减的长度)会使序列是来回的,即递增和递减都在同一条链上. 枚举每个点作为子序列的开头,然后维护一条链进行LIS的nlogn做法. import java.io.PrintWriter; import java.util.Array

Codeforces Round #279 (Div. 2) F. Treeland Tour(lis+dfs)

题目链接: huangjing 题意:告诉一个无向无环图,然后求在联通的路上的lis. 思路:枚举起点求lis 复杂度是n^2logn,貌似这复杂度对时间有点玄,估计是数据有点弱... 首先枚举起点,然后求lis,主要是用dfs求的,要用到回溯的思想,我觉得是只要更新了,就要保存那个操作,然后一旦这一次的搜索完成,那么就要立即回复g数组的值,因为有很多不同的路线,所以一条路走完后,就要回复以前的状态哦,免得影响另外一条路..我觉得尽管他们都说是暴力,但是我觉得这个题还是蛮好的... 题目: F.

CodeForces 18E Flag 2 dp

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<queue> #include<string> #include<stdlib.h> #include<a

Codeforces 41D Pawn 简单dp

题目链接:点击打开链接 给定n*m 的矩阵 常数k 下面一个n*m的矩阵,每个位置由 0-9的一个整数表示 问: 从最后一行开始向上走到第一行使得路径上的和 % (k+1) == 0 每个格子只能向或走一步 求:最大的路径和 最后一行的哪个位置作为起点 从下到上的路径 思路: 简单dp #include <cstdio> #include <algorithm> #include<iostream> #include<string.h> #include &

CodeForces 19B Checkout Assistant dp

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf 115292150460684697

HDU 1224 Free DIY Tour(DP)

Problem Description Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around the world. It's

Neko and Aki&#39;s Prank CodeForces - 1152D (括号序列,dp)

大意: 将所有长度为2*n的合法括号序列建成一颗trie树, 求trie树上选出一个最大不相交的边集, 输出边集大小. 最大边集数一定不超过奇数层结点数. 这个上界可以通过从底层贪心达到, 所以就转化为求奇数层结点数. 然后就dp求出前$i$为'('比')'多j个的方案数, 奇数层且合法的时候统计一下贡献即可. #include <iostream> #include <iostream> #include <algorithm> #include <cstdio

codeforces 148D 【概率dp】

题目链接: codeforces 148D Bag of mice 题意:一个包里面有w只白老鼠和b只黑老鼠,公主与龙依次从包中拿老鼠,每次取一只,当龙拿时还会从包中溜走一只,先拿到老鼠的获胜,当背包中没老鼠时且之前没人拿到白老鼠则龙获胜,问公主获胜的概率是多少. 题解: 设dp[i][j]为背包中有i只白老鼠j只黑老鼠时公主获胜的概率 则公主获胜的情况分成三种: 1.直接拿到白老鼠 p1=i/(i+j) 2.公主拿到黑老鼠,龙拿到黑老鼠,逃跑一只黑老鼠 p2=(j/(i+j)) ((j-1)/