
动态规划(dp)
文章平均质量分 75
xuanweiace
一个热爱算法竞赛的弱校ACMer路过。青大本,浙大硕,方向后端开发,菜鸡一枚,奋斗ing...
展开
-
LeetCode 375. 猜数字大小 II
题目大意:https://leetcode-cn.com/problems/guess-number-higher-or-lower-ii我们正在玩一个猜数游戏,游戏规则如下:我从1到 n 之间选择一个数字。你来猜我选了哪个数字。如果你猜到正确的数字,就会 赢得游戏 。如果你猜错了,那么我会告诉你,我选的数字比你的 更大或者更小 ,并且你需要继续猜数。每当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。如果你花光了钱,就会 输掉游戏 。给你一个特定的数字 n ,返回..原创 2021-11-19 21:46:09 · 142 阅读 · 0 评论 -
二进制枚举子集 CS Maxor 或运算,DP(SOS)
https://blog.youkuaiyun.com/noone0/article/details/78289517目前没有题目链接。题意:长度为n的序列a,选出两个元素,其或运算结果的最大值为多少,并求出a[i]|a[j]==mx的方案数?n<=1e5,0<=a[i]<=2^17,m<=17.假如最大值为mx,若x|y=mx 则x和y肯定为mx的子集.否则或运算结果...原创 2020-12-17 00:12:38 · 259 阅读 · 0 评论 -
【PAT - 甲级1045】Favorite Color Stripe(30分)(dp,LIS类问题)
题干:Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining ...原创 2020-01-31 13:32:58 · 742 阅读 · 0 评论 -
【Gym - 101612C】【2017-2018NEERC】Consonant Fencity(状压枚举,预处理)
题干:把26个字母分成19个辅音字母和7个元音字母,让你通过 将某些字母改为大写状态,使得字符串中连续的两个大小写状态不同的辅音字母组成的字母对数量最多,输出该状态下的字符串。注意输出的字符串中同一字母必须形态统一,也就是对于同一个字母,不能既有大写状态的,也有小写状态的。如果有多种可能的解,输出任意一种。解题报告:注意到除去元音字母,还剩下19个辅音字母,这样我们第一步可以2^19...原创 2019-11-02 17:08:25 · 255 阅读 · 0 评论 -
【51Nod - 1270】数组的最大代价(dp,思维)
题干:数组A包含N个元素A1, A2......AN。数组B包含N个元素B1, B2......BN。并且数组A中的每一个元素Ai,都满足1 <= Ai <= Bi。数组A的代价定义如下:(公式表示所有两个相邻元素的差的绝对值之和)给出数组B,计算可能的最大代价S。Input第1行:1个数N,表示数组的长度(1 <= N <= 50000...原创 2019-10-31 20:58:36 · 337 阅读 · 0 评论 -
【POJ - 2151】Check the difficulty of problems(概率dp)
题干:Organizing a programming contest is not an easy job. To avoid making the problems too difficult, the organizer usually expect the contest result satisfy the following two terms:1. All of th...原创 2019-10-25 20:41:33 · 314 阅读 · 0 评论 -
【CodeForces - 789C】Functions again(最大子段和变形,dp,思维)
题干:Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon th...原创 2019-10-25 13:41:09 · 304 阅读 · 1 评论 -
【POJ - 3744】Scout YYF I(概率dp,矩阵快速幂优化dp)
题干:题目大意:在一条不满地雷的路上(无限长),你现在的起点在1处。在N个点处布有地雷,1<=N<=10。地雷点的可能坐标范围:[1,100000000].每次前进p的概率前进一步,1-p的概率前进2步。问顺利通过这条路的概率。就是不要走到有地雷的地方。设dp[i]表示到达i点且不踩地雷的概率,如果1号点没有地雷,则初始值 dp[1]=1.很容易想到转移方程: dp...原创 2019-10-24 19:52:22 · 254 阅读 · 0 评论 -
对记忆化搜索(ms)和动态规划(dp)的深入理解
六月中旬了,马上就要期末考试了,期末考试结束以后就要迎来紧张刺激的留校集训,到那时博客会更新的比较频繁,而现在在准备期末考试,所以可能更新的博客稍微少一些。 话不多说,今天来更一篇刚刚吃饭的时候关于记忆化搜索和动态规划的一些区别的思考。 记忆化搜索(Memory Search),其实还是用递归函数实现的,通常函数名依然叫做dfs(  ̄□ ̄||)。核心语句就是那两部分关键的语句块...原创 2018-06-14 14:13:48 · 11914 阅读 · 3 评论 -
*【HDU - 1506】【POJ - 2559】Largest Rectangle in a Histogram(单调栈或动态规划)
题干: DescriptionA histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figu...原创 2018-07-16 23:44:49 · 235 阅读 · 0 评论 -
【51Nod - 1279】 扔盘子(思维)(on-p会超时)
题干:有一口井,井的高度为N,每隔1个单位它的宽度有变化。现在从井口往下面扔圆盘,如果圆盘的宽度大于井在某个高度的宽度,则圆盘被卡住(恰好等于的话会下去)。盘子有几种命运:1、掉到井底。2、被卡住。3、落到别的盘子上方。盘子的高度也是单位高度。给定井的宽度和每个盘子的宽度,求最终落到井内的盘子数量。 如图井和盘子信息如下:井:5 6 4 3 6 2 3盘子:2...原创 2018-08-05 15:56:42 · 266 阅读 · 0 评论 -
【CF - 699C】 Vacations (日程安排 dp)
题干:Vasya hasndays of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of thisndays: whether that gym opened and whether a contest ...原创 2019-05-09 21:26:04 · 306 阅读 · 0 评论 -
【HDU - 1069】Monkey and Banana (最长下降子序列 + 贪心,最长上升子序列类问题)
题干:A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the...原创 2018-12-24 08:47:43 · 479 阅读 · 0 评论 -
【HDU - 2571】 命运(记忆化搜索)
题干:穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个机关。要知道,不论何人,若在迷宫中被困1小时以上,则必死无疑! 可怜的yifenfei为了去救MM,义无返顾地跳进了迷宫。让我们一起帮帮执着的他吧! 命运大迷宫可以看成是一个两维的方格阵列,如下图所示: yifenfei...原创 2018-08-20 20:54:07 · 295 阅读 · 0 评论 -
【HDU - 1087】Super Jumping! Jumping! Jumping! (最大上升子序列类问题,dp)
题干:Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The ...原创 2018-08-15 22:29:22 · 357 阅读 · 0 评论 -
【qduoj - 142】 多重背包(0-1背包的另类处理,dp)
题干:ycb的ACM进阶之路Description ycb是个天资聪颖的孩子,他的梦想是成为世界上最伟大的ACMer。为此,他想拜附近最有威望的dalao为师。dalao为了判断他的资质,给他出了一个难题。dalao把他带到一个到处都是题的oj里对他说:“孩子,这个oj里有一些不同的题,做每一道题都需要一些时间,每一题也有它自身的rp(人品值)。我会给你一段时间,在这段时间里,...原创 2018-10-30 22:07:07 · 253 阅读 · 0 评论 -
【HDU - 2809】 God of War(状压dp)
题干:At 184~280 A.D ,there were many kingdoms in China. Three strongest among them are "Wei", "Shu", "Wu". People call this period as "Three Kingdoms". HH is a super "Three Kingdoms" fan, because at ...原创 2018-08-28 22:30:30 · 332 阅读 · 0 评论 -
【POJ - 2392】Space Elevator (dp,优秀的背包问题)
题干:The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to bu...原创 2018-12-05 20:42:23 · 316 阅读 · 0 评论 -
【nyoj-456】 邮票分你一半 (dp,0-1背包的中点问题)
题干:邮票分你一半时间限制:1000ms | 内存限制:65535KB难度:3描述 小珂最近收集了些邮票,他想把其中的一些给他的好朋友小明。每张邮票上都有分值,他们想把这些邮票分成两份,并且使这两份邮票的分值和相差最小(就是小珂得到的邮票分值和与小明的差值最小),现在每张邮票的分值已经知道了,他们已经分好了,你知道最后他们得到的邮票分值和相差多少吗?输入...原创 2019-05-09 21:51:52 · 275 阅读 · 0 评论 -
☆【UVA - 624 】CD(dp + 0-1背包 + 记录路径)
题干:You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tapeNminutes long....原创 2019-10-18 21:21:47 · 177 阅读 · 0 评论 -
【POJ - 3211】Washing Clothes (dp,0-1背包中点问题)
题干:Dearboy was so busy recently that now he has piles of clothes to wash. Luckily, he has a beautiful and hard-working girlfriend to help him. The clothes are in varieties of colors but each piece o...原创 2019-10-18 21:20:06 · 187 阅读 · 0 评论 -
【OpenJ_Bailian - 2711 】 合唱队形(dp,枚举中间顶点)
题干:N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学不交换位置就能排成合唱队形。合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1, 2, …, K,他们的身高分别为T1, T2, …, TK,则他们的身高满足T1 < T2 < … < Ti , Ti > Ti+1 > … > TK (1 <= i <=...原创 2019-10-19 19:22:26 · 182 阅读 · 0 评论 -
【HDU - 2089 】不要62 (dp)
题干:杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。 不吉利的数字为所有含有4或62的号码。例如: 62315 73418 88914 都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不...原创 2018-08-20 20:59:01 · 457 阅读 · 0 评论 -
【HDU - 3466 】Proud Merchants(dp,背包问题,巧妙排序)
题干:Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if th...原创 2018-08-20 21:47:03 · 273 阅读 · 0 评论 -
【HDU - 2546】饭卡 (dp,0-1背包,贪心思想)
电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。 某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。 Input多组数据。对于每组数据: 第一行为正整数n...原创 2018-11-03 13:00:12 · 353 阅读 · 0 评论 -
【 POJ - 3628 】Bookshelf 2(dfs 或 dp,0-1背包)
题干:Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.FJ hasNcows (1 ≤N≤ 20) ea...原创 2019-05-09 21:53:34 · 296 阅读 · 0 评论 -
【POJ - 3624 】Charm Bracelet (dp,0-1背包裸题)
题干:Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in...原创 2018-08-21 14:50:11 · 248 阅读 · 0 评论 -
【nyoj - 860】 又见0-1背包 (dp,反向0-1背包,好题好思路)
题干:又见01背包时间限制:1000 ms | 内存限制:65535 KB难度:3输入多组测试数据。每组测试数据第一行输入,n 和 W ,接下来有n行,每行输入两个数,代表第i个物品的wi 和 vi。输出满足题意的最大价值,每组测试数据占一行。样例输入4 52 31 23 42 2样例输出7来源飘谊系列上传者TC_张友谊...原创 2018-09-05 01:11:33 · 628 阅读 · 0 评论 -
【HDU - 1025】Constructing Roads In JGShining's Kingdom(dp最长上升子序列模型 + 二分优化)
题干: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29933 Accepted Submission(s): 8496...原创 2018-12-02 19:28:37 · 204 阅读 · 0 评论 -
【POJ - 2533】Longest Ordered Subsequence(四种方法解决最长上升子序列 含二分优化版本)
题干:Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41944 Accepted: 18453 DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subse...原创 2018-12-02 19:28:18 · 355 阅读 · 0 评论 -
【动态规划模型】金矿模型理解动态规划!(精彩的故事)
对于动态规划,每个刚接触的人都需要一段时间来理解,特别是第一次接触的时候总是想不通为什么这种方法可行,这篇文章就是为了帮助大家理解动态规划,并通过讲解基本的01背包问题来引导读者如何去思考动态规划。本文力求通俗易懂,无异性,不让读者感到迷惑,引导读者去思考,所以如果你在阅读中发现有不通顺的地方,让你产生错误理解的地方,让你难得读懂的地方,请跟贴指出,谢谢! -...转载 2018-08-28 22:29:36 · 237 阅读 · 0 评论 -
【nyoj - 252】 01串(简单dp)
题干:01串时间限制:1000 ms | 内存限制:65535 KB难度:2输入第一行有一个整数n(0<n<=100),表示有n组测试数据;随后有n行,每行有一个整数m(2<=m<=40),表示01串的长度;输出输出不含有“11”子串的这种长度的01串共有多少个,占一行。样例输入223样例输出35描述AC...原创 2018-08-21 20:21:32 · 269 阅读 · 0 评论 -
*由易到难的讲解动态规划(精)
简介(入门) 什么是动态规划,我们要如何描述它?动态规划算法通常基于一个递推公式及一个或多个初始状态。 当前子问题的解将由上一次子问题的解推出。使用动态规划来解题只需要多项式时间复杂度, 因此它比回溯法、暴力法等要快许多。现在让我们通过一个例子来了解一下DP的基本原理。首先,我们要找到某个状态的最优解,然后在它的帮助下,找到下一个状态的最优解。“状态”代表什么及如何找到它?...转载 2018-08-21 21:09:38 · 506 阅读 · 0 评论 -
【POJ - 1050】To the Max (dp)
题干:Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the s...原创 2019-10-18 22:06:06 · 164 阅读 · 0 评论 -
【 FZU - 2214 】Knapsack problem(逆向0-1背包)
题干:Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the tot...原创 2018-09-05 01:12:03 · 331 阅读 · 0 评论 -
【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)
题干: 题目大意:题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围。现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭。否则会被激发,留下自己。解题报告:dp求解:现在可以从右边放置一个灯塔,位置和power level都可以自己定义。问各种情况中最小的灯塔被毁灭的数量。dp[x]表示到x个灯塔...原创 2018-09-10 11:38:58 · 633 阅读 · 0 评论 -
【qduoj】【超级楼梯进阶版】
题干:描述 N级阶梯,人可以一步走一级,也可以一步走两级,求人从阶梯底端走到顶端可以有多少种不同的走法。输入 一个整数n,代表台阶的阶数。输出 求人从阶梯底端走到顶端可以有多少种不同的走法,输出结果的格式为大整数.输入样例 1 1输出样例 11输入样例 2 2输出样例 22输入样例 3 4输出样例 35...原创 2018-09-13 11:43:24 · 360 阅读 · 0 评论 -
【CodeForces - 580D】Kefa and Dishes (状压dp)
题干:kefa进入了一家餐厅,这家餐厅中有n个菜(0<n≤18),kefa对第i个菜的满意度为ai(0≤ai≤10^9),并且对于这n个菜有k个规则,如果kefa在吃完第xi个菜之后吃了第yi个菜(保证xi、yi不相等),那么会额外获得ci(0≤ci≤10^9)的满意度。kefa要吃m道任意的菜(0<m≤n),但是他希望自己吃菜的顺序得到的满意度最大,请你帮帮kefa吧! 输入第...原创 2018-09-18 17:31:43 · 296 阅读 · 0 评论 -
【CodeForces - 574D】Bear and Blocks (dp,思维)
题干:Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture ...原创 2018-09-19 19:19:02 · 388 阅读 · 0 评论 -
【POJ - 1661】Help Jimmy(记忆化搜索,dp)
题干:解题报告:AC代码1:#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>using namespace std;const int INF = 0x3f3f3f3f;int n,x,y,maxx;int dp[100...原创 2018-09-22 11:39:48 · 310 阅读 · 0 评论