dp--2019南昌网络赛B-Match Stick Game
dp--2019南昌网络赛B-Match Stick Game
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 o**p1 A2 o**p2 A3 o**p3 ⋯A**m−1 opm−1 A**m. mm must be count by himself, A_k(1 \le k \le m)A**k(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 TT denoting 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
\]
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
题意
给你一条式子,式子有火柴棒组成,可以移动火柴棒,要求:式子中运算符号的数目不变,即进行运算的数字数量不变。每组进行运算的数的位数不变。火柴棒的数目不变。式子最后得到的结果最大。
思路
把式子看成多组数进行加减运算
预处理:mx[i][j]表示在一组数中,第i位用了j根火柴所能达到的最大值,mi[i][j]同理为最小值
考虑加减号,状态转移看代码,dp[i][j]表示这条式子中用i组数,j根火柴所能达到的最大值
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 1e7 + 50;
const int MOD = 1e9 + 9;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define F(i, l, r) for(int i = l;i <= (r);++i)
#define RF(i, l, r) for(int i = l;i >= (r);--i)
int p[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};//0123456789
int sum, num, n;
string s;
LL dp[105][1005], dig[105];//第i组数第用j根火柴能到的最大值,每组数的位数
LL mx[15][1005], mi[15][1005];//一组数中第i位用j火柴可以到的最值
void solve()
{
fill(mx[0], mx[0] + 15 * 1005, -1);
fill(mi[0], mi[0] + 15 * 1005, INF);
mx[0][0] = mi[0][0] = 0;
F(i, 1, 11)//根据题目,最多有9位数
F(j, 0, i * 7)//一个数字最多用7根火柴
F(k, 0, 9)
{
if(p[k] > j) continue;
mx[i][j] = max(mx[i][j], mx[i - 1][j - p[k]] * 10 + k);
mi[i][j] = min(mi[i][j], mi[i - 1][j - p[k]] * 10 + k);
}
memset(dp, -1, sizeof(dp));
memset(dig, 0, sizeof(dig));
int len = s.size();
sum = 0, num = 1;//火柴数,组数
F(i, 0, len - 1)
{
if(s[i] == '+') {num++; sum += 2;}
else if(s[i] == '-') {num++; sum++;}
else {sum += p[s[i] - '0']; dig[num]++;}
}
F(i, 1, sum)
dp[1][i] = mx[dig[1]][i];
F(i, 2, num)//num组数
F(j, 0, sum)//一共的火柴数
F(k, 1, 7 * dig[i])//该组数所用的火柴数
{
if(j >= 2 + k && dp[i - 1][j - 2 - k] != -1 && mx[dig[i]][k] != -1)//火柴数目够,且前一组数有答案,且这一组数用k根火柴有最值
dp[i][j] = max(dp[i][j], dp[i - 1][j - 2 - k] + mx[dig[i]][k]);
if(j >= 1 + k && dp[i - 1][j - 1 - k] != -1 && mi[dig[i]][k] != INF)
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1 - k] - mi[dig[i]][k]);
}
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> s;
solve();
cout << dp[num][sum] << endl;
}
return 0;
}
参考博客
dp--2019南昌网络赛B-Match Stick Game的更多相关文章
- 南昌邀请赛网络赛 D.Match Stick Game(dp)
南昌邀请赛网络赛 D.Match Stick Game 题目传送门 题目就会给你一个长度为n的字符串,其中\(1<n<100\).这个字符串是一个表达式,只有加减运算符,然后输入的每一个字 ...
- 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)
题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...
- ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval
ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...
- 分治维护dp——19南昌网络赛C/cf750E
南昌网络赛,是cf的原题 第一次做到这种题,所以认真想了下,每次给一个询问[L,R],要求出这个区间里有2017子序列,但是不能有2016子序列需要删掉的最少元素个数 首先如果我们之询问一小段区间[L ...
- ACM-ICPC 2019南昌网络赛F题 Megumi With String
ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...
- 2019南昌网络赛 hello 2019
这道题和一道2017,2016的类似. A string t is called nice if a string “2017” occurs in t as a subsequence but a ...
- 2019南昌网络赛G. tsy's number
题意:\(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\frac{\phi(i)*\phi(j^2)*\phi(k^3)}{\phi(i)*\phi(j)*\phi(k)} ...
- 2019南昌网络赛-I(单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题意:定义一段区间的值为该区间的和×该区间的最小值,求给定数组的最大的区间值. 思路:比赛时还不会线段树,和队友在这题上 ...
- 2019南昌网络赛-M(二分)
题目链接:https://nanti.jisuanke.com/t/38232 题意:给定字符串s(长度<=1e5),然后N组样例(N<=1e5),每组输入一个字符串t判断t是否为s的字串 ...
- 2019南昌网络赛H The Nth Item(打表找询问循环节 or 分段打表)
https://nanti.jisuanke.com/t/41355 思路 从fib循环节入手,\(O(1e7log(1e9))\),tle 因为只需要输出所有询问亦或后的结果,所以考虑答案的循环节, ...
随机推荐
- [C#] Delegate, Multicase delegate, Event
声明:这篇博客翻译自:https://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in- ...
- JavaScript 操作对象属性(设置属性, setter/getter, 序列化)
参考自<<JavaScript权威指南 第6版>>, 文字太多反而不易理清其中的关系, 直接上代码和注释 /* * 对象的setter和getter属性: * 定义为一个或者两 ...
- 如何用Python实现常见机器学习算法-3
三.BP神经网络 1.神经网络模型 首先介绍三层神经网络,如下图 输入层(input layer)有三个units(为补上的bias,通常设为1) 表示第j层的第i个激励,也称为单元unit 为第j层 ...
- Android 密匙库导出
导出debug 签名----------------------------- C:\Documents and Settings\Administrator>keytool -list -v ...
- tpshop使用中遇到的问题
1.短信配置里:商家发货时是否给客户发短信 配置了 开启 如果购买者个人资料里的电话没填写,商家点击发货时, 程序会挂掉 解决方法:修改application\common\logic\SmsL ...
- javascript与java的相互调用,纯java的javascript引擎rhino(转载)
1.下载Rhino安装包,下载地址:官网http://www.mozilla.org/rhino. 2.rhino环境配置,把解压出来的js.jar文件加入到系统的环境变量classpath 3.在命 ...
- 在改变某一项目的svn的地址,relocate
不要忘记删除原有.svn文件.
- iOS Keychain 跨应用
Keychain 可以用来持久保存一些信息.通常每个应用都有自己的 Keychain Access.但有时你会需要多个应用共用一些信息.这时需要创建 Keychain Access Group. Ke ...
- LOJ121 【离线可过】动态图连通性
题目链接:戳我 [线段树分治版本代码] 这里面的线段树是时间线段树,每一个节点都要开一个vector,记录当前时间区间中存在的边的标号qwq #include<iostream> #inc ...
- Java50道经典习题-程序5 判断分数等级
题目:利用三元运算符来完成此题:从键盘录入一个整型的分数,没有负分满分为100分,学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示.分析:三元运算符的格式为:逻 ...