动态规划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. Spring security学习笔记(二)

    对比两种承载认证信息的方式: session vs token token验证方案: session验证方案: session即会话是将用户信息保存在服务端,根据请求携带的session_id,从服务 ...

  2. 小白CSS学习日记-----杂乱无序记录(3)

    1.后代选择器 .antzone li { } class='antzone' 所有子孙后代中的li   2.子选择器 .antzone > li { } class='antzone' 的子一 ...

  3. 20190121-n个人围成一圈,凡报到3的人退出圈子,最后留下的是原来第几号的那位

    1. 报数问题:有n个人围成一圈,顺序排号.从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位 思路:此题主要问题在于但凡报到3的人退出圈子,而报数的号码与圈子的 ...

  4. Python学习 :正则表达式

    正则表达式 python 使用正则表达式(re)来进行匹配引擎搜索 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串” 关于正则表达式 ...

  5. Qt :undefined reference to vtable for "xxx::xxx"

    现象: 类加上宏 Q_OBJECT 就会报错 :undefined reference to vtable for "xxx::xxx" 解决方法: 重新 qmake 其他情况,查 ...

  6. 【EXCEL】簡単に合計をとる方法

    下記のような表があるとして.合計を取るときみんなSUM関数を使用しています. その方法もよいですが.もっと簡単の方法を説明します. ①合計する部分を選択します. ②ALT+=を押します. ※ノートパソ ...

  7. postgresql 数据库schema 复制

    ------ --- 导出 pg_dump -h *.*.*.* -p 5432 -d you_databasename -n you_schema -f you_sqlfile.sql ---- 替 ...

  8. 构建工具——maven的补充

    1.安装jar到本地仓库 有时候有部分jar由于在maven的中央仓库,只能引用本地的,可以将jar安装到本地仓库进行操作(请先确保mvn命令可以正常运行) mvn install:install-f ...

  9. 【调试】Linux下超强内存检测工具Valgrind

    [调试]Linux下超强内存检测工具Valgrind 内容简介 Valgrind是什么? Valgrind的使用 Valgrind详细教程 1. Valgrind是什么? Valgrind是一套Lin ...

  10. netty之编解码

    1.netty的编码和解码,在数据传输的时候,考虑数据安全,数据完整性都是很有必要的.这里主要是介绍netty3和netty5的编解码方式.其实从StringEncoder和StringDecoder ...