[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了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...
随机推荐
- Deepin安装JavaFX
1.环境:JDK1.8(目前国内好用的版本里比较流行的是这个版本),deepin 15.11(linux内核是5.2.9,安装了大黄蜂驱动): 2.由于是用的JDK1.8,所以没法用最新的openjf ...
- LeetCode 279. 完全平方数(Perfect Squares) 7
279. 完全平方数 279. Perfect Squares 题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数 ...
- Git使用总结(二):分支管理
1.创建分支 a.直接创建 git branch dev(分支名) b.基于某个历史版本创建分支 git branch dev HEAD 2.查看分支 git branch -av 3.删除分支 gi ...
- Oracle通过ODBC链接SqlServer数据库
原网址:https://www.cnblogs.com/jijm123/p/11598515.html 第一步.创建ODBC数据源 这一步要考虑数据源是32位还是64位的问题,其实就是选择不同的exe ...
- springboot 使用consul 读取配置文件(遇到的坑太多,没记录)
最终成功版. pom引入mavn依赖: <!--consul--> <dependency> <groupId>org.springframework.cloud& ...
- hdu 2353 n皇后问题
Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上.你的任务是,对于给定的N, ...
- JDK8源码解析 -- HashMap(一)
最近一直在忙于项目开发的事情,没有时间去学习一些新知识,但用忙里偷闲的时间把jdk8的hashMap源码看完了,也做了详细的笔记,我会把一些重要知识点分享给大家.大家都知道,HashMap类型也是面试 ...
- idea for mac 快捷键整理
⌘O 查找类文件 ⌘⌥O 前往指定的变量 / 方法 ⌘⇧O 查找所有类型文件.打开文件.打开目录,打开目录需要在输入的内容前面或后面加一个反斜杠/ ⌘⌥← / ⌘⌥→ 退回 / 前进到上一个操作的地方 ...
- 线程三(Mutex)
C# 中 Mutex 类也是用于线程同步操作的类,例如,当多个线程同时访问一个资源时保证一次只能有一个线程访问资源. 在 Mutex 类中,WaitOne() 方法用于等待资源被释放, Release ...
- SqlServer2008 / SqlServer2012 禁用windows登录,sa忘记密码或密码过期如何登陆
以管理员身份运行cmd 1.cmd 下 停止SqlServer服务,net stop mssqlserver: 2.新建windows账号test,加入administrators组里,授予管理员权 ...