[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了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...
随机推荐
- linux tar包追加问题
只能已归档的文件才能追加文件. 如果tar.gz文件是如此生成:#tar -zcvf test.tar.gz a.txt即tar.gz是压缩(-z)和归档(-c)文件,则无法给它追加文件:若果tar ...
- Python re模块学习
这是re模块与正则的结合 re模块提供的函数 1.match 尝试在字符串的开头应用该模式,返回匹配对象,如果没有找到匹配,则为None. import re str1 = "Why ar ...
- PB笔记之取项次最大值(即使用.describe(" evaluate('ITM_max',0) ") 获取列的最大值) 的条件
dw_1.describe(" evaluate('ITM_max',0) ") :使用 describe 配合 evaluate 取列的最大最小值(或其它表达式)时,必须在数据 ...
- ES6语法基本使用
什么是ES6? ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.Mozilla公司将在这个标准的基础上,推出JavaScript 2. ...
- Linux踢出登陆用户的正确姿势
首先who(或w)查看需要杀死的终端名,然后执行: pkill -9 -t pts/? pkill相当于ps和kill的结合,用法和killall类似,根据进程名来杀死一类进程(kill是杀死单个) ...
- centos安装rocketMQ
1.下载安装包 http://rocketmq.apache.org/release_notes/ 这里选择 4.4.0 版本,点击进去 可以选择源码包或者二进制文件,这里选择二进制文件(ps:如果选 ...
- 【转载】网站配置Https证书系列(三):IIS网站设置Http链接直接跳转Https安全连接
Http链接请求是以明文的方式传输,在传输的过程中很容易被篡改数据,一个典型的例子就是运营商的网络劫持注入广告信息等,而Https请求则是安全加密的请求,报文数据以密文的形式进行传输.当IIS网站配置 ...
- 【日语】日语能力考试N2级核心词汇必备—接续词
日语能力考试N2级核心词汇必备—接续词 顺接 だから 因为......所以......(下文可用命令,意志劝诱等)その結果 其结果(口语,书面语都行,但是比较生硬)したがって 从而,因而(书面语, ...
- Android NDK 学习之接受Java传入的Int数组
本博客主要是在Ubuntu 下开发,且默认你已经安装了Eclipse,Android SDK, Android NDK, CDT插件. 在Eclipse中添加配置NDK,路径如下Eclipse-> ...
- Leaflet 调用百度瓦片地图服务
在使用 leaflet 调用第三方瓦片地图服务的项目,主要谷歌地图.高德地图.百度地图和 OSM 地图,与其他三种地图对比,百度地图的瓦片组织方式是不同的.百度从中心点经纬度(0,0)度开始计算瓦片, ...