codeforces gym 10222A Maximum Element In A Stack

http://codeforces.com/gym/102222/problem/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 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). The integer n 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 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 ⊕ means bitwise xor.

Example
inputCopy
2
4 1 1 4 23333 66666 233333
4 2 1 4 23333 66666 233333
outputCopy
Case #1: 19
Case #2: 1
Note
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().

题目大意:给一个生成数据的代码,一共 n n n次操作, p u s h push push操作表示入栈, p o p pop pop操作表示出栈,每次操作都要计算 i ⊕ m a x i\oplus max imax的值,其中 m a x max max是当前栈内最大的值,若栈为空则 m a x = 0 max=0 max=0,最后输出 ⊕ i = 1 n i ∗ m a x \oplus_{i=1}^{n}i*max i=1nimax的值。

思路:暴力肯定是不行的。维护两个栈,第一个栈维护操作的下标,第二个栈维护操作的下标和一个上升的序列(因为要取最大值),对于入栈操作,操作下标直接入第一个栈,当且仅当第二个栈为空或者该次操作的值大于第二个栈顶的值才将该值加入到第二个栈中;对于出栈操作,若第一个栈为空,不做任何操作, m a x = 0 max=0 max=0,否则拿第一个栈栈顶和第二个栈栈顶的操作下标做比较,若相等,则第二个栈也做出栈操作, m a x = max= max=第二个栈栈顶的值。

#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<cmath>
#define pr pair<int,int>
using namespace std;
typedef long long ll;

const int maxn=5e6+5;

int s1[maxn];
pr s2[maxn];
int top1,top2;
int n,p,q,m;
ll ans=0;
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 v=rng61()%m+1;
			s1[top1++]=i;
			if(top2==0||v>s2[top2-1].second)
				s2[top2++]=pr(i,v);
			ans^=i*1ll*s2[top2-1].second;
		}
		else
		{
			if(top1==0)
				continue;
			if(s1[top1-1]==s2[top2-1].first)
				top2--;
			top1--;
			if(top2==0)
				ans^=i*0;
			else
				ans^=i*1ll*s2[top2-1].second;
		}
	}
}

int main()
{
	int t;
	int times=0;
	scanf("%d",&t);
	while(t--)
	{
		top1=top2=ans=0;
		gen();
		printf("Case #%d: %lld\n",++times,ans);
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值