动态规划
exmy
Less is more, and more is less.
展开
-
nyoj--16 矩形嵌套(经典DP)
nyoj 16题意有n个矩形,每个矩形可以用a,b来描述,表示长和宽。矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<da<c,b<d或者b<c,a<db<c,a<d(相当于旋转X90度)。例如(1,5)可以嵌套在(6,2)内,但不能嵌套在(3,4)中。你的任务是选出尽可能多的矩形排成一行,使得除最后一个外,每一个矩形都可以嵌套在下一个矩形内。题解按白书上的分析,矩形之间的“可嵌套”原创 2016-02-27 11:37:04 · 380 阅读 · 0 评论 -
hihoCoder--1110 正则表达式(区间dp)
描述 给定一个字符串,判断其是否为合法的正则表达式。一个正则表达式定义为:1:0是正则表达式,1也是正则表达式。2:P和Q都是正则表达式,则PQ是正则表达式。3:P是正则表达式,则(P)是正则表达式4:P是正则表达式,则P*也是正则表达式5:P和Q都是正则表达式,则P|Q是正则表达式。输入 输入包含多组数据。每组数据为一行一个字符串,长度不超过100。输出 对于每组数据,如果输入是合法的正则表原创 2017-01-14 14:47:46 · 625 阅读 · 0 评论 -
nyoj--79 拦截导弹(dp)
nyoj 79题解单调递减最长子序列,把数组翻转过来就是一个单调递增最长子序列问题。#include <iostream>#include <cstdio>#include <fstream>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 25;int原创 2016-02-28 00:01:54 · 475 阅读 · 0 评论 -
nyoj--17 单调递增最长子序列(LIS)
nyoj 17题解O(n2)O(n^2) 和 O(nlogn)O(nlogn) 两种解法。#include <iostream>#include <string>#include <cstring>#include <algorithm>using namespace std;int main(){ int t; char dp[10005]; string src原创 2016-03-12 22:38:05 · 353 阅读 · 0 评论 -
nyoj--61 传字条(一)(多线程dp)
nyoj 61题意m∗nm*n的矩形方格,从左上角到右下角找到两条互不相交的路径(首尾两端除外),使得路径上的和最大。题解多线程dp,get new skill~ 定义状态:dp(k,x1,y1,x2,y2)dp(k, x_1, y_1, x_2, y_2)表示两人在第k步时分别到(x1,y1)(x_1, y_1),(x2,y2)(x_2, y_2)时的最大和。 状态转移:dp(k,x1,y1,原创 2016-03-13 13:20:57 · 394 阅读 · 0 评论 -
蓝桥杯--算法训练 方格取数(多线程dp)
题意取过的数会变成0,因此两条路径相交的部分只能取一次。#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 12;int dp[maxn][maxn][maxn][maxn], g[maxn][maxn];int原创 2016-03-13 15:22:19 · 933 阅读 · 0 评论 -
蓝桥杯--算法提高 01背包
题解水水更健康~#include <iostream>#include <algorithm>using namespace std;const int maxn = 5000 + 10;int dp[maxn];int n, m;int main(){ cin >> n >> m; int w, v; for(int i = 0; i < n; ++i)原创 2016-03-16 17:52:34 · 1021 阅读 · 0 评论 -
hdoj--1069 Monkey and Banana(dp)
hdoj 1069题解按面积排序,LIS.#include <iostream>#include <vector>#include <fstream>#include <algorithm>using namespace std;struct block{ int x, y, z; block(int x, int y, int z):x(x), y(y), z(z){}}原创 2016-03-29 18:28:12 · 623 阅读 · 0 评论 -
hdoj--2412 Party at Hali-Bula(树形dp)
hdoj 2412题解树形dp模型。状态: dp[i][0]dp[i][0]: 不选择 i 时, i 及其子树能选出 的最大人数。 dp[i][1]dp[i][1]: 选择 i 时,i 及其子树能选出的最大人数。边界: 叶子结点:dp[k][0]=0,dp[k][1]=1dp[k][0] = 0, dp[k][1] = 1转移: 非叶子结点: dp[i][0]=∑max(dp[j][0],原创 2016-04-04 12:14:12 · 364 阅读 · 0 评论 -
poj--1141 Brackets Sequence(dp)
poj 1141题解设阶段 r 为子序列的长度,状态 i 为当前子序列的首指针,则尾指针为 j = i + r,设当前子序列 si....sjs_i .... s_j需要添加的最少字符数为 dp[i][j]。 初始条件:dp[i][i] = 1 状态转移: * 当 (s[i] == '(' && s[j] == ')') || (s[i] == '[' && s[j] == ']'),dp[i原创 2016-05-20 09:14:30 · 423 阅读 · 0 评论 -
Uva--116 Unidirectional TSP(dp)
UVa 116题解设d(i, j)为从(i, j)出发到达最后一列的最小开销, 状态转移:d(i, j) = d(min{i, (i - 1 + m) % m, (i + 1) % m}, j + 1) 初始条件:d(i, j) = a(i, j), 当j = n - 1, 即最后一列的时候 从最后一列往前递推。#include <bits/stdc++.h>using namespace原创 2016-06-11 11:00:49 · 615 阅读 · 0 评论 -
poj--3249 Test for Job(topsort + dp)
Test for Job题解DAG图上的单源最短路径。#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int maxn = 100000 + 5;const int maxe = 1000000 + 5;原创 2016-09-23 20:48:29 · 391 阅读 · 0 评论 -
Round D APAC Test 2017--Problem C. Codejamon Cipher(dp)
Problem C. Codejamon Cipher题意The Codejamon monsters talk in enciphered messages. Here is how it works:Each kind of monster has its own unique vocabulary: a list of V different words consisting only of原创 2016-10-18 10:59:03 · 805 阅读 · 3 评论 -
poj--1080 Human Gene Functions(dp)
poj 1080题解dp[i][j]dp[i][j] 表示 s1 的前 i 个字符和 s2 的前 j 个字符 的最大相似度。 则, dp[i][j]=max(dp[i−1][j−1]+scoreMatrix[s1[i]][s2[j]],dp[i−1][j]+scoreMateix[s1[i]][‘−‘],dp[i][j−1]+scoreMatrix[‘−‘][s2[j]]])dp[i][j] =原创 2016-05-17 12:31:12 · 615 阅读 · 0 评论 -
hdoj--1950 Bridging signals(LIS with O(nlogn))
hdoj 1950题解O(n2)O(n^2)的LIS会TLE,见识到O(nlogn)O(nlogn)解法的LIS.#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int maxn = 40000 + 10;int p[maxn], dp[maxn];int n, t;原创 2016-03-12 11:40:55 · 386 阅读 · 0 评论 -
UVa--437 The Tower of Babylon(dp)
UVa 437题意有 n 种长方体石块(x,y,zx, y, z),每种无限多,从中挑选一些石块组成一座塔,要求每个长方体石块的底面长宽严格小于它下方石块的底面长宽。每个石块可以任意选择一边作为高。求塔的最大高度。n<=30n <= 30.题解每个长方体可以有6种形态,那么这 n 种长方体共有 6n6n种形态,两种形态的长方体的底面长宽是一个二元关系,因此,这便转化为一个DAG图,和矩形嵌套类似。#原创 2016-02-28 14:51:45 · 365 阅读 · 0 评论 -
nyoj--49 开心的小明(dp)
nyoj 49题解如果把 N 视为一个背包的容量,m 为物品数,viv_i,wi∗viw_i * v_i分别为物品的体积和价值,这便是一个纯粹的01背包问题。 用滚动数组计算。#include <iostream>#include <cstdio>#include <string>#include <fstream>#include <cstring>#include <algorith原创 2016-02-27 22:10:24 · 431 阅读 · 0 评论 -
nyoj--37 回文字符串(dp)
nyoj 37题意给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符串。题解设源字符串为 src,将其反转得到 rev,计算 src 和 rev 的最长公共子序列长度 L,则答案为 源字符串长度减去 L.#include <iostream>#include <cstdio>#include <string>#include <cstring>#inclu原创 2016-02-27 19:08:26 · 338 阅读 · 0 评论 -
PAT--1045. Favorite Color Stripe(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 parts原创 2017-08-09 23:47:48 · 426 阅读 · 0 评论