线性dp
文章平均质量分 54
tomjobs
别慌,慌也没用
展开
-
LeetCode 115. 不同的子序列(DP)
思路:定义dp[i][j]dp[i][j]dp[i][j]代表字符串sss前iii个字符中,存在多少个字符串ttt前jjj个字符的子序列。class Solution {public: int numDistinct(string s, string t) { int n = s.size(),m = t.size(); vector<vector<long long>>dp(n + 1,vector(m + 1,0ll)); .原创 2021-03-17 15:01:38 · 149 阅读 · 0 评论 -
LeetCode 188. 买卖股票的最佳时机 IV(DP)
给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。示例 1:输入:k = 2, prices = [2,4,1]输出:2解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。示例 2:输入:k = 2, pr原创 2021-03-17 12:55:08 · 157 阅读 · 0 评论 -
LeetCode 123. 买卖股票的最佳时机 III(DP)
思路:定义dp[i][0/1/2/3]dp[i][0/1/2/3]dp[i][0/1/2/3]代表考虑前iii个股票,第二维0代表买了一次,1代表买了一次卖了一次,2代表买了一次卖了一次又买了一次,3代表买卖了两次。转移就是dp[i][0] = max(dp[i - 1][0],-prices[i]);dp[i][1] = max(dp[i - 1][1],dp[i - 1][0] + prices[i]);dp[i][2] = max(dp[i - 1][2],dp[i - 1][1] - p.原创 2021-03-17 11:53:57 · 100 阅读 · 0 评论 -
LeetCode 403. 青蛙过河(基础DP)
一只青蛙想要过河。 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有)。 青蛙可以跳上石头,但是不可以跳入水中。给定石子的位置列表(用单元格序号升序表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一个石子上)。 开始时, 青蛙默认已站在第一个石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格1跳至单元格2)。如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1、k 或 k + 1个单位。 另请注意,青蛙只能向前方(终点的方向原创 2021-03-16 21:38:12 · 213 阅读 · 0 评论 -
LeetCode 542. 01 矩阵(多源BFS,DP)
给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。两个相邻元素间的距离为 1 。示例 1:输入:[[0,0,0],[0,1,0],[0,0,0]]输出:[[0,0,0],[0,1,0],[0,0,0]]示例 2:输入:[[0,0,0],[0,1,0],[1,1,1]]输出:[[0,0,0],[0,1,0],[1,2,1]]提示:给定矩阵的元素个数不超过 10000。给定矩阵中至少有一个元素是 0。矩阵中的元素只在四个方向上相邻: 上、下、左、原创 2021-03-11 15:02:15 · 187 阅读 · 0 评论 -
Codeforces-1485 F. Copy or Prefix Sum(DP)
思路:可以想到,结果每个位置由两种可能的值,所以理想结果应该是2n−12^{n-1}2n−1。但是不一定,原因是∑1i−1ai=0∑^{i-1}_1a_i=0∑1i−1ai=0,导致两种取法得到的结果一样。所以我们得用动态规划的方法去重。直观的动态规划思路肯定是dp[i][j]dp[i][j]dp[i][j]代表前缀iii,前缀和为jjj时候能得到的hybridhybridhybrid数组数目。转移就是dp[i][j]+=dp[i−1][j−b[i]],dp[i][b[i]]+=dp[i][j].原创 2021-02-15 18:30:12 · 249 阅读 · 0 评论 -
LeetCode 72. 编辑距离(DP)
给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。你可以对一个单词进行如下三种操作:插入一个字符删除一个字符替换一个字符示例 1:输入:word1 = “horse”, word2 = “ros”输出:3解释:horse -> rorse (将 ‘h’ 替换为 ‘r’)rorse -> rose (删除 ‘r’)rose -> ros (删除 ‘e’)示例 2:输入:word1 = “intention”原创 2021-02-01 12:04:39 · 138 阅读 · 0 评论 -
LeetCode 45. 跳跃游戏 II(DP)
给定一个非负整数数组,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。你的目标是使用最少的跳跃次数到达数组的最后一个位置。示例:输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2。从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。说明:假设你总是可以到达数组的最后一个位置。思路:初始的想法就是定义dp[i]dp[i]dp[i]代表到第iii个位置的最小时间。这样的话转移就得使用线段树来维原创 2021-01-21 00:09:07 · 200 阅读 · 0 评论 -
Codeforces-1467 D. Sum of Paths(记忆化搜索)
There are ???? cells, numbered 1,2,…,???? from left to right. You have to place a robot at any cell initially. The robot must make exactly ???? moves.In one move, the robot must move one cell to the left or right, provided that it doesn’t move out of boun原创 2021-01-12 21:43:02 · 260 阅读 · 0 评论 -
LeetCode 32. 最长有效括号(DP)
思路:很裸的DP。定义dp[i]dp[i]dp[i]代表以第iii个字符结尾所能得到的最长连续合法括号串。那么只存在两种转移:第iii个字符是右括号,和第i−1i-1i−1个字符结尾的合法串拼起来(dp[i−1]dp[i-1]dp[i−1]),再拼左边一个左括号。dp[i] = max(dp[i],dp[i - 1] + 2 + dp[pre - 1]);第iii个字符串是右括号,第i−1i-1i−1个字符串是左括号,然后和第i−2i-2i−2个字符结尾的合法串拼起来。dp[i] = max(.原创 2021-01-11 15:20:09 · 173 阅读 · 0 评论 -
2020 CCPC Wannafly Winter Camp DAY6 K重排列(置换,DP)
思路:K一定是排列中所有环长度的公倍数,所以枚举K中小于等于nnn的因数,然后进行转移。定义dp[i]dp[i]dp[i]为选择了长度为iii排列的方案数,则有dp[i]=∑dp[i−j]∗C(i−1,j−1)∗fac[now−1],j∣kdp[i]=∑dp[i-j]*C(i-1,j-1)*fac[now-1],j|kdp[i]=∑dp[i−j]∗C(i−1,j−1)∗fac[now−1],j∣k。#include <cstdio>#include <cstring>#i.原创 2020-12-26 00:01:10 · 172 阅读 · 0 评论 -
Codeforces 1459 B. Move and Turn(有意思的数学题)
A robot is standing at the origin of the infinite two-dimensional plane. Each second the robot moves exactly 1 meter in one of the four cardinal directions: north, south, west, and east. For the first step the robot can choose any of the four directions, b原创 2020-12-20 19:02:10 · 837 阅读 · 1 评论 -
I 变大!(DP) 2020 CCPC Wannafly Winter Camp Day6 Div.1&2
思路:很有意思的一道思维DP。定义dp[i][k]dp[i][k]dp[i][k]代表对于[1,i][1,i][1,i]中的数,进行至多kkk次操作能得到的最大和。对于一个区间[l,r][l,r][l,r],可以知道,最优的操作就是把这个区间所有数变成其中的最大数,这需要(r−l+1)2\frac{(r-l+1)}{2}2(r−l+1)次操作。那么我们就按照这(r−l+1)2\frac{(r-l+1)}{2}2(r−l+1)个操作次数进行转移。从f[l−1][k]+(r−l+1)∗max(a[.原创 2020-12-18 10:49:03 · 133 阅读 · 0 评论 -
Codeforces1453 D. Checkpoints(DP,期望)
Gildong is developing a game consisting of ???? stages numbered from 1 to ????. The player starts the game from the 1-st stage and should beat the stages in increasing order of the stage number. The player wins the game after beating the ????-th stage.The原创 2020-12-08 17:37:02 · 293 阅读 · 0 评论 -
Codeforces1453 F. Even Harder(DP)
Gildong is now developing a puzzle game. The puzzle consists of ???? platforms numbered from 1 to ????. The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the 1-st platform to the原创 2020-12-08 16:53:13 · 224 阅读 · 0 评论 -
C - Pyramid Gym - 102835C(DP)
题意:n∗nn*nn∗n的棋盘,从(0,0)(0,0)(0,0)出发,只能向右或者向下。如果走到了x+y=nx+y=nx+y=n,那么就停止。每个点有一个方向值,初始均为向右,限定了这点是向右还是向下,每遍历一次方向值就取反。求第kkk个球会在哪个点停止。思路:T∗N∗NT*N*NT∗N∗N,复杂度1e9的算法。。。定义dp[i][j]dp[i][j]dp[i][j]为k−1k-1k−1次后这个点的方向值被改变多少次。转移过程类似于bfs,一层一层的转移。f[x+1][y]+=(f[x][原创 2020-11-28 00:41:20 · 456 阅读 · 0 评论 -
Codeforces 1447D. Catching Cheaters(LCS)
You are given two strings ???? and ???? representing essays of two students who are suspected cheaters. For any two strings ????, ???? we define their similarity score ????(????,????) as 4⋅????????????(????,????)−|????|−|????|, where ????????????(????,????原创 2020-11-21 18:19:45 · 207 阅读 · 0 评论 -
GYM G - GCD and LCM of 3 numbers Gym - 101864G(素数,DP)
题意:询问存在多少x≤y≤zx≤y≤zx≤y≤z,满足F(x,y,z)=x∗y∗z/Gcd(x,y,z)F(x,y,z)=x*y*z/Gcd(x,y,z)F(x,y,z)=x∗y∗z/Gcd(x,y,z)思路:问题可以转换成n=a∗b∗cn=a*b*cn=a∗b∗c,且a,b,ca,b,ca,b,c的gcdgcdgcd为1。这意味着nnn的每个质因子只能分配给2个数,或者分配给1个数。其中n=F(x,y,z)Gcd(x,y,z)2n=\frac{F(x,y,z)}{Gcd(x,y,z)^2}n=G原创 2020-11-19 14:44:06 · 201 阅读 · 0 评论 -
AtCoder Regular Contest 107 D - Number of Multisets(DP)
题意:nnn个2i,i≤02^i,i≤02i,i≤0数构成kkk有多少种方案。思路:定义f[n][k]f[n][k]f[n][k]为选nnn个2i,i≤02^i,i≤02i,i≤0构成kkk的方案数。如果n=kn=kn=k,那么只能全部选1;如果n<kn<kn<k,那怎么选都选不够。那么这一步如果选择1,就转移到了f[n−1][k−1]f[n-1][k-1]f[n−1][k−1];如果这一步选择其他的数,相当于将除了1以外的可选数乘以2,并且将kkk乘以2,所以转移到了f[n].原创 2020-11-14 21:31:51 · 185 阅读 · 0 评论 -
2020小米网络赛第二场 Subsequence Pair(DP)
题意:从sss串中选出一个子序列xxx,ttt选出一个子序列yyy。要求xxx字典序小于yyy,且∣x∣+∣y∣|x|+|y|∣x∣+∣y∣最大思路:xxx的字典序比yyy大,一定是满足xxx前面字符和yyy对应位置字符相同,然后出现xxx比yyy大的一个字符。或者xxx是yyy的一个前缀所以我们只需要算出前面相同的部分,然后看我们是要让后面yyy出现比xxx更大的字符,或者让xxx是yyy的一个前缀。#include<cstdio>#include<iostream>原创 2020-11-08 11:58:31 · 149 阅读 · 0 评论 -
G - Gates of uncertainty Gym - 101889G(DP)
题意:一个电路由与非门构成,构成二叉树。但是有的与非门只能输出1,有的只能输出0。求有多少种输入方案,使得你能检测出错误。思路:定义dp[i][0/1][0/1]dp[i][0/1][0/1]dp[i][0/1][0/1]代表第iii个门电路本来要输出0/1,结果输出0/1的方案数。那么结果就是dp[1][0][1]+dp[1][1][0]dp[1][0][1]+dp[1][1][0]dp[1][0][1]+dp[1][1][0]。#include <bits/stdc++.h>原创 2020-10-28 11:16:03 · 146 阅读 · 0 评论 -
E - Enigma Gym - 101889E(DP)
题意:一个数字,有些数位是?,要求你替换掉这些?,使得这个数字是N的倍数,没有前导0并且尽量小。思路:定义dp[i][j][k]dp[i][j][k]dp[i][j][k]代表选择了前i个数位,模N的余数为j,当前这一位选择了k是否可行。因为我们要让这个数字最小,所以要从后往前遍历,这样回溯的时候就是从前往后,我们只要每次使用当前最小数字就可以保证这个数字最小了。#include <cstdio>#include <cstring>#include<algorit原创 2020-10-28 11:07:22 · 157 阅读 · 0 评论 -
2020ICPC·小米 网络选拔赛第一场 Intelligent Warehouse(DP)
题意:n个数,要求取得最多的数,使得任意两个数其中一个为另一个倍数。思路:则定义dp[i]dp[i]dp[i]为第i个数为取出序列数中最大数时,最多取多少数。则dp[i]dp[i]dp[i]向i∗2,i∗3,i∗4...i*2,i*3,i*4...i∗2,i∗3,i∗4...转移,复杂度O(n∗sqrt(1e7))O(n*sqrt(1e7))O(n∗sqrt(1e7))。#include<cstdio>#include<cmath>#include<algorit原创 2020-10-25 23:57:01 · 831 阅读 · 5 评论 -
Codeforces 1421 E. Swedish Heroes(线性DP,规律)
While playing yet another strategy game, Mans has recruited ???? Swedish heroes, whose powers which can be represented as an array ????.Unfortunately, not all of those mighty heroes were created as capable as he wanted, so that he decided to do something原创 2020-10-24 11:36:01 · 376 阅读 · 0 评论 -
E - Minlexes CodeForces - 1422E(DP,字符串)
Some time ago Lesha found an entertaining string s consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.Lesha chooses an arbitrary (possibly zero) numb原创 2020-10-09 20:09:07 · 263 阅读 · 0 评论 -
Codeforces 1408 D. Searchlights(DP)
There are ???? robbers at coordinates (????1,????1), (????2,????2), …, (????????,????????) and ???? searchlight at coordinates (????1,????1), (????2,????2), …, (????????,????????).In one move you can move each robber to the right (increase ???????? of eac原创 2020-10-02 20:05:37 · 235 阅读 · 0 评论 -
Codeforces1426 F. Number of Subsequences(DP)
You are given a string ???? consisting of lowercase Latin letters “a”, “b” and “c” and question marks “?”.Let the number of question marks in the string ???? be ????. Let’s replace each question mark with one of the letters “a”, “b” and “c”. Here we can o原创 2020-09-28 21:51:30 · 1146 阅读 · 3 评论 -
杭电多校第10场 6880 Permutation Counting(DP)
Problem DescriptionFor a given permutation a1,a2,⋯,an of length n, we defined the neighbor sequence b of a, the length of which is n−1, as following:bi={01ai<ai+1ai>ai+1.For example, the neighbor sequence of permutation 1,2,3,6,4,5 is 0,0,0,1,0.原创 2020-09-26 15:13:58 · 1814 阅读 · 0 评论 -
Codeforces 383 D. Antimatter(DP)
Iahub accidentally discovered a secret lab. He found there n devices ordered in a line, numbered from 1 to n from left to right. Each device i (1 ≤ i ≤ n) can create either ai units of matter or ai units of antimatter.Iahub wants to choose some contiguous原创 2020-09-24 21:50:26 · 191 阅读 · 0 评论 -
Codeforces Global Round 10 D. Omkar and Bed Wars(DP)
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are ???? players arranged in a circle, so that for all ???? such that 2≤????≤????, player ????−1 is to the left of the player ????, and player ???? is to the right of player ?原创 2020-09-21 19:07:04 · 482 阅读 · 0 评论 -
牛客IOI周赛18-提高组 B.拆分(矩阵快速幂优化DP)
题目描述牛牛准备送给牛妹一条项链,但是项链太长了,于是他准备将项链拆掉。牛妹有一些不喜欢的数,所以拆分出来的长度中一定要有至少一个不是牛妹不喜欢的(不然就送不出去了)。注意,由于项链上的每一颗珠子都不同,所以1 5 1和1 1 5是两种不同的拆分方案,不拆也是一种方案。输入描述:第一行:两个数N和P。第二行:一个数K,表示牛妹有K个不喜欢的数。第三行:K个牛妹不喜欢的数T。输出描述:一个整数表示拆分方案对P取模后的值,不保证P是质数。示例1输入复制5 1000722 3输出原创 2020-09-18 17:57:44 · 157 阅读 · 0 评论 -
Gym 101612 Problem A. Auxiliary Project(DP)
题意:用n个木棒凑成数字,和最大是多少思路:直接dp写。#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#include<iostream> typedef long long ll;using namespace std;const int maxn = 1e6 + 7; int dp[maxn];int a[20];void .原创 2020-09-10 23:40:59 · 231 阅读 · 0 评论 -
Codeforces1409 F. Subsequences of Length Two(DP)
You are given two strings ???? and ???? consisting of lowercase Latin letters. The length of ???? is 2 (i.e. this string consists only of two characters).In one move, you can choose any character of ???? and replace it with any lowercase Latin letter. Mor原创 2020-09-05 01:09:03 · 337 阅读 · 0 评论 -
Codeforces1327 E. Count The Blocks(DP,容斥)
You wrote down all integers from 0 to 10????−1, padding them with leading zeroes so their lengths are exactly ????. For example, if ????=3 then you wrote out 000, 001, …, 998, 999.A block in an integer ???? is a consecutive segment of equal digits that ca原创 2020-09-03 21:35:25 · 211 阅读 · 0 评论 -
Eeny Meeny Moo POJ - 2244(约瑟夫环递推)
Surely you have made the experience that when too many people use the Internet simultaneously, the net becomes very, very slow.To put an end to this problem, the University of Ulm has developed a contingency scheme for times of peak load to cut off net ac原创 2020-09-01 15:08:39 · 215 阅读 · 0 评论 -
Codeforces 1398D. Colored Rectangles(DP)
You are given three multisets of pairs of colored sticks:???? pairs of red sticks, the first pair has length ????1, the second pair has length ????2, …, the ????-th pair has length ????????;???? pairs of green sticks, the first pair has length ????1, the原创 2020-08-15 11:01:13 · 3295 阅读 · 3 评论 -
Codeforces1393 D. Rarity and New Dress(最大正方形变形,DP)
Carousel Boutique is busy again! Rarity has decided to visit the pony ball and she surely needs a new dress, because going out in the same dress several times is a sign of bad manners. First of all, she needs a dress pattern, which she is going to cut out原创 2020-08-08 10:20:55 · 372 阅读 · 0 评论 -
G - Painting Square CodeForces - 300D(DP,优化)
Vasily the bear has got a large square white table of n rows and n columns. The table has got a black border around this table.The example of the initial table at n = 5.Vasily the bear wants to paint his square table in exactly k moves. Each move is sequ原创 2020-08-07 19:12:32 · 270 阅读 · 1 评论 -
2020杭电多校第四场 Equal Sentences(DP)
Problem DescriptionSometimes, changing the order of the words in a sentence doesn’t influence understanding. For example, if we change “what time is it”, into “what time it is”; or change “orz zhang three ak world final”, into “zhang orz three world ak fi原创 2020-08-06 00:01:19 · 359 阅读 · 0 评论 -
2020杭电多校第五场 Set2(DP,组合数学)
Problem DescriptionYou are given a set S={1…n}. You have to do the following operations until there are no more than k elements left in the S:Firstly, delete the smallest element of S, and then randomly delete another k elements one by one from the eleme原创 2020-08-05 19:20:27 · 329 阅读 · 0 评论