[单调栈] 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
10.0 s
256 MB
standard input
standard output
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.
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 is asked to generate all operations by using the following code in C++.
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; } void gen(){ scanf(" for(int i = 1; i <= n; i++){ if(rng61() PUSH(rng61() else POP(); } }
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.
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 ⊕i=1n(i⋅ai) where ⊕
means bitwise xor.
2
4 1 1 4 23333 66666 233333
4 2 1 4 23333 66666 233333
Case #1: 19
Case #2: 1
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().
题意:
给你一个栈和一个叫rng61的算法,当符合条件就加入一些内容到栈中,并在每次操作后记录当前栈中最大值ai,最后输出答案1到n的异或和i*ai
思路:
rng61是优化不了了,但我们可以在找栈中最大值时优化,如果是要询问栈中最大值,那么如果当前加入的元素小于以前记录的最大值,那么这个信息是没有记录的必要的,
所以可以用一个单调不减的单调栈存每个状态的最大值,另一个普通栈存操作的原始序列,如果要往普通栈中推入元素,则如果这个元素大于等于单调栈的栈顶则加入单调栈,
如果在普通栈进行pop操作,则看当前pop的元素是否>=单调栈的栈顶,如果是就把单调栈的栈顶pop(因为我们在单调栈中存的是单调不减的元素,所以如果碰到普通栈pop的元素大于等于单调栈栈顶元素,则这个元素必定是加入过单调栈的),
如果普通栈为空或单调栈为空,则清空单调栈并向单调栈中推入一个0元素
然后每次操作的普通栈中的最大值就是单调栈的栈顶
注意:
1.栈不为空时才能进行pop操作(如果是数组模拟则tp--后如果tp<0则需要让tp=0)
2.ans要用long long存,这次比赛是算法是写正确了,但实现时用了unsigned int,一直卡在最后的数据,以后像ans,求和,求积之类的果断用long long来存(防溢出用long long)
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int n,p,q,m;bool adf;
long long ans;
unsigned int SA,SB,SC;
long long last,sttp,dsttp;
stack<long long> st,dst;
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);
while(st.size())st.pop();while(dst.size())dst.pop();
ans=;
for(unsigned int i=;i<=n;i++){
adf=;
if(dst.empty())dst.push();
if(rng61()%(p+q)<p)st.push(rng61()%m+);
else if(st.size()) last=st.top(),st.pop(),adf=;
if(st.empty()){
ans^=i*;
while(dst.size())dst.pop();
dst.push();
}
else {
sttp=st.top(),dsttp=dst.top();
if(adf){
if(sttp>=dsttp)dst.push(sttp);
}
else {
if(last>=dsttp&&dst.size())dst.pop();
}
if(dst.empty())dst.push();
ans^=i*dst.top();
}
}
}
int main(){
int T,cas=;
scanf("%d",&T);
while(T--){
gen();
printf("Case #%d: %lld\n",cas++,ans);
}
}
/**
给你一个栈和一个叫rng61的算法,当符合条件就加入一些内容到栈中,并在每次操作后记录当前栈中最大值ai,最后输出答案1到n的异或和i*ai
rng61是优化不了了,但我们可以在找栈中最大值时优化,如果是要询问栈中最大值,那么如果当前加入的元素小于以前记录的最大值,那么这个信息是没有记录的必要的,
所以可以用一个单调不减的单调栈存每个状态的最大值,另一个普通栈存操作的原始序列,如果要往普通栈中推入元素,则如果这个元素大于等于单调栈的栈顶则加入单调栈,
如果在普通栈进行pop操作,则看当前pop的元素是否>=单调栈的栈顶,如果是就把单调栈的栈顶pop(因为我们在单调栈中存的是单调不减的元素,所以如果碰到普通栈pop的元素大于等于单调栈栈顶元素,则这个元素必定是加入过单调栈的),
如果普通栈为空或单调栈为空,则清空单调栈并向单调栈中推入一个0元素
然后每次操作的普通栈中的最大值就是单调栈的栈顶
注意栈不为空时才能进行pop操作(如果是数组模拟则tp--后如果tp<0则需要让tp=0),ans要用long long存,这次比赛是算法是写正确了,但实现时用了unsigned int,一直卡在最后的数据,以后像ans,求和,求积之类的果断用long long来存
**/
题外话:2019银川网络赛竟然用原题,变成了大学生程序重构大赛,比搜索比手速,前有tourist5小时AKWF,今有北师大6分钟AK网络赛(写完C后被队友告知北师大已经只差1题就AK了)
[单调栈] 2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest-Maximum Element In A Stack的更多相关文章
- The 2018 ACM-ICPC Chinese Collegiate Programming Contest Maximum Element In A Stack
//利用二维数组模拟 #include <iostream> #include <cstdio> #include <cstring> #include <s ...
- 2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
我是铁牌选手 这次比赛非常得爆炸,可以说体验极差,是这辈子自己最脑残的事情之一. 天时,地利,人和一样没有,而且自己早早地就想好了甩锅的套路. 按理说不开K就不会这么惨了啊,而且自己也是毒,不知道段错 ...
- HZNU Training 4 for Zhejiang Provincial Collegiate Programming Contest 2019
今日这场比赛我们准备的题比较全面,二分+数论+最短路+计算几何+dp+思维+签到题等.有较难的防AK题,也有简单的签到题.为大家准备了一份题解和AC代码. A - Meeting with Alien ...
- HZNU Training 2 for Zhejiang Provincial Collegiate Programming Contest 2019
赛后总结: T:今天下午参加了答辩比赛,没有给予队友很大的帮助.远程做题的时候发现队友在H上遇到了挫折,然后我就和她们说我看H吧,她们就开始做了另外两道题.今天一人一道题.最后我们在研究一道dp的时候 ...
- The 10th Shandong Provincial Collegiate Programming Contest 2019山东省赛游记+解题报告
比赛结束了几天...这篇博客其实比完就想写了...但是想等补完可做题顺便po上题解... 5.10晚的动车到了济南,没带外套有点凉.酒店还不错. 5.11早上去报道,济南大学好大啊...感觉走了一个世 ...
- ACM ICPC China final G Pandaria
目录 ACM ICPC China final G Pandaria ACM ICPC China final G Pandaria 题意:给一张\(n\)个点\(m\)条边的无向图,\(c[i]\) ...
- ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbilisi, November 24, 2010
ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbil ...
- ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018
ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syr ...
- 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定理
2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定 ...
- 2020 ICPC Universidad Nacional de Colombia Programming Contest
2020 ICPC Universidad Nacional de Colombia Programming Contest A. Approach 三分 显然答案可以三分,注意\(eps\)还有两条 ...
随机推荐
- Ubuntu19.10安装OMNeT++ (omnetpp-5.6)中遇到的问题
在官网上下载对应版本的安装包,里面有说明性的文档,先在第五章ubuntu那里配置好前期的环境,再到linux那一章,看进行安装,本文即从这里开始记录. 安装包中的文档目录为:omnetpp-5.6/d ...
- CSS 加载动画
CSS加载动画 实现加载动画效果,需要的两个关键步骤: 1.做出环形外观 border:16px solid #f3f3f3; border-radius:50%; border-top:16px s ...
- js中如何判断属性是对象实例中的属性还是原型中的属性
ECMAScript5中的hasOwnProperty()方法,用于判断只在属性存在与对象实例中的时候,返回true,in操作符只要通过对象能访问到属性就返回true. 因此只要in操作符返回true ...
- 自定义checkbox, radio样式总结
任务目的 深入了解html label标签 了解CSS边框.背景.伪元素.伪类(注意和伪元素区分)等属性的设置 了解CSS中常见的雪碧图,并能自己制作使用雪碧图 任务描述 参考 样例(点击查看),实现 ...
- python入门到放弃-基本数据类型之tuple元组
#概述 元组俗称不可变的列表,又称只读列表,是python的基本数据类型之一, 用()小括号表示,里面使用,逗号隔开 元组里面可以放任何的数据类型的数据,查询可以,循环可以,但是就是不能修改 #先来看 ...
- startUML5.0中的tools下怎么没有java、c等选项
这也是帮一个直系学妹弄得,哈哈~~~ 具体做法如下: 进入到StartUML\modules目录下,里面有很多文件夹,比如startuml-cpp.startuml-csharp等等, 进入到每个文件 ...
- JavaWeb中登录验证码生成
1.页面代码 <html> <head> <title>Title</title> <script type="text/javascr ...
- OpenCV中Mat的基本用法:创建、复制
OpenCV中Mat的基本用法:创建.复制 一.Mat类的创建: 1.方法一: 通过读入一张图像,直接将其转换成Mat对象. Mat image = imread("test.jpg&quo ...
- mysql的锁与事务
1. MySQL中的事物 1.InnoDB事务原理 1. 事务(Transaction)是数据库区别于文件系统的重要特性之一,事务会把数据库从一种一致性状态转换为另一种一致性状态. 2. 在数据库提交 ...
- seo搜索优化教程05-SEO常用专业术语
SEO常用的专业术语很多,星辉信息科技专门抽空进行了整理,主要如下:. SEO 根据搜索引擎规则来进行搜索引擎优化,进而使得在搜索结果中获得较好的排名 关键词 关键词也叫keywords,表示在搜索引 ...