[HDOJ5929]Basic Data Structure(双向队列,规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5929

题意:维护一个栈,支持往栈里塞 0/1 ,弹栈顶,翻转栈,询问从栈底到栈顶按顺序 NAND 的值。

题解:只要知道最后的 00 后面 11 的个数的奇偶性就行。可以用链表把所有 00 的位置存下来。

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3
  4 typedef long long LL;
  5
  6 const int maxn = 866666;
  7 int n;
  8 int q[maxn], l, r;
  9 int ll[maxn], rr[maxn];
 10 int zerol, zeror;
 11 char cmd[10];
 12
 13 int main() {
 14   // freopen("in", "r", stdin);
 15   int T, _ = 1;
 16   scanf("%d", &T);
 17   while(T--) {
 18     scanf("%d", &n);
 19     l = maxn/2 + 1; r = maxn/2;
 20     int x, cur;
 21     zerol = zeror = -1;
 22     int dir = 0, zero = 0;
 23     printf("Case #%d:\n", _++);
 24     while(n--) {
 25       scanf("%s", cmd);
 26       if(strcmp(cmd, "PUSH")==0) {
 27         scanf("%d", &x);
 28         if(!dir) {
 29           q[--l] = x;
 30           cur = l;
 31         }
 32         else {
 33           q[++r] = x;
 34           cur = r;
 35         }
 36         if(x == 0) {
 37           zero++;
 38           if(zero == 1) {
 39             zerol = zeror = cur;
 40             ll[cur] = rr[cur] = -1;
 41           }
 42           else {
 43             if(!dir) {
 44               ll[zerol] = cur; rr[cur] = zerol;
 45               ll[cur] = -1; zerol = cur;
 46             }
 47             else {
 48               rr[zeror] = cur; ll[cur] = zeror;
 49               rr[cur] = -1; zeror = cur;
 50             }
 51           }
 52         }
 53       }
 54       if(strcmp(cmd, "POP")==0) {
 55         if(!dir) {
 56           x = q[l];
 57           cur = l++;
 58         }
 59         else {
 60           x = q[r];
 61           cur = r--;
 62         }
 63         if(x == 0) {
 64           zero--;
 65           if(zero == 0) zerol = zeror = -1;
 66           else {
 67             if(!dir) {
 68               ll[rr[cur]] = -1;
 69               zerol = rr[cur];
 70             }
 71             else {
 72               rr[ll[cur]] = -1;
 73               zeror = ll[cur];
 74             }
 75           }
 76         }
 77       }
 78       else if(strcmp(cmd, "REVERSE")==0) dir ^= 1;
 79       else if(strcmp(cmd, "QUERY")==0) {
 80         int siz = r - l + 1, one;
 81         if(!siz) printf("Invalid.\n");
 82         else {
 83           if(!zero) printf("%d\n", siz%2);
 84           else {
 85             int tmp;
 86             if(!dir) {
 87               one = r - zeror;
 88               tmp = (zeror == l);
 89             }
 90             else {
 91               one = zerol - l;
 92               tmp = (zerol == r);
 93             }
 94             printf("%d\n", one % 2 == tmp);
 95           }
 96         }
 97       }
 98     }
 99   }
100   return 0;
101 }
时间: 2024-10-06 00:43:12

[HDOJ5929]Basic Data Structure(双向队列,规律)的相关文章

hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called

HDU 5929 Basic Data Structure 模拟

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: p

HDU 5929 Basic Data Structure

题目:Basic Data Structure 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:t个测试数据,每个数据给出一个m表示操作数量,操作分为4种:PUSH x(x:0或1),POP,REVERSE(翻转栈里面的数),QUERY(假使栈内的数一个个出栈,并按出栈顺序将他们与非起来,问最终结果(注意,并不是真的出栈)) 思路: 做题的时候忘记了与非是按出栈顺序来的,一直按栈底到栈顶的顺序来,WA到死... 根据与非的性质,1.0与非

【推导】【线段树】hdu5929 Basic Data Structure

题意: 维护一个栈,支持以下操作: 从当前栈顶加入一个0或者1: 从当前栈顶弹掉一个数: 将栈顶指针和栈底指针交换: 询问a[top] nand a[top-1] nand ... nand a[bottom]的值. nand是这样定义的: ?? 0 nand 0 = 1 ?? 0 nand 1 = 1 ?? 1 nand 0 = 1 ?? 1 nand 1 = 0 关键是要发现性质,任何数nand 0,都会变成1.反复nand上1的话,则值会交替变化. 所以假设当前栈顶在左侧,只需要找到最右侧

B - I Can Guess the Data Structure!(建议使用栈、队列和优先队列来模拟)

There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. 2 Take out an element from the bag. Given a sequence of operations with return values, you're going to guess the data structure. It is a stack (Last-I

poj 2905 双向队列(待补充)

Parallel Computer Simulator Description Programs executed concurrently on a uniprocessor system appear to be executed at the same time, but in reality the single CPU alternates between the programs, executing some number of instructions from each pro

HDU 4217 Data Structure?(线段树 or 树状数组啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4217 Problem Description Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently

HDU 2217 Data Structure?

C - Data Structure? Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice id=27724" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="display:inline-

hdu4217 Data Structure?

Problem Description Data structure is one of the basic skills for Computer Science students, which is a particular way of storing and organizing data in a computer so that it can be used efficiently. Today let me introduce a data-structure-like probl