LDU 个人训练三 宁夏邀请赛 A Maximum Element In A Stack

Problem A. Ma

ximum Element In A Stack Time limit: 10 seconds 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

The input contains several test cases, and the first line is a positive integer T indicating the number of test cases which is up to 50. To avoid unconcerned time consuming in reading data, each test case is described by seven integers n (1 ≤ n ≤ 5 × 106 ), p, q, m (1 ≤ p, q, m ≤ 109 ), SA, SB and SC (104 ≤ SA, SB, SC ≤ 106 ). The integer n 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 v into the stack and the procedure POP() pops the topmost element in the stack or does nothing if the stack is empty.

Output

For each test case, output a line containing Case #x: y, where x is the test case number starting from 1, and y is equal to n ⊕ i=1 (i · ai) where ai is the answer after the i-th operation and ⊕ means bitwise xor. Page 1 of 17 The 2018 ACM-ICPC China Multi-Provincial Collegiate Programming Contest Sample standard input standard output

2

4 1 1 4 23333 66666 233333

4 2 1 4 23333 66666 233333

Case #1: 19

Case #2: 14

Hint The first test case in the sample input has 4 operations: • POP(); • POP(); • PUSH(1); • PUSH(4). The second test case also has 4 operations: • PUSH(2); • POP(); • PUSH(1); • POP().

题目大意:

. Maximum Element In A Stack 现在有一个栈,初始为空。接下来有若干次操作,每次可能向栈顶 push 一个正整数,也可能 pop 掉 栈顶元素。 你需要在每次操作之后计算出栈内所有元素的最大值。如果栈为空则认为此时最大值是 0。 为了避免输入文件过大,所有操作会使用 rng61 算法生成。同时为了避免输出文件过大,你只需要 输出一个数:表示每次操作之后的答案与下标乘积的异或和。 输入格式 第一行包含一个整数 T,表示测试数据的组数。 接下来依次描述 T 组测试数据,为了减少读入量,采用如下的方式来给出数据。 对于每组测试数据:一行给出了 7 个整数依次为 n, p, q, m, SA, SB, SC。其中 n 表示总的操作次数, 你的程序可以使用如下所示的 C++ 代码生成所有的操作。 (代码见原题面) 在这份代码中,PUSH(v) 表示向栈中插入一个新的元素,它的值为 v。POP() 表示弹出栈顶元素;如 果当前栈为空,则什么也不操作(即:没有东西可以弹出)。 输出格式 对于每组测试数据,输出一行信息 "Case #x: y"(不含引号),其中 x 表示这是第 x 组测试数据, y 等于 n ⊕ i=1 (i · ai),其中 ai 表示第 i 次操作后的答案,⊕ 表示异或和

思路:利用数组模拟过程,将所有的最大值保存,在这个过程上进行删除和添加注意要用 ll

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 dis[5000010];//数组定义必须要ll
int main()
{
    int t;
    int tt=1;
    scanf("%d",&t);
    while(t--)
    {
        int k=0;
        ll ans=0;
        scanf("%d%d%d%d%u%u%u", &n, &p, &q, &m, &SA, &SB, &SC);

        for(int i = 1; i <= n; i++)
        {
            if(rng61() % (p + q) < p)
            {

                ll cur=rng61()%m+1;//对m取余
                k++;
                dis[k]=cur;
                dis[k]=max(dis[k],dis[k-1]);

            }

            else
            {
                k=max(k-1,0);//将最大值删除,如果没有最大值从0开始

            }
            ans^=dis[k]*i;

        }
        printf("Case #%d: ",tt);
        tt++;
        printf("%lld\n",ans);

    }


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值