
DP
chr1st0pher
Dancer on the keyboard
展开
-
Leetcode 1691. 堆叠长方体的最大高度(拓扑排序 + DP)
DescriptionSolution一个立方体可以任意翻转, 我们可以当成多个不同的立方体看待。问题转化为:有最多N*6个立方体, 最高能垒多高考虑DP[i], 第i个立方体放在最上面时的最大高度, 发现当前的状态是由所有可以垒在其下面的立方体转移过来的,所以我们考虑一边拓扑排序一边DPCodeclass Solution {public: #define N 202 struct cube{ int a,b,c; }; vector<原创 2020-12-24 23:44:34 · 320 阅读 · 0 评论 -
2020 Jiangsu CPC H. Happy Morse Code(DP)
DescriptionExampleinput34 2a 01b 1001104 4a 01b 10c 01d 011001104 2a 1b 100110outputhappymorsecodepuppymousecat 3nononoSolution很明显的dp[i][j]:dp[i][j]:dp[i][j]: 前i位以第j个字母结尾的方案数这题的答案需要mod128,所以当结果为1时,不一定表示方案唯一。可以再开一个dp数组,记录某个dp状态是否存在原创 2020-12-05 13:59:08 · 390 阅读 · 0 评论 -
Codeforces 1457C. Bouncing Ball(DP)
DescriptionSolution先用dp预处理出以i为起点,以k为步长的路上会踩到几个砖块,以及有多少个格子然后枚举起点,更新答案Code#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5 + 7;int dp[maxn];int num[maxn];int a[maxn];void init(int n) { for(int i = 1原创 2020-11-30 21:43:43 · 208 阅读 · 0 评论 -
2020 CCPC 长春 A. Krypton(混合背包)
DescriptionExamplesinput100output1084input198output2108Solution混合背包Code#include <bits/stdc++.h>using namespace std;const int mac=1e5+10;typedef long long ll;const int inf=1e9+10;int v[50],w[50],mk[50];int dp[mac];int main(int argc原创 2020-11-13 15:50:36 · 287 阅读 · 0 评论 -
Codeforces 1433F. Zero Remainder Sum (线性DP)
DescriptionExamplesinput3 4 31 2 3 45 2 2 27 1 1 4output24input5 5 41 2 4 2 13 5 1 2 41 5 7 1 23 8 7 1 28 4 7 1 6output56Solutiondp[i][d][k] : 在第 iii行选择ddd个数(d+d<=md+d<=md+d<=m)的和 % mod==k\%~mod == k% mod==k 的最大值dp2[i原创 2020-10-21 21:36:28 · 285 阅读 · 0 评论 -
POJ 1159 Palindrome (区间DP)
DescriptionA palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the str原创 2020-09-04 21:21:38 · 124 阅读 · 0 评论 -
POJ 2955 Brackets (区间DP)
题目描述用以下方式定义合法的括号字符串1.空串是合法的2. 如果S是合法的, 那么(S)和[S]也都是合法的3. 如果A和B是合法的, 那么AB是一个合法的字符串.举个栗子, 下列字符串都是合法的括号字符串:(), [], (()), ([]), ()[], ()[()]下面这些不是:(, [, ), )(, ([)], ([(]给出一个由字符’(’, ‘)’, ‘[’, 和’]‘构成的字符串. 你的任务是找出一个最长的合法字符串的长度,使这个的字符串是给出的字符串的子序列。对于字符串a1原创 2020-09-04 20:15:59 · 190 阅读 · 0 评论 -
Gym 102091L. Largest Allowed Area (DP)
DescriptionSolution至多有1个1的情况下,求最大全0子正方形在裸的最大0子矩阵的DP状态下再加一位0/1,表示无1/有一个1的情况下的最优解用二维前缀和判断,暴力(乱搞)转移即可Hint2e7的读入,生而为人,别太过分Codeconst int maxn = 1e3 + 7;int read() { int num = 0, F = 1; char c = ' '; while (c < '0' || c > '9') F = (c == '原创 2020-08-12 19:38:56 · 260 阅读 · 0 评论 -
Gym 102091C. Evolution Game (DP)
DescriptionSolution(感觉像DAG上求最长路按照h排序后DPCodeint h[maxn];struct node{ int h,id;}a[maxn];bool cmp(node x,node y) { return x.h < y.h;}int dp[maxn];int main() { int n,w;scanf("%d%d",&n,&w); for(int i = 1;i <= n;++i) {scanf("%d",&am原创 2020-08-12 19:32:25 · 239 阅读 · 0 评论 -
Codeforces 577B. Modulo Sum (数学 + DP)
DescriptionYou are given a sequence of numbers a 1, a 2, …, a n, and a number m.Check if it is possible to choose a non-empty subsequence a i j such that the sum of numbers in this subsequence is divisible by m.InputThe first line contains two numbers,原创 2020-08-08 21:44:54 · 542 阅读 · 1 评论 -
Codeforces 2B. The least round way (数学 + DP)
DescriptionThere is a square matrix n × n, consisting of non-negative integer numbers.You should find such a way on it that starts in the upper left cell of the matrix;each following cell is to the right or down from the current cell;the way ends in th原创 2020-08-07 20:36:44 · 689 阅读 · 0 评论 -
Codeforces 432D. Prefixes and Suffixes (DP + next数组)
DescriptionYou have a string s = s 1 s 2…s |s|, where |s| is the length of string s, and s i its i-th character.Let’s introduce several definitions:A substring s[i…j] (1 ≤ i ≤ j ≤ |s|) of string s is string s i s i + 1…s j.The prefix of string s of len原创 2020-07-24 21:59:01 · 263 阅读 · 0 评论 -
Codeforces 479E. Riding in a Lift(DP + 差分优化)
int n,a,b,k;int d[2][maxn];inline int MAX(int a,int b){if(a > b) return a;return b;}inline int MIN(int a,int b){if(a < b) return a;return b;}inline void init(int op){for(re int i = 1;i <= n;++i) d[op][i] = 0;}void modify(int L,int R,int k,int原创 2020-07-23 23:12:38 · 792 阅读 · 0 评论 -
Codeforces 459E. Pashmak and Graph (DP)
DescriptionPashmak’s homework is a problem about graphs. Although he always tries to do his homework completely, he can’t solve this problem. As you know, he’s really weak at graph theory; so try to help him in solving the problem.You are given a weighte原创 2020-07-16 17:35:15 · 198 阅读 · 0 评论 -
Codeforces 159D. Palindrome pairs (Manacher +DP)
DescriptionYou are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string.In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤原创 2020-07-15 23:41:33 · 429 阅读 · 0 评论 -
Codeforces 453B. Little Pony and Harmony Chest (状压DP)
DescriptionPrincess Twilight went to Celestia and Luna’s old castle to research the chest from the Elements of Harmony.A sequence of positive integers bib_ibi is harmony if and only if for every two elements of the sequence their greatest common diviso原创 2020-07-15 10:58:41 · 469 阅读 · 0 评论 -
Codeforces 219D. Choosing Capital for Treeland (换根DP)
DescriptionThe country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don’t take the direction of the roads into consideration, we can get from an原创 2020-07-13 21:47:26 · 201 阅读 · 0 评论 -
LightOJ 1151 Snakes and Ladders (期望DP + 高斯消元)
Description‘Snakes and Ladders’ or ‘Shap-Ludu’ is a game commonly played in Bangladesh. The game is so common that it would be tough to find a person who hasn’t played it. But those who haven’t played it (unlucky of course!) the rules are as follows.1.Th原创 2020-07-12 10:49:31 · 270 阅读 · 0 评论 -
Codeforces 510D. Fox And Jumping (DP + 裴蜀定理 + 离散化)
DescriptionFox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0.There are also n cards, each card has 2 attributes: length l i原创 2020-07-10 19:10:30 · 199 阅读 · 0 评论 -
CodeForces 401D. Roman and Numbers (状压DP)
DescriptionRoman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn’t think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers ar原创 2020-07-08 21:56:00 · 197 阅读 · 0 评论 -
CodeForces 486D. Valid Sets (树形DP)
DescriptionAs you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value a i associated with it.We call a set S of tree nodes valid if follow原创 2020-07-08 21:40:55 · 173 阅读 · 0 评论 -
HDU 3236 Gift Hunting (01背包)
DescriptionAfter winning two coupons for the largest shopping mart in your city, you can’t wait inviting your girlfriend for gift hunting. Having inspected hundreds of kinds of souvenirs, toys and cosmetics, you finally narrowed down the candidate list to原创 2020-07-06 10:55:25 · 153 阅读 · 0 评论 -
HDU 4719 Oh My Holy FFF (线段树+DP)
DescriptionN soldiers from the famous “* FFF *army” is standing in a line, from left to right.o o o o o o o o o o o o o o o o o o/F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F\ /F/ \ / \ / \ / \ / \原创 2020-07-04 19:35:45 · 174 阅读 · 0 评论 -
Gym - 102448 K Kongey Donk (DP)
DescriptionThe monkey Kongey Donk loves to eat bananas, but he is always really tired. Kongey lives in a region that has n banana trees lined in a row, and he just woke up over a platform from which he can jump to the top of any of the banana trees in the原创 2020-07-03 19:24:50 · 301 阅读 · 0 评论 -
Codeforces 113D Museum (期望DP + 高斯消元)
题目链接题目大意:有NNN个房间,其中两个房间 AAA,BBB 里各有一个人。有MMM对房间通过双向边连通。每一个时刻,每个人有一定概率pip_ipi留在 iii 号房间,或者有1−pi1-p_i1−pi的概率离开,等概率前往相邻房间。两人一旦处于同一房间则立即停下来。求对于所有的房间 iii,两人在 iii 号房间相遇的概率是多少PS:1.在过道内相遇不算2.所有房间保证连通解法:考虑DP,设f[i][j]f[i][j]f[i][j] 为一个人在房间 iii ,同时另一个人在 房间 jj原创 2020-06-22 22:49:06 · 207 阅读 · 0 评论 -
BZOJ2748 [HAOI2012]音量调节 (基础DP)
Description一个吉他手准备参加一场演出。他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都要改变一次音量。在演出开始之前,他已经做好了一个列表,里面写着在每首歌开始之前他想要改变的音量是多少。每一次改变音量,他可以选择调高也可以调低。音量用一个整数描述。输入文件中给定整数beginLevelbeginLevelbeginLevel,代表吉他刚开始的音量,以及整数maxLevelmaxLevelmaxLevel,代表吉他的最大音量。音量不能小于 000 也不能大于maxLevelma原创 2020-06-21 00:24:45 · 278 阅读 · 0 评论 -
BZOJ1087 [SCOI2005] 互不侵犯King (状压DP)
Description 在N×NN×NN×N的棋盘里面放KKK个国王,使他们互不攻击,共有多少种摆放方案。国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子。Input 只有一行,包含两个数NNN,KKK (1<=N<=9,0<=K<=N∗N)( 1 <=N <=9, 0 <= K <= N * N)(1<=N<=9,0<=K<=N∗N)Output 方案数。 Sample In原创 2020-06-11 13:44:05 · 190 阅读 · 0 评论 -
BZOJ2707 [SDOI2012]走迷宫 【期望DP+高斯消元+tarjan缩点】
DescriptionMorenan被困在了一个迷宫里。迷宫可以视为NNN个点MMM条边的有向图,其中Morenan处于起点SSS,迷宫的终点设为TTT。可惜的是,Morenan非常的脑小,他只会从一个点出发随机沿着一条从该点出发的有向边,到达另一个点。这样,Morenan走的步数可能很长,也可能是无限,更可能到不了终点。若到不了终点,则步数视为无穷大。但你必须想方设法求出Morenan所走步数的期望值。Input第1行4个整数,N,M,S,TN,M,S,TN,M,S,T第[2, M+1]行每行两个原创 2020-06-11 00:23:18 · 912 阅读 · 0 评论 -
BZOJ1040 骑士(基环树DP)
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英。他们劫富济贫,惩恶扬善,受到社会各界的赞扬。最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争。战火绵延五百里,在和平环境中安逸了数百年的Z国又怎能抵挡的住Y国的军队。于是人们把所有的希望都寄托在了骑士团的身上,就像期待有一个真龙天子的降生,带领正义打败邪恶。骑士团是肯定具有打败邪恶势力的能力的,但是骑士们互相之间往往有一些矛盾。每个骑士都有且仅有一个自己最厌恶的骑士(当然不是他自己),他是绝对不原创 2020-06-10 21:53:48 · 391 阅读 · 0 评论 -
Codeforces 1353E. K-periodic Garland (DP)
题目链接题目大意: 给定一个01串,每次操作可以把某一位上的0变成1 or 1 变成0 ,求最少的操作数使得相邻两个1之间的距离都为k解法: DP设 dp[i]dp[i]dp[i]: 使得 111 ~ iii 符合条件且 s[i]==1s[i] == 1s[i]==1 的最少操作数分类讨论:① 当 s[i]==1s[i] == 1s[i]==1 为 111 ~ iii 中第一个的 ‘1’ 时 : dp[i]=sum[i−1]+(a[i]==0)dp[i] = sum[i-1] + (a[i] =原创 2020-06-10 01:54:07 · 231 阅读 · 0 评论 -
Codeforces 1359 D.Yet Another Yet Another Task (最大子段和 DP)
题意:给一个可能含有负数元素的数组,求连续一段子区间内区间和减去区间最大值的最大值解法:因为数组元素最大仅为30,故可以枚举区间最大元素,再在此基础上求一遍最大子段和(注意这里取长度为1的区间等效于取长度为0的区间),遇到比当前最大元素更大的数就断掉,最后再减去最大元素。PSPSPS: 每次枚举区间最大元素 kkk 时, 可能某一时刻的最大和子段的最大值是 xxx 而不是 kkk,却最后减去了kkk,故得到的答案比实际答案小,但当你枚举到 xxx 时,得到的答案将会是准确的。故最终还是能得到准确答案原创 2020-06-09 23:46:08 · 208 阅读 · 0 评论 -
ICPC NEAU Programming Contest 2020 G. 选根 (换根DP)
解法: 换根dpCode:const int MX = 2e5 + 7;int v[MX];ll sum[MX],dp[MX],ans[MX];int dep[MX];vector<int>e[MX];void init(int n){ for(int i = 1;i <= n;++i){ sum[i] = dp[i] = dep[i] = 0; e[i].clear(); }}void pre_dfs(int u,int fa){ dep[u] = .原创 2020-06-09 16:01:45 · 175 阅读 · 0 评论