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. git拉取远程分支到本地分支或者创建本地新分支

    git fetch origin branchname:branchname 可以把远程某各分支拉去到本地的branchname下,如果没有branchname,则会在本地新建branchname g ...

  2. linux c编程:make编译一

    一个工程中的源文件不计其数,按照不同的功能分类在若干的目录里面,makefile定义了一系列的规则,来制定那些文件需要先编译,那些文件后编译,那些文件重新编译.makefile最大的好处就是自动化编译 ...

  3. Nginx Cache中$request_filename(转)

    对于Nginx的$request_filename变量指的就是请求的资源路径.在原先 OpenCDN节点端配置里面是这样的. location ~ .*\.(png|html|htm|ico|jpg| ...

  4. 性能优化——Web前端性能优化

    核心知识点: 1.排查网站性能瓶颈的手法:分析各个环节的日志,找出异常部分 2.Web前端:网站业务逻辑之前的部分(浏览器.图片服务.CDN) 3.优化手段 1)浏览器优化 (1)减少http请求 a ...

  5. [2018-11-27]2018年12月1日宁波dotnet社区线下活动

    离上次活动,转眼又过了一个月,幸得各路大神支持,于本周六(12月1日),宁波dotnet社区的线下分享活动又来啦! 活动嘉宾及主题 董斌辉 2015-2019年微软全球最有价值专家(.NET方向) 2 ...

  6. video codec 学习笔记

    一. H.264 (http://www.baike.com/wiki/H264)  三大标准: AVC(Advanced Video Coding,AVC) H.264,同时也是MPEG-4第十部分 ...

  7. oracle 数据库运维知识

    1.在数据库中连接用某个用户连接数据库报错: Product: DbVisualizer Pro 9.1.1 Build: #2063 (2013/10/01 12:27) Java VM: Java ...

  8. HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 郑厂长系列故事——排兵布阵 Time Limit: 10000/5000 MS (Java/Ot ...

  9. 对于glut和freeglut的一点比较和在VS2013上的配置问题

    先大概说一下glut.h和freeglut.h 首先要知道openGL是只提供绘图,不管窗口的,所以你需要给它一个绘图的区域(openGL能跨平台也与此有些关系) glut.h和freeglut.h都 ...

  10. freemaker开发

    推荐书籍 百度云盘 密码: c3m9 1. 前言 本书为<FreeMarker 2.3.19 中文版手册>,包含了freemarker开发得方方面面,可以作为开发freemarker的字典 ...