QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#216196 | #6750. Calculate | Eunoiay | AC ✓ | 1ms | 3520kb | C++20 | 1.6kb | 2023-10-15 16:38:01 | 2023-10-15 16:38:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
map<char, int> pr {
{'(', 0}, {')', 0}, {'+', 1}, {'-', 1},
{'*', 2}, {'/', 2}
};
i64 eval(const string &s) {
int n = s.size();
stack<char> op;
stack<int> num;
auto calc = [&]() {
i64 rhs = num.top(); num.pop();
i64 lhs = num.top(); num.pop();
char o = op.top(); op.pop();
i64 res = 0;
if (o == '+') {
res = lhs + rhs;
}
if (o == '-') {
res = lhs - rhs;
}
if (o == '*') {
res = lhs - rhs;
}
if (o == '/') {
res = lhs / rhs;
}
num.push(res);
};
for (int i = 0; i < n; ++ i) {
char c = s[i];
if (isdigit(c)) {
i64 x = 0;
int j = i;
while (j < n && isdigit(s[j])) {
x = x * 10LL + (s[j] - '0');
++ j;
}
i = j - 1;
num.push(x);
} else if (c == '(') {
op.push(c);
} else if (c == ')') {
while (!op.empty() && op.top() != '(') {
calc();
}
op.pop();
} else {
while (!op.empty() && pr[op.top()] >= pr[c]) {
calc();
}
op.push(c);
}
}
while (!op.empty()) {
calc();
}
return num.top();
}
void solve() {
string s;
cin >> s;
int n = s.size();
vector<int> q;
for (int i = 0; i < n; ++ i) {
if (s[i] == '?') {
q.push_back(i);
}
}
int ans = -0x3f3f3f3f;
for (char x : {'0', '9'}) {
for (char y : {'0', '9'}) {
if (q.size() > 0) {
s[q[0]] = x;
}
if (q.size() > 1) {
s[q[1]] = y;
}
ans = max<i64>(ans, eval(s));
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3436kb
input:
?+?
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3440kb
input:
(?+9)-(?+1)
output:
17
result:
ok 1 number(s): "17"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3488kb
input:
((9)-(((8)-(2))+(((1+(1))-(1+((2)+2+2)))+(5)+4))+(((7)-((9)+3))-((8)-(0-(2))+0))+((6)-(6+(((4)-(9))-(8-((9)+(1))+(0)))+(2-((9)+7))-(1)))-((((7)+(1))-((3)+(3)))-((2)-((6)-((3)-(8)))))+(2+0-((6)-(1))))-((((3)-(((0)+((4)-(9))+((6+8)+4)+(5)-(4-(3)-(8)))-((8)-(2))))+(((2)-(4))+(6)-(2))+(6-(1))-((2+9)-(3+...
output:
-63
result:
ok 1 number(s): "-63"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3424kb
input:
(((((4)-((5)+(1))-(6-(8-(1))+((0)+(9))))+((2-(2))+(3+3-(((7)-(6))-(3))+(((3)+((5)-((0)-(6-(0)))))+((0)+((0)-(7)))+1-((3)-((8)-(8)))))))-(((((5-(3)-((((8)+0)+(9)-((7)-(9)))-(7)))+(2))+8+((4)-(6)+((5)-((7)+(2))))-(8-((7)+(3)+(9))))-(((9-(9-(8)))+1-((((4)+(9))+(8))+(2+(7)+3)))+((((6)+(2))-(1)-((((6+7-(...
output:
356
result:
ok 1 number(s): "356"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3448kb
input:
((((((0+(5))+6)-((4)+(((7)+2)+6)-((((7)-(9))-(1))-((8-(2))+(5))-(2-((5)-(8))))-(3)))+((8)-(6)+3))+(7-(((4)+(6))+(((6)-(4))-(5))-(5-(4))-(5)+(2-(2)))+(6+(3+(2)+(7)+3)))+(((3)+(4)+(7)-(6-(5)))-((9+(8))+((1)-(0)))+((((8)+1)-(4))+(4))+(((4)-(4))+9+(((8)+3)-(6)+5)))-(((((5)+(9))-((3)-(0)))+((((0)+3)-(2))...
output:
-185
result:
ok 1 number(s): "-185"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3484kb
input:
((((1-(4-(9)+((5)+(5)))+(((6)-(6)+((2)-(6)))-((7)+6)-(3)))-(2-((((7)+(8+(6)))-(5)+(7-((2)-(8)))+(4)-(((8)-(1))+(1)))-((3-(5)+(2-(1)))+2+(5)-(4))-(7)))+(9-(9+5)-(7)-((6)-(4)))+(((1+6)+(6+0-(9))+(((9+((2)+((1)-(5)))-(1))-(1))-(3+8-(2))))-((4)+((7)-(7))-(((2)-(6))+3)-((2-(3))+(4)-((0)-(1+(7)))+((0)+2))...
output:
-20
result:
ok 1 number(s): "-20"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3428kb
input:
(((0-(((6)+(2))+4-(4)-(2))-((8)+4+(2))+2+(1+(9))+((2+(3))+(((8-(6))-(9))+(6)-((4)+0))-((7)-(3)-((3)+((3)+6-(2)))-(4))))-(((6)-((5)-(5-((9)+1-(6)))))+8)+(((8+(5)-(1))-(5))+(7-(0)-(0))+(((3)+(2)+(6)+0+(7)+(7)+(0+0-((1)+3)))-(((5+(8))-(9))-(8-((4)+9+(2)-(6-(6)-(4+5)))))+(2)-((((2)+1)+(0)+(3))+2))))-(((...
output:
200
result:
ok 1 number(s): "200"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
(8)+((7)+4)+(3)-(((3-(0)+(3+4)+(6-(6)+(7-((8+(8))-(7)+((5)-(6+2)))))-((8+(4))+((0)-((4)-(6)+(4)+((7+9)+(5+4-(5))))-(8))))-((5)+((2)-(6))+(5)))+(8+(((3)-(8))-(7)+(7-(0+(4-(1))-((7)+(0)+((7)-(1)))))))+(7-(5))+((((((2-(((3)+1)+(((2)+(5))+4)))+(((5)+4-(6))+(((2)+(2))-(4)-(4-(9))+((0)+(1))))+9-(8)+((6)+(...
output:
-24
result:
ok 1 number(s): "-24"
Test #9:
score: 0
Accepted
time: 1ms
memory: 3492kb
input:
(((9-(4-(9-(1)))+((8)-(1))+(5-(3+2)))+(((((((1)+0)+9)-(8))+(6-(4)-(0)))-(2+((((6)-(6)-(8))-((5)-(6)))-((7)+(1)))))+(((1-(8)+(9-((7)-(1)))+(0)-((4)-(0)))+1+(9)-((((6)-(1))-(7))-(6+3+8))-((1)+(1)-(9-(9))+8-(((9)-(5))+3-(9))+(3-((8)-(3))))+(3)-(0)-(2-(2-(8)))-(5+7)+(4)+(5)+(8+((6)-(2))+(3-(8))))-((0)-(...
output:
-69
result:
ok 1 number(s): "-69"
Test #10:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
((((((2+(((9)+(9))-(5)))+(0))+((2)-(5+8)-((((3)+(4))+9+((5-(((6)+(9))-(6)))-((8)+0)+(6)))+((4-(7))+(4)+(5-(1))+(4)-(4)+4))))-(((9)+((2)-(9))-(1)-(0-(3)+1))+(5)-((6-((9)+8))-(6-(4)))-(6))-(((((7)+8)-((3)+(0)-(1)-(((2)-(1))-(5))))+((2+(0))-((8)-((4)-(6))-((5)-(2))+(7))))-((8)-(9)-((8+(0-(0-(4)))+5)+(5...
output:
40
result:
ok 1 number(s): "40"
Test #11:
score: 0
Accepted
time: 1ms
memory: 3416kb
input:
(((((((0)-(0))-((((5)-((0)-(0)))+(((1)-(7+6))-((5)+0)))+3+7))-((4+4)-(6)-(9-(3+(1)))))-((((0)-(9)-(2))-((1-(2))-(5-(?))-((6)+(0)-(0))-(8+(9-(2)))))+(((7+4+(0)-((9)+8+(0+3+0)))-(2-(8+0)))-(0))))+(((((9)-(9))+((3+3+4)+(1)+(6)+((1)-(5))))-(3)-((6)+(5)))+(5)+4+1)+(((((((1)-(7))-((9)+(2)+1-(9+(4))))+(((4...
output:
-143
result:
ok 1 number(s): "-143"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3480kb
input:
((((((8+(6))+(3)-(1)+((4-(8))+4)+(((9)-((0-(3))+(1))-(4))-((2-(6))-(((9-(3))+(1)-((7)-((1)+((2)-(2)))))+(2+(6-(8)))))+(2+(0)))+(5+9+((2+4-(0))-(((5)+(1+3)-(((7-(1))-(2))-(1)))-(9+((2)-(3)))))+(((7)-((5)+1+9-(1)))-((4)-(5-(0+5)-((8)-((5)+(0))-((8)+(0)-(4)-(0)))))))-(((3)+(0)+3)-(8-(0)+4)))+((1-(2)+((...
output:
52
result:
ok 1 number(s): "52"