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 ...
随机推荐
- 【Spark】Spark性能优化之Whole-stage code generation
一.技术背景 Spark1.x版本中执行SQL语句,使用的是一种最经典,最流行的查询求职策略,该策略主要基于 Volcano Iterator Model(火山迭代模型).一个查询会包含多个Opera ...
- 典型的 ajax 异步请求及错误处理
$.ajax({ url: path + '/emergency/saveEmergency.do', async: false,//同步,会阻塞操作 type: 'POST',//PUT DELET ...
- stm32串口发送数据复位 第一个数据丢失
http://blog.csdn.net/kevinhg/article/details/40991655 STM32串口发送必须先检测状态,否则第一个字节无法发出,发送完毕,必须检测发送状态是否完成 ...
- 笔记-python-float(‘inf’)
笔记-python-float(‘inf’) 看算法时发现了flaot(‘inf’). Python中可以用如下方式表示正负无穷: float("inf"), float(&quo ...
- 20145202mc《计算机病毒》实践2
网站检测 http://www.virustotal.com太慢了实在,所以我换成了http://www.virscan.org/ lab01-01.exe 文件行为 lab01-01.dll 可以基 ...
- spring作用
在SSH框假中spring充当了管理容器的角色.我们都知道hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句.Struts是用来 ...
- 成都Uber优步司机奖励政策(4月1日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Java:二进制(原码、反码、补码)与位运算
一.二进制(原码.反码.补码) 二进制的最高位是符号位(“0”代表正数,“1”代表负数): Java中没有无符号数: 计算机以整数的补码进行运算: 1. 原码:将一个整数转换成二进制表示 以 int ...
- 三 Hive 数据处理 自定义函数UDF和Transform
三 Hive 自定义函数UDF和Transform 开篇提示: 快速链接beeline的方式: ./beeline -u jdbc:hive2://hadoop1:10000 -n hadoop 1 ...
- iOS 开发库相关(持续更新)
01-给任意view添加毛玻璃效果 https://github.com/JagCesar/iOS-blur 02-浮动式的textfield输入框(可用于登录界面) https://github ...