动态规划2

1、树形DP

2、概率DP

3、区间DP

模板

 for (int len = ; len < n; len++) { //操作区间的长度
for (int i = , j = len; j <= n; i++, j++) { //始末
//检查是否匹配(非必须)
for (int s = i; s < j; s++) {
//update
}
}
}

石子归并

 #include <cstdio>
#define min(x, y) (x > y ? y : x)
#define INF 0x3f3f3f3f
using namespace std; const int maxn = ;
int dp[maxn][maxn];
int sum[maxn];
int a[maxn]; int main(int argc, const char * argv[]) { int n;
while (~scanf("%d", &n)) {
for (int i = ; i <= n; i++) {
scanf("%d", &a[i]);
sum[i] = sum[i - ] + a[i];
}
for (int len = ; len < n; len++) { //操作区间的长度
for (int i = , j = len + ; j <= n; i++, j++) { //始末
//检查是否匹配(非必须)
dp[i][j] = INF;
for (int s = i; s < j; s++) {
dp[i][j] = min(dp[i][j], dp[i][s] + dp[s + ][j] + sum[j] - sum[i - ]);
}
}
}
printf("%d\n", dp[][n]);
}
return ;
}

4、状态DP

练习题

E - Bag of mice

The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance.

They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning?

If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one.

Input

The only line of input data contains two integers w and b (0 ≤ w, b ≤ 1000).

Output

Output the probability of the princess winning. The
answer is considered to be correct if its absolute or relative error
does not exceed 10 - 9.

Examples

Input
1 3
Output
0.500000000
Input
5 5
Output
0.658730159

Note

Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag — one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; const int maxn = ;
double dp[maxn][maxn]; int main()
{
int w,b;
scanf("%d %d",&w,&b);
memset(dp,,sizeof dp);
for(int i=;i<=b;i++)
dp[][i]=;
for(int i=;i<=w;i++)
dp[i][]=;
for(int i=;i<=w;i++)
{
for(int j=;j<=b;j++)
{
dp[i][j]+=(double)i/(i+j);
if(j>=)
{
dp[i][j]+=(double)j/(j+i)*((double)(j-)/(i+j-))*((double)(j-)/(i+j-))*dp[i][j-];
}
if(j>=)
{
dp[i][j]+=((double)j/(j+i))*((double)(j-)/(i+j-))*((double)(i)/(i+j-))*dp[i-][j-];
}
}
}
printf("%.9lf\n",dp[w][b]);
return ;
}

F - Brackets

We give the following inductive definition of a “regular brackets” sequence:

  • the empty sequence is a regular brackets sequence,
  • if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
  • if a and b are regular brackets sequences, then ab is a regular brackets sequence.
  • no other sequence is a regular brackets sequence

For instance, all of the following character sequences are regular brackets sequences:

(), [], (()), ()[], ()[()]

while the following character sequences are not:

(, ], )(, ([)], ([(]

Given a brackets sequence of characters a1a2an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, im where 1 ≤ i1 < i2 < … < imn, ai1ai2 … aim is a regular brackets sequence.

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

Input

The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (, ), [, and ]; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.

Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.

Sample Input

((()))
()()()
([]])
)[)(
([][][)
end

Sample Output

6
6
4
0
6
 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f using namespace std; const int maxn = ;
int dp[maxn][maxn];
int sum[maxn];
int a[maxn]; int main()
{
char s[];
while(~scanf("%s",&s) && strcmp("end",s)!=)
{
int s1=strlen(s);
memset(dp,,sizeof(dp));
for(int i=;i<s1;i++)
dp[i][i]=;
for(int l=;l<s1;l++)
{
for(int i=;i<s1-l;i++)
{
int j=i+l;
dp[i][j]=INF;
if((s[i]=='('&&s[j]==')') ||(s[i]=='['&&s[j]==']'))
{
dp[i][j]=dp[i+][j-];
}
for(int k=i;k<j;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+][j]);
} }
}
printf("%d\n",s1-dp[][s1-]); }
return ;
}

G - Football

Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2n. In each round of the tournament, all teams still in the tournament are placed in a list in order of increasing index. Then, the first team in the list plays the second team, the third team plays the fourth team, etc. The winners of these matches advance to the next round, and the losers are eliminated. After n rounds, only one team remains undefeated; this team is declared the winner.

Given a matrix P = [pij] such that pij is the probability that team i will beat team j in a match determine which team is most likely to win the tournament.

Input

The input test file will contain multiple test cases. Each test case will begin with a single line containing n (1 ≤ n ≤ 7). The next 2n lines each contain 2n values; here, the jth value on the ith line represents pij. The matrix P will satisfy the constraints that pij = 1.0 − pji for all ij, and pii = 0.0 for all i. The end-of-file is denoted by a single line containing the number −1. Note that each of the matrix entries in this problem is given as a floating-point value. To avoid precision problems, make sure that you use either the double data type instead of float.

Output

The output file should contain a single line for each test case indicating the number of the team most likely to win. To prevent floating-point precision issues, it is guaranteed that the difference in win probability for the top two teams will be at least 0.01.

Sample Input

2
0.0 0.1 0.2 0.3
0.9 0.0 0.4 0.5
0.8 0.6 0.0 0.6
0.7 0.5 0.4 0.0
-1

Sample Output

2

Hint

In the test case above, teams 1 and 2 and teams 3 and 4 play against each other in the first round; the winners of each match then play to determine the winner of the tournament. The probability that team 2 wins the tournament in this case is:

P(2 wins)  = P(2 beats 1)P(3 beats 4)P(2 beats 3) + P(2 beats 1)P(4 beats 3)P(2 beats 4)
= p21p34p23 + p21p43p24
= 0.9 · 0.6 · 0.4 + 0.9 · 0.4 · 0.5 = 0.396.

The next most likely team to win is team 3, with a 0.372 probability of winning the tournament.

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f using namespace std; const int maxn = ;
double dp[][maxn];
int sum[maxn];
double a[maxn][maxn]; int main()
{
int n;
while(~scanf("%d",&n) && n!=-){
int m=<<n;
int s1=<<(n-);
for(int i=;i<m;i++)
{
for(int j=;j<m;j++)
{
scanf("%lf",&a[i][j]);
}
}
for(int i=;i<m;i++)
dp[][i]=;
for(int i=;i<=n;i++)
{
for(int j=;j<m;j++)
{
int t=j/(<<(i-));
t^=;
dp[i][j]=;
for(int k=t*(<<(i-));k<t*(<<(i-))+(<<(i-));k++)
{
dp[i][j]+=dp[i-][j]*dp[i-][k]*a[j][k];
}
}
}
int ans;
double temp=;
for(int i=;i<m;i++)
{
if(dp[n][i]>temp)
{
ans=i;
temp=dp[n][i];
}
} printf("%d\n",ans+);
}
return ;
}

C - 区间dp*2

现在有n堆石子,第i堆有ai个石子。现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数。求合并所有石子的最小代价。

Input

第一行包含一个整数T(T<=50),表示数据组数。
每组数据第一行包含一个整数n(2<=n<=100),表示石子的堆数。
第二行包含n个正整数ai(ai<=100),表示每堆石子的石子数。

Output

每组数据仅一行,表示最小合并代价。

Sample Input

2
4
1 2 3 4
5
3 5 2 1 4

Sample Output

19
33

Hint

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits> using namespace std; const int maxn = ;
int dp[maxn][maxn],a[maxn],sum[maxn]; int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i = ;i <= n;i++){
scanf("%d",&a[i]);
sum[i] = sum[i - ] + a[i];
}
memset(dp,,sizeof(dp));
for(int l = ;l <= n;l++){
for(int i = ;i <= n - l;i++){
int j = i + l;
dp[i][j] = INT_MAX;
for(int k = i;k <= j - ;k++){
dp[i][j] = min(dp[i][j],dp[i][k] + dp[k + ][j] + sum[j] - sum[i - ]);
}
}
}
printf("%d\n",dp[][n]);
}
return ;
}

ACM 第十天的更多相关文章

  1. ACM 第十九天

    积性函数 积性函数线性筛,筛素数,u(n),欧拉函数: vis[]=vis[]=,mu[]=,phi[]=; ;i<=N;++i){ ,phi[i]=i-,prime[++cnt]=i; ,k= ...

  2. ACM 第十六天

    计算几何 练习题: F - Beauty Contest POJ - 2187 Bessie, Farmer John's prize cow, has just won first place in ...

  3. ACM 第十五天

    计算几何基础 练习题 C - Wasted Time Mr. Scrooge, a very busy man, decided to count the time he wastes on all ...

  4. ACM 第十四天

    字符串: 1.KMP算法(模式串达到1e6) 模式串达到1e4直接暴力即可. 字符串哈希 字符串Hash的种类还是有很多种的,不过在信息学竞赛中只会用到一种名为“BKDR Hash”的字符串Hash算 ...

  5. 蚂蚁金服合作的RISE实验室到底有多牛?

    近日,蚂蚁金服与美国加州伯克利大学近期新成立的RISE实验室达成合作意向.RISE实验室的前身是著名伯克利AMP实验室,主导研发了当今大数据计算领域最前沿的开源系统:Apache Spark.Apac ...

  6. 从ACM会议分析我国计算机科学近十年发展情况

    从ACM会议分析我国计算机科学近十年发展情况 来源:<中国计算机学会通讯>2015年第10期<专栏> 作者:陈 钢 2006年,承蒙李国杰院士推荐,<中国计算机学会通讯& ...

  7. 山东省第十届ACM省赛参赛后的学期总结

    5.11,5.12两天的济南之旅结束了,我也参加了人生中第一次正式的acm比赛,虽然是以友情队的身份,但是我依旧十分兴奋. 其实一直想写博客来增加自己的能力的,但是一直拖到现在,正赶上老师要求写一份总 ...

  8. 西南科技大学第十届ACM程序设计竞赛题解

    A.德州扑克 B. 我恨11(1089) 问题描述 11是一个孤独的数字,小明十分讨厌这个数字,因此如果哪个数字中出现了11或者该数字是11的倍数,他同样讨厌这个数字.现在问题来了,在闭区间[L,R] ...

  9. 湖南大学第十四届ACM程序设计新生杯(重现赛)I:II play with GG(博弈论||DP)

    链接:https://ac.nowcoder.com/acm/contest/338/I 来源:牛客网 题目描述 IG won the S championship and many people a ...

随机推荐

  1. go加密算法:CBC对称加密(一)--DES

    package main import ( "bytes" //"crypto/aes" "crypto/cipher" "cry ...

  2. Python-知识点小计

    1.python赋值.浅拷贝.深拷贝区别:https://www.cnblogs.com/xueli/p/4952063.html: 2.python的hasattr(),getattr(),sett ...

  3. Spark运行模式_spark自带cluster manager的standalone cluster模式(集群)

    这种运行模式和"Spark自带Cluster Manager的Standalone Client模式(集群)"还是有很大的区别的.使用如下命令执行应用程序(前提是已经启动了spar ...

  4. 『Python题库 - 简答题』 Python中的基本概念 (121道)

    ## 『Python题库 - 简答题』 Python中的基本概念 1. Python和Java.PHP.C.C#.C++等其他语言的对比? 2. 简述解释型和编译型编程语言? 3. 代码中要修改不可变 ...

  5. Django模板语言与视图(view)

    常用语法 {{  }}和{% %} 变量相关的用{{}} , 逻辑相关的用{% %} 变量 在Django的模板语言中按此语法使用:{{ 变量名 }}. 当模版引擎遇到一个变量,它将计算这个变量,然后 ...

  6. C语言实验报告(四)完全数

    完全数,又称完美数或者完备数.是一些特殊的自然数.它所有的真因子的和,恰好等于它本身.编程找出1000以内的所有完全数,并输出该数成为完全数的因子. (例如6=1+2+3.按照6,its factor ...

  7. C指针(4)——数据结构中指针的应用(非常重要)

    5-1动态内存分配,分配的是堆内存的空间 分配内存函数 (都集中在库函数 stdlib.h  中) void *malloc (unsigned int num_bytes); //指定分配内存空间大 ...

  8. HyperLedger Fabric 1.4 基础环境搭建(7)

    学习了前面几章理论知识后,本章开始介绍实践操作,先介绍Fabric基础环境搭建,采用的操作系统为Centos 7 64位,依次介绍Docker安装.Docker-Compose安装.GO语言环境安装. ...

  9. 【8086汇编-Day5】第三次实验

    练习一 结果展示: 这个程序执行下来貌似打印了数字36,但其实是两个数字3.6: 这段就是用来打印3:首先ah里放2,跟后面的int 21h结合起来就是调用21号中断例程的2号子程序,用来打印到标准输 ...

  10. JDK核心源码(2)

    Java的基础知识有很多,但是我认为最基础的知识应该要属jdk的基础代码, jdk的基础代码里面,有分了很多基础模块,其中又属jdk包下面的lang包最为基础. 我们下面将总结和分析一下lang包下面 ...