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

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

H - Basic Data Structure HDU - 5929

2019-11-08 20:07:15
字體:
供稿:網(wǎng)友

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,atop?1,?,a1atop,atop?1,?,a1 is corresponding to the element of the Stack from top to the bottom, value=atopvalue=atop nand atop?1atop?1 nand ... nand a1a1. 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. InputThe first line contains only one integer T (T≤20T≤20), which indicates the number of test cases. For each test case, the first line contains only one integers N (2≤N≤2000002≤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 ?? QUERY It 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 0REVERSEQUERYSample Output
Case #1:11Invalid.Case #2:0

思路:雙向隊列存儲0的位置。因為不管0前面的值為多少,碰到0就會變成1.然后看一下剩余的1的個數(shù)就可以了。

坑點:

1.雙向隊列首先要判空。直接算1的個數(shù)就可以了。

2.0前面沒有值得情況。這時候0的1不計算。

#include <bits/stdc++.h>using namespace std;const int MAXN=4e5+7;const int inf=1e9;char s[10];int num[MAXN];int main(){    int i;    int t;    int n;    int l,r;    scanf("%d",&t);    for(int tt=1; tt<=t; ++tt)    {        deque<int>q;        scanf("%d",&n);        printf("Case #%d:/n",tt);        int x;        l=r=200001;        int flag=0;        for(i=0; i<n; ++i)        {            scanf("%s",s);            if(s[0]=='P')            {                if(s[1]=='U')                {                    scanf("%d",&x);                    if(!flag)                    {                        num[r++]=x;                        if(!x)q.push_back(r-1);                    }                    else                    {                        num[--l]=x;                        if(!x)q.push_front(l);                    }                }                else                {                    if(!flag)                    {                        r--;                        if(!num[r])q.pop_back();                    }                    else                    {                        if(!num[l])q.pop_front();                        l++;                    }                }            }            else if(s[0]=='R')flag=(flag+1)%2;            else if(s[0]=='Q')            {                if(l>=r)puts("Invalid.");                else if(q.empty())printf("%d/n",(r-l)%2);                else                {                    int ans;                    if(r-l==1)printf("%d/n",num[l]);                    else                    {                        if(!flag)                        {                            ans=(q.front()-l+(q.front()!=r-1))%2;                        }                        else                        {                            ans=(r-q.back()-1+(q.back()!=l))%2;                        }                        printf("%d/n",ans);                    }                }            }        }    }    return 0;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 汽车| 黄冈市| 荥阳市| 平陆县| 蓬溪县| 青冈县| 金湖县| 基隆市| 同江市| 安徽省| 石门县| 府谷县| 金昌市| 福泉市| 隆林| 贵定县| 白山市| 饶阳县| 寻甸| 化州市| 湖口县| 东城区| 西宁市| 钟祥市| 蒙自县| 吉林市| 东乌| 宝应县| 池州市| 麻城市| 老河口市| 镇坪县| 陕西省| 那曲县| 谷城县| 泊头市| 弥勒县| 康乐县| 新乡市| 门源| 宁德市|