
动态规划
不可知论大祭司
Know how to solve every problem that has been solved.
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
【LuoguP1133 教主的花园】动态规划
link主要是写起来比较麻烦,思路很简答。可以先把第一个的状态定下来,然后向后更新,统计答案。第一个元素(j, k)所确定的以外的状态设为负无穷。注意,由于n为偶数, 奇偶性相同的土地同时位于波峰(波谷)#include <cstdio>#include <iostream>#include <algorithm>#include <cctype>#include <cmath>#include <vector>#i.原创 2021-05-14 21:50:52 · 190 阅读 · 0 评论 -
【P1439】最长公共子序列
link一个很巧妙的映射关系,由于LCS的下标是递增的。于是转化成了求LIScode#include <cstdio>#include <iostream>#include <algorithm>#include <algorithm>#include <vector>#include <queue>#include <cctype>#include <cmath>#include <.原创 2021-04-03 10:10:36 · 79 阅读 · 0 评论 -
【AcWing482】 合唱队形 LIS问题
code很经典的二分求法,复习一下#include <cstdio>#include <iostream>#include <algorithm>#include <algorithm>#include <vector>#include <queue>#include <cctype>#include <cmath>#include <cstring>#define forn(i原创 2021-04-03 09:47:53 · 103 阅读 · 0 评论 -
【AcWing291】棋盘类压状dp
linkcode#include <cstdio>#include <iostream>#include <algorithm>#include <algorithm>#include <vector>#include <queue>#include <cctype>#include <cmath>#include <cstring>#define forn(i, n) for.原创 2021-04-02 21:22:06 · 110 阅读 · 0 评论 -
【LuoguP1776】 宝物筛选 多重背包 二进制优化
link时间复杂度变为O(wlogk) k为背包的总数code#include <cstdio>#include <iostream>#include <algorithm>#include <cctype>#include <cmath>#include <unordered_map>using namespace std;using ll = long long;const int N = 3e5 + 10;原创 2021-03-20 16:13:48 · 83 阅读 · 0 评论 -
leetcode213 打家劫舍 II
要打劫元素构成了一个环,也就是说在满足不取相邻的数的前提下,第一个和最后一个不能同时取到。于是我们分别去掉第一个和最后一个数,进行两次动态规划,结果最大的就是答案。class Solution { public int rob(int[] nums) { if(nums.length == 1) return nums[0]; return Math.max(func(Arrays.copyOfRange(nums, 1, nums.length)),原创 2021-03-05 18:15:38 · 91 阅读 · 0 评论 -
【CF1415C】Bouncing Ball
链接传送门code先dp, 再枚举。时间复杂度O(n)#include <iostream>#include <queue>#include <vector>#include <set>#include <cstdio>#include <algorithm>#include <cstring>#include <cctype>using namespace std;inline i原创 2021-01-20 18:57:35 · 162 阅读 · 0 评论 -
【CF1472G】Moving to the Capital
链接传送门code#include <iostream>#include <queue>#include <vector>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int N = 2e5 + 10;int n, m;int dis[N], vis[N], dp[N];vector<v原创 2021-01-19 19:34:36 · 203 阅读 · 1 评论 -
CF1472C Long Jumps
动态规划,倒着扫一遍即可code#include <cstdio>#include <cctype>#include <cstring>#include <algorithm>const int N = 2e5 + 10;int a[N], dp[N];inline int read(){ int x = 0, op = 1; char ch = getchar(); while (!isdigit(ch)){if (ch =原创 2021-01-15 13:07:09 · 205 阅读 · 0 评论 -
【CF161D】Distance in Tree (dfs + 动态规划浅析)
Distance in Tree题目描述题目链接分析代码图解题目描述A tree is a connected graph that doesn’t contain any cycles.The distance between two vertices of a tree is the length (in edges) of the shortest path between these vertices.You are given a tree with n vertices and a po原创 2020-12-10 21:15:07 · 405 阅读 · 0 评论 -
【CF212E】IT Restaurants (01背包问题)
IT Restaurant题目概述题目链接分析代码题目概述Сity N. has a huge problem with roads, food and IT-infrastructure. In total the city has n junctions, some pairs of them are connected by bidirectional roads. The road network consists of n - 1 roads, you can get from any jun原创 2020-12-10 14:39:22 · 426 阅读 · 0 评论 -
【CF431C】k-tree 完全背包浅析
k - tree题目概述题目链接浅析代码题目概述Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired and came up with the tree of his own which he called a k-tree.A k-tree is an infinite rooted tree where:each vertex has exactly原创 2020-12-09 19:36:39 · 395 阅读 · 0 评论 -
【LuoguP1180】石子合并(环形dp)
石子合并题目描述题目描述在一个圆形操场的四周摆放 N 堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分。试设计出一个算法,计算出将 N 堆石子合并成 1堆的最小得分和最大得分。题目链接传送门思路如果是普通的区间动态规划,我们可以直接从2到n枚举每一个区间的长度,确定区间的起始点和终点,然后再枚举分点,通过前缀和计算每次费用增量就可以。代码如下 for (int len = 2; len <= n; ++le原创 2020-12-05 15:53:44 · 152 阅读 · 0 评论 -
【CF1420C1】Pokémon Army
@[TOC](这里写目录标题题目链接分析代码题目链接传送门分析这里使用动态规划的做法,dp[i][0]表示第i个之前(包括i),选中的宝可梦有偶数个,dp[i][1]表示有奇数个。有如下状态转移方程。dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - a[i]); dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + a[i]);代码#include <iostream>#inclu原创 2020-12-08 08:48:32 · 119 阅读 · 0 评论