下班时无聊随手瞎写的,对不对你自己检查一下
程序代码:
#include <stdio.h>
#include <limits.h>
long long foo( unsigned n, unsigned k, unsigned maxdist, const int a[] )
{
#define __MAX(a,b) (((a) > (b)) ? (a) : (b))
#define __MIN(a,b) (((a) < (b)) ? (a) : (b))
long long retval = LLONG_MIN;
long long buf[50][10][2];
for( unsigned i=0; i!=n; ++i )
{
buf[i][0][0] = a[i];
buf[i][0][1] = a[i];
for( unsigned j=1; j!=__MIN(i+1,k); ++j )
{
long long minval = LLONG_MAX;
long long maxval = LLONG_MIN;
for( unsigned p=__MAX(j-1,i>maxdist?i-maxdist:0); p!=i; ++p )
{
minval = __MIN( minval, __MIN(buf[p][j-1][0]*a[i],buf[p][j-1][1]*a[i]) );
maxval = __MAX( maxval, __MAX(buf[p][j-1][0]*a[i],buf[p][j-1][1]*a[i]) );
}
buf[i][j][0] = minval;
buf[i][j][1] = maxval;
}
if( i+1 >= k )
retval = __MAX( retval, buf[i][k-1][1] );
}
return retval;
#undef __MAX
#undef __MIN
}
#include <assert.h>
int main( void )
{
{
const int a[] = { 7, 4, 7 };
long long r = foo( 3, 2, 1, a );
assert( r == 28 );
printf( "%lld\n", r );
}
{
const int a[] = { 7, 4, 7 };
long long r = foo( 3, 2, 50, a );
assert( r == 49 );
printf( "%lld\n", r );
}
{
const int a[] = { -3, -5, -8, -9, -1, -2 };
long long r = foo( 6, 3, 3, a );
assert( r == -10 );
printf( "%lld\n", r );
}
{
const int a[] = { 3, 0, -2, 10, 0, 0, 3, -8, 0, 2 };
long long r = foo( 10, 2, 2, a );
assert( r == 0 );
printf( "%lld\n", r );
}
{
const int a[] = { 27, 42, 1, 31, -23, 19, 25, -32, 46, -3, -46, 5, -26, 45, 8, -48, -15, -20, 43, 15, 39, -50, 29, 25, -14, -1, -43, 21, 38, 32, -23, 9, 49, 9, -7, 49, 20, -19, 47, -33, 1, 18, 23, -46, 5, -28, 5, 47 };
long long r = foo( 48, 1, 7, a );
assert( r == 49 );
printf( "%lld\n", r );
}
return 0;
}