2018宁夏A题
Posted zlwjy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018宁夏A题相关的知识,希望对你有一定的参考价值。
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 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 imes 10^6),p,q,m(1 le p,q,m le 10^9),SA,SB and SC(10^4 le SA,SB,SC le 10^6)n(1≤n≤5×106),p,q,m(1≤p,q,m≤109),SA,SB and SC(104≤SA,SB,SC≤106). Theinteger 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 v 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 xx is the test case number starting from 11, and yy is equal to oplus^n_{i=1} (i cdot a_i)⊕i=1n?(i⋅ai?) where a_iai? is the answer after the ii-th operation and oplus⊕ 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 44 operations:
- POP();
- POP();
- PUSH(1);
- PUSH(4).
The second test case also has 44 operations:
- PUSH(2);
- POP();
- PUSH(1);
- POP().
注意用longlong就可以了,这道题被一个空格卡了一天,复制的时候多了一个空格一直没有发现。实在是蠢死
#include <bits/stdc++.h>
using namespace std;
#define _for(i, a, b) for (int i = (a); i < (b); i++)
#define ll long long
const int N = 1e7;
int n, p, q, m;
unsigned int SA, SB, SC;
ll maxv, opr,t = 0, top;
ll ans[N];
stack<int> max_index;
void PUSH(unsigned x)
{
ans[top++] = x;
if (max_index.empty())
{
max_index.push(0);
}
else if (x > ans[max_index.top()])
{
max_index.push(top-1);
}else{
max_index.push(max_index.top());
}
opr = opr ^ (1LL * ans[max_index.top()] * t);
}
void POP()
{
if (max_index.empty())
return;
else
{
top--;
max_index.pop();
if (max_index.empty())
return;
opr = opr ^ (1LL * ans[max_index.top()] * t);
}
}
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);
top = 0;
_for(i, 1, n + 1)
{
if (rng61() % (p + q) < p)
{
++t;
PUSH(rng61() % m + 1);
}
else
{
++t;
POP();
}
}
}
int main()
{
int T;
cin >> T;
_for(i, 1, T + 1)
{
maxv = 0;
t = 0, opr = 0;
while (!max_index.empty())
{
max_index.pop();
}
gen();
printf("Case #%d: %lld ", i, opr);
}
return 0;
}
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 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). Theinteger n is the number of operations, and your program should generate all operations using the following code in C++.
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.
Output
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 ai? is the answer after the i-th operation and ⊕ 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().
以上是关于2018宁夏A题的主要内容,如果未能解决你的问题,请参考以下文章
[ICPC 2018 宁夏邀请赛] A-Maximum Element In A Stack(思维)