zoukankan      html  css  js  c++  java
  • ACM-ICPC 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
    题意:输入n个数m个字符,然后说,必须要把运算符一个一个按顺序都用完,开始有个数k然后你在利用每个运算符和数字,尽量使这个数最大,
    思路:然后因为里面含有负数,所有的话我们要考虑最大值最小值两种情况去
    ,然后还要考虑当前不取得情况,我们取最优的,当前位置取最优的最大值与最小值
     #include<bits/stdc++.h>
     2 using namespace std;
     3 const long long INF=0x3f3f3f3f3f3f3f3f;
     4 long long dpmax[1005][6];
     5 long long dpmin[1005][6];
     6 long long room[1005];
     7 int main()
     8 {
     9     int T;
    10     scanf("%d",&T);
    11     while(T--)
    12     {
    13         long long n,m,k;
    14         scanf("%lld%lld%lld",&n,&m,&k);
    15         for(int i=1;i<=n;i++)
    16             scanf("%lld",&room[i]);
    17         char op[10];
    18         scanf("%s",op+1);
    19         memset(dpmax,0,sizeof(dpmax));
    20         memset(dpmin,0,sizeof(dpmin));
    21         dpmax[0][0]=k;
    22         dpmin[0][0]=k;
    23         for(int i=1;i<=n;i++)
    24         {
    25             for(int j=0;j<=m;j++)
    26             {
    27                 if(j==0)
    28                     dpmax[i][j]=k,dpmin[i][j]=k;
    29                 else{
    30                     long long temp=dpmax[i-1][j-1],temp2=dpmin[i-1][j-1];
    31                     long long maxx,minn;
    32                     if(op[j]=='+')
    33                     {
    34                         maxx=max(temp+room[i],temp2+room[i]);
    35                         minn=min(temp+room[i],temp2+room[i]);
    36                     }
    37                     else if(op[j]=='-')
    38                     {
    39                         maxx=max(temp-room[i],temp2-room[i]);
    40                         minn=min(temp-room[i],temp2-room[i]);
    41                     }
    42                     else if(op[j]=='/')
    43                     {
    44                         maxx=max(temp/room[i],temp2/room[i]);
    45                         minn=min(temp/room[i],temp2/room[i]);
    46                     }
    47                     else{
    48                         maxx=max(temp*room[i],temp2*room[i]);
    49                         minn=min(temp*room[i],temp2*room[i]);
    50                     }
    51                     dpmax[i][j]=max(maxx,dpmax[i-1][j]);
    52                     dpmin[i][j]=min(minn,dpmin[i-1][j]);
    53                     if(i==j){
    54                         dpmax[i][j]=maxx;
    55                         dpmin[i][j]=minn;
    56                     }
    57                 }
    58             }
    59         }
    60         printf("%lld
    ",dpmax[n][m]);
    61     }
    62 
    63 }
  • 相关阅读:
    超强、超详细Redis数据库入门教程
    性能测试学习线路图(建议)
    性能测试分析软件汇总–开源、商业全部收集
    基于Locust、Tsung的百万并发秒杀压测案例[转]
    brew update失败提示:/System/Library/Frameworks/Ruby.framework/。。。解决方法
    XML特殊符号
    tsuang简易教程-MAC版
    【转】SQL Server获取日期或时间
    autoIT 传参数含引号
    Autoit 脚本以及 Java 调用
  • 原文地址:https://www.cnblogs.com/Lis-/p/9655748.html
Copyright © 2011-2022 走看看