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

题目来源

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

题解: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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

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

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

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

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

  8. 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 ...

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

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

随机推荐

  1. css的块级元素和行级元素

    块级元素 概念: 每个块级元素都是独自占一行.  元素的高度.宽度.行高和边距都是可以设置的.   元素的宽度如果不设置的话,默认为父元素的宽度(父元素宽度100%) <address>/ ...

  2. 020.掌握Pod-Pod基础使用

    一 Pod定义详解 1.1 完整Pod定义文件 apiVersion: v1 #必选,版本号,例如v1,版本号必须可以用 kubectl api-versions 查询到 kind: Pod #必选, ...

  3. nyoj 114-某种序列 (python EOFError, List, append)

    114-某种序列 内存限制:64MB 时间限制:3000ms 特判: No 通过数:6 提交数:13 难度:4 题目描述: 数列A满足An = An-1 + An-2 + An-3, n >= ...

  4. 领扣(LeetCode)最大连续1的个数 个人题解

    给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组 ...

  5. Pashmak and Parmida's problem(树状数组)

    题目链接:http://codeforces.com/contest/459/problem/D 题意: 数列A, ai表示 i-th 的值, f(i,j, x) 表示[i,j]之间x的数目, 问:当 ...

  6. 如何在后台封装el-tree所需要的数据格式

    背景 最近遇到了一个分层级展示指标的需求,前端使用el-tree树形组件,要求按官方文档的格式提供数据. 数据格式: id: 1, label: '一级 1', children: id: 4, la ...

  7. opencv 5 图像转换(2 霍夫变换)

    霍夫线变换 标准霍夫变换和多尺度霍夫变换(HoughLines()函数) 实例: #include <opencv2/opencv.hpp> #include <opencv2/im ...

  8. convert svn repo to git

    https://john.albin.net/git/convert-subversion-to-git 1. 抓取Log 在linux 上做的,其余是在win上做的. 2. svn co svn:/ ...

  9. 2019-11-28:ssrf基础学习,笔记

    ssrf服务端请求伪造ssrf是一种由恶意访问者构造形成由服务端发起请求的一个安全漏洞,一般情况下,ssrf访问的目标是从外网无法访问的内部系统,正式因为它是由服务端发起的,所以它能请求到它相连而外网 ...

  10. docker快速部署DNS,实现快速上线

    概念Docker 是一个开源的应用容器引擎,Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.这里我将使用do ...