随笔分类 - DP
摘要:LIS nlogn的时间复杂度,之前没有写过。 思路是d[i]保存长度为i的单调不下降子序列末尾的最小值。 更新时候,如果a[i]>d[len],(len为目前最长的单调不下降子序列) d[++len]=a[i] 否则 二分查找 d[j-1] #include #include #include #define LL long long using namespace std; LL re...
阅读全文
摘要:区间dp。 用f[l][r]表示从l到r最少需要染几次色。 状态转移方程: 1.f[l][r]=min(f[l][i],f[i+1][r]) (l #include #include using namespace std; const int maxn = 200 + 10; int n; int f[maxn][maxn]; char s[maxn]; int dp(int l,in...
阅读全文
摘要:区间dp。 用f[l][r]代表从l合并到r的最小得分。 显然 r-l #include #include using namespace std; const int maxn = 100 + 10; const int inf = 0x3f3f3f3f; int n; int a[maxn],f[maxn][maxn]; int dp(int l,int r) { if(f[l...
阅读全文
摘要:树形dp。 本来是想做一系列树分治的,结果这道题树形dp就可以了(膜popoqqq大神) f数组保存每个节点距离为0,1,2的点对数量。 不断统计就可以辣。 #include #include #include using namespace std; const int maxn = 20000 + 10; const int maxm = 40000 + 10; stru...
阅读全文
摘要:状压dp。 500分解质因数的话,除了最大的质因数只需要8个质数,用二进制x储存,最大的质因数用y来储存(若没有比那8个质数大的质因数就使y=1) 用f[i][j]表示第一个人方案为i,第二个人方案为j时的方案数。 递推时用p[0/1][i][j]表示第1/2个人选当前数,i和j分别为两人方案时的方案数。 有f[i][j]=(p[0][i][j]+p[1][i][j]-f[i][j])%m...
阅读全文
摘要:树形dp。 好坑的dp。不会。 f[i][j][k]表示第i件装备和它的基础装备一共花费j个金币且向上提供j个装备时能取得的最大的力量值。 #include #include #include using namespace std; const int maxn = 100 + 10; const int maxm = 2000 + 10; int g[maxn],v[maxm],n...
阅读全文
摘要:斜率优化+树分治。 点分治:找出当前子树的重心,分治根到重心这一段,更新根到重心这一段的值,将剩下的点按能到达的高度从低到高排序,更新。分治其他子树。
阅读全文
摘要:dp. 用f[i][j]表示长度为i,开头数为[1,j]的第一位下降的序列个数。 f[i][j]=f[i][j-1]+f[i-1][i-j]。 f[i-1][i-j]可以表示长度为i-1,开头数为[1,j-1]的第一位上升的序列个数。(各位取反以后,俩者一一对应,所以值相同) 要使用滚动数组。 好像%2和&1性能上没差别。 #include #include #include u...
阅读全文
摘要:spfa+dp。 首先用一个类似spfa的过程求出a[s][t],聪聪在s,可可在t时,聪聪第一步怎么走。 然后dp求出f[s][t],表示聪聪在s,可可在t的期望步数。 #include #include #include using namespace std; const int maxn = 1000 + 10; const int maxm = 2000 + 10; int...
阅读全文
摘要:状态压缩dp。 一道很好的dp题。 我们在选的时候并不知道它会对后面的选择造成什么影响,所以不能正向dp。 f[i][s]表示第i次选择后,选择的宝物二进制为s的期望得分。 初始状态均为0,答案为f[0][0]。 #include #include #include using namespace std; const int maxn = 20; const int maxm =...
阅读全文
摘要:dp。 如果状压dp的话,只能拿到50分。而正解既比状压好写,又是正解。。 f[i][j][k]表示第i行有j列有一个棋子,有k列有俩个棋子,然后dp转移一下就好了(方程太难写,不写了。。。) 100*100*mod可能爆int,所以用了long long。
阅读全文
摘要:dp. 用g[x][i]代表第x根柱子的i个原盘会到哪个柱子,f[x][i]代表所用步数。 则根据g[x][i-1]和g[g[x][i-1]][i-1]就能知道搬运的过程是怎样的。 #include #include #include using namespace std; char p[7][3]; int g[40][3],n; long long f[40][3]; ...
阅读全文
摘要:dp. 用到俩次dp,用1和0代表俩种颜色,首先对于每块木板我们进行一次dp,g[i][j]代表前j个格子刷i次最多能涂到几个格子。 则 g[i][j]=max(g[i-1][k],max(cnt[j]-cnt[k],j-k-cnt[j]+cnt[k])。 k #include #include using namespace std; const int maxn = 100 + 10; ...
阅读全文
摘要:dp. f[i][j]表示放置第i个数有j个逆序对的方案数。 s[i][j]维护前缀和(f[i][0]~f[i][j])。 状态转移方程 f[i][j]=s[i-1][j]-s[i-1][max(j-1,0)]。 oi界十大水题。 #include #include #include using namespace std; const int mod = 10000; const ...
阅读全文

浙公网安备 33010602011771号