ACM-ICPC2018焦作网络赛 Mathematical Curse(dp)

一位因年轻时轻视数学而被囚禁的王子,决定利用成年后的数学知识逃离城堡。面对一系列数学诅咒和房间里的巫师,王子必须在消除诅咒的同时最大化自己的怨恨值,以确保成功逃脱。

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

Mathematical Curse

  • 22.25%
  •  1000ms
  •  65536K
 

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 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
题目来源

ACM-ICPC 2018 焦作赛区网络预赛

 

 

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int kMaxN = 1005;
const int kMaxM = 6;

long long f[kMaxN][kMaxM];
long long g[kMaxN][kMaxM];
int a[kMaxN];
char curse[kMaxM];

int main() {
  int T;
  scanf("%d", &T);
  for (int cas = 1; cas <= T; ++cas) {
    int n, m, k;
    scanf("%d %d %d", &n, &m, &k);
    memset(f, 0xa0, sizeof(f));
    memset(g, 0x70, sizeof(g));
    for (int i = 0; i <= n; ++i) {
      f[i][0] = k;
      g[i][0] = k;
    }
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
    }
    scanf("%s", curse);
    for (int j = 1; j <= m; ++j) {
      for (int i = j; i <= n; ++i) {
        f[i][j] = f[i - 1][j];
        g[i][j] = g[i - 1][j];
        if (curse[j - 1] == '+') {
          f[i][j] = max(f[i][j], f[i - 1][j - 1] + a[i]);
          g[i][j] = min(g[i][j], g[i - 1][j - 1] + a[i]);
        } else if (curse[j - 1] == '-') {
          f[i][j] = max(f[i][j], f[i - 1][j - 1] - a[i]);
          g[i][j] = min(g[i][j], g[i - 1][j - 1] - a[i]);
        } else if (curse[j - 1] == '*') {
          f[i][j] = max(f[i][j], f[i - 1][j - 1] * a[i]);
          f[i][j] = max(f[i][j], g[i - 1][j - 1] * a[i]);
          g[i][j] = min(g[i][j], f[i - 1][j - 1] * a[i]);
          g[i][j] = min(g[i][j], g[i - 1][j - 1] * a[i]);
        } else if (curse[j - 1] == '/') {
          f[i][j] = max(f[i][j], f[i - 1][j - 1] / a[i]);
          f[i][j] = max(f[i][j], g[i - 1][j - 1] / a[i]);
          g[i][j] = min(g[i][j], f[i - 1][j - 1] / a[i]);
          g[i][j] = min(g[i][j], g[i - 1][j - 1] / a[i]);
        }
        //printf("[%d][%d] = %lld %lld\n", i, j, f[i][j], g[i][j]);
      }
    }
    printf("%lld\n", f[n][m]);
  }
  return 0;
}

 

转载于:https://www.cnblogs.com/yzm10/p/9651687.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值