题解 | #大雨吃小鱼#
大雨吃小鱼
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; }