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. BUILDING ANGULAR APPS USING FLUX ARCHITECTURE

    Flux is an architectural pattern based on unidirectional data flow. Although it is originated in the ...

  2. pcd转obj

    文件转换 从PCD文件写入和读取点云数据:https://www.cnblogs.com/li-yao7758258/p/6435568.html 点云数据格式PCD(Point Cloud Data ...

  3. Altera FPGA 开启引脚片上上拉电阻功能

    本博文以矩阵键盘实验为例,介绍了如何开启FPGA管脚的片上上拉电阻. Cyclone IV E FPGA的通用输入输出管脚都支持内部弱上拉电阻,但是时钟输入脚不支持.所以,当需要上拉电阻的信号(如本例 ...

  4. 简述负载均衡和CDN技术

    曾经见到知乎上有人问“为什么像facebook这类的网站需要上千个工程师维护?”,下面的回答多种多样,但总结起来就是:一个高性能的web系统需要从无数个角度去考虑他,大到服务器的布局,小到软件中某个文 ...

  5. SQL -- What Tables Queries are Used to Display the Counts in the Inventory Account Periods form (INVTTGPM.fmb) (Doc ID ID 357997.1)

    Applies to: Oracle Inventory Management - Version 11.5.9 to 12.1.3 [Release 11.5 to 12.1] Informatio ...

  6. SqlServer 的一个坑

    以前一直以为sqlserver 在做ddl 操作的时候是锁表的,而oracle 是锁行,感觉oracle 要比sqlserver 先进一些,但是这是我的认识错误.其实sqlserver 也是可以锁行的 ...

  7. [leetcode] 13. Remove Duplicates from Sorted List

    这个题目其实不难的,主要是我C++的水平太差了,链表那里绊了好久,但是又不像用python,所以还是强行上了. 题目如下: Given a sorted linked list, delete all ...

  8. [Postgres]postgresql.conf : Permission denied处理一法

    使用yum安装完postgresql,没有使用默认的DATA地址,自己配置了DATA地址以后,使用root权限启动service service postgresql start ,报出了" ...

  9. Spring中ApplicationContext和beanfactory区别---解析一

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

  10. C# print pos winform

    先将pos机设置为默认 控制面板->打印机和传真->右键->服务器属性 首先创建 ClassPrint 对象 using System; using System.Drawing; ...