int max(int x, int y)
{
return (x > y) ? x : y;
}
int min(int x, int y)
{
return (x < y ) ? x : y;
}
int n, arr[10000], arr2[10000], arr3[10000];
void process(int *table)
{
int temp[10000], len = 0, i;
int left = 0, right = len - 1, mid;
temp[0] = arr[0]; len = 1; table[0] = 1;
for(i = 1; i < n; i++)
{
if(temp[len - 1] < arr[i])
{
temp[len++] = arr[i]; table[i] = len;
}
else if(temp[0] >= arr[i])
{
temp[0] = arr[i]; table[i] = 1;
}
else
{
left = 0; right = len - 1;
while(1)
{
mid = (left + right) / 2;
if(temp[mid - 1] < arr[i] && temp[mid] >= arr[i])
{
temp[mid] = arr[i];
table[i] = mid + 1;
break;
}
else if(temp[mid] > arr[i]) right = mid - 1;
else left = mid + 1;
}
}
/*printf("res == %d %d %d\n",i,temp[i],table[i]);*/
}
}
int main()
{
int i, t;
while(scanf("%d",&n) == 1)
{
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
process(arr2);
for(i = 0; i < n / 2; i++)
{
t = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = t;
}
process(arr3);
int sol = 0;
for(i = 0; i < n; i++)
sol = max(sol, min(arr2[i], arr3[n - i - 1]));
printf("%d\n", 2 * (sol - 1) + 1);
}
return 0;
}
/*
Input:
10
1 2 3 4 5 4 3 2 1 10
19
1 2 3 2 1 2 3 4 3 2 1 5 4 1 2 3 2 2 1
5
1 2 3 4 5
Output:
9 9 1
*/
No comments:
Post a Comment