HZAU 1205 Sequence Number

題目鏈接:http://acm.hzau.edu.cn/5th.pdf

G. Sequence Number

In Linear algebra, we have learned the definition of inversion number:

Assuming A is a ordered set with n numbers ( n > 1 ) which are different from eachother. If exist positive integers i , j, ( 1 ≤ i < j ≤ n and A[i] > A[j]),isregarded as one of A’s inversions. The number of inversions is regarded as inversionnumber. Such as, inversions of array <2,3,8,6,1> are <2,1>, <3,1>, <8,1>, <8,6>,<6,1>,and the inversion number is 5.Similarly, we define a new notion —— sequence number, If exist positive integers i, j, ( 1≤ i ≤ j ≤ n and A[i] <= A[j],is regarded as one of A’s sequence pair. Thenumber of sequence pairs is regarded as sequence number. Define j – i as the length of thesequence pair.

Now, we wonder that the largest length S of all sequence pairs for a given array A.

Input

There are multiply test cases.In each case, the first line is a number N(1<=N<=50000 ), indicates the size of the array,the 2th ~n+1th line are one number per line, indicates the element Ai (1<=Ai<=10^9) ofthe array.

Output

Output the answer S in one line for each case.

Sample Input

5

2 3 8 6 1

Sample Output

3

提示

題意:

給出一個數列,求出前面的數小於等於後面的數的若干個數對中相距最長那個的距離,

如樣例所示:

符合條件的有(2,3)(2,8)(2,6)(3,8)(3,6),它們本身就不列出來了。

距離最長的是(2,6)數對,長度爲3

思路:

我的是單調棧(遞減)做法,如果當前元素小於棧頂元素入棧,否則依次和棧中元素計算相對距離且取最長的那一個。

這題對於大部分人應該不算難,反正是難倒我了(⊙﹏⊙)b。

示例程序

#include <cstdio>
#include <algorithm>
using namespace std;
struct jj
{
    int pos,x;
}a[50000];
int main()
{
    int n,i,i1,x,top,maxnum;
    while(scanf("%d",&n)!=EOF)
    {
        top=0;
        maxnum=0;
        for(i=0;n>i;i++)
        {
            scanf("%d",&x);
            if(top==0||a[top-1].x>x)
            {
                a[top].x=x;
                a[top].pos=i;
                top++;
            }
            else
            {
                for(i1=top-1;i1>=0&&a[i1].x<=x;i1--)
                {
                    maxnum=max(maxnum,i-a[i1].pos);
                }
            }
        }
        printf("%d\n",maxnum);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章