版权声明:转载请附带原文链接,请勿随意删除原文内容,允许少量格式和/或内容修改,谢谢! https://blog.csdn.net/weixin_37661548/article/details/88601299
单调栈
题目链接
struct node
{
int value,nxt;
node(void) : value(0),nxt(0) {}
node(int _value,int _nxt) : value(_value),nxt(_nxt) {}
}S[20];
int n,top;
int seq[20],ans[20];
...
void work()
{
for (int i = 1;i <= n;++i)
{
if (top == 0)
{
S[++top] = node(seq[i],i);
continue;
}
while (seq[i] > S[top].value && top != 0)
ans[S[top--].nxt] = seq[i];
S[++top] = node(seq[i],i);
}
for (int i = 1;i <= n;++i) printf("%d ",ans[i] == 0 ? -1 : ans[i]);
}