计蒜客 Mathematical Curse(ACM-ICPC 2018 焦作赛区网络预赛 B)(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 N\ge MN≥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 \le T \le 1000)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 \le N \le 1000), M(1 \le M \le 5)N(1≤N≤1000),M(1≤M≤5) and K(-1000 \le K \le 1000K(−1000≤K≤1000), 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](−1000≤a[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 焦作赛区网络预赛

 

题意:有很多个房间,房间里有一个巫师,能量值为a,王子有4种操作,跟巫师战斗会让他的能量值变化,问王子用完所有操作之后的最大值。操作必须按顺序用,房间也必须按顺序打。但是可以选择不打。

 

解题思路:赤裸裸的DP了,开场题意看了好久……dp[i][j]表示打了前i个房间,用了j种操作的最大值和最小值。然后遍历的时候就分两种情况,打或不打。直接递推过去就好了。重点是要记录最大值和最小值,因为有负数的存在。这里直接重载运算符,很方便的一个操作。

 

#include 
#include 
#include 
using namespace std;
const long long INF=1e15+5;
const int maxn=1e4+5;
struct node{
	long long max,min;
	node operator +(const int &a) const { return {max+a,min+a};};
	node operator -(const int &a) const { return {max-a,min-a};};
	node operator *(const int &a) const {
		long long arr[2]={max*a,min*a};
		sort(arr,arr+2);
		return {arr[1],arr[0]};
	}
	node operator /(const int &a) const {
		long long arr[5]={max/a,min/a};
		sort(arr,arr+2);
		return {arr[1],arr[0]};
	}
}dp[maxn][10];
int n,m,k;
long long a[maxn];
char s[10];
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d%d",&n,&m,&k);
		for (int i=1;i<=n;i++)
			scanf("%lld",&a[i]);
		scanf("%s",s);
		for (int i=1;i<=m;i++)
			dp[0][i].max=-INF,dp[0][i].min=INF;
		dp[0][0].max=dp[0][0].min=k;
		node u;
		for (int i=1;i<=n;i++)
		{
			for (int j=0;j<=m;j++) dp[i][j]=dp[i-1][j];//不打的情况
			for (int j=1;j<=m;j++)//打的情况,直接递推
			{
				if (j>i) break;
				switch (s[j-1]) 
				{
					case '+':{u=dp[i-1][j-1]+a[i];break;}
					case '-':{u=dp[i-1][j-1]-a[i];break;}
					case '*':{u=dp[i-1][j-1]*a[i];break;}
					case '/':{u=dp[i-1][j-1]/a[i];break;}
				}
				dp[i][j].max=max(dp[i][j].max,u.max);
				dp[i][j].min=min(dp[i][j].min,u.min);
			}
		}
		printf("%lld\n",dp[n][m].max);
	}
}


 

 

 

你可能感兴趣的:(————ACM相关————,——动态规划——,ACM,-,递推)