例题

#A 传纸条(Accepted)
    #B 乘积最大 (Unaccepted)
    #C 石子合并 (Accepted)
    #D 加分二叉树 (Unaccepted)
    #E 没有上司的舞会(Unaccepted)
    #F 选课 (Accepted)
    #G 警卫安排 (Unaccepted)
    #H 通向自由的钥匙 (Unaccepted)

#I 导弹拦截 (Unaccepted)
    #J [HAOI2010]最长公共子序列 (Unaccepted)
    #K 排列LCS问题 (Unaccepted)
    #L 尼克的任务(Accepted)
    #M 多米诺骨牌(Accepted)
    #N 最大子树和(Unaccepted)
    #O “访问”美术馆(Unaccepted)
    #P 石子合并(Accepted)
    #Q 关路灯(Unaccepted)
    #R M国王(Unaccepted)
    #S 愤怒的小鸟(Unaccepted)


1. LCS 最长公共子序列

/* LCS
* Au: GG
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1005;
int n, m, d[N][N], A[N], B[N]; int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
for (int i = 1; i <= m; i++) scanf("%d", &B[i]); for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
d[i][j] = max(d[i - 1][j], d[i][j - 1]);
if (A[i] == B[j])
d[i][j] = max(d[i][j], d[i - 1][j - 1] + 1);
}
} printf("%d\n", d[n][m]);
return 0;
}

2. LIS 最长上升自序列

/**
* LIS
* Au: GG
**/ #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 1000000 + 3;
int n, a[N], d[N];
int ans; int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
} for (int i = 1; i <= n; i++) {
d[i] = 1;
for (int j = 1; j < i; j++) {
if (a[i] > a[j] && d[j] + 1 > d[i])
d[i] = d[j] + 1;
}
ans = max(ans, d[i]);
} printf("%d\n", ans);
return 0;
}

3. 01 背包

// 01 Knapsack
// Au: GG #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std; int n, v, w[33], f[33][20003]; int main() {
scanf("%d%d", &v, &n);
for (int i = 1; i <= n; i++) scanf("%d", &w[i]); for (int i = 1; i <= n; i++) {
for (int j = 1; j <= v; j++) {
if (j - w[i] < 0) f[i][j] = f[i - 1][j];
else f[i][j] = max(f[i - 1][j - w[i]] + w[i], f[i - 1][j]);
}
} printf("%d", v - f[n][v]); return 0;
}

4. 完全背包

/**
* Luogu P1616 疯狂的采药
* Au: GG
**/ #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m, d[100000+4], w[100000+3], v[100000+3]; int main() {
scanf("%d%d", &m, &n);
for (int i = 1; i <= n; i++) scanf("%d%d", &v[i], &w[i]); for (int i = 1; i <= n; i++) {
for (int j = v[i]; j <= m; j++) {
d[j] = max(d[j], d[j - v[i]] + w[i]);
}
} printf("%d\n", d[m]);
return 0;
}

5. 多维背包

/**
* Luogu P1855 榨取kkksc03
* Au: GG
**/ #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 100 + 3, M = 200 + 3;
int n, m, t, time[N], w[N], d[N][M][M]; int main() {
scanf("%d%d%d", &n, &m, &t);
for (int i = 1; i <= n; i++) scanf("%d%d", &time[i], &w[i]); for (int i = 1; i <= n; i++) {
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= t; k++) {
d[i][j][k] = d[i - 1][j][k];
if (j - time[i] >= 0 && k - w[i] >= 0 && d[i - 1][j - time[i]][k - w[i]] + 1 > d[i][j][k])
d[i][j][k] = d[i - 1][j - time[i]][k - w[i]] + 1;
}
}
} printf("%d\n", d[n][m][t]);
return 0;
}

6. 树形 DP (Unaccepted)

7. 区间 DP

/* Luogu P1880 石子合并
* Au: GG
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100 + 5;
const int inf = 2147483647;
int n, d[2 * N][2 * N], e[2 * N][2 * N], a[N], sum[2 * N];
int ans1 = inf, ans2 = - inf; int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + a[i > n ? i % n : i]; for (int i = 1; i <= 2 * n; i++) d[i][i] = 0;
for (int len = 2; len <= n; len++) {
for (int i = 1; i + len - 1 <= 2 * n; i++) {
int j = i + len - 1;
d[i][j] = inf;
for (int k = i; k < j; k++) {
d[i][j] = min(d[i][j], d[i][k] + d[k + 1][j] + sum[j] - sum[i - 1]);
}
}
}
for (int i = 1; i <= n; i++) ans1 = min(ans1, d[i][i + n - 1]); for (int i = 1; i <= 2 * n; i++) e[i][i] = 0;
for (int len = 2; len <= n; len++) {
for (int i = 1; i + len - 1 < 2 * n; i++) {
int j = i + len - 1;
e[i][j] = - inf;
for (int k = i; k < j; k++) {
e[i][j] = max(e[i][j], e[i][k] + e[k + 1][j] + sum[j] - sum[i - 1]);
}
}
} for (int i = 1; i <= n; i++) ans2 = max(ans2, e[i][i + n - 1]); printf("%d\n%d\n", ans1, ans2); return 0;
}

8. 状态压缩 DP (Unaccepted)

动态规划 List的更多相关文章

  1. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

  2. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  3. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  4. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  5. C#动态规划查找两个字符串最大子串

     //动态规划查找两个字符串最大子串         public static string lcs(string word1, string word2)         {            ...

  6. C#递归、动态规划计算斐波那契数列

    //递归         public static long recurFib(int num)         {             if (num < 2)              ...

  7. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  8. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  10. hdu FatMouse's Speed 动态规划DP

    动态规划的解决方法是找到动态转移方程. 题目地址:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3&sectionid ...

随机推荐

  1. python 正则表达式 re.match

    #coding:utf-8 import re #匹配内容:单词+空格+单词+任意字符 #\w 单词字符[A-Za-z0-9_] #(?P<name>...) 分组,除了原有的编号外在指定 ...

  2. mysql续集(查询部分)

    mysql查询部分,从基础的查询到关键字,where子句,group by, order by, limit ,having,子查询分为from子查询和where子查询,左连接和右连接,内连接的连表查 ...

  3. jsp常问面试题集

    1.Servlet总结 在Java Web程序中,Servlet主要负责接收用户请求 HttpServletRequest,在doGet(),doPost()中做相应的处理,并将回应HttpServl ...

  4. [BNDSOJ] 小P的数列代码

    感谢gjznb大佬的帮助Orz #include<bits/stdc++.h> using namespace std; ; vector<int> dp[N][N]; ]; ...

  5. ssh1

    #coding=utf-8 import paramiko ssh = paramiko.SSHClient() #允许链接不在linux上.ssh文件中不在known_hosts文件中的主机 ssh ...

  6. 攻防世界--The_Maya_Society

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/17574fc423474b93a0e6e6a6e583e003.zip 我们直接将Li ...

  7. 什么是服务端伪造(SSRF)

    什么是服务端伪造(SSRF) 原文:GitHub Pages and Single-Page Apps 译者:neal1991 welcome to star my articles-translat ...

  8. sqoop简单使用

    一,通过sqoop将MySQL里面的数据加载到HDFS 先查看有哪些数据库 查看表person sqoop list-databases --connect jdbc:mysql://ly-p2p4: ...

  9. 线程池-连接池-JDBC实例-JDBC连接池技术

    线程池和连接池   线程池的原理:     来看一下线程池究竟是怎么一回事?其实线程池的原理很简单,类似于操作系统中的缓冲区的概念,它的流程如下:先启动若干数量的线程,并让这些线程都处于睡眠状态,当客 ...

  10. sigprocmask()函数学习笔记

    sigprocmask()函数用于改变进程的当前阻塞信号集,也可以用来检测当前进程的信号掩码. 函数原型: int sigprocmask(int how, const sigset_t *restr ...