Mathematical Curse(dp)

编程入门 行业动态 更新时间:2024-10-18 18:18:39

<a href=https://www.elefans.com/category/jswz/34/1698185.html style=Mathematical Curse(dp)"/>

Mathematical Curse(dp)

题目描述
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 N rooms from the place where he was imprisoned to the exit of the castle. In the ith room, there is a wizard who has a resentment value of a[i]. The prince has M curses, the jth curse is f[j], and f[j] represents one of the four arithmetic operations, namely addition(’+’), subtraction(’-’), multiplication(’*’), and integer division (’ / ‘). The prince’s initial resentment value is K. 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] with the wizard’s resentment value. That is, if the prince eliminates the jth curse in the ithroom, then his resentment value will change from x to (x f[j] a[i]), for example, when x=1, a[i]=2, f[j]=’+’, then x will become 1+2=3.
Before the prince escapes from the castle, he must eliminate all the curses. He must go from a[1] to a[N] in order and cannot turn back. He must also eliminate the f[1] to f[M] curses in order(It is guaranteed that N≥M). What is the maximum resentment value that the prince may have when he leaves the castle?

输入
The first line contains an integer 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≤N≤1000), M(1≤M≤5) and K(-1000≤K≤1000), the second line contains N non-zero integers: a[1], a[2], … , a[N] (-1000≤a[i]≤1000), and the third line contains M characters: f[1], f[2], … , f[M] (f[j] 〖= 〗^’ +’,’-’,’*’,’ / '), with no spaces in between.

输出
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推最大值即可,注意乘除操作会将负值转正,因此需要两个数组储存最大值最小值

代码实现

#include<bits/stdc++.h>using namespace std;
typedef long long ll;
const int N=1005;
const int mod=1e9+7;
const ll INF=(ll)1e18;
int T;
int n,m;
char op[10];
ll dpa[N][10],dpi[N][10],a[N],k;inline ll cal(ll a,char c,ll b)
{if(c=='+') return (a+b);if(c=='-') return (a-b);if(c=='*') return (a*b);if(c=='/') return (a/b);
}
inline ll getmax(ll a,ll b)
{if(a>b) return a;return b;
}
inline ll getmin(ll a,ll b)
{if(a<b) return a;return b;
}
int main()
{scanf("%d",&T);while(T--){scanf("%d%d%lld",&n,&m,&k);for(int i=1;i<=n;i++){scanf("%lld",&a[i]);}scanf("%s",op+1);for(int i=0;i<=n;i++){for(int j=0;j<=m;j++){dpa[i][j]=-INF;dpi[i][j]=INF;}}for(int i=0;i<=n;i++){dpa[i][0]=dpi[i][0]=k;}for(int i=1;i<=n;i++){for(int j=1;j<=m && j<=i;j++){dpa[i][j]=getmax(dpa[i-1][j],dpa[i][j]);dpi[i][j]=getmin(dpi[i-1][j],dpi[i][j]);ll x=cal(dpa[i-1][j-1],op[j],a[i]);ll y=cal(dpi[i-1][j-1],op[j],a[i]);dpa[i][j]=getmax(dpa[i][j],getmax(x,y));dpi[i][j]=getmin(dpi[i][j],getmin(x,y));}}printf("%lld\n",getmax(dpa[n][m],dpi[n][m]));}return 0;
}

更多推荐

Mathematical Curse(dp)

本文发布于:2024-02-10 17:10:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1676359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mathematical   Curse   dp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!