Maximum Element In A Stack

  • 20.91%
  • 10000ms
  • 262144K
 

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 44 operations:

  • 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]

思路:向栈中加入当前最大值即可。

8.31update:2019银川网络赛原题。。

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int MAX = ;
const int INF = 0x3f3f3f3f;
const int MOD = ; int n, p, q, m;
ll ans;
stack<ll> s;
unsigned int SA, SB, SC;
unsigned int rng61(){
SA ^= SA << ;
SA ^= SA >> ;
SA ^= SA << ;
unsigned int t = SA;
SA = SB;
SB = SC;
SC ^= t ^ SA;
return SC;
}
void gen(){
scanf("%d%d%d%d%u%u%u", &n, &p, &q, &m, &SA, &SB, &SC);
for(int i = ; i <= n; i++){
if(rng61() % (p + q) < p){
int x=rng61() % m + ;
if(!s.size()) s.push(x);
else if(x>s.top()) s.push(x);
else s.push(s.top());
}
else{
if(s.size()) s.pop();
else continue;
}
if(s.size()) ans^=i*s.top();
}
} int main(void)
{
int t,tt=,i;
scanf("%d",&t);
while(t--){
while(s.size()) s.pop();
ans=;
gen();
printf("Case #%d: %lld\n",++tt,ans);
}
return ;
}

2018ACM-ICPC宁夏邀请赛 A-Maximum Element In A Stack(栈内最大值)的更多相关文章

  1. Codeforces - 102222A - Maximum Element In A Stack - 模拟

    https://codeforc.es/gym/102222/problem/F 注意到其实用unsigned long long不会溢出. #include<bits/stdc++.h> ...

  2. The 2018 ACM-ICPC Chinese Collegiate Programming Contest Maximum Element In A Stack

    //利用二维数组模拟 #include <iostream> #include <cstdio> #include <cstring> #include <s ...

  3. Maximum Element In A Stack Gym - 102222A【思维+栈】

    2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest https://vjudge.net/problem ...

  4. [单调栈] 2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest-Maximum Element In A Stack

    题目:https://codeforces.com/gym/102222/problem/A Maximum Element In A Stack time limit per test 10.0 s ...

  5. [ICPC 2018 宁夏邀请赛] A-Maximum Element In A Stack(思维)

    >传送门< 前言 辣鸡网络赛,虽然我是个菜鸡,然而好几个队伍十几分钟就AK???我心态那会彻底崩了,后来群里炸了,话题直接上知乎热搜,都是2018ICPC宁夏网络赛原题,这怎么玩,拼手速? ...

  6. 2017 ICPC 广西邀请赛1004 Covering

    Covering Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. 【CF886E】Maximum Element DP

    [CF886E]Maximum Element 题意:小P有一个1-n的序列,他想找到整个序列中最大值的出现位置,但是他觉得O(n)扫一遍太慢了,所以它采用了如下方法: 1.逐个遍历每个元素,如果这个 ...

  8. 【CodeForces】889 C. Maximum Element 排列组合+动态规划

    [题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...

  9. Codeforces 889C Maximum Element(DP + 计数)

    题目链接  Maximum Element 题意  现在有这一段求序列中最大值的程度片段: (假定序列是一个1-n的排列) int fast_max(int n, int a[]) { int ans ...

随机推荐

  1. 解决集群搭建找不到datanode的问题

    解决"no datanode to stop"问题当我停止Hadoop时发现如下信息:    no datanode to stop原因:每次namenode format会重新创 ...

  2. 九度OJ 1171:C翻转 (矩阵计算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4649 解决:1530 题目描述: 首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作 ...

  3. 九度OJ 1059:abc (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3642 解决:2869 题目描述: 设a.b.c均是0到9之间的数字,abc.bcc是两个三位数,且有:abc+bcc=532.求满足条件的 ...

  4. CF148D. Bag of mice(概率DP)

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. Mac环境,React native错误解决方案

    运行react-native run-android,报错如下图:     运行react-native run-ios正常,但 react-native run-android时,提示错误: 在网上 ...

  6. SDUT OJ类型转换函数的应用

    题目描述 处理一个复数与一个double数相加的运算,结果存放在一个double型变量d1中,输出d1的值.定义Complex(复数)类,在成员函数中包含重载类型转换运算符:operator doub ...

  7. jquery.dataTables.min.js: Uncaught TypeError: Cannot read property 'style' of undefined

    原因:datatable表格内容有操作列,而表头没有定义操作列 少写了一行:<th>操作</th>

  8. 分布式锁的实现方式——ACID数据库、缓存或者是zk

    针对分布式锁的实现,目前比较常用的有以下几种方案: 基于数据库实现分布式锁 基于缓存(redis,memcached,tair)实现分布式锁 基于Zookeeper实现分布式锁 在分析这几种实现方案之 ...

  9. nginx的fastcgi_param参数详解

    在配置nginx的时候,有很多fastcgi_param参数,具体对应是什么值呢 引入:fastcgi_params 文件: fastcgi_params文件具体内容: postman 发送请求: n ...

  10. tensorboard 用法

    step1:  代码中把summary写到文件中 step2: dos窗口执行tensorboard命令 切换到代码所在目录下,输入: tensorboard --logdir=./tmp/graph ...