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≤km) is an integer without leading zeros and less than 10^9109 , op_k (1 \le k \le m)opk(1≤km) 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 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

\[1≤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

题意

给你一条式子,式子有火柴棒组成,可以移动火柴棒,要求:式子中运算符号的数目不变,即进行运算的数字数量不变。每组进行运算的数的位数不变。火柴棒的数目不变。式子最后得到的结果最大。

思路

把式子看成多组数进行加减运算

预处理: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的更多相关文章

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

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

  2. 2019南昌网络赛I:Yukino With Subinterval(CDQ) (树状数组套主席树)

    题意:询问区间有多少个连续的段,而且这段的颜色在[L,R]才算贡献,每段贡献是1. 有单点修改和区间查询. 思路:46min交了第一发树套树,T了. 稍加优化多交几次就过了. 不难想到,除了L这个点, ...

  3. ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval

    ACM-ICPC 2019南昌网络赛I题 Yukino With Subinterval 题目大意:给一个长度为n,值域为[1, n]的序列{a},要求支持m次操作: 单点修改 1 pos val 询 ...

  4. 分治维护dp——19南昌网络赛C/cf750E

    南昌网络赛,是cf的原题 第一次做到这种题,所以认真想了下,每次给一个询问[L,R],要求出这个区间里有2017子序列,但是不能有2016子序列需要删掉的最少元素个数 首先如果我们之询问一小段区间[L ...

  5. ACM-ICPC 2019南昌网络赛F题 Megumi With String

    ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...

  6. 2019南昌网络赛 hello 2019

    这道题和一道2017,2016的类似. A string t is called nice if a string “2017” occurs in t as a subsequence but a ...

  7. 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)} ...

  8. 2019南昌网络赛-I(单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题意:定义一段区间的值为该区间的和×该区间的最小值,求给定数组的最大的区间值. 思路:比赛时还不会线段树,和队友在这题上 ...

  9. 2019南昌网络赛-M(二分)

    题目链接:https://nanti.jisuanke.com/t/38232 题意:给定字符串s(长度<=1e5),然后N组样例(N<=1e5),每组输入一个字符串t判断t是否为s的字串 ...

  10. 2019南昌网络赛H The Nth Item(打表找询问循环节 or 分段打表)

    https://nanti.jisuanke.com/t/41355 思路 从fib循环节入手,\(O(1e7log(1e9))\),tle 因为只需要输出所有询问亦或后的结果,所以考虑答案的循环节, ...

随机推荐

  1. jquery.cookie 介绍 和 用法

    1.依赖jQuery库 2.浏览器兼容性情况 3.下载 官方github:https://github.com/carhartl/jquery-cookie 4.使用 创建一个整站cookie $.c ...

  2. mvc数组绑定-jquery ajax

    var list=[];//数组 list[0]=1001; list[1]=1002; list[1]=1003; var json_data = { selected: list}; $.ajax ...

  3. python sublime run快捷键设置

    一.Ctrl+Shift+P进行插件“sublimeREPL”安装 二.打开preferences->Key Binding-User,写入以下内容 [ { "keys": ...

  4. HTTP 499 状态码 nginx下 499错误

    日志记录中HTTP状态码出现499错误有多种情况,我遇到的一种情况是nginx反代到一个永远打不开的后端,就这样了,日志状态记录是499.发送字节数是0. 老是有用户反映网站系统时好时坏,因为线上的产 ...

  5. 加载 bean*.xml

    入口 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:bean*.xml"); /** ...

  6. Codeforces766A Mahmoud and Longest Uncommon Subsequence 2017-02-21 13:42 46人阅读 评论(0) 收藏

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  7. php autoload 笔记

    php auotload 实现了类的延迟加载机制,需要的时候在include,平时很少用到.它的实现原理搜了一下如下(不是本人研究的结果): 检查执行器全局变量函数指针autoload_func是否为 ...

  8. 深海划水队项目---七天冲刺day2

    上完选修后的站立式会议: 工作进度 昨天已完成的工作: 这周任务分配,每日任务要求. 今天计划完成的工作: 推进开发进度,进一步理清开发思路. 工作中遇到的困难: 站立式会议好难统一时间啊. 燃尽图 ...

  9. eclipse中配置server中选择tomcat8无法进行下一步处理

    在创建server的时候,选择tomcat8后,server name为空,并且无法手动输入,同时无法进行下一步操作. 解决方案如下: 1.退出eclipse. 2.找到eclipse[工作空间][当 ...

  10. Javascript设计模式理论与实战:简单工厂模式

    通常我们创建对象最常规的方法就是使用new关键字调用构造函数,这会导致对象之间的依赖性.工厂模式是一种有助于消除类之间依赖性的设计模式,它使用一个方法来决定要实例化哪一个类.本文详细介绍了简单工厂模式 ...