Basic Data Structure HDU 5929(模拟)

来源:互联网 发布:注塑机生产厂家 知乎 编辑:程序博客网 时间:2024/05/21 22:34

Basic Data Structure

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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 doNAND 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 not change 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
28PUSH 1QUERYPUSH 0REVERSEQUERYPOPPOPQUERY3PUSH 0REVERSEQUERY
 

Sample Output
Case #1:11Invalid.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.

题意 :

模拟栈给你定义几种操作

push pop REVERSE QUERY.....对应的分别有几种操作

后两个比较重要  1是栈翻转操作 2是询问操作

首先1  我的做法是用数组模拟  (体会到了数据结构的重要性) top bottom 记录临时的头尾  比较大小就可以得知谁是头尾

其次2  这是本t的重点难点 遍历一遍肯定超时 我们可以看到与非逻辑关系 只要有0不管和谁运算都出现1 利用这个性质记录栈中最后出栈的0的位置

例如 top11011110bottom   因为栈底是0 那么答案就是1  top1100111011bottom 判断0后面出现1的奇偶性就可

又例如 top01111bottom

还有 top0000bottom  以及top1111bottom top0bottom 这几种特殊情况



#include <algorithm>#include <iostream>#include <cstring>#include <iomanip>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>using namespace std;#define For(i,a,b) for(i=a;i<=b;i++)#define _For(i,a,b) for(i=b;i>=a;i--)#define Out(x) cout<<x<<endl#define Outdouble(x,a) cout<<fixed<<setprecision(a)<<1.0*x<<endl#define pf printf#define sf scanf#define mset(arr,num) memset(arr,num,sizeof(arr))#define ll long longconst ll inf = 800010; ///#define ok std::ios::sync_with_stdio(0)#pragma comment(linker, "/STACK:102400000,102400000")// #define debug#if defined (debug)---check---#endif/// ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^  ^_^ ///const int start = 300000;bool arr[inf];int bottom = start;int top = bottom + 1;int oddsum = 0;int sum;deque <int> q;void Push(int y){    sum++;    bool x = (bool)y;    if(top < bottom)    {        arr[top] = x;        if(x == 0)        {            q.push_front(top);  ///left        }        top--;    }    else    {        arr[top] = x;        if(x == 0)        {            q.push_back(top);  ///right        }        top++;    }}void Reve(){    swap(top,bottom);}void Que(){    int num;    if(abs(top-bottom) == 1)    {        pf("Invalid.\n");  ///empty        return ;    }    if(q.size() == 0)  ///all of deque is 1    {        pf("%d\n",sum%2);        return ;    }    if(top < bottom)    {        if(q.back() == bottom - 1) ///序列最后(back)一个是0那么答案是1/0        {            if(sum > 1)            {                pf("1\n");            }            else if(sum == 1)            {                pf("0\n");            }            return ;        }        num = bottom - 1 - q.back();        if(q.back() == top + 1)  ///如果back是top元素说明前面无1  奇1偶0        {            if(num%2 == 1)            {                pf("1\n");            }            else pf("0\n");        }        else        {            if(num%2 == 1)            {                pf("0\n");            }            else pf("1\n");        }    }    else ///top > bottom    {        if(q.front() == bottom + 1) ///最前(front)一个是0那么答案是1        {            if(sum > 1)            {                pf("1\n");            }            else if(sum == 1)            {                pf("0\n");            }            return ;        }        num = q.front() - bottom -1; ///--17:18        if(q.front() == top - 1) ///如果front是top元素说明前面无1 奇1偶0        {            if(num%2 == 1)            {                pf("1\n");            }            else pf("0\n");        }        else        {            if(num%2 == 1)            {                pf("0\n");            }            else pf("1\n");        }    }}void Pop(){    sum--;    if(top < bottom)    {        top++;        if(arr[top] == 0)        {            q.pop_front();        }    }    else    {        top--;        if(arr[top] == 0)        {            q.pop_back();        }    }}int main(){    int i,cnt = 0;;    int t,n;    char s[20];    sf("%d",&t);    while(t--)    {        bottom = start;        top = bottom + 1;        sum = 0;        cnt++;        q.clear();        sf("%d",&n);        pf("Case #%d:\n",cnt);        For(i,1,n)        {            sf("%s",s);            if(s[0] == 'P' && s[1] == 'U')            {                int x;                cin>>x;                Push(x);            }            else if(s[0] == 'P' && s[1] == 'O')            {                Pop();            }            else if(s[0] == 'R')            {                Reve();            }            else if(s[0] == 'Q')            {                Que();            }        }    }    return 0;}


1 0
原创粉丝点击