题解 | #大雨吃小鱼#

大雨吃小鱼

https://www.nowcoder.com/practice/77199defc4b74b24b8ebf6244e1793de

#include <stdio.h>
#define Max 100001
// 输入准备
int nums[Max];
int numsSize;
// 单调栈
int stack[Max][2];
int top;
int getReturns(void);
int fmax(int a,int b);
int main(){
    while(scanf("%d",&numsSize) != EOF){
        for(int i=0;i<numsSize;i++){
            scanf("%d",&nums[i]);
        }
        printf("%d\n",getReturns());
    }
    return 0;
}
int fmax(int a,int b){
    return a >= b ? a : b;
}
int getReturns(void){
    top = 0;
    int ans = 0;
    for(int i=numsSize-1;i>=0;i--){
        int turn = 0;
        while(top > 0 && stack[top-1][0] < nums[i]){
            turn = fmax(turn + 1, stack[--top][1]);
        }
        stack[top][0] = nums[i];
        stack[top++][1] = turn;
        ans = fmax(ans, turn);
    }
    return ans;
}

全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务