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 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 焦作赛区网络预赛

题解:DP;

分别记录最大值和最小值,(因为可能出现两个都是负数的情况),转移方程为:

dp[i][j] = dp[i - 1][j];  dp1[i][j] = dp1[i - 1][j];分别记录最大和最小对于不同符号,有不同的转移方程

参考代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <bitset>
#define INF 0x3f3f3f3f3f3f3f3fll
#define clr(x, y) memset(x, y, sizeof(x))
#define mod 1000000007
using namespace std;
typedef long long LL;
const int maxn = 1010;
const int maxm = 10;
int a[maxn],t;
LL dp[maxn][maxm],dp1[maxn][maxn];
char f[maxm];
LL Max(LL a,LL b) {return a>=b? a:b; }
LL Min(LL a,LL b)  { return a<b? a:b; }
int main()
{
    scanf("%d", &t);
    while(t--)
    {
        memset(dp, -INF, sizeof dp);
        memset(dp1,INF,sizeof dp1);
        int n, m, k;
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            dp[i][0] =dp1[i][0]=k;
        }
        dp[0][0] = dp1[0][0] = k;
        scanf("%s", f + 1);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
            	if(i<j) continue;
                dp[i][j] = dp[i - 1][j]; dp1[i][j] = dp1[i - 1][j];
                if(f[j] == ‘+‘) dp[i][j]=Max(dp[i][j],dp[i - 1][j - 1] + a[i]), dp1[i][j]=Min(dp1[i][j],dp1[i - 1][j - 1] + a[i]);
                else if(f[j] == ‘-‘) dp[i][j] = Max(dp[i][j], dp[i - 1][j - 1] - a[i]), dp1[i][j] = Min(dp1[i][j], dp1[i - 1][j - 1] - a[i]);
                else if(f[j] == ‘*‘)
				{
					dp[i][j] = Max(dp[i][j], Max(dp[i - 1][j - 1] * a[i],dp1[i - 1][j - 1] * a[i]) );
					dp1[i][j] = Min(dp1[i][j], Min(dp[i - 1][j - 1] * a[i],dp1[i - 1][j - 1] * a[i]) );
				}
                else
				{
					dp[i][j] = Max(dp[i][j], Max(dp[i - 1][j - 1] / a[i], dp1[i - 1][j - 1] / a[i]) );
					dp1[i][j]=Min(dp1[i][j], Min(dp[i - 1][j - 1] / a[i], dp1[i - 1][j - 1] / a[i]) );
				}
            }
        }
        printf("%lld\n", Max(dp[n][m],dp1[n][m]) );
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/songorz/p/9651880.html

时间: 2024-11-06 23:16:42

ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse的相关文章

ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)

Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring wonderful substring when the times it appears in that string is between AA and BB (A \le times \le BA≤times≤B). Can you calculate the number of wonderful

ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 

ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship

There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry the weight of V[i]V[i] and the number of the i^{th}ith kind of ship is 2^{C[i]} - 12C[i]?1. How many different schemes there are if you want to use thes

ACM-ICPC 2018 焦作赛区网络预赛 I题(滑稽)

不解释,看代码 #include <bits/stdc++.h> #define ll long long using namespace std; const int N=10; const ll mod=1e9+7; struct AC { AC() { ll a,b,c; while(cin>>a>>b>>c) { if((a&1)&&(b&1)&&(c&1)) cout<<"

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest with n (0 < n \le 20)n(0<n≤20) problems. And he knows the answer of all of these problems. However, he can submit ii-th problem if and only if he has s

ACM-ICPC 2018 焦作赛区网络预赛 L:Poor God Water(矩阵快速幂)

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 3 c

ACM-ICPC 2018 焦作赛区网络预赛 E. Jiu Yuan Wants to Eat (树链剖分-线性变换线段树)

树链剖分若不会的话可自行学习一下. 前两种操作是线性变换,模\(2^{64}\)可将线段树全部用unsigned long long 保存,另其自然溢出. 而取反操作比较不能直接处理,因为其模\(2^{64}\)的特殊性,可将其转化为线性变换. 显然 \[-x\equiv (2^{64}-1)*x (mod\ 2^{64})\] 因为\[!x = (2^{64}-1) -x \] 所以 \[ !x = (2^{64}-1) + (2^{64}-1)x\] #include<bits/stdc++

ACM-ICPC 2018 焦作赛区网络预赛 B. Mathematical Curse &lt;&lt; DP

题意 有n个数和m个运算符,按顺序选m个数进行运算,初值为k,问最后能得到的最大值是多少. 思路 dp[i][j]表示选到了第i个数时用了j个运算符,观察发现,一个数只能由他前一个状态的最大值或最小值转移过来(因为乘上一个负数会使最小的数变最大),所以我们同时维护最大最小. 然后转移就行了,需要注意$i-1>j$时是不合法的状态. 转移方程:$dp[i][j]=max(dp[i-1][j-1]\circ now[i],dp[i-1][j])(i<j)$ 代码 1 #include<bit

ACM-ICPC 2018 焦作赛区网络预赛 F. Modular Production Line (区间K覆盖-最小费用流)

很明显的区间K覆盖模型,用费用流求解.只是这题N可达1e5,需要将点离散化. 建模方式步骤: 1.对权值为w的区间[u,v],加边id(u)->id(v+1),容量为1,费用为-w; 2.对所有相邻的点加边id(i)->id(i+1),容量为正无穷,费用为0; 3.建立源点汇点,由源点s向最左侧的点加边,容量为K,费用为0,由最右侧的点向汇点加边,容量为K,费用为0 4.跑出最大流后,最小费用取绝对值就是能获得的最大权 #include<bits/stdc++.h> using n