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

题目

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 ith room, there is a wizard who has a resentment value of a[i]. The prince has MMcurses, the j^{th}jth curse is f[j], and 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 jth curse in the ith room, then his resentment value will change from x to (x f[j] a[i]), for example, when x = 1,a[i] = 2,f[j] = '+', then x will become 1+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] 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[i]='+','-','*','/', 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

解析:

T组样例

n个数字,m个符号,初始值为k(保证m >= n, 0 < m < 6, 0 < n < 1001)

m个符号从左到右每个用一遍,每次选一个数字,问最后结果最大是多少(每个数只能用一次)。

开两个数组保存dpmax[n][m],dpmin[n][m]分别表示前i个数用j个符号的最大值和最小值

dpmin  = min ( dpmin[i - 1][j], dpmin[i - 1][j] op[j] room[i], dpmax[i - 1][j] op[j] room[i]);

dpmax = max(dpmax[i - 1][j], dpmin[i - 1][j] op[j] room[i], dpmax[i - 1][j] op[j] room[i]);

开始dp,注意当 i == j 的时候 不能从dp[i-1][j]直接转移过来,因为此时dp[i-1][j]不合法!!!!

(有j个符号却只有i个数)

#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#include <queue>
#include <set>
#include <cmath>
#include <sstream>
#include <stack>
#include <fstream>
#include <ctime>
#pragma warning(disable:4996);
#define mem(sx,sy) memset(sx,sy,sizeof(sx))
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-8;
const double PI = acos(-1.0);
const ll llINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
using namespace std;


ll dpmax[1005][6];
ll dpmin[1005][6];
ll room[1005];
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		ll n, m, k;
		scanf("%lld%lld%lld", &n, &m, &k);
		for (int i = 1; i <= n; i++)
			scanf("%lld", &room[i]);
		char op[10];
		scanf("%s", op + 1);
		mem(dpmin, 0);
		mem(dpmax, 0);
		for (int i = 0; i <= n; i++) {
			dpmin[i][0] = k;
			dpmax[i][0] = k;
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				if (i < j)break;
				ll temp = dpmax[i - 1][j - 1], temp2 = dpmin[i - 1][j - 1];
				ll maxx, minn;
				if (op[j] == '+') {
					maxx = max(temp + room[i], temp2 + room[i]);
					minn = min(temp + room[i], temp2 + room[i]);
				}
				else if (op[j] == '-') {
					maxx = max(temp - room[i], temp2 - room[i]);
					minn = min(temp - room[i], temp2 - room[i]);
				}
				else if (op[j] == '/') {
					maxx = max(temp / room[i], temp2 / room[i]);
					minn = min(temp / room[i], temp2 / room[i]);
				}
				else {
					maxx = max(temp*room[i], temp2*room[i]);
					minn = min(temp*room[i], temp2*room[i]);
				}
				dpmax[i][j] = max(maxx, dpmax[i - 1][j]);
				dpmin[i][j] = min(minn, dpmin[i - 1][j]);
				if (i == j) {
					dpmax[i][j] = maxx;
					dpmin[i][j] = minn;
				}
			}
		}
		printf("%lld\n", dpmax[n][m]);
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值