【ACM-ICPC 2018 焦作赛区网络预赛】Mathematical Curse【dp+线段树优化】

一位科学大陆的王子因年轻时轻视数学而被囚禁于城堡中,成年后决心使用数学知识逃脱。面对房间中的巫师和数学诅咒,王子如何通过策略最大化自己的仇恨值?本文探讨了一个涉及数学运算、策略规划和最大值求解的问题。

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

https://nanti.jisuanke.com/t/31711

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]), for example, when x=1, a[i]=2, f[j]=x=1,a[i]=2,f[j]='+', then xx 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?

Input

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

 

题意:

 

要用顺序利用m个符号,对n个数中的m个数顺序进行操作,求最大值

分析:

简单的01背包超时,dp[i][j]代表操作前i个数用j个符号的最大值,此时最后一个操作数为i

第二维for循环查找太慢,只能用用线段树优化查找前i个操作

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3fLL
using namespace std;
const int maxn=1010;
ll dp[maxn][6],k,aa[maxn],bb[maxn];
int n,m;
char c[10];
ll a[maxn],Max,Min;
struct node
{
    ll maxn,minx;
}v[maxn<<2];
ll getMax(int k,int l,int r,int lt,int rt)
{
    if(lt<=l&&rt>=r){
        Max=max(Max,v[k].maxn);
        return v[k].maxn;
    }
    int mid;
    mid=(l+r)>>1;
    if(v[k].maxn<=Max) return Max;
    if(lt<=mid)
        Max=max(Max,getMax(k<<1,l,mid,lt,rt));
    if(rt>mid)
        Max=max(Max,getMax((k<<1)+1,mid+1,r,lt,rt));
    return Max;
}
ll getMin(int k,int l,int r,int lt,int rt)
{
    if(lt<=l&&rt>=r){
        Min=min(Min,v[k].minx);
        return Min;
    }
    int mid;
    mid=(l+r)>>1;
    if(v[k].minx>=Min) return Min;
    if(lt<=mid)
        Min=min(Min,getMin(k<<1,l,mid,lt,rt));
    if(rt>mid)
        Min=min(Min,getMin((k<<1)+1,mid+1,r,lt,rt));
    return Min;
}
void update1(int k,int l,int r,int pos,ll val)
{
    if(l==r)
    {
        v[k].maxn=val;
        return ;
    }
    int mid;
    mid=(l+r)>>1;
    if(pos<=mid)
        update1(k<<1,l,mid,pos,val);
    if(pos>mid)
        update1((k<<1)+1,mid+1,r,pos,val);
    v[k].maxn=max(v[k<<1].maxn,v[(k<<1)+1].maxn);
    return ;
}
void update2(int k,int l,int r,int pos,ll val)
{
    if(l==r)
    {
        v[k].minx=val;
        return ;
    }
    int mid;
    mid=(l+r)>>1;
    if(pos<=mid)
        update2(k<<1,l,mid,pos,val);
    if(pos>mid)
        update2((k<<1)+1,mid+1,r,pos,val);
    v[k].minx=min(v[k<<1].minx,v[(k<<1)+1].minx);
    return ;
}
int main()
{
    int t,i,j,l,p;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%lld",&n,&m,&k);
        p=n<<2;
        for(i=-1;i<=p;i++)
            v[i].maxn=v[i].minx=k;
        for(i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        scanf("%s",&c);
        for(j=0;j<m;j++)
        {
            for(i=j+1;i<=n;i++)
            {
                ll aaa,bbb;
                Max=-inf;
                Min=inf;
                if(i>1){
                aaa=getMax(1,1,n,j,i-1);
                bbb=getMin(1,1,n,j,i-1);
                }
                else
                    aaa=bbb=k;
                if(c[j]=='/')
                {
                    aa[i]=aaa/a[i],bb[i]=bbb/a[i];
                }
                else
                if(c[j]=='+')
                aa[i]=aaa+a[i],bb[i]=bbb+a[i];
                else
                if(c[j]=='-')
                aa[i]=aaa-a[i],bb[i]=bbb-a[i];
                else
                if(c[j]=='*')
                aa[i]=aaa*a[i],bb[i]=bbb*a[i];
            }
            for(i=j+1;i<=n;i++)
            {
                update1(1,1,n,i,max(aa[i],bb[i]));
                update2(1,1,n,i,min(aa[i],bb[i]));
            }
        }
        ll aaa,bbb;
        Max=-inf;
        Min=inf;
        aaa=getMax(1,1,n,m,n);
        bbb=getMin(1,1,n,m,n);
        printf("%lld\n",max(aaa,bbb));
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值