【2018icpc宁夏邀请赛现场赛】【Gym - 102222A】Maximum Element In A Stack(动态的栈中查找最大元素)
https://nanti.jisuanke.com/t/41285
題干:
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
The input contains several test cases, and the first line is a positive integer?TTindicating 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≤n≤5×106)n?(1≤n≤5×106),?pp,?qq,?m?(1≤p,q,m≤109)m?(1≤p,q,m≤109),?SASA,?SBSB?and?SC?(104≤SA,SB,SC≤106)SC?(104≤SA,SB,SC≤106). The integer?nn?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?vv?into the stack and the procedure?POP()?pops the topmost element in the stack or does nothing if the stack is empty.
Output
For each test case, output a line containing?Case #x: y, where?x?is the test case number starting from?11, and?y?is equal to?⊕i=1n(i?ai)⊕i=1n?(i?ai)?where?⊕⊕?means bitwise xor.
Example
Input
2 4 1 1 4 23333 66666 233333 4 2 1 4 23333 66666 233333Output
Case #1: 19 Case #2: 1Note
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().
題目大意:
? 剛開始是一個空棧, 每次可以在其中加入元素,并且要求出此時棧中元素的最大值。對于每個樣例只需要輸出一個數:
解題報告:
? 對于push操作,讀入一個數x,每次加入sk.top()和x的較大值,這樣保證棧頂元素一定是最大值。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; 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; }ll gen() {stack<ll> sk;ll res = 0;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) {ll tmp = rng61() %m + 1;if(sk.empty()) sk.push(tmp);else sk.push(max(tmp,sk.top()));}else if(sk.size()) sk.pop();if(sk.size()) res ^= i*sk.top();else res ^= 0;}return res; } int main() {int t,iCase=0;cin>>t;while(t--) {printf("Case #%d: %lld\n",++iCase,gen());}return 0 ; }?
總結
以上是生活随笔為你收集整理的【2018icpc宁夏邀请赛现场赛】【Gym - 102222A】Maximum Element In A Stack(动态的栈中查找最大元素)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浦发信用卡审核要多久 审核通过花两天制卡
- 下一篇: 信用卡审核未通过怎么办 再次申请先找准被