PAT (Advanced Level) 1057. Stack (30)

树状数组+二分。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<stack>
#include<vector>
using namespace std;

const int maxn=100000+10;
int n;
int c[maxn];
stack<int>S;

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

int getsum(int pos)
{
    int res=0;
    while(pos>0)
    {
        res=res+c[pos];
        pos=pos-lowbit(pos);
    }
    return res;
}

void update(int pos,int val)
{
    while(pos<=100000)
    {
        c[pos]=c[pos]+val;
        pos=pos+lowbit(pos);
    }
}

int main()
{
    while(!S.empty()) S.pop();
    memset(c,0,sizeof c);
    scanf("%d",&n);
    int sz=0;
    for(int i=1;i<=n;i++)
    {
        char op[15]; scanf("%s",op);
        if(strcmp(op,"Pop")==0)
        {
            if(sz==0) printf("Invalid\n");
            else
            {
                sz--;
                printf("%d\n",S.top());
                update(S.top(),-1);
                S.pop();
            }
        }
        else if(strcmp(op,"PeekMedian")==0)
        {
            if(sz==0) printf("Invalid\n");
            else
            {
                int l=1,r=100000;
                int ans;
                while(l<=r)
                {
                    int mid=(l+r)/2;
                    if(getsum(mid)>=(sz+1)/2)
                    {
                        ans=mid;
                        r=mid-1;
                    }
                    else l=mid+1;
                }
                printf("%d\n",ans);
            }
        }
        else
        {
            int num; scanf("%d",&num);
            update(num,1);
            sz++;
            S.push(num);
        }
    }
    return 0;
}
时间: 2024-08-03 22:05:49

PAT (Advanced Level) 1057. Stack (30)的相关文章

PAT (Advanced Level) 1057 Stack

题解 第一种方法:令数组tree[]记录栈中的元素,栈中的数值 x 的个数为 tree[x] .树状数组维护tree[],然后二分查找.   第二种方法:利用分块,以一定长度区间为单位,记录栈中数值的个数,然后暴力查找. 代码 //树状数组 + 二分 #include<bits/stdc++.h> using namespace std; const int maxn=100010; int tree[maxn]; stack<int> sta; void update(int p

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 甲级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 (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d