DP
文章平均质量分 50
ID_BePosit
如果今天不比昨天多做一点什么,那么明天还有什么意义。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
饭卡- HDU - 2546(01背包)
01背包问题求解思路 ①、确认子问题和状态 01背包问题需要求解的就是,为了体积V的背包中物体总价值最大化,NN件物品中第ii件应该放入背包中吗?(其中每个物品最多只能放一件) 为此,我们定义一个二维数组,其中每个元素代表一个状态,即前ii个物体中若干个放入体积为VV背包中最大价值。数组为:f[N][V]f[N][V],其中fijfij表示前ii件中若干个物品放入体积为jj的背包...原创 2018-08-06 20:40:59 · 361 阅读 · 0 评论 -
D - Weather Station -子串合并种类
给出1个字符串,由8各方向组成,问按照字符串从左到右的顺序,能有几种不同的走法。 思路: 找出 NE ,NW,SE,SW,的数目分合两种状态。2^ans次方 #include<bits/stdc++.h>using namespace std;#define maxn 100000int main(){ string s; in...原创 2018-08-15 19:15:00 · 249 阅读 · 0 评论 -
In Action-HDU - 最短路-01包
题意: Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the number of nuclear weapons have soared across the globe. 自从1945年曼哈顿计划小组在美国引爆了第一颗原子弹,世界核武器的数量已经飙升了...原创 2018-08-24 10:19:04 · 241 阅读 · 0 评论 -
Revamping Trails -k层最短路
通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 速公路.在高速公路上的通行几乎是瞬间完成的,所以高速公路的通行时间为0. 请帮助约翰决定对哪些小径进行升级,使他每天从1号牧场到第N号牧场所花的时间最短 #include<bits/stdc++.h>using namespace std;#define maxn 10555#define i...原创 2018-09-04 15:09:00 · 224 阅读 · 0 评论 -
E - Tickets -简单dp
E - Tickets HDU - 1260 题意: 买票可以单独买也可以相邻两人合着买。 #include<bits/stdc++.h>using namespace std;#define maxn 2230#define inf 0x3f3f3f3flong long a[maxn],b[maxn],n,sum,k,dp[maxn];void ...原创 2018-09-02 20:00:05 · 295 阅读 · 0 评论 -
J - Boxes Game -区间dp
J - Boxes Game Gym - 101502J 题意:a player can take one box from either ends of the line, add the value of the card inside this box to his score, then remove this box from the line. ends of the...原创 2018-09-05 17:54:36 · 308 阅读 · 0 评论 -
D - Jumping Haybales -线段优化坐标DP
D - Jumping Haybales Gym - 101617D 题意: 从1,1到n, n求最小步数只是每一秒可以向右或向下走k步若是直接搜索无疑会超时 即便是dp也会超时 只有维护k步内最小值直接进行优化即可, 维护方法有多重选择了简单点的线段树 注意线段树的=的位置,Otherwise, it will RE.... 不过线段树也在TLE MLE的边缘 ...原创 2018-10-01 08:37:48 · 253 阅读 · 0 评论 -
石子归并 -记忆化搜索or区间DP
石子归并 51Nod - 1021 记忆化搜索:#include<bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3f#define maxn 111int dp[maxn][maxn];int sum[maxn],n,a;int dfs(int l,int r){ if(l==r)ret...原创 2018-10-02 10:08:47 · 322 阅读 · 0 评论 -
N - Treats for the Cows -记忆-区间DP
N - Treats for the Cows POJ - 3186 #include<iostream>#include<cstring>using namespace std;#define inf 0x3f3f3f3f#define maxn 2111int dp[maxn][maxn];int a[maxn],n;int dfs(...原创 2018-10-02 10:37:59 · 167 阅读 · 0 评论 -
P - Phalanx -坐标DP
P - Phalanx HDU - 2859 #include<iostream>#include<cstring>using namespace std;#define inf 0x3f3f3f3f#define maxn 2111char mmp[maxn][maxn];int dp[maxn][maxn],n;int main()...原创 2018-10-02 12:25:49 · 228 阅读 · 0 评论 -
Ignatius and the Princess IV -dp思想
动态更新num与ans ,ans可以随变假设。只有那个大于n+1/2的才会被留下来转自:https://www.cnblogs.com/kuangbin/archive/2011/07/30/2122217.html我们很容易的看出来,在一个序列中如果去掉2个不同的元素,那么原序列中的多元素,在新的序列中还是多元素,因此我们只要按照序列依次扫描,先把t赋值给result,增加个计...原创 2018-09-26 14:52:09 · 215 阅读 · 0 评论 -
Monkey and Banana -dp单调序列
Monkey and Banana HDU - 1069 有序长宽高存储各种长方体; 从小到大排序 从小到大动态更新。 if(a[i].x>a[j].x&&a[i].y>a[j].y) dp[i]=max(dp[i],dp[j]+a[i].z); ...原创 2018-09-26 16:02:07 · 183 阅读 · 0 评论 -
Doing Homework-状压DP
Doing Homework HDU - 1074 枚举所有 作业完成状态 初始状态0末状态(1<<n)-1 第一层i枚举各种状态 第二层j查找当前i状态下第一个没完成的作业 然后k循环查找一下当前完成的作业把时间记录求和来 判断 找到的这个未完成的作业 到底是怎么罚时的 dp更新i当前状态和把j位置完成的状态,并且更新前缀数组来记录罚时最少的排列方式。。...原创 2018-10-02 22:19:54 · 186 阅读 · 0 评论 -
Max Sum HDU - DP最大连续和
Max Sum HDU - 100 注意更新时一旦有使得sum<0的情况直接跳到下个开始就行 #include <iostream>using namespace std;#define maxn 111000#define inf 0x3f3f3f3fint t,a,ans,sum,n,l,r,s;int main(){ cin>...原创 2018-09-26 17:04:48 · 228 阅读 · 0 评论 -
B - 饭卡 HDU - 最值型01背包
B - 饭卡 HDU - 2546 预处理一下最大价值的用5元去买。特判初始状态钱小于5的情况 #include<bits/stdc++.h>using namespace std;#define maxn 1552int n,m,a[maxn];int dp[maxn],ans;int main(){ while(cin>>...原创 2018-10-11 16:34:14 · 369 阅读 · 0 评论 -
E - Robberies -最值型01背包
E - Robberies HDU - 2955 思路:背包容量是钱数,银行有限钱数有限的。然后是求最大逃跑概率,而题中每项给出的是被抓概率,所以要先被1减一下。转化为概率的乘积,所以动态方程也要做出改变。最后遍历,剩余的钱数越多,说明所抢的钱数越少,逃跑几率越大。所以从大到小遍历背包容量,一旦大于p,即为最大概率跳出。 #include<bits/stdc++.h&...原创 2018-10-11 17:05:02 · 182 阅读 · 0 评论 -
C - Brackets POJ - 2955-区间DP
C - Brackets POJ - 2955 对于区间L,R如果str[L]==str[R] dp[L][R]=dp[L+1][R-1]+2转移而来 第一层for枚举长度 第二层for枚举再次长度下的始末点 第三层for切分求最优解 #include<iostream>#include<cstring>using namespac...原创 2018-10-05 16:30:22 · 264 阅读 · 1 评论 -
B - Halloween Costumes -区间DP
B - Halloween Costumes LightOJ - 1422 那么对于第 i 件衣服,我们有 1.如果在之后的区间内都不再重复利用这件衣服,那么明显 dp[i][j] = dp[i+1][j] + 1; 2.如果在之后的区间 i+1 ~ j 中存在一件衣服 k 是跟 i 一样的,那么我们便可以将i那件衣服在k这个地方重复利用, 那么转移方程为 dp[i...原创 2018-10-05 17:33:56 · 248 阅读 · 0 评论 -
E - Super Jumping! Jumping! Jumping!DP-最大子序列和
E - Super Jumping! Jumping! Jumping! HDU - 1087 #include<bits/stdc++.h>using namespace std;#define maxn 1111#define inf 0x3f3f3f3fint n,ans,dp[maxn];int a[maxn],sum;int main(){ w...原创 2018-09-27 20:10:54 · 192 阅读 · 0 评论 -
F - Piggy-Bank-完全背包
F - Piggy-Bank HDU - 1114 基础版本: f[i][j] = max{f[i][j],f[i-1][j - k * c[i]] + k * w[i]}(0<=k*c[i]<=v) for (int i = 1; i < n; i++){ for (int j = 1; j <= v; j++){ fo...原创 2018-09-27 20:51:18 · 205 阅读 · 0 评论 -
D - Bomb -上界数位DP模板
D - Bomb HDU - 3555 #include <iostream>using namespace std;#define ll long longll dp[20][2],digit[20],t,n;ll dfs(int len,bool if4,bool limit){ if(len==0)return 1; if(!lim...原创 2018-10-06 09:51:10 · 241 阅读 · 0 评论 -
C - 不要62 -上下界数位DP
C - 不要62 HDU - 2089 一个dp题目汇总:https://wenku.baidu.com/view/d4104419c77da26924c5b078.html #include <iostream>using namespace std;#define ll long longll dp[20][2],digit[20],t,n,m;ll...原创 2018-10-06 10:02:39 · 350 阅读 · 0 评论 -
G - B-number HDU - 数位DP
G - B-number HDU - 3652 题意:A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 a...原创 2018-10-12 20:47:20 · 175 阅读 · 0 评论 -
J - Just Too Lucky-数位DP
J - Just Too Lucky Gym - 100623J Since mass transit (公共交通)was invented, people who buy tickets look for lucky ticket numbers. There are many notions of lucky tickets, for example sometimes ti...原创 2018-10-06 11:36:09 · 301 阅读 · 0 评论 -
A - Beautiful numbers-取模数位DP
A - Beautiful numbers CodeForces - 55D 题意:It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits 在他看来,正整数是优美的,当且仅当它是由它的每个非零数字...原创 2018-10-06 15:53:05 · 310 阅读 · 0 评论 -
E - Maximum Sum -状压DP
E - Maximum Sum Gym - 101853E 题意:choose a subset of the grid's cells, such that their summation is as maximal as possible. 限制: there are no two adjacent cells in that subset. Two cells are cons...原创 2018-10-07 09:57:56 · 315 阅读 · 0 评论 -
J - FatMouse's Speed -序列DP
J - FatMouse's Speed HDU - 1160 题目大意: 很多肥老鼠认为,长的越肥,奔跑速度就越快,为了反驳这个观点,你现在需要对老鼠的体重和速度进行研究,你要在老鼠序列中找出一个子序列,使得老鼠的体重在增加,但是速度却在减慢 #include<bits/stdc++.h>using namespace std;#define maxn ...原创 2018-09-28 16:33:03 · 359 阅读 · 0 评论 -
最少拦截系统 -NlogN-LIS
最少拦截系统 HDU - 1257 #include<bits/stdc++.h>#include<algorithm>using namespace std;#define MAXN 40005int arr[MAXN],ans[MAXN],len;int main(){ int T,p,i; while(~scanf("%d"...原创 2018-10-07 11:06:29 · 226 阅读 · 0 评论 -
B - XHXJ's LIS -数位DP-nlogn-状压LIS
B - XHXJ's LIS HDU - 4352 S^(1<<i)把第i位变为0其余的保持不变 题意:给出l,r求出L-R之间的数字有哪些是最长上升序列为k的数目通过O(nlogn)的LIS因为是数字只有10个数 可以状态压缩模拟LIS过程,最终统计1的个数即可(小细节这里全是真正数字所以数字状态不能由0更新而来,会导致出现前导零情况) #inclu...原创 2018-10-07 11:44:17 · 277 阅读 · 0 评论 -
O - FatMouse and Cheese -记忆化搜索
O - FatMouse and Cheese HDU - 1078 根据题意只好扩散性搜索,就是那种在一个点对于它能走到的所有点暴力搜索,这样会超时。 记忆化搜索是这样实现的:可以理解为结果的形成是反向的,从终点向起点(0,0)慢慢形成,每次搜索一个点周围能够满足条件的点,每找到一个点就继续递归找这个点周围满足条件的点,最终找到的点将会是数值最大的,也就是终点,这时把矩阵map...原创 2018-09-28 17:12:20 · 168 阅读 · 0 评论 -
L - Common Subsequence -最长公共子序列
L - Common Subsequence POJ - 1458 打印序列的版本: #include<iostream>#include<stdio.h>#include<cstring>using namespace std;#define maxn 1111#define inf 0x3f3f3f3fint len1,d...原创 2018-09-28 17:47:16 · 203 阅读 · 0 评论 -
E - Round Numbers-二进制数位DP
E - Round Numbers POJ - 3252 题意:Round Number 被称为其二进制形式中0的个数>=1的个数。求[x,y]区间内“Round Number”的个数. 初步想法正常数位dp进行搜缩搜到一个数时去getsum一下里面0,1数,但没想好如何记忆化,而且题目的要求按照十进制的数位进行 记忆化确实没有意义,这里他想要找的数字都是对二进制有要求所以...原创 2018-10-07 16:13:36 · 337 阅读 · 0 评论 -
J - Treasure Map -逆向DP
J - Treasure Map Gym - 101617J 题意: You can collect all of the day’s gold from a mine when you are there, but you have to move on, you cannot stay for multiple days at the same mine. However, ...原创 2018-09-29 09:30:03 · 250 阅读 · 0 评论 -
F - Warm up HDU - 缩点dp求树直径
F - Warm up HDU - 4612 定义: 一棵树的直径就是这棵树上存在的最长路径。 求法: 两次dfs或bfs。第一次任意选一个点进行dfs(bfs)找到离它最远的点,此点就是最长路的一个端点,再以此点进行dfs(bfs),找到离它最远的点,此点就是最长路的另一个端点,于是就找到了树的直径。 证明: 假设此树的最长路径是从s到t,我们选择的点为...原创 2018-10-14 21:59:40 · 428 阅读 · 2 评论 -
A - Max Sum Plus Plus -滚动数组优化DP
A - Max Sum Plus Plus HDU - 1024 题意:n个数,分m段可以不全取但必须连续。 思路:这一行只与上一行有关系一维dp数组即可。状态转移为:dp[j]=max(dp[j-1]+a[j],maxx[j-1]+a[j]); 我们用一个二维数组dp[i][j]表示前j个数分为i段的最大和。 这时就会有转移方程dp[i][j]=max(dp[i][j-1]...原创 2018-10-15 19:50:11 · 201 阅读 · 0 评论 -
A - 旅行商问题(TSP) -状压DP
A - 旅行商问题(TSP) POJ - 3311 题意:一个送外卖的人,要从0送去所有地点再回到0,每个点可以走任意多次求最短路 思路:floy求出最短路。状态压缩各种状态之间相互更新, 转移方程为:dp[i][j]=min(dp[i][j],dp[i^1<<(j-1)][k]+mmp[k][j]); #include<stdio.h>#in...原创 2018-10-15 20:28:28 · 885 阅读 · 0 评论 -
G - Corn Fields POJ - 状压DP
G - Corn Fields POJ - 3254 题意: 题目大意是有M×N的玉米地,但其中有些是不肥沃的,不能种植。用1来代表肥沃,0代表不肥沃。另外奶牛不喜欢挨着吃,也就是说要间隔着种植,求有几种种植方式, 思路: ①在读入时就将格子状态取反,压缩成行状态存到row[]数组里; ②枚举所有有效的种植状态,存到legal[]...原创 2018-10-15 21:37:19 · 214 阅读 · 0 评论 -
B - 炮兵阵地 POJ - 状压DP
B - 炮兵阵地 POJ - 1185 对于原始的矩阵,我们用0来表示可以放置炮兵,即对应图中的P,这样每一行都有一个可以放置炮兵的状态,存到row[N]中,用来check该行的状态是否合法。 由于当前行和前两行有关系,所以得用3维矩阵来保存一个状态下最多的炮兵个数,用dp[i][curst][prest]表示当前第i行状态对curst,前一行状态为prest的最大炮兵数。 转移...原创 2018-10-16 09:14:44 · 258 阅读 · 0 评论 -
D - Dividing coins -划分性01背包
D - Dividing coins UVA - 562 题意:一堆钱都是只有一个,分两堆求能够使差最小的分配思路:只要其中一个尽可能接近总数的一半即可#include<bits/stdc++.h>using namespace std;#define maxn 500111int t,n,m,a[maxn],s;int dp[maxn],sum;int...原创 2018-10-10 14:22:33 · 278 阅读 · 0 评论 -
L - Coin Change-种类型完全背包
L - Coin Change UVA - 674 看完这个问题,你也许会觉得这个不就是01背包的升级版吗,其实就是这样,完全背包问题与01背包问题的区别在于完全背包每一件物品的数量都有无限个,而01背包每件物品数量只有1个 所以说与它相关的策略已经不是只有取和不取这两种策略了,而是有取0件、取1件、取2件……等等很多种策略 下面给出状态转移方程: f[i][j] = ma...原创 2018-10-10 14:52:37 · 445 阅读 · 0 评论
分享