PAT 甲级 1057 Stack

https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian -- return the median value of all the elements in the stack. With N elements, the median value is defined to be the (-th smallest element if N is even, or (-th if N is odd.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤). Then N lines follow, each contains a command in one of the following 3 formats:

Push key
Pop
PeekMedian

where key is a positive integer no more than 1.

Output Specification:

For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print Invalid instead.

Sample Input:

17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop

Sample Output:

Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

代码:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e5 + 10;
int N;
int c[maxn];
stack<int> s;

int lowerbit(int x) {
    return x & -x;
}

void update(int x, int v) {
    for(int i = x; i < maxn; i += lowerbit(i))
        c[i] += v;
}

int getsum(int x) {
    int sum = 0;
    for(int i = x; i >= 1; i -= lowerbit(i))
        sum += c[i];
    return sum;
}

void PeekMedian() {
    int left = 1, right = maxn, mid, k = (s.size() + 1) / 2;
    while(left < right) {
        mid = (left + right) / 2;
        if(getsum(mid) >= k)
            right = mid;
        else left = mid + 1;
    }
    printf("%d\n", left);
}

int main() {
    scanf("%d", &N);
    while(N --) {
        string op;
        cin >> op;
        if(op == "Pop") {
            if(s.empty()) printf("Invalid\n");
            else {
                printf("%d\n", s.top());
                update(s.top(), -1);
                s.pop();
            }
        } else if(op == "Push") {
            int num;
            scanf("%d", &num);
            s.push(num);
            update(num, 1);
        } else {
            if(s.empty()) printf("Invalid\n");
            else PeekMedian();
        }
    }
    return 0;
}

  树状数组 第一次写到树状数组 看博客还没看的很懂但是隐约感觉到这是一个模板题 emmmm 以后多做一点就会明白了叭 唉 哭唧唧

FH

原文地址:https://www.cnblogs.com/zlrrrr/p/10357628.html

时间: 2024-11-08 02:16:08

PAT 甲级 1057 Stack的相关文章

PAT 甲级1057 Stack (30 分)(不会,树状数组+二分)*****

1057 Stack (30 分) Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). No

PAT甲级1057 Stack【树状数组】【二分】

题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 题意:对一个栈进行push, pop和找中位数三种操作. 思路: 好久没写题.感觉傻逼题写多了稍微有点数据结构的都不会写了. pop和push操作就不说了. 找中位数的话就二分去找某一个数前面一共有多少小于他的数,找到那个小于他的数刚好等于一半的. 找的过程中要用到前缀和,所以自然而然就应该上树状数组. 要注意树状数组的界应该是1e5而

PAT甲级——A1057 Stack

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed

PAT 1057. Stack (30)

题目地址:http://pat.zju.edu.cn/contests/pat-a-practise/1057 用树状数组和二分搜索解决,对于这种对时间复杂度要求高的题目,用C的输入输出显然更好 #include <cstdio> #include <string> #include <vector> #include <stack> using namespace std; const int NUM=100001; struct TreeArray {

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT甲级题分类汇编——理论

理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 分数 大意 Author 1051 Pop Sequence 25 判断一个序列是否是pop序列 CHEN, Yue 1052 Linked List Sorting 25 链表排序 CHEN, Yue 1057 Stack 30 一个有中位数功能的stack CHEN, Yue 1074 Reversing Linked List

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级专题|最短路

PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做,只能得20分 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 510; int cmax,n,ter,m; int caps[maxn]; int g[maxn][m