PAT 1057 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 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 (N/2)-th smallest element if N is even, or ((N+1)/2)-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 (≤10​5​​). 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 10​5​​.

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

思路: pop和push就是常见的栈操作,但PeekMedian是将栈中的值排序后取中间值输出,这一点栈做不到。

 代码 

  最容易想到的思路(但明显超时):进行PeekMedian操作时,将栈中元素排序输出中间值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int Max=1e3+5;
int main()
{
    vector<int> v;
    vector<int> Temp;
    int N,x;
    char op[20];
    scanf("%d",&N);
    while(N--)
    {
        scanf("%s",op);
        int Size=v.size();
        if(op[1]=='o')
        {
            if(!Size)
                printf("Invalid\n");
            else
            {
                printf("%d\n",v[Size-1]);
                v.erase(v.begin()+Size-1);
            }
        }
        else if(op[1]=='u')
        {
            scanf("%d",&x);
            v.push_back(x);
        }
        else if(op[1]=='e')
        {
            if(!Size)
                printf("Invalid\n");
            else
            {
                Temp=v;
                sort(Temp.begin(),Temp.end());
                if(Size%2==0)
                    printf("%d\n",Temp[(Size-1)/2]);
                else
                    printf("%d\n",Temp[Size/2]);
            }
        }
    }
    return 0;
}

  另一种巧妙的思路是利用 树状数组+二分,详细见代码注释

#include<cstdio>
#include<cstdlib>
#include<stack>
#define MAX 100005

using namespace std;
stack<int> s;
int TreeArray[MAX];//树状数组里A[i]的值,表示i出现的次数

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

void update(int n,int num)
{
    while(n<=MAX)
    {
        TreeArray[n]+=num;
        n+=lowbit(n);
    }
}

int GetSum(int n)
{
    int sum=0;
    while(n>0)
    {
        sum+=TreeArray[n];
        n-=lowbit(n);
    }
    return sum;
}

int BinarySearch(int target)  //对栈中间元素进行二分查找,GetSum(mid)返回的值
{                             //表示0~mid之间元素的个数,如果等于target,则mid
                              //就是要求的结果
    int left=0;
    int right=MAX;
    int mid;
    while(left<=right)
    {
        mid=left+(right-left)/2;
        int x=GetSum(mid);
        if(x>target||x==target)
            right=mid-1;
        else if(x<target)
            left=mid+1;
    }
    //左边界值
    return left;
}

void Pop()
{
    if(s.empty())
        printf("Invalid\n");
    else
    {
        int key=s.top();
        s.pop();
        printf("%d\n",key);
        update(key,-1);
    }
}

void Push()
{
    int key;
    scanf("%d",&key);
    s.push(key);
    update(key,1);
}

void PeekMedian()
{
    if(s.empty())
        printf("Invalid\n");
    else
    {
        int midvalue=BinarySearch((s.size()+1)/2);
        printf("%d\n",midvalue);
    }
}
int main()
{
    char str[20];
    int i,N;
    scanf("%d",&N);
    getchar();
    while(N--)
    {
        scanf("%s",str);
        switch(str[1])
        {
          case 'o':Pop();break;
          case 'u':Push();break;
          case 'e':PeekMedian();break;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ZCMU_2024/article/details/84993945