A 了,但有疑问
查看原帖
A 了,但有疑问
833737
Lyw_and_Segment_Tree楼主2024/12/10 22:16

rt,首先,自然的模拟做法能拿 40 pts,栈的做法也只能拿到后 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> s;
    
    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 (s.empty()) {
                    ans = work(ans, 1);
                } else {
                    s.pop();
                }
            } else if (opt == 'L') {
                s.push(2);
            } else if (opt == 'R') {
                s.push(3);
            }
        }
    }
    
    if (n > 50) {
        
        while (!s.empty()) {
            op = s.top(), s.pop(), ans = work(ans, op);
        }
        
    }
    
    fastio::write(ans);
}
2024/12/10 22:16
加载中...