国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HDU 5292 Basic Data Structure

2019-11-08 19:42:53
字體:
來源:轉載
供稿:網友

Basic Data Structure

Time Limit: 7000/3500 MS (java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1393    Accepted Submission(s): 347PRoblem DescriptionMr. 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,atop?1,?,a1 is corresponding to the element of the Stack from top to the bottom, value=atop nand atop?1 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 = 0Because 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. InputThe first line contains only one integer T (T≤20), which indicates the number of test cases.For each test case, the first line contains only one integers N (2≤N≤200000), 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? QUERYIt is guaranteed that the current stack will not be empty while doing POP operation. OutputFor 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
28PUSH 1QUERYPUSH 0REVERSEQUERYPOPPOPQUERY3PUSH 0REVERSEQUERY Sample Output
Case #1:11Invalid.Case #2:0HintIn 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.       題意:

    Mr.frog最近在學習棧。棧有一些基礎的操作:

PUSH x:將x放入棧中,x必須是0或1

POP:將棧頂元素取出

    這些都太簡單,Mr.Frog又增加了兩個操作:

REVERSE:將棧中的元素翻轉

QUERY:從左到右對棧中元素做NAND運算,輸出運算結果,棧空輸出“Invalid”。

    另外,NAND是基于二進制的操作:

0 nand 0 = 1

0 nand 1 = 1

1 nand 0 = 1

1 nand 1 = 0

     思路:用模擬棧+deque(雙端隊列)來解決。

     用普通方法做,果斷T了,現學了一下deque的用法,附學習的網址:

deque的使用

   

#include <iostream>#include <algorithm>#include <deque>#include <cstdio>#include <string>#include <cstring>using namespace std;deque<int > q;//存放data[]中0的位置int data[450000];//模擬棧int T;//測試數據int n;//操作數int Case;int t, h;//data[]的下標int main(){	scanf("%d",&T);	Case = 1;	while(T--)	{		q.clear();		t = 200000;		h = 200001;		bool flag = false;//false則不需翻轉;true則需要翻轉		int index = 0;//模擬棧中元素的個數		scanf("%d",&n);		printf("Case #%d:/n",Case);		Case++;		while(n--)		{			char str[8];			scanf("%s",str);			if(str[2] == 'S')//PUSH			{				int value;				scanf("%d",&value);				if(flag)//REVERSE				{					data[t--] = value;					if(!value)						q.push_front(t + 1);				}				else				{					data[h++] = value;					if(!value)						q.push_back(h - 1);				}				index++;			}			else if(str[2] == 'P')//POP			{				index--;				if(flag)				{					if(data[++t] == 0)						q.pop_front();				}				else				{					if(data[--h] == 0)						q.pop_back();				}													}			else if(str[2] == 'V')//REVERSE				flag = !flag;			else			{				if(index == 0)					printf("Invalid./n");				else if(index == 1)					printf("%d/n",data[h - 1]);				else				{					int num = 0;					if(q.empty())						num = index;					else					{						if(flag)							num = (q.back() == t+1) ? index - 1 : h - q.back();						else							num = (q.front() == h-1) ? index - 1 : q.front() - t;					}								if(num % 2)						printf("1/n");					else						printf("0/n");				}			}		}	}	return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和顺县| 仁化县| 若羌县| 博乐市| 莱阳市| 伊川县| 安岳县| 西青区| 梧州市| 绥阳县| 拜城县| 澜沧| 横山县| 霍林郭勒市| 库车县| 铜陵市| 勃利县| 安宁市| 临高县| 沁源县| 怀化市| 施秉县| 顺平县| 晋州市| 综艺| 莎车县| 广汉市| 遵义市| 江西省| 通州市| 壶关县| 定边县| 明水县| 清涧县| 盐城市| 佛山市| 修文县| 南岸区| 长顺县| 社旗县| 泽州县|