博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2018ACM-ICPC宁夏邀请赛 A-Maximum Element In A Stack(栈内最大值)
阅读量:5039 次
发布时间:2019-06-12

本文共 3569 字,大约阅读时间需要 11 分钟。

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 (1n5×106), pp, qq, m~(1\le p, q, m\le 10^9)m (1p,q,m109), SASA, SBSB and SC~(10^4 \le SA, SB, SC\le 10^6)SC (104SA,SB,SC106).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=1n(iai) 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().

样例输入

24 1 1 4 23333 66666 2333334 2 1 4 23333 66666 233333

样例输出

Case #1: 19Case #2: 1

题目来源

]

 

 

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

 

#include 
using namespace std;typedef long long ll;const int MAX = 100005;const int INF = 0x3f3f3f3f;const int MOD = 1000000007;int n, p, q, m;ll ans;stack
s;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){ int x=rng61() % m + 1; 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=0,i; scanf("%d",&t); while(t--){ while(s.size()) s.pop(); ans=0; gen(); printf("Case #%d: %lld\n",++tt,ans); } return 0;}

 

转载于:https://www.cnblogs.com/yzm10/p/9383400.html

你可能感兴趣的文章
(转)Intent的基本使用方法总结
查看>>
Mac 下的Chrome 按什么快捷键调出页面调试工具
查看>>
Windows Phone开发(24):启动器与选择器之发送短信
查看>>
JS截取字符串常用方法
查看>>
Google非官方的Text To Speech和Speech Recognition的API
查看>>
stdext - A C++ STL Extensions Libary
查看>>
Django 内建 中间件组件
查看>>
bootstrap-Table服务端分页,获取到的数据怎么再页面的表格里显示
查看>>
进程间通信系列 之 socket套接字及其实例
查看>>
天气预报插件
查看>>
Unity 游戏框架搭建 (十三) 无需继承的单例的模板
查看>>
模块与包
查看>>
mysql忘记root密码
查看>>
apache服务器中设置目录不可访问
查看>>
嵌入式Linux驱动学习之路(十)字符设备驱动-my_led
查看>>
【NOIP模拟】密码
查看>>
java容器---------手工实现Linkedlist 链表
查看>>
three.js 性能优化的几种方法
查看>>
《梦断代码》读书笔记(三)
查看>>
FreeMarker解析json数据
查看>>