zoukankan      html  css  js  c++  java
  • 【2018焦作网络赛】 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 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 Nge MNM). 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(1T1000), 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(1N1000),M(1M5) and K(-1000 le K le 1000K(1000K1000), 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](1000a[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

    SOLUTION:
    f1 f2 i,j 代表到第i天已经使用的状态为j是的最大和最小值
    dp就完事了

    CODE:
    #include"bits/stdc++.h"
    using namespace std;
    #define sc(x) scanf("%lld",&(x))
    #define ll long long
    #define int long long
    ll a[1010];
    ll f1[1010][1<<(7)];
    ll f2[1010][1<<7];
    const int mod = 1e9+7;
    inline ll cal(ll a,char s,ll b)
    {
        if(s=='+')return a+b;
        else if(s=='-')return a-b;
        else if(s=='*')return a*b;
        else return a/b;
    }
    vector<int >v;
    ll Max(ll a,ll b)
    {
        if(a==(ll)-1000000000000000009)return b;
        else if(b>a)return b;
        else return a;
    }
    ll Min(ll a,ll b)
    {
        if(a==(ll)1000000000000000009)return b;
        else if(b<a)return b;
        else return a;
    }
    signed main()
    {
     //freopen("datte.txt","r",stdin);
    //freopen("my.out","w",stdout);
    
     int T;cin>>T;
        while(T--)
        {
           int n,m;ll k;string s;
           cin>>n>>m>>k;
           for(int i=1;i<=n;i++)sc(a[i]);
           cin>>s;
           for(int i=1;i<=n+1;i++)
            {
                for(int j=0;j<(1<<m);j++)
                {
                   f1[i][j]=(ll)(-1000000000000000009);
                   f2[i][j]=(ll)(1000000000000000009);
                }
            }
            f2[1][0]=f1[1][0]=k;
           
            for(int i=1;i<=n;i++)
            {
                for(int j=0;j<(1<<m);j++)
                {
                    if(f1[i][j]!=(ll)-1000000000000000009)
                    {// cout<<i<<" "<<j<<endl;
                        f1[i+1][j]=Max(f1[i+1][j],f1[i][j]);
                        for(int w=0;w<m;w++)if(!(j&(1<<w)))
                        {
                            f1[i+1][j|(1<<w)]=Max(f1[i+1][j|(1<<w)],cal(f1[i][j],s[w],a[i]));
                            f2[i+1][j|(1<<w)]=Min(f2[i+1][j|(1<<w)],cal(f1[i][j],s[w],a[i]));
                            break;
                        }
                    }
                    if(f2[i][j]!=1000000000000000009)
                    {
                        f2[i+1][j]=Min(f2[i+1][j],f2[i][j]);
                        for(int w=0;w<m;w++)if(!(j&(1<<w)))
                        {
                            f1[i+1][j|(1<<w)]=Max(f1[i+1][j|(1<<w)],cal(f2[i][j],s[w],a[i]));
                            f2[i+1][j|(1<<w)]=Min(f2[i+1][j|(1<<w)],cal(f2[i][j],s[w],a[i]));
                            break;
                        }
                    }
                }
            }
            cout<<f1[n+1][(1<<m)-1]<<endl;
        }
    }
    
    
    

      













  • 相关阅读:
    76、HTTP如何禁用缓存?如何确认缓存?
    多态与多态性,鸭子类型,内置方法,反射,异常处理
    继承的应用,继承实现原理,super(),多继承的代码规范,组合
    类的装饰器,绑定方法与非绑定方法,继承,继承背景下的属性查找
    面向对象,类与对象,__nit__方法,属性查找,隐藏属性
    xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块
    模块:re,json,pickle,time , datetime,random,猴子补丁
    区分py文件的两种用途,包的介绍和使用,日志模块
    模块的概念、使用,搜索路径与优先级,软件开发的目录规范
    三元表达式,列表、字典、集合生成式,生成器表达式,函数递归,匿名函数,面向过程编程
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11207560.html
Copyright © 2011-2022 走看看