QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#345267 | #3419. Infix to Prefix | PetroTarnavskyi# | TL | 0ms | 0kb | C++20 | 2.7kb | 2024-03-06 18:44:23 | 2024-03-06 18:44:23 |
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 pair<LL, LL> PLL;
typedef double db;
const LL LINF = 1e18 + 44447;
const int N = 1047;
bool was[N][N];
PLL dp[N][N];
string s;
VI idx;
void clear(int n)
{
n++;
FOR (i, 0, n)
{
FOR (j, 0, n)
{
was[i][j] = false;
dp[i][j] = {LINF, -LINF};
}
}
}
PLL solve(int l, int r)
{
if (was[l][r])
return dp[l][r];
was[l][r] = true;
if (l >= r || s[r - 1] == '+' || s[r - 1] == '-')
{
return dp[l][r] = {-LINF, -LINF};
}
if (s[l] == '+')
{
int j = lower_bound(ALL(idx), l) - idx.begin() + 1;
while (j < SZ(idx) && idx[j] < r)
{
PLL p1 = solve(l + 1, idx[j]);
PLL p2 = solve(idx[j], r);
j++;
if (p1.F == -LINF || p2.F == -LINF)
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);
}
FOR (i, max(r - 10, l + 1), r + 1)
{
PLL p1 = solve(l + 1, i);
PLL p2 = solve(i, r);
if (p1.F == -LINF || p2.F == -LINF)
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] == '-')
{
int j = lower_bound(ALL(idx), l) - idx.begin() + 1;
while (j < SZ(idx) && idx[j] < r)
{
PLL p1 = solve(l + 1, idx[j]);
PLL p2 = solve(idx[j], r);
j++;
if (p1.F == -LINF || p2.F == -LINF)
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);
}
FOR (i, max(r - 10, l + 1), r + 1)
{
PLL p1 = solve(l + 1, i);
PLL p2 = solve(i, r);
if (p1.F == -LINF || p2.F == -LINF)
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);
}
PLL p = solve(l + 1, r);
if (p.F != -LINF)
{
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] = {-LINF, -LINF};
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));
FOR (i, 0, SZ(s))
{
if (s[i] == '+' || s[i] == '-')
idx.PB(i);
}
auto p = solve(0, SZ(s));
cout << p.F << ' ' << p.S << '\n';
}
//cerr << db(clock()) / CLOCKS_PER_SEC << '\n';
return 0;
}
詳細信息
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...