zoukankan      html  css  js  c++  java
  • 数据结构实验之栈与队列六:下一较大值(二)(SDUT 3333)

    #include <bits/stdc++.h>
    using namespace std;
    int a[1000006];
    int b[1000006];
    int sta[100006];
    int main()
    {
        int t,n,i,j,top;
        while(~scanf("%d",&t))
        {
            while(t--)
            {
                scanf("%d",&n);
                for( i = 1; i <= n; i ++)
                {
                    scanf("%d", &a[i]);
                }
                top = 0;
                b[n] = -1;
                for(i = n - 1; i >= 1; i --)
                {
                    if(a[i + 1] > a[i])
                    {
                        b[i] = a[i + 1];
                        sta[top ++] = b[i];
                       // cout << "sta[top] = "<< sta[top - 1] << endl;
                     }
                    else {
                        int f = 1;
                        while(top)
                        {
                           // cout << "top = " <<top << endl;
                            if(sta[top - 1] > a[i])
                            {
                                b[i] = sta[top - 1];
                                f = 0;
                                break;
                            }
                            top --;
                        }
                        if(top == 0 || f  == 1) b[i] = -1;
                    }
                }
                for(i = 1; i <= n; i ++)
                {
                    printf("%d-->%d
    ",a[i],b[i]);
                }
                if(t != 0) printf("
    ");
            }
    
        }
        return 0;
    }
    
    
  • 相关阅读:
    JavaScript 教程
    C#基础实例
    Angularjs实例5
    Angularjs实例4
    Angularjs 数据过滤
    Angularjs实例3
    Angularjs 数据循环
    Angularjs模块
    Angularjs 数据双向绑定
    Angularjs实例应用
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139485.html
Copyright © 2011-2022 走看看