The 2018 ACM-ICPC CCPC 宁夏 A-Maximum Element In A Stack
题意
对一个栈有入栈和出栈两种操作,求每次操作后栈的最大值的异或。
分析
类似于单调栈,但是还没有那么复杂。
只需保持栈顶为最大值,如果入栈元素小于栈顶元素,则重复一次栈顶元素入栈;否则,直接入栈。
大概长这样:
#include<bits/stdc++.h> using namespace std; typedef long long ll; int n, p, q, m; unsigned int SA, SB, SC; const int maxn = 5e6 + 10; ll sta[maxn], top = 0; ll PUSH(ll x) { sta[++top] = x; return sta[top] = max(sta[top], sta[top-1]); } ll POP() { return sta[top = max(top-1, 0ll)]; } unsigned int rng61() { SA ^= SA << 16; SA ^= SA >> 5; SA ^= SA << 1; unsigned int t = SA; SA = SB; SB = SC; SC ^= t ^ SA; return SC; } ll gen() { ll ret = 0, tmp; scanf("%d%d%d%d%d%d%d", &n, &p, &q, &m, &SA, &SB, &SC); for(int i = 1;i <= n;i++){ if(rng61() % (p+q) < p) tmp = PUSH(rng61()%m + 1); else tmp = POP(); //printf("%d\n", tmp); ret ^= (i * tmp); } return ret; } int main() { int T, kase = 0; scanf("%d", &T); while(T--) { top = 0; ll ans = gen(); printf("Case #%d: %lld\n", ++kase, ans); } return 0; }
参考链接:https://blog.csdn.net/Aerry_ale/article/details/81089375
个性签名:时间会解决一切