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,int w);
int sum(int p);
int main()
{
    int n,w,left,right,mid,k;
    string op;
    scanf("%d",&n);
    while(n--)
    {
        cin>>op;
        if(op[1]==‘u‘)
        {
            scanf("%d",&w);
            sta.push(w);
            update(w,1);
        }
        else
        {
            if(sta.size()<1)    printf("Invalid\n");
            else if(op[1]==‘o‘)
            {
                printf("%d\n",sta.top());
                update(sta.top(),-1);
                sta.pop();
            }
            else
            {
                left=0;right=maxn;k=(sta.size()+1)/2;
                while(left<=right)
                {
                    mid=(left+right)/2;
                    if(sum(mid)>=k) right=mid-1;
                    else    left=mid+1;
                }
                printf("%d\n",left);
            }
        }
    }
    system("pause");
    return 0;
}
void update(int p,int w)
{
    for(;p<maxn;p += p&(-p))
        tree[p]+=w;
}
int sum(int p)
{
    int ans=0;
    for(;p>0;p -= p&(-p))
        ans+=tree[p];
    return ans;
}
//分块
#include<bits/stdc++.h>
using namespace std;
const int maxn=100010,block_size=367;
int cnt[maxn],block[280];
stack<int> sta;
void PUSH_POP(int index,int v);
int Find(int k);
int main()
{
    int n,w;
    string op;
    scanf("%d",&n);
    while(n--)
    {
        cin>>op;
        if(op[1]==‘u‘)
        {
            scanf("%d",&w);
            sta.push(w);
            PUSH_POP(w,1);
        }
        else
        {
            if(sta.size()<1)    printf("Invalid\n");
            else if(op[1]==‘o‘)
            {
                printf("%d\n",sta.top());
                PUSH_POP(sta.top(),-1);
                sta.pop();
            }
            else  printf("%d\n",Find((sta.size()+1)/2));
        }
    }
    system("pause");
    return 0;
}
void PUSH_POP(int index,int v)
{
  cnt[index]+=v;
  block[index/block_size]+=v;
}
int Find(int k)
{
  int sum=0,i=0,index;
  while(sum + block[i] < k)
    sum+=block[i++];
  index=block_size*i;
  while(1)
  {
    if(sum + cnt[index] >= k) return index;
    else sum+=cnt[index],index++;
  }
}

原文地址:https://www.cnblogs.com/VividBinGo/p/12233459.html

时间: 2024-11-08 18:29:05

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

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

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

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

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

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c