[2019南昌邀请赛网络赛D][dp]
https://nanti.jisuanke.com/t/38223
Xiao Ming recently indulges in match stick game and he thinks he is good at it. His friend Xiao Jun decides to test him. Xiao Jun gives him an expression of length , made by match sticks and asks him to calculate the maximum value of the expression by moving any match sticks (but he can’t discard any of them). The expression is made up of some numbers, plus signs and minus signs represented as A_1 \ op_1 \ A_2 \ op_2 \ A_3 \ op_3 \ \cdots A_{m - 1} \ op_{m - 1} \ A_mA1 op1 A2 op2 A3 op3 ⋯Am−1 opm−1 Am. mm must be count by himself, A_k(1 \le k \le m)Ak(1≤k≤m) is an integer without leading zeros and less than 10^9109 , op_k (1 \le k \le m)opk(1≤k≤m) is a plus sign or a minus sign. At the same time, there are some requirements of the new expression:
- The new expression should also be made up of mm numbers and m - 1m−1 operators.
- The number of digits per number should keep consistent with the original.
- There couldn’t be any leading zeros per number.
Input
The first line consists of a single integer TTdenoting the number of test cases.
There’re two lines in each test case.
The first line contains an integer nn.
A string of length nn follows in the next line, denoting the expression given.
The expression is guaranteed to be valid.
Output
For each test case, print a single integer denoting the maximum result of the expression.
Constraints
1 \le n \le 1001≤n≤100
Note
Expression with the maximum result for the second sample is 7 - 17−1 .
Expression with the maximum result for the second sample is 7 + 7 + 97+7+9.
样例输入复制
3
3
1-1
3
1+1
5
1+2+3
样例输出复制
0
6
23
题意:给出每个数字和加号减号需要的火柴数,然后给出t组多项式,求不改变多项式项数以及每项数字位数的前提下得到的多项式的最大值
题解:由于不存在括号而且加法和减法是同级运算,所以这个求解过程满足dp的子问题性质,可以使用dp解决,由于项数以及位数不能变,所以先dp出i个火柴能拼出的j位最大值和最小值,然后dp枚举每一项的前面的符号是+还是-,是加法就使用i火柴能拼出j位数字的最大值更新dp数组,否则就使用最小值更新
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
using namespace std;
char ch[];
map<char,int>mp;
typedef long long ll;
int q[];
ll dp[][],dp2[][],dp3[][];
vector<int>g[];
int main(){
int t;
scanf("%d",&t);
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
g[].push_back();
mp['']=;
mp['+']=;
mp['-']=;
memset(dp2,-,sizeof(dp2));
memset(dp3,-,sizeof(dp3));
dp2[][]=;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
for(int k=;k<;k++){
if(dp2[i-j][k-]!=-&&i>=j)dp2[i][k]=max(dp2[i-j][k-]*+g[j][g[j].size()-],dp2[i][k]);
// if(dp2[i-j]!=-1&&dp2[i][k]==-1&&i>=j)dp2[i][k]=dp2[i-j][k-1]*10+g[j][g[j].size()-1];
}
}
}
dp3[][]=;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
for(int k=;k<;k++){
if(dp3[i-j][k-]!=-&&i>=j)dp3[i][k]=min(dp3[i-j][k-]*+g[j][],dp3[i][k]);
if(dp3[i-j][k-]!=-&&dp3[i][k]==-&&i>=j)dp3[i][k]=dp3[i-j][k-]*+g[j][];
}
}
}
//for(int i=1;i<=12;i++)cout<<dp2[i]<<endl;
while(t--){
int n;
scanf("%d",&n);
scanf("%s",ch);
int tot=;
ll sum=;
int f=;
for(int i=;i<n;i++){
if(ch[i]=='+'||ch[i]=='-'){tot++;q[tot]=i-f;f=i+;}
sum+=mp[ch[i]];
}
q[++tot]=n-f;
memset(dp,-,sizeof(dp));
dp[][]=;
// cout<<sum<<endl;
for(int i=;i<=tot;i++){
for(int j=;j<=sum;j++){
for(int k=;k<=;k++){
//cout<<dp2[k][q[i]]<<endl;
if(i>&&j-k->=&&dp[i-][j-k-]!=-&&dp2[k][q[i]]!=-)dp[i][j]=max(dp[i-][j-k-]+dp2[k][q[i]],dp[i][j]);
if(i==&&j-k>=&&dp[i-][j-k]!=-&&dp2[k][q[i]]!=-)dp[i][j]=max(dp[i-][j-k]+dp2[k][q[i]],dp[i][j]);
if(j-k->=&&dp[i-][j-k-]!=-&&dp3[k][q[i]]!=-)dp[i][j]=max(dp[i-][j-k-]-dp3[k][q[i]],dp[i][j]);
if(dp[i][j]==-){
if(i>&&j-k->=&&dp[i-][j-k-]!=-&&dp2[k][q[i]]!=-){
dp[i][j]=dp[i-][j-k-]+dp2[k][q[i]];
}
if(i==&&j-k>=&&dp[i-][j-k]!=-&&dp2[k][q[i]]!=-){
dp[i][j]=dp[i-][j-k]+dp2[k][q[i]];
}
if(j-k->=&&dp[i-][j-k-]!=-&&dp3[k][q[i]]!=-){
dp[i][j]=dp[i-][j-k-]-dp3[k][q[i]];
}
}
}
}
}
printf("%lld\n",dp[tot][sum]);
}
return ;
}
[2019南昌邀请赛网络赛D][dp]的更多相关文章
- POJ-2796 & 2019南昌邀请赛网络赛 I. 区间最大min*sum
http://poj.org/problem?id=2796 https://nanti.jisuanke.com/t/38228 背景 给定一个序列,对于任意区间,min表示区间中最小的数,sum表 ...
- 2019南昌邀请赛网络赛:J distance on the tree
1000ms 262144K DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...
- 南昌邀请赛网络赛 D.Match Stick Game(dp)
南昌邀请赛网络赛 D.Match Stick Game 题目传送门 题目就会给你一个长度为n的字符串,其中\(1<n<100\).这个字符串是一个表达式,只有加减运算符,然后输入的每一个字 ...
- 2019南昌邀请赛网络预选赛 M. Subsequence
传送门 题意: 给出一个只包含小写字母的串 s 和n 个串t,判断t[i]是否为串 s 的子序列: 如果是,输出"YES",反之,输出"NO": 坑点: 二分一 ...
- 2019 ICPC南昌邀请赛网络赛比赛过程及题解
解题过程 中午吃饭比较晚,到机房lfw开始发各队的账号密码,byf开始读D题,shl电脑卡的要死,启动中...然后听到谁说A题过了好多,然后shl让blf读A题,A题blf一下就A了.然后lfw读完M ...
- 计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解
题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...
- 2019 ICPC南昌邀请赛 网络赛 K. MORE XOR
说明 \(\oplus x\)为累异或 $ x^{\oplus(a)}$为异或幂 题意&解法 题库链接 $ f(l,r)=\oplus_{i=l}^{r} a[i]$ $ g(l,r)=\ ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- icpc 南昌邀请赛网络赛 Max answer
就是求区间和与区间最小值的积的最大值 但是a[i]可能是负的 这就很坑 赛后看了好多dalao的博客 终于a了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...
随机推荐
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- [转帖]Dockerfile: ENTRYPOINT和CMD的区别
Dockerfile: ENTRYPOINT和CMD的区别 https://zhuanlan.zhihu.com/p/30555962 在我们查阅Dockerfile的官方文档时, 有可能发现一些命令 ...
- lnmp二级域名配置相关
阿里云那域名解析那有误读 我在偏远的电信网选择中国联通解析死活解析不出来 以上这么配置就对了....选择默认.瞬间解析出来.... 出于对nginx 配置不够熟悉 后来一点点理出来. 不带www 也正 ...
- Windows10+Anaconda+PyTorch(cpu版本)环境搭建
1.安装Anaconda,具体参考网上相关教程 2.安装PyTorch 2.1 在Anaconda自带的Anaconda Prompt中创建名为PyTorch的虚拟环境[conda create -- ...
- Python27之集合
集合说:“在我的世界里,你就是唯一” 一.集合的概念和使用 集合的概念和数学里数学里集合的概念是一致的,都是一组元素的集,且元素之间不能重复.元素必须是不可变的数据类型,例如元组也可以作为其中的一个元 ...
- hadoop 节点退役和服役
节点的服役和退役(hdfs)---------------------- 黑白名单的组合情况-------------------------include //dfs.includeexclude ...
- MySQL 5.7使用xtabackup报错解决
报错信息: InnoDB: An optimized (without redo logging) DDLoperation has been performed. All modified page ...
- Java8新特性 - Java内置的四大核心函数式接口
Java内置的四大核心函数式接口 Consumer:消费型接口 对类型为T的对象应用操作,包含方法:void accept(T t) public class TestLambda02 { publi ...
- Presto个人常用操作
时间戳转日期: from_unixtime(1569168000,'yyyy-MM-dd') = '2019-09-23' '20190903'转为'2019-09-23': unix_timesta ...
- linux--安全加固脚本
Linux安全加固配置 #! /bin/bash# copyright by hwb# Function:对账户的密码的一些加固read -p "设置密码最多可多少天不修改:" A ...