PAT(A) - 1057. Stack (30)

来源:互联网 发布:机械行业转行it 编辑:程序博客网 时间:2024/06/05 02:59


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 (<= 105). 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 105.

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:
17PopPeekMedianPush 3PeekMedianPush 2PeekMedianPush 1PeekMedianPopPopPush 5Push 4PeekMedianPopPopPopPop
Sample Output:
InvalidInvalid322124453Invalid

思路分析:直接暴力会超时,我以前参考别人的代码,利用树状数组做的。今天看了晴神的书,用的分块思想,好神奇呀~


#include <cstdio>#include <stack>#include <cmath>#include <string.h>#define MAX 100000 + 10using namespace std;int block[316];int table[MAX];int sqr = ( int )sqrt( MAX );stack<int> s;using namespace std;void Push( int num ) {    s.push( num );    block[num / sqr]++;    table[num]++;}void Pop() {    if( s.empty() ) {        printf( "Invalid\n" );        return;    }    int top = s.top();    s.pop();    table[top]--;    block[top / sqr]--;    printf( "%d\n", top );}void PeekMedian() {    if( s.empty() ) {        printf( "Invalid\n" );        return;    }    int k = s.size();    if( k % 2 == 1 ) k = ( k + 1 ) / 2;    else k = k / 2;    int sum = 0;    int id = 0;    while( sum + block[id] < k ) {        sum = sum + block[id];        id++;    }    int num = id * sqr;    while( sum + table[num] < k ) {        sum += table[num];        num++;    }    printf( "%d\n", num );}int main() {    //freopen( "123.txt", "r", stdin );    int n;    scanf( "%d", &n );    char op[20];    int num;    for( int i = 0; i < n; i++ ) {        scanf( "%s", op );        if( !strcmp( op, "Push" ) ) {            scanf( "%d", &num );            Push( num );        }        else if( !strcmp( op, "Pop" ) ) {            Pop();        }        else if( !strcmp( op, "PeekMedian" ) ) {            PeekMedian();        }    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 喝了酵素胃疼怎么办 海科融通不到账怎么办 美团外卖没生意怎么办 淘宝联盟领券销售怎么办 微信返利被骗了怎么办 众筹失败后资金怎么办 健身房不给退卡怎么办 婆婆陷入民间传销组织怎么办 被三生公司骗了怎么办? ppt保存成了图片怎么办 苹果6速度变慢了怎么办 苹果6s速度很慢怎么办 微信支付上限了怎么办 佳享健康骗老人怎么办 宝宝吃了硅胶乳贴怎么办 用完卫生巾后阴部有些不舒服怎么办 指甲上有荧光剂怎么办 小孩吃了荧光剂怎么办 毛巾上有荧光剂怎么办 用过劣质面膜后怎么办 液体硅胶奶嘴煮完有味怎么办 后跟贴粘在鞋上怎么办 优化营商环境公安怎么办 提升营商环境公安怎么办 准予迁入证明过期了怎么办 粉底液容易脱妆怎么办 家人进了火疗传销怎么办 自发热护膝洗了怎么办 用气垫bb卡粉怎么办 贴药膏后皮肤过敏红肿怎么办 贴完膏药皮肤痒怎么办 猕猴桃吃的嘴疼怎么办 摩拜单车怎么办月卡 出国忘了带护照怎么办 雅漾喷雾失压了怎么办 洗衣服时衣服粘上卫生纸怎么办 一晚上卫生巾都是满的怎么办 宝宝头上痱子痒怎么办 短裤里的宽松紧带拧了怎么办 肉色内衣被染黑色了怎么办 安全裤总往上缩怎么办