HDU 5929 Basic Data Structure (two-way queue + simulation)

Topic link

VJ-HDU-5929

answer

Title

Create a new data structure and complete the four operations given in the title.

Ideas

The more troublesome of the four operations is the latter two: Reverseand Query.

  1. Reverse: You can record the toplocation and bottomlocation, and you can O(1)complete it by just swapping ;
  2. Query: Slightly troublesome:
    1. First of all, we need to determine the rules:

      When encountering 0, the result of NAND must be 1;
      in the case of only 1, if the number is odd, the final result is 1; if it is even, the final result is 0;

    2. Then we only need to record the position of the last digit 0(the one closest to the bottom of the stack 0) to solve the problem (be sure to pay attention to 0the situation that the last digit is at the top, at this time, there is no previous number and 0 for NAND operation , Cannot directly use the "odd 1 even 0" rule);

    3. Note that the Popsum Reverseoperation may cause the last bit obtained 0to be popped or changed, then we need to find a new one at this time 0, traversal is very unrealistic, we need to use a two-way queue to store all 0positions.

AC code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<deque>
#include<vector>
#include<ctime>

using namespace std;
//#pragma GCC optimize(2)
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ull unsigned long long
#define ll long long
#define rep(i, x, y) for(int i=x;i<=y;i++)
#define mms(x, n) memset(x, n, sizeof(x))
/// mms中使用 0x3f 可以使得其初始化为INF
#define mmc(A, tree) memcpy(A, tree, sizeof(tree))
#define eps (1e-8)
#define PI (acos(-1.0))
#define INF (0x3f3f3f3f)
#define mod (ll)(1e9+7)
typedef pair<int, int> P;
int const N = 1e7;
int const TOP = 3e5;

deque<int> q;
int st[N];
int top, bottom;
int size;
int zero, one;
int flag;

void init() {
    
    
    mms(st, 0);
    top = bottom = TOP;
    size = zero = one = 0;
    flag = 1;
    q.clear();
}

void PUSH() {
    
    
    size += 1;
    int t;
    scanf("%d", &t);
    if (flag) {
    
    
        top += 1;
        st[top] = t;
        if (t == 0) {
    
    
            zero += 1;
            q.push_back(top);
        } else one += 1;
    } else {
    
    
        top -= 1;
        st[top] = t;
        if (t == 0) {
    
    
            zero += 1;
            q.push_front(top);
        } else one += 1;
    }
}

void POP() {
    
    
    size -= 1;
    if (flag) {
    
    
        if (st[top] == 0) {
    
    
            q.pop_back();
            zero -= 1;
        } else one -= 1;
        top -= 1;
    } else {
    
    
        if (st[top] == 0) {
    
    
            q.pop_front();
            zero -= 1;
        } else one -= 1;
        top += 1;
    }
}

void REVERSE() {
    
    
    if (flag) {
    
    
        int tmp = top;
        top = bottom + 1;
        bottom = tmp + 1;
    } else {
    
    
        int tmp = top;
        top = bottom - 1;
        bottom = tmp - 1;
    }
    flag = !flag;
}

void QUERY() {
    
    
    if (size == 0) {
    
    
        printf("Invalid.\n");
        return;
    } else if (size == 1) {
    
    
        printf("%d\n", st[top]);
        return;
    } else {
    
    
        int t = flag ? 1 : -1;
        if (st[bottom + t] == 0) {
    
    
            printf("1\n");
            return;
        }
        if (q.size() == 1) {
    
    
            /// 这里不一定输出结果,如果用if-else嵌套,会导致后续无输出。
            if (q.front() == top) {
    
    
                if (one & 1) printf("1\n");
                else printf("0\n");
                return;
            }
        } else if (q.empty()) {
    
    
            if (one & 1) printf("1\n");
            else printf("0\n");
            return;
        }
        // 这里一定不要嵌套啊!!!!!!!!!!!!!!!!!!
        int cnt;
        if (flag) cnt = q.front() - bottom;
        else cnt = bottom - q.back();
        if (cnt & 1) printf("1\n");
        else printf("0\n");

    }
}

int main() {
    
    
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int T;
    scanf("%d", &T);
    for (int _ = 1; _ <= T; _++) {
    
    
        printf("Case #%d:\n", _);
        init();
        int n;
        scanf("%d", &n);
        char ch[10];
        while (n--) {
    
    
            scanf("%s", ch);
            switch (ch[2]) {
    
    
                case 'S':
                    PUSH();
                    break;
                case 'P':
                    POP();
                    break;
                case 'E':
                    QUERY();
                    break;
                case 'V':
                    REVERSE();
                    break;
                default:
                    break;
            }
        }
    }
    return 0;
}

postscript

This question, I will remember it for a lifetime!
The rules of the game were not considered clearly (the situation where 0 was in the first place was not taken into consideration), and the code was of no use if it was played correctly. I typed the code four times for one question, but after the game was over, the rules were wrong and I cracked!
What is even more uncomfortable is that there have obviously been different patterns in the game, and we have found a counterexample, but we have miscalculated it! Hanban Hanban Hanban Hanban Hanban!
Finally changed the rules and kept WA! ! ! ! I exploded!
After changing it for a long time, I finally found a logical error. QAQ
if-else nesting resulted in a situation where there is no output! Naive!

(After writing test cases, you need to write them on a piece of paper, otherwise they are scattered everywhere, and you don’t know what you have written after a long time.
( If there are multiple code errors, you must consider the correctness of the rules!

Guess you like

Origin blog.csdn.net/qq_45934120/article/details/108828764