[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了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...
随机推荐
- [转帖]Redis未授权访问漏洞复现
Redis未授权访问漏洞复现 https://www.cnblogs.com/yuzly/p/11663822.html config set dirconfig set dbfile xxxx 一. ...
- Jenkins+maven+gitlab自动化部署之Jenkins系统管理配置(四)
一.Jenkins全局工具配置 在jenkins首页依次进入系统管理>>全局工具配置: 1) jdk.git.maven配置 指定其在服务器中的目录位置 二.插件管理 1)依次点开系统管理 ...
- Model 的使用
1. 设计数据结构 问题表Question:作用存放问题 id 主键 自增 question_text 题目 varchar120 created 创建时间 datetime 选项表Choice: ...
- shell从简单到脱坑
1.计算1-100的和(seq 1 100 使用反引号括起来的比较坑) #!/bin/bash ` do sum=$[$i+$sum] done echo $sum 2.编写shell脚本,要求输入一 ...
- AJAX个人草稿
/*var CUSTOMS_SEX=arr[2]; var CUSTOMS_TELEPHONE=arr[6]; mui.openWindow({ url:'userinfol.html', id:'u ...
- Fiddler讲解3
想要 浏览更多Fiddler内容:请点击进入Fiddler官方文档 阅读目录: 一.Fiddler自定义请求: 二.Fiddler修改请求: 三.减少期望的延迟:100个继续标题: 四.重命名无效的P ...
- Qt更新组件出现(“要继续此操作,至少需要一个有效且已启用的储存库”)
Qt更新组件出现(“要继续此操作,至少需要一个有效且已启用的储存库”) 目的: 当时在安装Qt时,有些组件暂时没用着,然后过一段时间后,需要用到某些该组件时,不用删掉重新再安装. 操作: Wind ...
- 题解-AtCoder ARC-078F Mole and Abandoned Mine
problem ATC-arc078F 题意概要:给定一个 \(n\) 点 \(m\) 边简单无向图(无自环无重边),边有费用,现切去若干条边,使得从 \(1\) 到 \(n\) 有且仅有一条简单路径 ...
- 升级win10 1903版后,vmware打开虚拟机黑屏的解决办法
按照网上给的方法(1-3),又增加了几步(从4开始,只在我自己电脑上实践过): 1. 打开cmd,执行以下命令 netsh winsock reset 2. 重启电脑 3. 以管理员身份执行vmwar ...
- python实现ssh及sftp功能
1.在Linux上我们通过scp命令实现主机间的文件传送,通过ssh实现远程登录 ,比如 我们经常使用的xshell远程登录工具,就是基础ssh协议实现window主机远程登录Linux主机 下面简单 ...