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 ...
随机推荐
- vue实现多级弹窗
webpack + vue 实现 弹窗功能 对于刚入门webpack + vue 不久的新人来说,这技术,确实有些不太友好,相比较于直接操纵dom元素的jQuery,直接操纵数据的 vue 在webp ...
- Android内存分析工具
在Android系统开发过程中,经常会要去分析进程的内存的使用情况,简单介绍下Android内存分析的相关工具. 文章参考: 1.dumpsys 2.memory-analysis-command 1 ...
- python网络编程之进程
一.什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实 ...
- Go语言中的运算符
## 1 概述Go语言提供了,算术,关系,逻辑,位,指针,赋值运算符.本篇整体说明一下. ## 2 算术运算 * \+ 相加* \- 相减* \* 相乘* / 相除* % 求余* ++ 自增* \-\ ...
- 在线接口文档工具——ShowDoc
ShowDoc:https://www.showdoc.cc/ --待更.
- 北京Uber优步司机奖励政策(12月15日)
用户组:人民优步及电动车(适用于12月15日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...
- 北京Uber优步司机奖励政策(高峰期5倍奖励)(12月7日)
用户组:人民优步及电动车(适用于12月7日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http ...
- 杭州优步uber司机第二组奖励政策
-8月9日更新- 优步杭州第二组: 定义为激活时间在2015/6/8之后2015/8/3之前的车主(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com ...
- spring data jap操作
package com.example.demo; import com.example.entity.UserJ; import com.example.respository.UserJRespo ...
- macOS 10.13 High Sierra PHP开发环境配置
命令:sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems/MySQLCOM ...