A. Maximum Element In A Stack (模拟堆栈)

A. Maximum Element In A Stack (模拟堆栈)

题目链接:https://nanti.jisuanke.com/t/41285

思路:把题目中说的代码复制过来,在push和pop操作时,记录一个堆栈每一层的最大值,并根据题目要求计算答案。

注:有一点是在运算过程p*i超int了, 在p*i的前面加一个1LL*就A了, ans=(ans^( 1LL*p*i ));

代码:

#include<bits/stdc++.h>
#define uint unsigned int
using namespace std;
int n,q,p,m;
unsigned int SA,SB,SC;
unsigned int maxx[50000005];
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;
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int u=1; u<=T; u++)
    {
        scanf("%d%d%d%d%u%u%u",&n,&p,&q,&m,&SA,&SB,&SC);
        long long ans=0;
        int max1,max2;
        max1=max2=-1;
        stack<unsigned int>sta;
        memset(maxx,0,sizeof(maxx));
        for(int i=1; i<=n; i++)
        {
            if((rng61()%(q+p))<p)
            {
                uint o=rng61()%m+1;
                if ( maxx[sta.size()]<=o ) {
                    maxx[sta.size()+1] = o;
                }
                else {
                    maxx[sta.size()+1] = maxx[sta.size()];
                }
                sta.push(o);
            }
            else
            {
                if(sta.size()!=0)
                {
                    sta.pop();
                }
            }
            if(sta.size()!=0)
            {
                unsigned int p=maxx[sta.size()];
                ans=(ans^(1LL*(p)*i));
            }
        }
        printf("Case #%d: ",u);
        printf("%lld\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值