2018ACM-ICPC宁夏邀请赛 A-Maximum Element In A Stack(栈内最大值)
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++.
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("%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)
PUSH(rng61() % m + 1);
else
POP();
}
}
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(栈内最大值)的更多相关文章
- Codeforces - 102222A - Maximum Element In A Stack - 模拟
https://codeforc.es/gym/102222/problem/F 注意到其实用unsigned long long不会溢出. #include<bits/stdc++.h> ...
- The 2018 ACM-ICPC Chinese Collegiate Programming Contest Maximum Element In A Stack
//利用二维数组模拟 #include <iostream> #include <cstdio> #include <cstring> #include <s ...
- Maximum Element In A Stack Gym - 102222A【思维+栈】
2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest https://vjudge.net/problem ...
- [单调栈] 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 ...
- [ICPC 2018 宁夏邀请赛] A-Maximum Element In A Stack(思维)
>传送门< 前言 辣鸡网络赛,虽然我是个菜鸡,然而好几个队伍十几分钟就AK???我心态那会彻底崩了,后来群里炸了,话题直接上知乎热搜,都是2018ICPC宁夏网络赛原题,这怎么玩,拼手速? ...
- 2017 ICPC 广西邀请赛1004 Covering
Covering Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【CF886E】Maximum Element DP
[CF886E]Maximum Element 题意:小P有一个1-n的序列,他想找到整个序列中最大值的出现位置,但是他觉得O(n)扫一遍太慢了,所以它采用了如下方法: 1.逐个遍历每个元素,如果这个 ...
- 【CodeForces】889 C. Maximum Element 排列组合+动态规划
[题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...
- Codeforces 889C Maximum Element(DP + 计数)
题目链接 Maximum Element 题意 现在有这一段求序列中最大值的程度片段: (假定序列是一个1-n的排列) int fast_max(int n, int a[]) { int ans ...
随机推荐
- sql server单引号和双引号的区别
--当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔--当 SET QUOTED_IDENTIFIER 为 OFF(默认值)时,表达式中的文 ...
- 一些重要的地址:md5在线解密破解
md5在线解密破解:https://www.cmd5.com/
- Python中的staticmethod和classmethod
谈谈Python中的staticmethod和classmethod 首先值得说明的是staticmethod和classmethod都是python中定义的装饰器,用的时候需要在前面加@ 即@sta ...
- Python爬虫-- BeautifulSoup库
BeautifulSoup库 beautifulsoup就是一个非常强大的工具,爬虫利器.一个灵活又方便的网页解析库,处理高效,支持多种解析器.利用它就不用编写正则表达式也能方便的实现网页信息的抓取 ...
- 我的Java开发学习之旅------>Java经典排序算法之插入排序
一.算法原理 插入排序法:所谓插入排序法乃是将一个数目插入该占据的位置. 假设我们输入的是 "53,27,36,15,69, 42" 我们从第二个数字开始,这个数字是27,我们的 ...
- spring-data-redis RedisTemplate操作
使用RedisTemplate来对对象.String等做缓存处理 首先定义一个对象并重写toString方法 public class UserInfo implements Serializable ...
- centos下更换默认的python版本
1.在命令行输入python查看版本 2.到官网下载自己的python版本 wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz ...
- Android适配API23之后权限的动态申请
一.权限介绍 对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装,造成了我们想要使用某个app,就要默默忍受其一些不必要的权限(比如是个app都要 ...
- oracle数据库如何备份一张表
--用户名:tms--创建表ts_dictionary的备份create table ts_dictionary_20160715 as select * from ts_dictionary; 补充 ...
- JAVA- JDBC之DBHelper
package com.myit.util; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.D ...