SDUT3333&&SDUT3332数据结构实验之栈五:下一较大值(一)

来源:互联网 发布:栅格数据 链式编码 编辑:程序博客网 时间:2024/05/17 06:59
#include<bits/stdc++.h>using namespace std;struct node{    int num,next,id;};void nextmax(int n){    stack<struct node>s;    struct node a[100050],t;    for(int i=1; i<=n; i++)    {        scanf("%d",&a[i].num);        a[i].next=-1;        a[i].id=i;        if(s.empty())            s.push(a[i]);        else        {            while(!s.empty())            {                t=s.top();                if(a[i].num>t.num)                {                    a[t.id].next=a[i].num;                    s.pop();                }                else                    break;            }            s.push(a[i]);        }    }    for(int i=1; i<=n; i++)        printf("%d-->%d\n",a[i].num,a[i].next);}int main(){    int n,T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        nextmax(n);        if(T)printf("\n");    }    return 0;}

0 0
原创粉丝点击