1. 程式人生 > >ACM-ICPC 2018 焦作賽區網路預賽 B. Mathematical Curse

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]);
	}
}