2019年我能变强组队训练赛热身赛 问题 B: Mathematical Curse(线性dp)

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

2019年<a href=https://www.elefans.com/category/jswz/34/1699977.html style=我能变强组队训练赛热身赛 问题 B: Mathematical Curse(线性dp)"/>

2019年我能变强组队训练赛热身赛 问题 B: 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 i^th room, there is a wizard who has a resentment value of a[i]. The prince has M curses, the j^th 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 j^th curse in the i^th room, 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[i][j]表示前i个房间选了j个物品的最大价值,如果为负的话乘的时候可以乘以最小值会更大,除法同

想到初始化要分类初始化了,但没想到同时开两个dp数组维护最大最小值…wa哭了。。。

#include<iostream>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N =1005;
ll dpmin[N][N];
ll dpmax[N][N];
ll a[N];
char b[10];
ll f(ll a,char c,ll b)
{switch(c){case '+':return a+b;break;case '-':return a-b;break;case '*':return a*b;break;case '/':return a/b;break;}
}
int main()
{int t;cin>>t;while(t--){ll n,m,k;scanf("%lld%lld%lld",&n,&m,&k);memset(dpmax,-inf,sizeof dpmax);memset(dpmin,inf,sizeof dpmin);for(int i=1;i<=n;i++)scanf("%lld",&a[i]);for(int i=1;i<=m;i++)cin>>b[i];for(int i=0;i<=n-m;i++){dpmax[i][0]=dpmin[i][0]=k;}for(int i=1;i<=n;i++){for(int j=1;j<=m&&j<=i;j++){dpmax[i][j]=max(dpmax[i-1][j],max(f(dpmax[i-1][j-1],b[j],a[i]),f(dpmin[i-1][j-1],b[j],a[i])));dpmin[i][j]=min(dpmin[i-1][j],min(f(dpmax[i-1][j-1],b[j],a[i]),f(dpmin[i-1][j-1],b[j],a[i])));}}cout<<dpmax[n][m]<<endl;}return 0;} 

更多推荐

2019年我能变强组队训练赛热身赛 问题 B: Mathematical Curse(线性dp)

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

发布评论

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

>www.elefans.com

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