https://nanti.jisuanke.com/t/31711

题意

m个符号必须按顺序全用,n个房间需顺序选择,有个初始值,问最后得到的值最大是多少。

分析

如果要求出最大解,维护最大值是不能得到的,因为有负数的参与,所以我们维护最大值和最小值。不管那么多,反正答案肯定是由极值产生的。

定义dp1[i][j]为用了i个符号,走了j间房后的最大值。因而dp2[][]就是对应的最小值。然后按要求转移。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f;
ll dp1[][],dp2[][];
int a[];
char f[];
int main(){
int n,m,k;
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
scanf("%s",f+);
//puts(f+1);
memset(dp1,-inf,sizeof(dp1));
memset(dp2,inf,sizeof(dp2));
for(int j=;j<=n;j++) dp1[][j]=dp2[][j]=k;
for(int i=;i<=m;i++){
for(int j=i;j<=n;j++){
dp1[i][j]=dp1[i][j-];
dp2[i][j]=dp2[i][j-];
if(f[i]=='+'){
dp1[i][j]=max(dp1[i][j],dp1[i-][j-]+a[j]);
dp2[i][j]=min(dp2[i][j],dp2[i-][j-]+a[j]);
}else if(f[i]=='-'){
dp1[i][j]=max(dp1[i][j],dp1[i-][j-]-a[j]);
dp2[i][j]=min(dp2[i][j],dp2[i-][j-]-a[j]);
}else if(f[i]=='*'){
dp1[i][j]=max(dp1[i][j],dp1[i-][j-]*a[j]);
dp1[i][j]=max(dp1[i][j],dp2[i-][j-]*a[j]);
dp2[i][j]=min(dp2[i][j],dp2[i-][j-]*a[j]);
dp2[i][j]=min(dp2[i][j],dp1[i-][j-]*a[j]);
}else{
dp1[i][j]=max(dp1[i][j],dp1[i-][j-]/a[j]);
dp1[i][j]=max(dp1[i][j],dp2[i-][j-]/a[j]);
dp2[i][j]=min(dp2[i][j],dp2[i-][j-]/a[j]);
dp2[i][j]=min(dp2[i][j],dp1[i-][j-]/a[j]);
}
}
}
printf("%lld\n",dp1[m][n]);
}
return ;
}

ACM-ICPC 2018 焦作赛区网络预赛 B Mathematical Curse(DP)的更多相关文章

  1. ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)

    There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...

  2. ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  3. ACM-ICPC 2018 焦作赛区网络预赛

    这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...

  4. ACM-ICPC 2018 焦作赛区网络预赛J题 Participate in E-sports

    Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...

  5. ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

    There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry th ...

  6. ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

    God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...

  7. ACM-ICPC 2018 焦作赛区网络预赛 I题 Save the Room

    Bob is a sorcerer. He lives in a cuboid room which has a length of AA, a width of BB and a height of ...

  8. ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

    Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring won ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies

    There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more inte ...

随机推荐

  1. codeforces 1065F Up and Down the Tree

    题目链接:codeforces 1065F Up and Down the Tree 题意:给出一棵树的节点数\(n\)以及一次移动的最大距离\(k\),现在有一个标记在根节点1处,每一次可以进行一下 ...

  2. 【支付宝】"验签出错,sign值与sign_type参数指定的签名类型不一致:sign_type参数值为RSA,您实际用的签名类型可能是RSA2"

    问题定位:从描述就可以看的出来了,你现在sign_type是  RSA类型的,要改成跟你现在用的签名类型一致的类型,也就是 要改为 RSA2 PHP为例 // 新版只支持此种签名方式 商户生成签名字符 ...

  3. JLOI2015 DAY1 简要题解

    「JLOI2015」有意义的字符串 题意 给你 \(b, d, n\) 求 \[ [(\frac{b + \sqrt d}2)^n] \mod 7528443412579576937 \] \(0 & ...

  4. [JSOI2008]Blue Mary的职员分配

    由于Blue Mary呕心沥血的管理,Blue Mary的网络公司蒸蒸日上.现在一共拥有了n名职员,可惜没有任何的金钱和声誉.平均每名每天职员都可以给公司带来x单位金钱或者y单位声誉(名利不能双全). ...

  5. springAop 使用@Around,@After等注解时,代码运行两边的问题

    springAop使用@Around,@After等注解时,代码运行两边的问题 将@Component注解删掉就好了

  6. java == 与 equals

    1.基本数据类型用"==" java的基本数据类型,也称为原始的数据类型.它们分别是: byte, short, char, int, long, float, double, b ...

  7. BZOJ2288 生日礼物

    本题是数据备份的进阶版. 首先去掉所有0,把连续的正数/负数连起来. 计算所有正数段的个数与总和. 然后考虑数据备份,有一点区别: 如果我们在数列中选出一个负数,相当于把它左右连起来. 选出一个正数, ...

  8. [luogu3834][可持久化线段树 1(主席树)]

    题目链接 思路 裸的主席树.查询的时候,通过相减求出区间内左子树中数的个数a.然后判断要查找的k是否比这个z要大.如果比这个值大,那么就去右子树中查找第k - z大,否则去左子树中查找第k大. 代码 ...

  9. linux:提取匹配含有小数点的数字(grep函数)

    学艺不精,一开始用了 “grep -ne '46.5743' file.txt” 提取含有46.5743的小数不成功,后面查资料才知道正则表达式中,小数点代表的是:一定有一个任意字节. 正确的写法应该 ...

  10. fcntl F_GETFL

    F_GETFL 我的理解是file get flag #include <stdio.h>#include <fcntl.h>#include <unistd.h> ...