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 ...
随机推荐
- java多线程与线程并发一:线程基础回顾
本文章内容整理自:张孝祥_Java多线程与并发库高级应用视频教程 线程简单来讲就是程序正在做的事情.多线程即一个程序同时做多件事情,一个线程就是一件事情. 在java中创建线程的方法有两种. 方法一是 ...
- 使用JSP脚本在页面输出九九乘法表
<% int i,j; for(i=1;i<10;i++) { for(j=1;j<=i;j++) { out.println(i+"*"+j+"=&q ...
- 微擎框架商业版 V2.1.2 去后门一键安装版+去除云平台+无附带模块
下载地址:http://dd.ma/AdVvoDu5 关注微信公众号codervip,点击公众号菜单,获取提取码! 这个是一键安装版本,所以微擎安装比较简单,不用大家手动去改数据库了,而且修复上个2. ...
- 让块元素在div中水平居中,并且垂直居中的五种方法
在写代码前,先做下准备工作,写两个div,设置下div的大小,把小的div放在大的div里面.可以给小的div设置下颜色,方便观看. 方法一:写一个伪元素,将它设置为行内块元素,高度与父元素相同,写一 ...
- 某些机root也不能访问dma-buf
从4.3后,回顾<从surfaceflinger历史变更谈截屏>,只能通过生产消费者队列向surfaceflinger服务申请显示缓冲,这个缓冲就是dma-buf映射的共享内存. bind ...
- ubuntu 16.04上源码编译dlib教程 | compile dlib on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/c6ead512/,欢迎阅读! compile dlib on ubuntu 16.04 Series Part 1: compil ...
- robatframework+jenkins+email集成部署方案
准备工作: 1.jenkins.war包 下载地址:https://jenkins.io/zh/download/ 2.Jdk1.8 下载地址:http://www.oracle.com/techne ...
- 【Linux系列】配置Centos 7的软件源(二)
目的 本文主要介绍以下内容: 设置centos的国内软件源,默认源都是国外的下载软件超级麻烦. ssh登录 下载一个shell或者cmder ssh root@192.168.10.18 #上篇设置的 ...
- IT人该如何未雨绸缪,不断提升自己的竞争力?同时尽量避免风险?
人会慢慢变老,变老后精力,记忆力乃至身体会慢慢变差,这是无法逆转的自然规律.随之会产生的是对中年危机的忧虑乃至恐惧,比如担心能力精力不及年轻人,从而导致收入锐减乃至失业. 对此我有如下三点不解.第一, ...
- 线程池ThreadPoolExecutor的使用方法
方法我们通过继承Thread类和实现runnable接口或者callable接口三种方式实现. 继承Thread类实际上也是实现了runnable接口,被继承的类主要是实现run()方法,通过star ...