ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse
A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics when he was young, and was entangled in some mathematical curses. He studied hard until he reached adulthood and decided to use his knowledge to escape the castle.
There are NN rooms from the place where he was imprisoned to the exit of the castle. In the i^{th}ith room, there is a wizard who has a resentment value of a[i]a[i]. The prince has MM curses, the j^{th}jth curse is f[j]f[j], and f[j]f[j] represents one of the four arithmetic operations, namely addition('+'
), subtraction('-'
), multiplication('*'
), and integer division('/'
). The prince's initial resentment value is KK. Entering a room and fighting with the wizard will eliminate a curse, but the prince's resentment value will become the result of the arithmetic operation f[j]f[j] with the wizard's resentment value. That is, if the prince eliminates the j^{th}jth curse in the i^{th}ith room, then his resentment value will change from xx to (x\ f[j]\ a[i]x f[j] a[i]), for example, when x=1, a[i]=2, f[j]=x=1,a[i]=2,f[j]='+'
, then xx will become 1+2=31+2=3.
Before the prince escapes from the castle, he must eliminate all the curses. He must go from a[1]a[1] to a[N]a[N] in order and cannot turn back. He must also eliminate the f[1]f[1] to f[M]f[M] curses in order(It is guaranteed that N\ge MN≥M). What is the maximum resentment value that the prince may have when he leaves the castle?
Input
The first line contains an integer T(1 \le T \le 1000)T(1≤T≤1000), which is the number of test cases.
For each test case, the first line contains three non-zero integers: N(1 \le N \le 1000), M(1 \le M \le 5)N(1≤N≤1000),M(1≤M≤5) and K(-1000 \le K \le 1000K(−1000≤K≤1000), the second line contains NN non-zero integers: a[1], a[2], ..., a[N](-1000 \le a[i] \le 1000)a[1],a[2],...,a[N](−1000≤a[i]≤1000), and the third line contains MM characters: f[1], f[2], ..., f[M](f[j] =f[1],f[2],...,f[M](f[j]='+','-','*','/'
, with no spaces in between.
Output
For each test case, output one line containing a single integer.
样例输入复制
3
2 1 5
2 3
/
3 2 1
1 2 3
++
4 4 5
1 2 3 4
+-*/
样例输出复制
2
6
3
题目来源
题解:DP;
分别记录最大值和最小值,(因为可能出现两个都是负数的情况),转移方程为:
dp[i][j] = dp[i - 1][j]; dp1[i][j] = dp1[i - 1][j];分别记录最大和最小
对于不同符号,有不同的转移方程
参考代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <bitset>
#define INF 0x3f3f3f3f3f3f3f3fll
#define clr(x, y) memset(x, y, sizeof(x))
#define mod 1000000007
using namespace std;
typedef long long LL;
const int maxn = ;
const int maxm = ;
int a[maxn],t;
LL dp[maxn][maxm],dp1[maxn][maxn];
char f[maxm];
LL Max(LL a,LL b) {return a>=b? a:b; }
LL Min(LL a,LL b) { return a<b? a:b; }
int main()
{
scanf("%d", &t);
while(t--)
{
memset(dp, -INF, sizeof dp);
memset(dp1,INF,sizeof dp1);
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
dp[i][] =dp1[i][]=k;
}
dp[][] = dp1[][] = k;
scanf("%s", f + );
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
if(i<j) continue;
dp[i][j] = dp[i - ][j]; dp1[i][j] = dp1[i - ][j];
if(f[j] == '+') dp[i][j]=Max(dp[i][j],dp[i - ][j - ] + a[i]), dp1[i][j]=Min(dp1[i][j],dp1[i - ][j - ] + a[i]);
else if(f[j] == '-') dp[i][j] = Max(dp[i][j], dp[i - ][j - ] - a[i]), dp1[i][j] = Min(dp1[i][j], dp1[i - ][j - ] - a[i]);
else if(f[j] == '*')
{
dp[i][j] = Max(dp[i][j], Max(dp[i - ][j - ] * a[i],dp1[i - ][j - ] * a[i]) );
dp1[i][j] = Min(dp1[i][j], Min(dp[i - ][j - ] * a[i],dp1[i - ][j - ] * a[i]) );
}
else
{
dp[i][j] = Max(dp[i][j], Max(dp[i - ][j - ] / a[i], dp1[i - ][j - ] / a[i]) );
dp1[i][j]=Min(dp1[i][j], Min(dp[i - ][j - ] / a[i], dp1[i - ][j - ] / a[i]) );
}
}
}
printf("%lld\n", Max(dp[n][m],dp1[n][m]) );
}
return ;
}
ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse的更多相关文章
- 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 ...
- 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 ...
随机推荐
- pip的简单用法
pip的用法: 其实跟linux的yum很像,它可以帮我们安装python所需要的环境包,并且可以包解决依赖关系 eg: 列出已安装的包 pip list 安装要安装的包 pip install xx ...
- saprk性能调优参考
1.Tuning Spark 文档 原文:http://spark.apache.org/docs/latest/tuning.html 翻译参考:https://www.cnblogs.com/lh ...
- rabittmq详解
交换机(exchange): 声明交换机: Name Durability (消息代理重启后,交换机是否还存在) Auto-delete (当所有与之绑定的消息队列都完成了对此交换机的使用后,删掉它) ...
- 【SpringBoot | Druid】SpringBoot整合Druid
SpringBoot整合Druid Druid是个十分强大的后端管理工具,具体的功能和用途请问阿里爸爸 1. 在pom.xml中导入包 <!-- alibaba 的druid数据库连接池 --& ...
- linux下制作linux系统盘(光盘、U盘)
cdrecord制作启动光盘 首先cdrecord -scanbus输出设备列表和标识,(我的此次为5,0,0) [ˈrekərd] 然后用cdrecord -v dev=5,0,0 -eject ...
- Composer依赖管理 – PHP的利器
别再到处搜PHP类扩展包了,对于现代语言而言,包管理器基本上是标配.Java 有 Maven,Python 有 pip,Ruby 有 gem,Nodejs 有 npm.PHP 的则是 PEAR,不过 ...
- vue 学习 渲染、v-指令
vue渲染 在组件中data是一个方法里面的值要是一个对象return出去 export default { name: "HelloWorld", data() { return ...
- Django--导出项目依赖库requirements.txt
虚拟环境下: 1.导出项目依赖库: pip freeze > requirements.txt 2.使用 pip 一次性安装项目的所有依赖项,方法是在命令行中输入: pip install - ...
- 2019-10-9:渗透测试,基础学习the-backdoor-factory-master(后门工厂)初接触
该文章仅供学习,利用方法来自网络文章,仅供参考 the-backdoor-factory-master(后门工制造厂)原理:可执行二进制文件中有大量的00,这些00是不包含数据的,将这些数据替换成pa ...
- 几行代码轻松搞定python的sqlite3的存取
很简单: 存数据: 1.加载sqlite3驱动(只需一行代码) 2.用驱动执行查询语句(只需一行代码) 取数据: 1.加载sqlite3驱动(只需一行代码) 2.用驱动执行查询语句(只需一行代码) 乍 ...