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
题目来源
题解: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 = ;
const int maxm = ;
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 = ; i <= n; i++)
{
scanf("%d", &a[i]);
dp[i][] =dp1[i][]=k;
}
dp[][] = dp1[][] = k;
scanf("%s", f + );
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
if(i<j) continue;
dp[i][j] = dp[i - ][j]; dp1[i][j] = dp1[i - ][j];
if(f[j] == '+') dp[i][j]=Max(dp[i][j],dp[i - ][j - ] + a[i]), dp1[i][j]=Min(dp1[i][j],dp1[i - ][j - ] + a[i]);
else if(f[j] == '-') dp[i][j] = Max(dp[i][j], dp[i - ][j - ] - a[i]), dp1[i][j] = Min(dp1[i][j], dp1[i - ][j - ] - a[i]);
else if(f[j] == '*')
{
dp[i][j] = Max(dp[i][j], Max(dp[i - ][j - ] * a[i],dp1[i - ][j - ] * a[i]) );
dp1[i][j] = Min(dp1[i][j], Min(dp[i - ][j - ] * a[i],dp1[i - ][j - ] * a[i]) );
}
else
{
dp[i][j] = Max(dp[i][j], Max(dp[i - ][j - ] / a[i], dp1[i - ][j - ] / a[i]) );
dp1[i][j]=Min(dp1[i][j], Min(dp[i - ][j - ] / a[i], dp1[i - ][j - ] / a[i]) );
}
}
}
printf("%lld\n", Max(dp[n][m],dp1[n][m]) );
}
return ;
}
ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse的更多相关文章
- ACM-ICPC 2018 焦作赛区网络预赛J题 Participate in E-sports
Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...
- 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 th ...
- ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ACM-ICPC 2018 焦作赛区网络预赛 I题 Save the Room
Bob is a sorcerer. He lives in a cuboid room which has a length of AA, a width of BB and a height of ...
- 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 won ...
- ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies
There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more inte ...
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...
- ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
随机推荐
- open-falcon监控系统
官方文档 https://book.open-falcon.org/zh/intro/index.html 一.Open-Falcon介绍 1.监控系统,可以从运营级别(基本配置即可),以及应用级别( ...
- PHP 富文本解码为 HTML 并显示
PHP 富文本解码为 HTML 并显示 使用 html_entity_decode 函数 参考文档 PHP实例: // html_entity_decode(待解码内容, 如何处理引号) html_ ...
- javascript jquery 修改指定标签中的内容
javascript jquery 修改指定标签中的内容 $("#test1").text("Hello world!"); document.getEleme ...
- nyoj 739 笨蛋难题四
笨蛋难题四 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 这些日子笨蛋一直研究股票,经过调研,终于发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价, ...
- 力扣(LeetCode)键盘行 个人题解
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad& ...
- PostGIS 递归方法
在Oracle数据库中,有可以实现递归的函数 select * from table_name start with [condition1] connect by [condition2] 最近发现 ...
- Matplotlib入门简介
Matplotlib是一个用Python实现的绘图库.现在很多机器学习,深度学习教学资料中都用它来绘制函数图形.在学习算法过程中,Matplotlib是一个非常趁手的工具. 一般概念 图形(figur ...
- 读取FANUC进给倍率
读取FANUC机床的倍率信息需要用到 FOCAS链接库. 根据FANUC的连接手册可以知道,进给倍率信号存在 Gn012寄存器中.在机床上更改倍率开关,发现G0012寄存器值变化,经验证G0012就是 ...
- <<代码大全>>阅读笔记之一 使用变量的一般事项
一.使用变量的一般事项 1.把变量引用局部化 变量应用局部化就是把变量的引用点尽可能集中在一起,这样做的目的是增加代码的可读性 衡量不同引用点靠近程度的一种方法是计算该变量的跨度(span) 示例 a ...
- Caffe 图像分类
本文主要描述如何使用 CAFFE 进行图像分类. 开发环境要求:windows 10 64位.Visual Studio 2017..NET framework 4.6.1 分类 在一个项 ...