URAL 1200 Horns and Hoofs 枚举

时间:2020-11-30 16:45:29

设horns和hoofs的数量分别为 x 和 y ,题目要求:

满足 x+y <= K,使得A*x + B*y - x*x - y*y 最大。

枚举 i 从0~K,直接解方程得对称轴 x = ( 2*i + A - B ) / 4,判断对称轴是否在 [ 0, i ] 区间内。

注意:

1.精度

2.x需要上下个取整一次

3.如果最大值都<=0,那么最大收益直接为 0 即可。

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm> const double eps = 1e-;
const double INF = 1e10; using namespace std; double A, B;
int N; int dcmp( double a )
{
if ( fabs(a) < eps ) return ;
return a < ? - : ;
} double cal( int x, int y )
{
return A*x + B*y -x*x - y*y;
} int main()
{
while ( ~scanf( "%lf%lf", &A, &B ) )
{
scanf( "%d", &N );
double ans = -INF;
int ansX, ansY; for ( int i = ; i <= N; ++i )
{
int tmp = floor(( * i + A - B ) / 4.0) ;
if ( tmp < ) tmp = ;
else if ( tmp > i ) tmp = i; double tmpcal = cal( tmp, i - tmp );
if ( dcmp( ans - tmpcal ) < )
{
ans = tmpcal;
ansX = tmp;
ansY = i - tmp;
}
else if ( dcmp( ans - tmpcal ) == )
{
if ( tmp < ansX )
{
ansX = tmp;
ansY = i - tmp;
}
else if ( tmp == ansX )
{
if ( ansY > i - tmp )
{
ansY = i - tmp;
}
}
} tmp = ceil( ( * i + A - B ) / 4.0 ) ; if ( tmp < ) tmp = ;
else if ( tmp > i ) tmp = i; tmpcal = cal( tmp, i - tmp );
if ( dcmp( ans - tmpcal ) < )
{
ans = tmpcal;
ansX = tmp;
ansY = i - ansX;
}
else if ( dcmp( ans - tmpcal ) == )
{
if ( tmp < ansX )
{
ansX = tmp;
ansY = i - tmp;
}
else if ( tmp == ansX )
{
if ( ansY > i - tmp )
{
ansY = i - tmp;
}
}
}
} if ( dcmp(ans) <= || ( ansX <= && ansY <= ) )
{
puts("0.00");
puts("0 0");
continue;
} printf( "%.2f\n", ans );
printf( "%d %d\n", ansX, ansY );
}
return ;
}