ACM-ICPC 2018 焦作赛区网络预赛 B Mathematical Curse(DP)
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)的更多相关文章
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...
- 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 ...
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies
There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more inte ...
随机推荐
- 数字平滑 前端插件JS&CSS库
CDN DEMO 拷贝可用: <!DOCTYPE html> <link rel="stylesheet" href="https://cdn.boot ...
- Haproxy 优化
Haproxy 自身健康检查vi /usr/local/haproxy/sbin/check_haproxy.sh #!/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/b ...
- Before NOIP 2018
目录 总结 刷题 2018 - 9 - 24 2018 - 9 - 25 2018 - 9 - 26 2018 - 9 - 27 2018 - 9 - 28 2018 - 9 - 29 2018 - ...
- Android 性能优化提示
原文 http://developer.android.com/guide/practices/design/performance.html 性能优化 Android应用程序运行的移动设备受限于其运 ...
- SP8791 DYNALCA - Dynamic LCA 解题报告
SP8791 DYNALCA - Dynamic LCA 有一个森林最初由 \(n (1 \le n \le 100000)\) 个互不相连的点构成 你需要处理以下操作: link A B:添加从顶点 ...
- [ZJOI2010]贪吃的老鼠(网络流+建图)
题目描述 奶酪店里最近出现了m只老鼠!它们的目标就是把生产出来的所有奶酪都吃掉.奶酪店中一天会生产n块奶酪,其中第i块的大小为pi,会在第ri秒被生产出来,并且必须在第di秒之前将它吃掉.第j只老鼠吃 ...
- Mysql 系统表
Information_schema: 1. tables 提供表信息: 表所属数据库,表名,表类型,行数,最大自增数等等.
- print
说一说这个print函数,我们经常使用,但有一些细节却往往错过了 print print()输出会换行是因为默认end="\n" 想要不换行,且覆盖 print("\r第 ...
- 关于overflow-x: hidden隐藏滚动条失效的解决方案
在苦逼写页面的时候,发现有种情况overflow-x: hidden失效了,chrome表现完好,qq浏览器有问题,微信上面展示有问题. 微信上面展示是一样能够滑动的. 如果出现类似问题的同学,请试一 ...
- Spring Data JPA Batch Insertion
转自:https://www.jeejava.com/spring-data-jpa-batch-insertion/ Spring Data JPA Batch Insertion will sho ...