宁夏区域赛A题 A. Maximum Element In A Stack

本文介绍了一种特殊的数据结构——智能栈,它能够在进行插入和删除操作的同时,实时更新并提供当前栈内的最大值。文章通过一个具体的编程挑战详细阐述了如何实现这一功能,并附带了解决方案的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As an ACM-ICPC newbie, Aishah is learning data structures in computer science. She has already known that a stack, as a data structure, can serve as a collection of elements with two operations:

  • push, which inserts an element to the collection, and
  • pop, which deletes the most recently inserted element that has not yet deleted.

Now, Aishah hopes a more intelligent stack which can display the maximum element in the stack dynamically. Please write a program to help her accomplish this goal and go through a test with several operations.

Aishah assumes that the stack is empty at first. Your program will output the maximum element in the stack after each operation. If at some point the stack is empty, the output should be zero.

Input Format

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.

To avoid unconcerned time consuming in reading data, each test case is described by seven integers n~(1\le n\le 5 \times 10^6)n (1≤n≤5×106), pp, qq, m~(1\le p, q, m\le 10^9)m (1≤p,q,m≤109), SASA, SBSB and SC~(10^4 \le SA, SB, SC\le 10^6)SC (104≤SA,SB,SC≤106).The integer nn is the number of operations, and your program should generate all operations using the following code in C++.

 

 

1

int n, p, q, m;

2

unsigned int SA, SB, SC;

3

unsigned int rng61(){

4

    SA ^= SA << 16;

5

    SA ^= SA >> 5;

6

    SA ^= SA << 1;

7

    unsigned int t = SA;

8

    SA = SB;

9

    SB = SC;

10

    SC ^= t ^ SA;

11

    return SC;

12

}

13

void gen(){

14

    scanf("%d%d%d%d%u%u%u", &n, &p, &q, &m, &SA, &SB, &SC);

15

    for(int i = 1; i <= n; i++){

16

        if(rng61() % (p + q) < p)

17

            PUSH(rng61() % m + 1);

18

        else

19

            POP();

20

    }

21

}

The procedure PUSH(v) used in the code inserts a new element with value vv into the stack and the procedure POP() pops the topmost element in the stack or does nothing if the stack is empty.

Output Format

For each test case, output a line containing Case #x: y, where xx is the test case number starting from 11, and yy is equal to \mathop{\oplus}\limits_{i = 1}^{n}{\left(i \cdot a_i\right)}i=1⊕n​(i⋅ai​)where a_iai​ is the answer after the ii-th operation and \oplus⊕ means bitwise xor.

Hint

The first test case in the sample input has 44operations:

  • POP();
  • POP();
  • PUSH(1);
  • PUSH(4).

The second test case also has 44 operations:

  • PUSH(2);
  • POP();
  • PUSH(1);
  • POP().

样例输入

2
4 1 1 4 23333 66666 233333
4 2 1 4 23333 66666 233333

样例输出

Case #1: 19
Case #2: 1

题目来源

The 2018 ACM-ICPC Chinese Collegiate Programming Contest

题解:又学习了一个好玩的东西,单调栈,顾名思义是维护一个单调递增的有着栈的性质的数组,当push一个元素时当且仅当该元素>栈顶元素才将元素push进来,这样从你的栈最后一个元素的下标到push进去下一个元素的区间最大值即栈顶元素。数据结构还是很有意思的,需要我耐下心来仔细琢磨。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5000005;

ll ans;
ll s[maxn];
int n,p,q,m;
unsigned int SA, SB, SC;
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()
{
    int top=0;
    for(ll i = 1; i <= n; i++)
    {
        if(rng61() % (p + q) < p)
        {
            ll res=rng61()%m + 1;
            s[++top]=res;
            s[top]=max(s[top-1],s[top]);
        }
        else
            top=max(top-1,0);

        ans^=(s[top]*1LL*i);
    }
    printf("%lld\n",ans);
}

int main()
{
    int T;
    cin>>T;
    for(int k=1;k<=T;k++)
    {
        scanf("%d%d%d%d%u%u%u", &n, &p, &q, &m, &SA, &SB, &SC);
        ans=0;
        printf("Case #%d: ",k);
        gen();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值