HZAU 网络赛 Sequence Number


1205: Sequence Number

Time Limit: 1 Sec   Memory Limit: 1280 MB
Submit: 869   Solved: 217
[ Submit][ Status][ Web Board]

Description

     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 each other. If exist positive integers i , j, ( 1 ≤ i < j ≤ n and A[i] > A[j]), <A[i], A[j]> is regarded as one of A’s inversions. The number of inversions is regarded as inversion number. 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], <A[i], A[j]> is regarded as one of A’s sequence pair. The number of sequence pairs is regarded as sequence number. Define j – i as the length of the sequence 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) of the array.  

Output

 Output the answer S in one line for each case. 

Sample Input

5
2 3 8 6 1

Sample Output

3


暴力(72ms)

#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
const int maxn = 50005;
const int mod = 10000007;
#define ll long long
#define rush() int T;scanf("%d",&T);while(T--)
int a[maxn];
int n;
int main()
{
    while(~scanf("%d",&n))
    {
        bool flag=0;
        f(i,0,n)
        {
            scanf("%d",&a[i]);
            if(i!=0&&a[i]>a[i-1])
                flag=true;
        }
        if(flag==0)                     //优化一
        {
            printf("0\n");
            continue;
        }
        int ans=0;
        int k=0;
        f(i,0,n)
        {
            k=max(k,i+1);               //优化二
            for(int j=k;j<n;j++)
            {
                if(a[j]>=a[i]&&j-i>ans)
                {
                    ans=j-i;
                    k=j;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值