HDU 5929 Basic Data Structure(模擬,deque)——2016CCPC東北地區大學生程序設計競賽 - 重現賽

此文章可以使用目錄功能喲↑(點擊上方[+])

 HDU 5929 Basic Data Structure

Accept: 0    Submit: 0
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 is corresponding to the element of the Stack from top to the bottom, value=. Note that the Stackwill 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 (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

∙ 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.

 Problem Idea

解題思路:

【題意】
衆所周知,數據結構中的棧具有PUSH x(此題的x限定爲0 or 1)和POP兩種操作

現在增加兩種操作:REVERSE、QUERY

REVERSE:將棧內元素倒置,即本身從棧頂到棧底的數是a1,a2,...,an,經此操作後,從棧頂到棧底的數爲an,...,a2,a1

QUERY:輸出從棧頂到棧底元素(a1,a2,...,an)的與非值a1 nand a2 nand ... nand an

【類型】
模擬,deque
【分析】
數據結構模擬題,用deque就可以了(deque:雙端隊列,可以對隊列兩端進行操作,REVERSE可以看成是對隊列的兩端進行操作,只需借用一個bool型變量is_top來標記雙端隊列的哪一頭模擬棧頂即可)

但解決問題的關鍵還是在於發現與非運算的規律,這樣,才能將每次查詢時O(n)時間複雜度的計算變爲O(1)

(1):0和任何數與非均爲1(這個任何數不僅僅指0,1,也指任意一個01序列)
(2):奇數個1與非得到1,偶數個1與非得到0
那麼,我們可以知道:我們需要統計的是連續的0或1的個數(用pair(x,y)表示,並放入deque中)
x代表0或者1,y代表當前的x連續有多少個
那麼,如何來計算呢?
特殊情況:deque中就一個元素:根據規律算一下
否則:
我們需要判斷一下:如果棧頂元素是0,那麼答案是1(0和任何數與非得到1)
如果棧頂元素是1,那我們知道,第二個元素必定是0
如果連續的0只有1個而且deque中只有兩個元素那麼這個0元素是個特殊情況
否則:
因爲0與任何數與非均爲1
相當於棧頂元素是1的連續個數多了一個,用O(1)的複雜度就可以解決了

爲了進一步的理解,本人會對代碼中的每種情況加以說明

其中⑴~⑸是PUSH x操作,⑹⑺是POP操作,⑻是REVERSE操作,⑼~⒇+⑶是QUERY操作

⑴往空棧中壓入一個數

因爲此時deque中是空的,所以從哪一頭插入這個數的效果都是一樣的,故d.push_back(make_pair(x,1))和d.push_front(make_pair(x,1))均可


⑵往棧頂壓入一個數(隊尾爲棧頂,且棧頂元素與壓入的數不一致)

由於我們是將連續個1或連續個0當成一個整體存放的,此時放入的數與棧頂元素不同,意味着連續在這裏截斷了,故需另外壓入一個,即d.push_back(make_pair(x,1))


⑶往棧頂壓入一個數(隊尾爲棧頂,且棧頂元素與壓入的數相同)

相同意味着還是一個連續段,那我們只要在原有基礎上+1,就可以表示

同理,後續操作類似,要是有需要博主額外講解的,可在此題留下評論

【時間複雜度&&優化】
O(N)

題目鏈接→HDU 5929 Basic Data Structure

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 10;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
char s[N];
deque< pair<int,int> > d;
deque< pair<int,int> >::iterator it;
deque< pair<int,int> >::reverse_iterator rit;
int main()
{
    int t,n,x,p=1;
    bool is_top;
    pair<int,int> k;
    scanf("%d",&t);
    while(t--)
    {
        d.clear();
        is_top=true;//默認隊尾是棧頂
        scanf("%d",&n);
        printf("Case #%d:\n",p++);
        while(n--)
        {
            scanf("%s",s);
            if(s[2]=='S')//PUSH
            {
                scanf("%d",&x);
                if(d.empty())//⑴
                    d.push_back(make_pair(x,1));
                else
                {
                    if(is_top)
                    {
                        k=d.back();
                        if(k.first!=x)//⑵
                            d.push_back(make_pair(x,1));
                        else//⑶
                        {
                            d.pop_back();
                            k.second++;
                            d.push_back(k);
                        }
                    }
                    else
                    {
                        k=d.front();
                        if(k.first!=x)//⑷
                            d.push_front(make_pair(x,1));
                        else//⑸
                        {
                            d.pop_front();
                            k.second++;
                            d.push_front(k);
                        }
                    }
                }
            }
            else if(s[2]=='P')//POP
            {
                if(is_top)//⑹
                {
                    k=d.back();
                    d.pop_back();
                    k.second--;
                    if(k.second)
                        d.push_back(k);
                }
                else//⑺
                {
                    k=d.front();
                    d.pop_front();
                    k.second--;
                    if(k.second)
                        d.push_front(k);
                }
            }
            else if(s[2]=='V')//REVERSE
                is_top=!is_top;//⑻
            else//QUERY
            {
                if(d.empty())//⑼
                    puts("Invalid.");
                else if(d.size()==1)
                {
                    k=d.back();
                    if(k.first)
                    {
                        if(k.second%2)//⑽
                            puts("1");
                        else//⑾
                            puts("0");
                    }
                    else
                    {
                        if(k.second!=1)//⑿
                            puts("1");
                        else//⒀
                            puts("0");
                    }
                }
                else
                {
                    if(is_top)
                    {
                        k=d.front();
                        if(!k.first)//⒁
                            puts("1");
                        else
                        {
                            it=d.begin();it++;
                            if(d.size()==2&&(*it).second==1)
                            {
                                if(k.second%2)//⒂
                                    puts("1");
                                else//⒃
                                    puts("0");
                            }
                            else
                            {
                                if(k.second%2)//⒄
                                    puts("0");
                                else//⒅
                                    puts("1");
                            }
                        }
                    }
                    else
                    {
                        k=d.back();
                        if(!k.first)//⒆
                            puts("1");
                        else
                        {
                            rit=d.rbegin();rit++;
                            if(d.size()==2&&(*rit).second==1)
                            {
                                if(k.second%2)//⒇
                                    puts("1");
                                else//⒇+⑴
                                    puts("0");
                            }
                            else
                            {
                                if(k.second%2)//⒇+⑵
                                    puts("0");
                                else//⒇+⑶
                                    puts("1");
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}
菜鳥成長記

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章