QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#345261#3419. Infix to PrefixPetroTarnavskyi#TL 0ms0kbC++201.9kb2024-03-06 18:21:572024-03-06 18:21:57

Judging History

你现在查看的是最新测评结果

  • [2024-03-06 18:21:57]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-03-06 18:21:57]
  • 提交

answer

#include <bits/stdc++.h> 

using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int INF = 1e9 + 47;
const int N = 1047;
bool was[N][N];
PII dp[N][N];
string s;
	
void clear(int n)
{
	n++;
	FOR (i, 0, n)
	{
		FOR (j, 0, n)
		{
			was[i][j] = false;
			dp[i][j] = {INF, -INF};
		}
	}
}

PII solve(int l, int r)
{
	if (was[l][r])
		return dp[l][r];
	was[l][r] = true;
	if (l == r)
	{
		return dp[l][r] = {-INF, -INF};
	}
	if (s[l] == '+')
	{
		FOR (i, l + 1, r + 1)
		{
			PII p1 = solve(l + 1, i);
			PII p2 = solve(i, r);
			if (p1.F == -INF || p2.F == -INF)
				continue;
			dp[l][r].F = min(dp[l][r].F, p1.F + p2.F);
			dp[l][r].S = max(dp[l][r].S, p1.S + p2.S);
		}
	}
	else if (s[l] == '-')
	{
		FOR (i, l + 1, r + 1)
		{
			PII p1 = solve(l + 1, i);
			PII p2 = solve(i, r);
			if (p1.F == -INF || p2.F == -INF)
				continue;
			dp[l][r].F = min(dp[l][r].F, p1.F - p2.S);
			dp[l][r].S = max(dp[l][r].S, p1.S - p2.F);
		}
		PII p = solve(l + 1, r);
		if (p.F != -INF)
		{
			dp[l][r].F = min(dp[l][r].F, -p.S);
			dp[l][r].S = max(dp[l][r].S, -p.F);
		}
	}
	else
	{
		if (l + 1 == r)
		{
			return dp[l][r] = {s[l] - '0', s[l] - '0'};
		}
		if (s[l] == '0' || r - l > 9)
			return dp[l][r] = {-INF, -INF};
		int x = stoi(s.substr(l, r - l));
		return dp[l][r] = {x, x};
	}
	
	return dp[l][r];
}


int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	while (cin >> s)
	{
		clear(SZ(s));
		auto p = solve(0, SZ(s));
		cout << p.F << ' ' << p.S << '\n';
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

+123456789012
+102
+012
+210
0
2
-9-065
-0+60-73191753+057
-79+93+-0516+1-745503--638-+83
++9+25107+33611++20-14+3+1041+3-+6+97+-086
-8836+553609318-246-9-19+7+5400+48+-8+19-845139-049-65
+432863-90216+-973-0-+2486319++-37926-7582+++719478285231+9-2+6332
-47-94752-714+5033-61-91+67+9714119-++99284-4...

output:


result: