题目来源:2017华中农大网络赛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]), < 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 —— equence 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. 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
题意概括
给出n个数字,问在这n个数字中存在两个数a[i]<=a[j],求j-i的最大值;
解题思路
首先计算出第一个数字差值,查找第二个数字的时候懂i+max开始查找优化,如果不优化的的话,会超时。
代码
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <algorithm>
using namespace std;
int a[50010];
int main ()
{
int n,i,j,k,maxx;
while (scanf("%d",&n)!=EOF)
{
k = 0;
for (i = 1; i <= n; i ++)
scanf("%d",a+i);
maxx = 0; j = 1;
for (i = n; i > 0; i --)
if (a[i] >= a[1])
break;
maxx = i-1;
for (j = 2; j <= n-maxx; j ++)
{
for (k = n; k >= j+maxx; k --)
{
if (a[j] <= a[k])
{
maxx = max(maxx,k-j);
break;
}
}
}
printf("%d
",maxx);
}
return 0;
}