![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
动态规划
文章平均质量分 72
追风者_
要我自我介绍,挺秃然的
展开
-
ACwing 311. 月之谜 数位dp
题意:问你[l,r]内有多少数,是可以被其数位和整除的。思路:思路:直观想法,用dp[pos][sum1][sum2]dp[pos][sum1][sum2]dp[pos][sum1][sum2],表示枚举到第pos位,组成sum1的数,累加和为sum2时的方案数。由于sum1很大,所以要考虑每步取模。想到,若给定一个m,问你有多少数是被m整除,这个就是一个裸的数位dp板子,用一个dp[pos][sum]dp[pos][sum]dp[pos][sum]就可以表示枚举到第pos位,逐步取模结果为sum原创 2021-08-10 00:03:49 · 271 阅读 · 0 评论 -
2021牛客多校5 D Double Strings dp 数学
思路:先预处理出dp[i][j]dp[i][j]dp[i][j]表示a的前i位和b的前j位能组成的相同子序列个数。然后对于每个a[i]<b[j]的位置,前面有dp[i−1][j−1]dp[i-1][j-1]dp[i−1][j−1]的方案数,乘上后面 ∑k=1min(len1−i,len2−j)Clen1kClen2k\sum_{k=1}^{min(len1-i,len2-j)}C_{len1}^{k}C_{len2}^{k}∑k=1min(len1−i,len2−j)Clen1kClen2k原创 2021-07-31 18:47:06 · 156 阅读 · 0 评论 -
A. Acing the contest 状压dp 背包
A common programming contest format includes teams with three contestants working to solve the maximum amount of programming challenges in the less amount of time using a single computer. Some contests have been created using a similar idea. In this contes原创 2021-02-01 12:25:04 · 272 阅读 · 0 评论 -
【AtCoder】E - Magical Ornament 图论 状压dp
题意:告诉你有哪些两两可以相邻,问你能不能排列出一个包含一串数的序列,且序列长度最小。思路:不知道是不是想复杂了。写完感觉不是这个位置的题该有的难度(应该是我太菜了)。1.首先,建图,相邻的对就连边,把题意转化成在该图上跑出一个最短路径包含序列中的所有点(点可重复)。2.因为走的点的先后顺序未知,所以我们只能通过bfs得到单次从s开始到各点的最短路径,下一步要考虑顺序问题。3.用dp[i][j]dp[i][j]dp[i][j]表示,走完i所表示的集合中的点,且最后一步是走到第j个点时的最优解。其中i原创 2021-01-30 23:44:02 · 399 阅读 · 3 评论 -
【HDU 1257】最少拦截系统 DP or 贪心 详解
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.Input输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于原创 2020-08-19 14:07:24 · 288 阅读 · 0 评论 -
【HDU 2159】 FATE 完全背包
最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务。久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最后一级。现在的问题是,xhd升掉最后一级还需n的经验值,xhd还留有m的忍耐度,每杀一个怪xhd会得到相应的经验,并减掉相应的忍耐度。当忍耐度降到0或者0以下时,xhd就不会玩这游戏。xhd还说了他最多只杀s只怪。请问他能升掉这最后一级吗?Input输入数据有多组,对于每组数据第一行输入n,m,k,s(0 < n,m,k,s < 100)四个正原创 2020-08-18 01:05:15 · 204 阅读 · 0 评论 -
【SCAU 10新生赛】9515 终极BOSS 01背包 dp
DescriptionDragon123是某游戏的高级玩家,经常轻松秒杀游戏的终极boss。游戏公司得知这个消息后十分不服气,决定对游戏里面的终极boss进行一下改造,使dragon123在游戏中有可能永远不能够消灭它。一开始,终极boss有n滴血。Dragon123有opt种武器,每种武器都能够对该终极boss造成Ki滴血的伤害。但是,游戏公司偷偷作了一个小小的修改:他们设定了f个数值:R1, R2…Ri。每当终极boss的血恰好降至这些值的时候,boss的血会自动增加一个固定值M。当boss的原创 2020-08-03 22:29:48 · 200 阅读 · 0 评论 -
【SCAU新生赛 9513】 防空洞 背包问题 DP
Description有一天,dragon123偷偷地拿锄头在学校里挖开了一个尘封已久的防空洞。他在这个防空洞里面找到许多贵重的东西:一些石头和一些液体。dragon123知道,只要他把这些石头和液体拿出去卖,那么就一定可以赚大钱。但是,他只有一个载重量为W的瓶子来装这些东西。防空洞里面有很多块石头,每块石头的重量为Wi,价值为Mi,但是石头不能够砸烂,否则就不值钱了。此外,洞里面很多种贵重的液体。对于某种液体,洞内存储了Wi重量,且这Wi重量液体的总价值为Mi。液体是可以部分放进瓶子里面的。也原创 2020-08-02 20:56:03 · 269 阅读 · 0 评论 -
【AcWing 281】 硬币 多重背包问题
给定N种硬币,其中第 i 种硬币的面值为Ai,共有Ci个。从中选出若干个硬币,把面值相加,若结果为S,则称“面值S能被拼成”。求1~M之间能被拼成的面值有多少个。输入格式输入包含多组测试用例。每组测试用例第一行包含两个整数N和M。第二行包含2N个整数,分别表示A1,A2,…,AN和C1,C2,…,CN。当输入用例N=0,M=0时,表示输入终止,且该用例无需处理。输出格式每组用例输出一个结果,每个结果占一行。数据范围1≤N≤100,1≤M≤105,1≤Ai≤105,1≤Ci≤100原创 2020-07-31 17:42:45 · 256 阅读 · 0 评论 -
【LeetCode 871】最低加油次数 动态规划DP 详解
汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 station[i][0] 英里处,并且有 station[i][1] 升汽油。假设汽车油箱的容量是无限的,其中最初有 startFuel 升燃料。它每行驶 1 英里就会用掉 1 升汽油。当汽车到达加油站时,它可能停下来加油,将所有汽油从加油站转移到汽车中。为了到达目的地,汽车所必要的最低加油次数是多少?如果无法到达目的地,则返回 -1 。注意:如原创 2020-07-30 19:48:18 · 799 阅读 · 0 评论 -
【HDU 1171】Big Event in HDU 多重背包巧妙优化 DP
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002.The splitting is absolutely a big event in HDU! At the sam原创 2020-07-30 14:37:34 · 147 阅读 · 0 评论 -
【HDU 1114】 Piggy-Bank 背包问题 DP
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes原创 2020-07-30 13:01:24 · 139 阅读 · 0 评论 -
【HDU 1059】 Dividing 多重背包问题 DP
Problem DescriptionMarsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just s原创 2020-07-28 14:49:03 · 149 阅读 · 0 评论 -
【HDU 1003】 Max Sum 最大子串和 DP
Problem DescriptionGiven a sequence a[1],a[2],a[3]…a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.InputThe first line of the input contains an integer原创 2020-07-28 12:49:21 · 137 阅读 · 0 评论 -
【暑训排位 #D】Gym - 100952H 动态规划DP
A sequence of positive and non-zero integers called palindromic if it can be read the same forward and backward, for example:15 2 6 4 6 2 1520 3 1 1 3 20We have a special kind of palindromic sequences, let’s call it a special palindrome.A palindromic s原创 2020-07-10 18:55:34 · 189 阅读 · 0 评论 -
【暑训排位 #8 A】动态规划 LIS
In the army, a platoon is composed by n soldiers. During the morning inspection, the soldiers are aligned in a straight line in front of the captain. The captain is not satisfied with the way his soldiers are aligned; it is true that the soldiers are align原创 2020-07-07 18:12:44 · 190 阅读 · 1 评论 -
【暑训排位 #4 F】 数位dp
Create a code to determine the amount of integers, lying in the set [ X; Y] and being a sum of exactly K different integer degrees of B.Example. Let X=15, Y=20, K=2, B=2. By this example 3 numbers are the sum of exactly two integer degrees of number 2:17原创 2020-07-03 21:33:39 · 138 阅读 · 0 评论 -
【暑训排位#3 F】URAL - 1501 记忆化搜索
The owner of a casino for New Russians has a very refined sense of beauty. For example, after a game there remain two piles with the same number of cards on the table, and the owner likes the cards to be arranged into two piles according to the color: one原创 2020-06-27 19:24:06 · 109 阅读 · 0 评论 -
【CodeForces 577B】 Modulo Sum 鸽巢(抽屉)原理 DP
You are given a sequence of numbers a1, a2, …, an, and a number m.Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m.InputThe first line contains two numbers, n and m (1 ≤ n ≤原创 2020-06-21 23:52:22 · 351 阅读 · 0 评论 -
【HDU 2670】 Girl Love Value 动态规划dp
Problem DescriptionLove in college is a happy thing but always have so many pity boys or girls can not find it.Now a chance is coming for lots of single boys. The Most beautiful and lovely and intelligent girl in HDU,named Kiki want to choose K single bo原创 2020-06-21 18:06:13 · 166 阅读 · 0 评论 -
【SCAU 14校赛】17996 Daily Cool Run 动态规划DP or 记忆化搜索
DescriptionDaily Cool Run is a popular game, and Xdp enjoys playing the game recently.While playing the game, you may get normal coins or flying coins by running and jumping.Now, he meets a problem that what is the maximum score he can obtain.To simpli原创 2020-06-13 13:20:53 · 208 阅读 · 0 评论 -
【Petrozavodsk Programming Camp, Winter 2020 Problem B】 Binomial DP 详解
There are people that wont be happy with solving a problem, unless the actual task is obscured by an elaborateand somewhat unnecessary story. If you are one of these people, then this problem is NOT for you.You are given a sequence of non-negative intege原创 2020-05-24 19:10:38 · 912 阅读 · 0 评论 -
【AcWing 91】 最短Hamilton路径 状压DP + 位运算 详解
给定一张 n 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径。 Hamilton路径的定义是从 0 到 n-1 不重不漏地经过每个点恰好一次。输入格式第一行输入整数n。接下来n行每行n个整数,其中第i行第j个整数表示点i到j的距离(记为a[i,j])。对于任意的x,y,z,数据保证 a[x,x]=0,a[x,y]=a[y,x] 并且 a[x,y]+a[y,z]>=a[x,z]。输出格式输出一个整数,表示最短Hamilton路径的长度。数据原创 2020-05-20 22:44:55 · 350 阅读 · 0 评论 -
【HDU 1846】 Brave Game DP爆破法
Problem Description十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫《勇敢者的游戏》(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻。今天,大家选择上机考试,就是一种勇敢(brave)的选择;这个短学期,我们讲的是博弈(game)专题;所以,大家现在玩的也是“勇敢者的游戏”,这也是我命名这个题目的原因。当然,除了“勇...原创 2020-04-29 19:13:27 · 152 阅读 · 0 评论 -
【HDU 3555】 Bomb 数位dp
Problem DescriptionThe counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current...原创 2020-04-29 12:04:46 · 176 阅读 · 0 评论 -
【Codeforces Beautiful numbers】 数位dp + 离散
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with...原创 2020-04-28 13:18:08 · 186 阅读 · 3 评论 -
【18726 查找最接近的元素】 dp 预处理 SCAU
Description已知长度为n的非下降序列。现在有q个查询,每个查询给出一个指定值。输出序列中第一个大于等于给定值的元素下标,若不存在这样的元素,输出n+1。输入格式第一行一个整数n,为非降序列长度。1=<n<=100000。第二行n个整数,为非降序列元素。所有元素的大小均在int范围内。第三行包含一个整数q,为要询问的给定值个数。1=<q<=100000...原创 2020-04-28 12:50:25 · 432 阅读 · 0 评论 -
【POJ 3252】 Round Numbers 数位dp
DescriptionThe cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone’ (also known as ‘Rock, Paper, Scissors’, ‘Ro, Sham, Bo’, and a host of other names) in o...原创 2020-04-27 19:18:56 · 159 阅读 · 0 评论 -
【HDU 4734】 F(x) 数位dp 优化版
Problem DescriptionFor a decimal number x with n digits (AnAn-1An-2 … A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + … + A2 * 2 + A1 * 1. Now you are given two numbers A and B, pleas...原创 2020-04-27 15:27:17 · 189 阅读 · 0 评论 -
【Codeforces #632 Div2】 C. Eugene and an array 动态规划 详解
Eugene likes working with arrays. And today he needs your help in solving one challenging task.An array c is a subarray of an array b if c can be obtained from b by deletion of several (possibly, zer...原创 2020-04-09 13:47:43 · 313 阅读 · 0 评论 -
【HDU 1176】 免费馅饼 记忆化搜索 DP简洁版 【校队排位赛#13 E】
免费馅饼Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 81577 Accepted Submission(s): 28527Problem Description都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,...原创 2020-04-05 16:59:01 · 200 阅读 · 0 评论 -
【校队排位赛#11 H】 HDU Sequence LIS
Problem DescriptionThere is a sequence X (i.e. x[1], x[2], …, x[n]). We define increasing subsequence of Xas x[i1], x[i2],…,x[ik], which satisfies follow conditions:x[i1] < x[i2],…,<x[ik];1...原创 2020-03-28 18:11:30 · 199 阅读 · 0 评论 -
【POJ Balanced Lineup】 RMQ算法
DescriptionFor the daily milking, Farmer John’s N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To ...原创 2020-03-25 22:20:01 · 140 阅读 · 0 评论 -
【18718 航行】 贪心 or DP SCAU 数据结构习题
18718 航行时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0题型: 编程题 语言: 不限定Description银河帝国正走向覆亡。为保留文明的种子,你需要驾驶飞船将一批“颛家”从帝国首都护送至银河边缘的基地。现在已知航线是一条直线,帝国首都为起点(坐标0),基地为终点(坐标L),在这条航线上有N个空间站可以补充飞船的能源。第i个空间站的坐标为ai,...原创 2020-03-24 15:45:49 · 443 阅读 · 0 评论 -
135. 分发糖果 LeetCode Candy 动态规划
题目说每人最少为1颗糖(即每个dp的初始化可以先设为1),且相邻两个分数高的糖要多。那么我们就可以发现当前位置的状态(糖果数量)和两边人的分数及糖果量有关。但是,可能有人会问,相邻的状态可以由前一个已经得到的状态转移可以理解,但是右边一个人的状态我还没遍历到,怎么利用呢?这时候,只需要进行两次顺序和逆序循环遍历就行了举个例子[1,0,2]初次顺序遍历(只看当前是否比左边大,大就dp+...原创 2019-11-25 12:47:12 · 293 阅读 · 1 评论 -
LeetCode 完全平方数 C的方法(动态规划)
大一,来刷下力扣的算法题巩固一下基础。思路比较纯粹, 就是动态规划及一点点优化。下面说下我的思路1.分解子问题:原题问的是把一个n分解成若干完全平方数相加的结果。那我们不妨这样想,我们可以把问题想成该数n等于一个完全平方数m加上(n-m),然后转化成求(n-m)可拆成怎样的完全平方数和。这样,状态转移方程就可以列出来了。2.取最优现在我们就有一个问题,怎么遍历一遍n以内的m,然后对...原创 2019-11-13 14:31:30 · 406 阅读 · 0 评论 -
【10687 东方迷宮】 SCAU 新生赛题解
题意:从左上顶点开始,到右下顶点结束,求最短路径上的最大权值。动态规划思路(递归法):其实题目简化了问题,看出来了就好做很多。题目说要路径最短,那肯定不能往回走,每一步要么向下要么向右,这样其实就很好写状态转移方程了,如果用递推法的话,状态转移方程为dp[i][j] = max(dp[i-1][j]+dp[i][j-1])+a[i][j]即当前位置由上边或者左边得到,取前一个状态的最优...原创 2020-02-21 17:28:45 · 255 阅读 · 0 评论 -
【校队排位赛#2 D】 动态规划DP
题意:有三种状态分别是休息,做事件A,做事件B,两种事件不能在同一天做,问怎么安排可以让休息的天数最少。每天可以做的事情可能是能做所有事情或者只能做一个或者只能休息动态规划思路:其实这道题的状态转移的影子很明显,当前如果可以做事情,那么当前的最优解一定是前面异于当前选择的最优解的最小值。比如当前做事件A,那么最优解就是前一天做事件B的最优解和休息的最优解的最小值。即dp[i][1]=mi...原创 2020-02-23 20:58:08 · 120 阅读 · 0 评论 -
SCAU ACM校队寒训题 DP专题
A - String Partition动态规划思想:1.外层循环遍历下标,内层循环遍历一遍当前下边前十个数(int大小就是10位)。2.dp[i]就表示当前下标下的sum最大值,那么内层循环就相当于是“分割”,把i之前的元素切成左右两部分,dp[j-1]部分及j-i部分,后者就是要额外算的,而前面j-1部分因为已经遍历过了所以已经是存好的。3.不妨设j-i部分的数合并起来后为sum,那...原创 2020-02-13 20:44:03 · 1395 阅读 · 0 评论 -
【校队排位赛#7 J】 HDU 2196 Computer 树形DP 详解
Problem DescriptionA school bought the first computer some time ago(so this computer’s id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of...原创 2020-03-15 11:55:24 · 140 阅读 · 0 评论