ACM 第十天
动态规划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.
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
1 3
0.500000000
5 5
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 a1a2 … an, 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 < … < im ≤ n, 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 i ≠ j, 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 第十天的更多相关文章
- ACM 第十九天
积性函数 积性函数线性筛,筛素数,u(n),欧拉函数: vis[]=vis[]=,mu[]=,phi[]=; ;i<=N;++i){ ,phi[i]=i-,prime[++cnt]=i; ,k= ...
- ACM 第十六天
计算几何 练习题: F - Beauty Contest POJ - 2187 Bessie, Farmer John's prize cow, has just won first place in ...
- ACM 第十五天
计算几何基础 练习题 C - Wasted Time Mr. Scrooge, a very busy man, decided to count the time he wastes on all ...
- ACM 第十四天
字符串: 1.KMP算法(模式串达到1e6) 模式串达到1e4直接暴力即可. 字符串哈希 字符串Hash的种类还是有很多种的,不过在信息学竞赛中只会用到一种名为“BKDR Hash”的字符串Hash算 ...
- 蚂蚁金服合作的RISE实验室到底有多牛?
近日,蚂蚁金服与美国加州伯克利大学近期新成立的RISE实验室达成合作意向.RISE实验室的前身是著名伯克利AMP实验室,主导研发了当今大数据计算领域最前沿的开源系统:Apache Spark.Apac ...
- 从ACM会议分析我国计算机科学近十年发展情况
从ACM会议分析我国计算机科学近十年发展情况 来源:<中国计算机学会通讯>2015年第10期<专栏> 作者:陈 钢 2006年,承蒙李国杰院士推荐,<中国计算机学会通讯& ...
- 山东省第十届ACM省赛参赛后的学期总结
5.11,5.12两天的济南之旅结束了,我也参加了人生中第一次正式的acm比赛,虽然是以友情队的身份,但是我依旧十分兴奋. 其实一直想写博客来增加自己的能力的,但是一直拖到现在,正赶上老师要求写一份总 ...
- 西南科技大学第十届ACM程序设计竞赛题解
A.德州扑克 B. 我恨11(1089) 问题描述 11是一个孤独的数字,小明十分讨厌这个数字,因此如果哪个数字中出现了11或者该数字是11的倍数,他同样讨厌这个数字.现在问题来了,在闭区间[L,R] ...
- 湖南大学第十四届ACM程序设计新生杯(重现赛)I:II play with GG(博弈论||DP)
链接:https://ac.nowcoder.com/acm/contest/338/I 来源:牛客网 题目描述 IG won the S championship and many people a ...
随机推荐
- PHP Mysql字符集utf8mb4支持Emoji表情
项目开发中经常会遇到用户在评论或者发表文章的时候会打一些表情在里面,如果我们在开发中不去做一些处理的话,表情会出不来的,甚至是报错,下面简单介绍处理方式.原文地址:小时刻个人博客:http://sma ...
- FireDAC的SQLite初探
// uses FireDAC.VCLUI.Wait 之后, 可不用添加 TFDGUIxWaitCursor TFDConnection // 数据连接 TFDQuery ...
- day 20 约束 异常处理 MD5
1.类的约束(重点): 写一个父类. 父类中的某个方法要抛出一个异常 NotImplementError # 项目经理 class Base: # 对子类进行了约束. 必须重写该方法 ...
- HBase学习(一):认识HBase
一.大数据发展背景 现今是数据飞速膨胀的大数据时代,大数据强调3V特征,即Volume(量级).Varity(种类)和Velocity(速度). ·Volume(量级):TB到ZB. ·Varity( ...
- python学习笔记:第11天 闭包及迭代器
目录 1. 函数名的使用 2. 闭包 3. 迭代器 1. 函数名的使用 其实函数名也是一个变量,但它是一个比较特殊的变量,与小括号配合可以执行函数的变量: 函数名其实和内存一样,也可以使用print查 ...
- ruby中的extend 和 include
include include是把module中定义的instance_method给mixin,然后当做类的实例方法使用(是因为module本身不能使用module的实例方法),给类进行实例化一个对 ...
- java中有关Volatile的几个小题
1.java中能创建volatile数组吗? 能,java中可以创建volatile数组,不过只是一个指向数组的引用,而不是整个数组,如果改变引用指向的数组,将会受到volatile的保护,但是如果多 ...
- C语言:一个数组中只有两个数字是出现一次
//1.一个数组中只有两个数字是出现一次, //其他所有数字都出现了两次. //找出这两个数字,编程实现.a //^=单独两个数的^结果 //单独出现的两个数不同位的标记 //position: ^结 ...
- Fedora 下面安装FTP服务
1. yum install vsftpd 2. systemctl disable vsftpd.service 3. systemctl stop vsftpd.service 4. system ...
- gp与 pg 查询进程
select now()-query_start as cost_time,* from pg_stat_activity where current_query not in ( '<IDLE ...