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 焦作赛区网络预赛

编辑代码
 

题意:

有1-n间房 每间有一个数ai

有1-m个操作fj 每种操作可能是+-*/

有一个初始值k 走到第i个房间如果进行了第j个操作 得到结果k fj ai

房间和操作的顺序不能改变

问最后得到的最大值

思路:

就是一个比较简单的dp 发现自己dp总是写不好

最近不如多练点dp吧

dp[i][j]表示在第i间房做j个操作 i一定是不能小于j

加和减的话比较常规 乘除涉及到负数的话就不一定了

所以需要既存最大值也要存最小值

还要注意初始化的赋值

 //#include"pch.h"

 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<cstring>
#include<set>
#include<stack>
//#include<bits/stdc++.h> #define inf 0x3f3f3f3f
using namespace std;
typedef long long LL; const int maxn = ;
int t;
int n, m, k;
int a[maxn];
LL dpmin[maxn][], dpmax[maxn][];
char f[]; int main()
{ scanf("%d", &t);
while (t--) {
memset(dpmax, -inf, sizeof(dpmax));
memset(dpmin, inf, sizeof(dpmin));
//cout<<dpmax[0][0]<<endl<<dpmin[0][0]<<endl;
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
getchar();
for (int i = ; i <= m; i++) {
scanf("%c", &f[i]);
} for (int i = ; i <= n; i++) {
dpmax[i][] = dpmin[i][] = k;
}
for (int j = ; j <= m; j++) {
for (int i = j; i <= n; i++) {
dpmax[i][j] = dpmax[i - ][j];//第i间不做
dpmin[i][j] = dpmin[i - ][j];
if (f[j] == '+') {
dpmax[i][j] = max(dpmax[i][j], dpmax[i - ][j - ] + a[i]);
dpmin[i][j] = min(dpmin[i][j], dpmin[i - ][j - ] + a[i]);
}
if (f[j] == '-') {
dpmax[i][j] = max(dpmax[i][j], dpmax[i - ][j - ] - a[i]);
dpmin[i][j] = min(dpmin[i][j], dpmin[i - ][j - ] - a[i]);
}
if (f[j] == '*') {
dpmax[i][j] = max(dpmax[i][j], dpmax[i - ][j - ] * a[i]);
dpmax[i][j] = max(dpmax[i][j], dpmin[i - ][j - ] * a[i]);
dpmin[i][j] = min(dpmin[i][j], dpmax[i - ][j - ] * a[i]);
dpmin[i][j] = min(dpmin[i][j], dpmin[i - ][j - ] * a[i]);
}
if (f[j] == '/') {
dpmax[i][j] = max(dpmax[i][j], dpmax[i - ][j - ] / a[i]);
dpmax[i][j] = max(dpmax[i][j], dpmin[i - ][j - ] / a[i]);
dpmin[i][j] = min(dpmin[i][j], dpmax[i - ][j - ] / a[i]);
dpmin[i][j] = min(dpmin[i][j], dpmin[i - ][j - ] / a[i]);
}
}
}
printf("%lld\n", dpmax[n][m]);
}
return ;
}

焦作网络赛B-Mathematical Curse【dp】的更多相关文章

  1. ACM-ICPC 2018 焦作赛区网络预赛 B Mathematical Curse(DP)

    https://nanti.jisuanke.com/t/31711 题意 m个符号必须按顺序全用,n个房间需顺序选择,有个初始值,问最后得到的值最大是多少. 分析 如果要求出最大解,维护最大值是不能 ...

  2. 焦作网络赛K-Transport Ship【dp】

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

  3. ACM-ICPC2018焦作网络赛 Mathematical Curse(dp)

    Mathematical Curse 22.25% 1000ms 65536K   A prince of the Science Continent was imprisoned in a cast ...

  4. 2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat

    题意:四个操作,区间加,区间每个数乘,区间的数变成 2^64-1-x,求区间和. 题解:2^64-1-x=(2^64-1)-x 因为模数为2^64,-x%2^64=-1*x%2^64 由负数取模的性质 ...

  5. 2018焦作网络赛Mathematical Curse

    题意:开始有个数k,有个数组和几个运算符.遍历数组的过程中花费一个运算符和数组当前元素运算.运算符必须按顺序花费,并且最后要花费完.问得到最大结果. 用maxv[x][y]记录到第x个元素,用完了第y ...

  6. ACM-ICPC2018焦作网络赛 Transport Ship(二进制背包+方案数)

    Transport Ship 25.78% 1000ms 65536K   There are NN different kinds of transport ships on the port. T ...

  7. 焦作网络赛E-JiuYuanWantstoEat【树链剖分】【线段树】

    You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, she wil ...

  8. 焦作网络赛L-Poor God Water【矩阵快速幂】

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

  9. ACM-ICPC2018焦作网络赛 Participate in E-sports(大数开方)

    Participate in E-sports 11.44% 1000ms 65536K   Jessie and Justin want to participate in e-sports. E- ...

随机推荐

  1. Oracle数据库表空间与数据文件的关系描述正确的是( )

    Oracle数据库表空间与数据文件的关系描述正确的是( ) A.一个表空间只能对应一个数据文件 B.一个表空间可以对应多个数据文件 C.一个数据文件可以对应多个表空间 D.表空间与数据文件没任何对应关 ...

  2. IoC就是IoC,不是什么技术,与GoF一样,是一种 设计模式。

    IoC就是IoC,不是什么技术,与GoF一样,是一种 设计模式. Interface Driven Design接口驱动,接口驱动有很多好处,可以提供不同灵活的子类实现,增加代码稳定和健壮性等等,但是 ...

  3. Android ListView使用BaseAdapter与ListView的优化 (转至 http://www.open-open.com/lib/view/open1339485728006.html)

    在ListView的使用中,有时候还需要在里面加入按钮等控件,实现单独的操作.也就是说,这个ListView不再只是展示数据,也不仅仅是这一行要来处理用户的操作,而是里面的控件要获得用户的焦点.读者可 ...

  4. hadoop 安装笔记

    http://www.powerxing.com/install-hadoop/ 查询相关链接~!

  5. Windows "计划任务"功能设置闹钟~

    相信很多人和我一样在使用电脑时都会遇到这样一个麻烦:不知道如何在windows 中设置一个闹铃.当我们在“开始”菜单的所有程序中找了一遍又一遍,甚至使用Everything.exe做全盘的搜索,都没有 ...

  6. 第四章 Spring.Net 如何管理您的类___自定义对象行为

    Spring.Net 一直讲求 ” 配置应用程序“,有一种需求在实际开发中不太常用,却非常有用 -> 配置对象的行为,Spring.Net 也能够完美的实现.Spring.Net 通过几个专门的 ...

  7. ionic 页面加载事件及loading动画

    页面加载完成事件(非刷新情况下,页面切换是不会重复触发此事件的,只在第一次进入页面时触发,需要重复触发的话请使用 $ionicView.enter 事件) angular.module('app.co ...

  8. RF使用的相关库API

    RF内置库: http://robotframework.org/robotframework/ SSHLibrary:   ---WEB自动化测试 http://robotframework.org ...

  9. C# string.Format("{0:C3}", 2)

  10. ARM、MCU、DSP、FPGA、SOC各是什么?区别是什么?(转)

    ARM ARM处理器是Acorn计算机有限公司面向低预算市场设计的第一款RISC微处理器.更早称作Acorn RISC Machine.ARM处理器本身是32位设计,但也配备16位指令集,一般来讲比等 ...