rt, 模拟做法只能拿到前 40pts,栈做法只能拿到后 60 pts,拼在一起就过了,没理解,求问为什么。
code :
#include <bits/stdc++.h>
#define ll __int128
#define db double
#define endl "\n"
namespace fastio {
char buf[1 << 21], *p1 = buf, *p2 = buf;
const ll getc() {
return p1 == p2 && ( p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++;
}
const ll read() {
ll x = 0, f = 1;
char ch = getc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1; ch = getc();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getc();
}
return x * f;
}
const void write(ll x) {
if (x < 0) {
putchar('-'), x = -x;
}
ll sta[35], top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}
}
using namespace std;
ll n = fastio::read(), s = fastio::read(), ans = s, op;
ll work(ll x, ll opt) {
if (opt == 1) {
if (x <= 1) {
return 1;
} else {
return (x % 2 == 1 ? (x - 1) / 2 : x / 2);
}
} else if (opt == 2) {
return x * 2;
} else {
return x * 2 + 1;
}
}
int main() {
stack<ll> sta;
for (ll i = 1; i <= n; i++) {
char opt = fastio::getc();
if (n <= 50) {
if (opt == 'U') {
op = 1;
} else if (opt == 'L') {
op = 2;
} else if (opt == 'R') {
op = 3;
}
ans = work(ans, op);
} else {
if (opt == 'U') {
if (sta.empty()) {
ans = work(ans, 1);
} else {
sta.pop();
}
} else if (opt == 'L') {
sta.push(2);
} else if (opt == 'R') {
sta.push(3);
}
}
}
if (n > 50) {
while (!sta.empty()) {
op = sta.top(), sta.pop(), ans = work(ans, op);
}
}
fastio::write(ans);
}