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 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
1. 题意分析:给一串数字,让找出里面1 ≤ i ≤ j ≤ n and A[i] <= A[j]的每一对,计算出最大的j-i的值。
2. 解题思路:这个题的思路是,用双循环来找出种可能,每次记录一个最大j-i的值。但是我们优化了一下,就是每次内循环的第一次查找就直接跳
#include <stdio.h>
int a[50010];
int main()
{
int max, i, j, n, flag, l, ret;
while(~scanf("%d", &n))
{
for(i=1; i<=n; i++)
scanf("%d", &a[i]);
max=1;
for(ret=0,i=1; i<=n-1; i++)
{
l=max;
for(j=i+l; j<=n; j++)
{
if(a[i]<a[j])
{
flag=j-i;
if(flag>=max)
{
max=flag;
ret=1;
}
}
}
}
if(ret==1)
printf("%d\n", max);
else
printf("0\n");
}
}
到 max+i 的地方直接查找;