Problem - 9D - Codeforces

https://codeforces.com/problemset/problem/9/D

一开始居然还想直接找公式的,想了想还是放弃了。原来这种结构是要动态规划。

状态是知道怎么设了,tnh表示节点数为n个,树高为h的BST的个数

为什么要这么设状态呢?是考虑到题目关心BST的高度,而且BST具有递归性。

但是这个关键就是要加上n个节点的标记,因为不同节点数量明显可以构造出的高为h的树不同,而且也会影响BST另一侧的构造。

所以说设对状态就做对了一半。

思路就是按高为h的BST是怎么由高为h-1的BST加上根节点转移过来的,重点是向上转移而不是向下转移。

首先你要分类讨论,这个根节点的数字是m,那么左边会有1~m-1共m-1个节点,右边则有n-m个节点,然后分

1.左子树的高是h-1,右子树的高是0~h-1(假如可以放得下的话)。

2.右子树的高是h-1,左子树的高是0~h-2(注意是h-2,否则和上面重复)

左右子树的构造是独立的,所以相乘。两种情况的分类的,所以相加。

最后遍历一遍m得到所有tnh的和。

原文地址:https://www.cnblogs.com/Yinku/p/10287466.html

时间: 2024-11-10 11:20:16

Problem - 9D - Codeforces的相关文章

Problem E CodeForces 237C

Description You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors. Consider positive integers a, a + 1, ..., b (a ≤ b).

Problem F CodeForces 16E

Description n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability 

Problem B Codeforces 295B 最短路(floyd)

Description Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists

Problem - D - Codeforces Fix a Tree

Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作数. 如果有环和线,环被打开的同时,接入到线上.那就是线和环的总数-1. 如果只有环的话,把所有的环打开,互相接入,共需n次操作. #include <cstdio> #include <algorithm> using namespace std; const int maxn =

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力

Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is a

Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

Digital collectible card games have become very popular recently. So Vova decided to try one of these. Vova has n cards in his collection. Each of these cards is characterised by its power pi, magic number ci and level li. Vova wants to build a deck

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebo

Problem A CodeForces 560A

Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of mone