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. 九度OJ 1160:放苹果 (DFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:998 解决:680 题目描述: 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和 ...

  2. await 暂停 等待 暂停的是什么

    体验异步的终极解决方案-ES7的Async/Await var sleep = function (time) { return new Promise(function (resolve, reje ...

  3. Xshell 连接centOS虚拟机、centOS内置python版本升级

    Xshell 连接虚拟机 前置条件:VMware Workstation 12.5.1.Xshell 5 .  centOS 6.6 1.打开虚拟机,输入用户名和密码: 2.输出命令:ifconfig ...

  4. cordova 插件创建

    peng@PENG-PC /E/_My_File_____/_work/MyCode/myCode/cordova-workspace/plugman-test/ABCD $ npm install ...

  5. iOS app submission : missing 64-bit support

  6. B. Two Buttons

    这是Codeforces Round #295 (Div. 2) 的B 题,题意为: 给出n, m, 有两种操作,n 减一 和 n 乘以 2,问最少要多少次操作才能把n 变成 m. Sample te ...

  7. uboot 2013.01 s3c6400编译失败

    通常我们对s3c6410平台开发u-boot是在s3c6400的基础上修改而成的,但是从uboot 2013.01这个版本之后的版本都把smdk6400对应的配置给删除了. 这是因为该版本smdk64 ...

  8. debian7 amd64版本添加对x86包的支持

    dpkg --add-architecture i386apt-get updateapt-get install ia32-libs ia32-libs-gtk

  9. POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串

    题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072 ...

  10. Javascript类型转换的规则实例解析

    http://www.jb51.net/article/79916.htm 类型转换可以分为隐式转换和显式转换,所谓隐式转换即程序在运行时进行的自动转换,显式转换则是人为的对类型进行强制转换.Java ...