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 因为只需要输出所有询问亦或后的结果,所以考虑答案的循环节, ...
随机推荐
- cpuinfo
在Linux系统中,如何详细了解CPU的信息呢? 当然是通过cat /proc/cpuinfo来检查了,但是比如几个物理CPU/几核/几线程,这些问题怎么确定呢? 经过查看,我的开发机器是1个物理CP ...
- 创建圆角 抛出一个错误:二元运算符“|”不能用于两个UIRectCorner操作数
// let beizer:UIBezierPath = UIBezierPath(roundedRect: btn5.bounds, byRoundingCorners: UIRect ...
- (转)【javascript基础】原型与原型链
原文地址:http://www.cnblogs.com/allenxing/p/3527654.html 前言 原型是什么 理解原型对象 原型对象 isPrototypeOf hasOwnProper ...
- ZOJ3703 Happy Programming Contest 2017-04-06 23:33 61人阅读 评论(0) 收藏
Happy Programming Contest Time Limit: 2 Seconds Memory Limit: 65536 KB In Zhejiang University P ...
- underscore arrays
1._.first():返回array(数组)的第一个元素.传递 n参数将返回数组中从第一个元素开始的n个元素 _.first = _.head = _.take = function(array, ...
- Oracle EBS Patch Demo
Oracle EBS APP & DB 打补丁过程简述l例子: 打 Patch# 11843100:R12.CAC.B 打PATCH之前先查询一下是否已经有了这个PATCH. SELECT ...
- Python学习-14.Python的输入输出(三)
在Python中写文件也是得先打开文件的. file=open(r'E:\temp\test.txt','a') file.write('append to file') file.close() 第 ...
- .Net工程师面试笔试宝典
.Net工程师面试笔试宝典 传智播客.Net培训班内部资料 http://net.itcast.cn 这套面试笔试宝典是传智播客在多年的教学和学生就业指导过程中积累下来的宝贵资料,大部分来自于学员从面 ...
- [转载]灵动思绪EF(Entity FrameWork)
很久之前就想写这篇文章了,但是由于种种原因,没有将自己学习的EF知识整理成一片文章.今天我就用CodeFirst和ModelFirst两种方式的简单案例将自己学习的EF知识做个总结. 在讲解EF之前, ...
- Jenkins pipeline中使用内置全局变量
在pipeline中不像在windows batch command中直接%WORKSPACE%这样 需要写成这样: echo env.WORKSPACE