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:

  1. The new expression should also be made up of mm numbers and m - 1m−1 operators.
  2. The number of digits per number should keep consistent with the original.
  3. 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]的更多相关文章

  1. POJ-2796 & 2019南昌邀请赛网络赛 I. 区间最大min*sum

    http://poj.org/problem?id=2796 https://nanti.jisuanke.com/t/38228 背景 给定一个序列,对于任意区间,min表示区间中最小的数,sum表 ...

  2. 2019南昌邀请赛网络赛:J distance on the tree

    1000ms 262144K   DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...

  3. 南昌邀请赛网络赛 D.Match Stick Game(dp)

    南昌邀请赛网络赛 D.Match Stick Game 题目传送门 题目就会给你一个长度为n的字符串,其中\(1<n<100\).这个字符串是一个表达式,只有加减运算符,然后输入的每一个字 ...

  4. 2019南昌邀请赛网络预选赛 M. Subsequence

    传送门 题意: 给出一个只包含小写字母的串 s 和n 个串t,判断t[i]是否为串 s 的子序列: 如果是,输出"YES",反之,输出"NO": 坑点: 二分一 ...

  5. 2019 ICPC南昌邀请赛网络赛比赛过程及题解

    解题过程 中午吃饭比较晚,到机房lfw开始发各队的账号密码,byf开始读D题,shl电脑卡的要死,启动中...然后听到谁说A题过了好多,然后shl让blf读A题,A题blf一下就A了.然后lfw读完M ...

  6. 计蒜客 2019南昌邀请网络赛J Distance on the tree(主席树)题解

    题意:给出一棵树,给出每条边的权值,现在给出m个询问,要你每次输出u~v的最短路径中,边权 <= k 的边有几条 思路:当时网络赛的时候没学过主席树,现在补上.先树上建主席树,然后把边权交给子节 ...

  7. 2019 ICPC南昌邀请赛 网络赛 K. MORE XOR

    说明 \(\oplus x​\)为累异或 $ x^{\oplus(a)}​$为异或幂 题意&解法 题库链接 $ f(l,r)=\oplus_{i=l}^{r} a[i]$ $ g(l,r)=\ ...

  8. 计蒜客 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 ...

  9. icpc 南昌邀请赛网络赛 Max answer

    就是求区间和与区间最小值的积的最大值 但是a[i]可能是负的 这就很坑 赛后看了好多dalao的博客 终于a了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...

随机推荐

  1. python 之 数据库(数据库安装方法、基本sql语句、存储引擎)

    第十章 数据库 10.1 数据库介绍 1.数据库相关概念 数据库服务器:本质就是一个台计算机,该计算机之上安装有数据库管理软件的服务端 数据库管理系统RDBMS:本质就是一个C/S架构的套接字软件 库 ...

  2. 18 COUNTIF函数

    求大于小于等于某个数字的数字有多少 格式:=COUNTIF(数据区,"条件") 注意条件需要用英文双引号引起来. 举个例子: =COUNTIF(A2:D5,">20 ...

  3. PAT(B) 1038 统计同成绩学生(C)统计

    题目链接:1038 统计同成绩学生 (20 point(s)) 题目描述 本题要求读入 N 名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式 输入在第 1 行给出不超过 10​5​​ 的正整 ...

  4. PAT甲级题分类汇编——线性

    本文为PAT甲级分类汇编系列文章. 线性类,指线性时间复杂度可以完成的题.在1051到1100中,有7道: 题号 标题 分数 大意 时间 1054 The Dominant Color 20 寻找出现 ...

  5. python装饰器无参及有参案例

    装饰器无参和有参案例:

  6. truncate删除一个分区,测试全局索引是否失效

    目的,有一个清理数据的需求,需要删除历史的一个分区所有记录信息,但是存在主键global索引,如何更好的维护. 如下测试流程一 提前创建好一个已时间created 字段作为分区键的范围分区表 SQL& ...

  7. VC串口通讯,WriteFile或ReadFile没有任何返回??

    别犯低级错误,一定要设置读写超时!!!

  8. NetCore.SignalR.Demo演示

    项目github,点击https://github.com/wangpengzong/NetCore.SignalR.Demo 1.打开服务端Server(\SignalR.Server\bin\De ...

  9. .net core 3.0更改默认身份认证的的表。

    public class ApplicationDbContext : IdentityDbContext<WebUser, WebRole, Guid, WebUserClaim, WebUs ...

  10. Java集合源码阅读之HashMap

    基于jdk1.8的HashMap源码分析. 引用于:http://blog.stormma.me/2017/05/31/Java%E9%9B%86%E5%90%88%E6%BA%90%E7%A0%81 ...