ACM-ICPC 2018 焦作赛区网络预赛 B.Mathematical Curse

一位科学大陆的王子因年轻时轻视数学而被囚禁在城堡中,受困于数学诅咒。通过努力学习,他决定用知识逃离。在N间房间中,每个房间的巫师都有怨念值,王子需消除M个诅咒,经过加减乘除运算,求逃离时的最大怨念值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目链接:https://nanti.jisuanke.com/t/31711

题意:给定初始值k,N个数以及M个运算符,从N个数中挑选M个数进行运算,求最大值。

思路:因为运算是要按顺序的,那么规定dp1[i][j]为前i个数中运算了前j个运算符的最大值,因为数字有可能为负数,那么乘法和除法运算就要特殊处理了,因为乘上一个负数或者除一个负数的话,值越大结果就越小,所以还需要dp2[i][j]来表示前i个数运算了前j个运算符的最小值。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll dp1[1005][6],dp2[1005][6];
char f[6];
int main()
{
    int T;
    int n,m,k,a[1005];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        scanf("%s",f+1);
        //for(int i=1;i<=m;i++)
           // printf("%c\n",f[i]);
        for(int i=0;i<=n;i++)
            for(int j=0;j<=m;j++)
                dp1[i][j]=-1e18;
        for(int i=0;i<=n;i++)
            for(int j=0;j<=m;j++)
                dp2[i][j]=inf;
        for(int i=0;i<=n;i++)
        {
            dp1[i][0]=k;
            dp2[i][0]=k;
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(j>i) break;
                if(f[j]=='+')
                {
                    dp1[i][j]=max(dp1[i][j],dp1[i-1][j-1]+a[i]);
                    dp1[i][j]=max(dp1[i][j],dp1[i-1][j]);
                    dp2[i][j]=min(dp2[i][j],dp2[i-1][j-1]+a[i]);
                    dp2[i][j]=min(dp2[i][j],dp2[i-1][j]);
                }
                if(f[j]=='-')
                {
                    dp1[i][j]=max(dp1[i][j],dp1[i-1][j-1]-a[i]);
                    dp1[i][j]=max(dp1[i][j],dp1[i-1][j]);
                    dp2[i][j]=min(dp2[i][j],dp2[i-1][j-1]-a[i]);
                    dp2[i][j]=min(dp2[i][j],dp2[i-1][j]);
                }
                if(f[j]=='*')
                {
                    if(a[i]>0)
                    {
                        dp1[i][j]=max(dp1[i][j],dp1[i-1][j-1]*a[i]);
                        dp1[i][j]=max(dp1[i][j],dp1[i-1][j]);
                        dp2[i][j]=min(dp2[i][j],dp2[i-1][j-1]*a[i]);
                        dp2[i][j]=min(dp2[i][j],dp2[i-1][j]);
                    }
                    else
                    {
                        dp1[i][j]=max(dp1[i][j],dp2[i-1][j-1]*a[i]);
                        dp1[i][j]=max(dp1[i][j],dp1[i-1][j]);
                        dp2[i][j]=min(dp2[i][j],dp1[i-1][j-1]*a[i]);
                        dp2[i][j]=min(dp2[i][j],dp2[i-1][j]);
                    }
                }
                if(f[j]=='/')
                {
                    if(a[i]>0)
                    {
                       // printf("%d %d %lld %lld %lld\n",i,j,dp1[i][j],dp1[i-1][j-1]/a[i]);
                        dp1[i][j]=max(dp1[i][j],dp1[i-1][j-1]/a[i]);
                        dp1[i][j]=max(dp1[i][j],dp1[i-1][j]);
                        dp2[i][j]=min(dp2[i][j],dp2[i-1][j-1]/a[i]);
                        dp2[i][j]=min(dp2[i][j],dp2[i-1][j]);
                    }
                    else
                    {
                        dp1[i][j]=max(dp1[i][j],dp2[i-1][j-1]/a[i]);
                        dp1[i][j]=max(dp1[i][j],dp1[i-1][j]);
                        dp2[i][j]=min(dp2[i][j],dp1[i-1][j-1]/a[i]);
                        dp2[i][j]=min(dp2[i][j],dp2[i-1][j]);
                    }
                }
            }
        }
        printf("%lld\n",dp1[n][m]);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值