【HDU5929 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 H】【打表找规律 队列模拟】Basic Data Structure 双端栈下连续1和0做nand的结果

本文介绍了一种基于栈的数据结构实现,通过PUSH、POP、REVERSE和QUERY操作进行管理。特别关注了QUERY操作中NAND逻辑运算的实现细节及其优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Basic Data Structure

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 516    Accepted Submission(s): 150


Problem Description
Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack:

 PUSH x: put x on the top of the stack, x must be 0 or 1.
 POP: throw the element which is on the top of the stack.

Since it is too simple for Mr. Frog, a famous mathematician who can prove "Five points coexist with a circle" easily, he comes up with some exciting operations:

REVERSE: Just reverse the stack, the bottom element becomes the top element of the stack, and the element just above the bottom element becomes the element just below the top elements... and so on.
QUERY: Print the value which is obtained with such way: Take the element from top to bottom, then do   NAND  operation one by one from left to right, i.e. If   atop,atop1,,a1  is corresponding to the element of the Stack from top to the bottom,   value=atop  nand   atop1  nand ... nand   a1 . Note that the Stack   will notchange after QUERY operation. Specially, if the Stack is empty now,you need to print ” Invalid.”(without quotes).

By the way,   NAND  is a basic binary operation:

 0 nand 0 = 1
 0 nand 1 = 1
 1 nand 0 = 1
 1 nand 1 = 0

Because Mr. Frog needs to do some tiny contributions now, you should help him finish this data structure: print the answer to each QUERY, or tell him that is invalid.
 

Input
The first line contains only one integer T ( T20 ), which indicates the number of test cases.

For each test case, the first line contains only one integers N ( 2N200000 ), indicating the number of operations.

In the following N lines, the i-th line contains one of these operations below:

 PUSH x (x   must  be 0 or 1)
 POP
 REVERSE
 QUERY

It is guaranteed that the current stack will not be empty while doing POP operation.
 

Output
For each test case, first output one line "Case #x:w, where x is the case number (starting from 1). Then several lines follow,  i-th line contains an integer indicating the answer to the i-th QUERY operation. Specially, if the i-th QUERY is invalid, just print " Invalid."(without quotes). (Please see the sample for more details.)
 

Sample Input
  
2 8 PUSH 1 QUERY PUSH 0 REVERSE QUERY POP POP
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 4e5 + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
char op[10]; int val;
int h, t; bool rev;
int q[N];
int g[N];
int n;
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		printf("Case #%d:\n", casei);
		h = 2e5 + 1; t = 2e5; rev = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i)
		{
			scanf("%s", op);
			if (op[0] == 'P' && op[1] == 'U')
			{
				scanf("%d", &val);
				if (!rev)
				{
					if (h>t || q[h] != val)
					{
						q[--h] = val;
						g[h] = 1;
					}
					else ++g[h];
				}
				else
				{
					if (h>t || q[t] != val)
					{
						q[++t] = val;
						g[t] = 1;
					}
					else ++g[t];
				}
			}
			else if (op[0] == 'P' && op[1] == 'O')
			{
				if (h>t)
				{
					while (1);
				}
				if (!rev)
				{
					if (--g[h] == 0)++h;
				}
				else
				{
					if (--g[t] == 0)--t;
				}
			}
			else if (op[0] == 'R')
			{
				rev ^= 1;
			}
			else
			{
				if (!rev)
				{
					if (q[t] == 1)
					{
						int num = g[t];
						if (h<t)
						{
							if (g[t - 1] > 1 || h + 1<t)
							{
								++num;
							}
						}
						num %= 2;
						printf("%d\n", num);
					}
					else
					{
						int num = (g[t]>1 || h < t);
						printf("%d\n", num);
					}
				}
				else
				{
					if (q[h] == 1)
					{
						int num = g[h];
						if (h<t)
						{
							if (g[h + 1] > 1 || h + 1<t)
							{
								++num;
							}
						}
						num %= 2;
						printf("%d\n", num);
					}
					else
					{
						int num = (g[h]>1 || h < t);
						printf("%d\n", num);
					}
				}
			}
		}
	}
	return 0;
}
/*
【题意】
有一个栈,存在n(2e5)次操作。
对于每个操作,包括——
PUSH(x),x不是0就是1
POP()
REVERSE() 对栈做从首到尾的翻转
QUERY() 从栈顶元素到栈底元素,做连续NAND操作并输出
 0 nand 0 = 1
 0 nand 1 = 1
 1 nand 0 = 1
 1 nand 1 = 0

【类型】
打表找规律 队列模拟

【分析】
这题的栈可以用队列模拟
关键如何快速知道nand这种运算的结果。

nand是不满足结合律的
于是打表找了个规律

发现,如果最后一个数是0,不管前面是什么,最后结果都是1
如果最后一个数不是0,
最后的连续1的个数如果是奇数,最后结果就是1
最后的连续1的个数如果是偶数,最后结果就是0

需要注意的是,这个连续1是要算上这串1之前的那个0的(因为这个0可能会生成1)
用队列模拟就做完啦!

【时间复杂度&&优化】
O(n)

*/

3PUSH 0REVERSEQUERY
 

Sample Output
  
Case #1: 1 1 Invalid. Case #2: 0
Hint
In the first sample: during the first query, the stack contains only one element 1, so the answer is 1. then in the second query, the stack contains 0, l (from bottom to top), so the answer to the second is also 1. In the third query, there is no element in the stack, so you should output Invalid.
 

Source
2016CCPC东北地区大学生程序设计竞赛 - 重现赛

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 4e5 + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
char op[10]; int val;
int h, t; bool rev;
int q[N];
int g[N];
int n;
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		printf("Case #%d:\n", casei);
		h = 2e5 + 1; t = 2e5; rev = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i)
		{
			scanf("%s", op);
			if (op[0] == 'P' && op[1] == 'U')
			{
				scanf("%d", &val);
				if (!rev)
				{
					if (h>t || q[h] != val)
					{
						q[--h] = val;
						g[h] = 1;
					}
					else ++g[h];
				}
				else
				{
					if (h>t || q[t] != val)
					{
						q[++t] = val;
						g[t] = 1;
					}
					else ++g[t];
				}
			}
			else if (op[0] == 'P' && op[1] == 'O')
			{
				if (h>t)
				{
					while (1);
				}
				if (!rev)
				{
					if (--g[h] == 0)++h;
				}
				else
				{
					if (--g[t] == 0)--t;
				}
			}
			else if (op[0] == 'R')
			{
				rev ^= 1;
			}
			else
			{
				if (!rev)
				{
					if (q[t] == 1)
					{
						int num = g[t];
						if (h<t)
						{
							if (g[t - 1] > 1 || h + 1<t)
							{
								++num;
							}
						}
						num %= 2;
						printf("%d\n", num);
					}
					else
					{
						int num = (g[t]>1 || h < t);
						printf("%d\n", num);
					}
				}
				else
				{
					if (q[h] == 1)
					{
						int num = g[h];
						if (h<t)
						{
							if (g[h + 1] > 1 || h + 1<t)
							{
								++num;
							}
						}
						num %= 2;
						printf("%d\n", num);
					}
					else
					{
						int num = (g[h]>1 || h < t);
						printf("%d\n", num);
					}
				}
			}
		}
	}
	return 0;
}
/*
【题意】
有一个栈,存在n(2e5)次操作。
对于每个操作,包括——
PUSH(x),x不是0就是1
POP()
REVERSE() 对栈做从首到尾的翻转
QUERY() 从栈顶元素到栈底元素,做连续NAND操作并输出
 0 nand 0 = 1
 0 nand 1 = 1
 1 nand 0 = 1
 1 nand 1 = 0

【类型】
打表找规律 队列模拟

【分析】
这题的栈可以用队列模拟
关键如何快速知道nand这种运算的结果。

nand是不满足结合律的
于是打表找了个规律

发现,如果最后一个数是0,不管前面是什么,最后结果都是1
如果最后一个数不是0,
最后的连续1的个数如果是奇数,最后结果就是1
最后的连续1的个数如果是偶数,最后结果就是0

需要注意的是,这个连续1是要算上这串1之前的那个0的(因为这个0可能会生成1)
用队列模拟就做完啦!

【时间复杂度&&优化】
O(n)

*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值