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
题目来源
题解:当到达一个个房间时在此房间做运算操作可以得到一个大值和一个小值如果运算符中存在乘除运算并且在逐个运算的个过程中遇到负数且为乘除操作此时就需要用前面的得到的最小值来进行运算操作,所有我们只需要维护达到第i个房间时用第j种运算符时得到的最大值和最小值最后返回dpmax[n][m[就好。
#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<functional>
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define rep(a,b,c) for(ll a=b;a<c;a++)
#define dec(a,b,c) for(int a=b;a>c;a--)
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define ps(x) push(x)
#define MAX_N 100000+5
#define MAX_M 100
typedef long long ll;
typedef unsigned long long ull;
typedef priority_queue<ll,vector<ll>,greater<ll> >pqg;
const ll maxn=1000+5;
const ll inf=1e7;
ll mod=1e9+7;
int n,m,k;
ll room[maxn];
ll dpmax[maxn][6],dpmin[maxn][6];
char f[7];
ll solve()
{
ll ans=-inf;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m&&j<=i;j++)
{
ll tmax=dpmax[i-1][j-1],tmin=dpmin[i-1][j-1];
ll maxx,minn;
switch(f[j])
{
case '+':
{
maxx=tmax+room[i];
minn=tmin+room[i];
break;
}
case '-':
{
maxx=tmax-room[i];
minn=tmin-room[i];
break;
}
case '*':
{
maxx=max(tmax*room[i],tmin*room[i]);
minn=min(tmin*room[i],tmax*room[i]);
break;
}
case '/':
{
maxx=max(tmax/room[i],tmin/room[i]);
minn=min(tmin/room[i],tmax/room[i]);
break;
}
}
dpmax[i][j]=max(dpmax[i-1][j],maxx);
dpmin[i][j]=min(dpmin[i-1][j],minn);
if(i==j){dpmax[i][j]=maxx;dpmin[i][j]=minn;}
}
}
return dpmax[n][m];
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d",&t);
while(t--)
{
clr(dpmax,0);
clr(dpmin,0);
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
{
scanf("%lld",&room[i]);
dpmin[i][0]=dpmax[i][0]=k;
}
dpmin[0][0]=dpmax[0][0]=k;
cin>>f+1;
printf("%lld\n",solve());
}
return 0;
}
一位王子因年轻时轻视数学而被囚禁在城堡中,受困于数学诅咒。通过努力学习,他决定利用知识逃脱。从囚禁地到城堡出口,王子必须依次消除诅咒,与各房间的巫师战斗,使用加减乘除进行计算。挑战在于维持最高怨恨值以逃离。此问题需策略性选择运算以最大化最终得分。
437

被折叠的 条评论
为什么被折叠?



