hdu 5929 Basic Data Structure (双端队列)

本文介绍了一种基于栈的数据结构实现及其操作模拟,包括PUSH、POP、REVERSE和QUERY等操作。通过双端队列记录0的位置来高效地处理各种操作,并分析了不同操作下栈的状态变化及QUERY操作中特定的逻辑。

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

Basic Data Structure

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


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 QUERY 3 PUSH 0 REVERSE QUERY
 

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.

 

赛后补的题,比赛的时候TL,WA,心累,后来发现是缺少对于0的标记,扫描的话超时,栈模拟队列稳的一匹呀,然后双端队列(刚开始不懂)记录0的位置就好了,记住一定要比较0和相应尾进行比较。

还有一个规律就是如果头是0 无论后边是啥  最后结果都是1   如果奇数个1在一块进行运算最后结果也是1,偶数相反

#include<stdio.h>
#include<iostream> 
#include <algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include<deque>//双端队列 
#include<set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int KGCD(int a,int b){if(a==0)return b;if(b==0)return a;if(~a&1){ if(b&1) return KGCD(a>>1,b);else return KGCD(a>>1,b>>1) <<1; } if(~b & 1)  return KGCD(a, b>>1);  if(a > b) return KGCD((a-b)>>1, b);return KGCD((b-a)>>1, a);}  
int LCM(int a,int b){ return a/KGCD(a,b)*b; } 
int dir[5][2]={0,1,0,-1,1,0,-1,0};
using namespace std;
int stack[600000];
int main()
{
	int t,n,flag,x;
	char str[20];
	deque<int>d; //记录0的位置 
	scanf("%d",&t);
	for(int k=1;k<=t;k++)
	{
		d.clear();
		scanf("%d",&n);
		flag=1;
		int left=299999;
		int right=300000; 
		printf("Case #%d:\n",k);
		while(n--)
		{
			scanf("%s",str);
			if(strcmp(str,"PUSH")==0)	
			{
				scanf("%d",&x);
				if(flag)//往右走 
				{
					stack[right]=x;
					if(x==0)
						d.push_back(right);
					right++;
				}
				else//往左走 
				{
					stack[left]=x;
					if(x==0)
						d.push_front(left); 
					left--; 
				}
			}
			else if(strcmp(str,"POP")==0)
			{
				if(flag)
				{
					right--;
					if(stack[right]==0)
						d.pop_back();
				}
				else
				{
					left++;
					if(stack[left]==0)
						d.pop_front();
				}
			}
			else if(strcmp(str,"REVERSE")==0)
			{
				flag=flag^1;	
			}
			else if(strcmp(str,"QUERY")==0)
			{
				if(right-left==1)
					printf("Invalid.\n");
				else if(d.empty())//都是1 没有0  如果是空的话 
				{
					int temp=right-left-1;
					if(temp%2==0)
						printf("0\n");
					else
						printf("1\n");
				}
				else//就是看那个方向和尾巴
				{
					int num=0;
					if(flag)
					{
						int temp=d.front();
						if(temp==right-1)
							num=d.front()-left-1;
						else
							num=d.front()-left;
					} 
					else
					{
						int temp=d.back();
						if(temp==left+1)
							num=right-temp-1;
						else
							num=right-temp;	
					}
					printf("%d\n",num%2);
				}		
			}	
		}
	}
	return 0;
}


HDU 3732 (Queue) 是一个经典的队列操作题目,在这个题目的设定下,你需要处理一系列关于入队、出队的操作,并最终输出特定的结果。这类问题通常会涉及到数据结构中的“队列”这一概念。 ### 题目概述 在 HDU 3732 中,你将面对的是一个标准的队列入栈和出栈的问题变种。它可能会给出一些序列化的指令集,包括: - 入队(push) - 出队(pop) 并且最后询问某些元素的状态或顺序等信息。 ### 解决思路 为了应对这个问题,你可以采用双端队列的数据结构或者两个栈模拟队列的方式来解决问题。以下是具体的步骤: 1. **初始化**:创建所需的辅助变量及容器,如 `queue` 或者一对用于模拟队列行为的栈 (`stackIn`, `stackOut`)。 2. **处理命令流**:遍历输入命令列表,根据不同类型的命令做相应动作: - 对于每一个 push 操作直接添加到指定位置; - 当遇到 pop 命令,则从头部移除元素;如果是用栈实现的话需要特殊处理,比如先全部倒入另一个栈再弹出顶部元素作为当前最先进来的那个值被移走。 3. **生成结果**:按照题目要求整理并返回正确的输出内容。 4. **注意事项** - 确保每次只对有效范围内的索引执行插入/删除操作。 - 考虑边界条件,例如空队列出队等情况下的异常处理。 ### 示例代码片段(Python 实现) ```python from collections import deque def process_queue_commands(commands): queue = deque() for command in commands: if "in" == command[0]: value = int(command.split()[1]) queue.append(value) elif "out" == command and len(queue)>0: print("Pop element:", queue.popleft()) # 示例用法 commands = ["5", "in 8", "in 9", "out"] process_queue_commands(commands[1:]) ``` 请注意实际比赛中给定的具体细节可能有所差异,请参考原题描述进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值