比赛时水过的,现在贴一下正规做法。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <map>
#include <algorithm>
#define LL long long
#define LLU unsigned long long
#define INF 0x7fffffff
using namespace std;
int main()
{
double ans = -1e30, temp, a, b;
int ansk, anskk, k;
scanf("%lf%lf%d", &a, &b, &k);
for(int x = 0; x <= k; ++x)
{
int y = b/2;
if(x+y>k) y = k-x;
if(y<0) y = 0;
temp = a*x+b*y-x*x-y*y;
if(temp>ans) { ans = temp; ansk = x; anskk = y; }
}
printf("%.2lf\n%d %d\n", ans, ansk, anskk);
return 0;
}
比较数学化的另一种做法
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <map>
#include <algorithm>
#define LL long long
#define LLU unsigned long long
#define INF 0x7fffffff
#define eps 1e-9
using namespace std;
double calcu(double a, double b, double x, double y)
{
return a*x + b*y - x*x - y*y;
}
int main()
{
double a, b;
int k, x, y;
scanf("%lf%lf%d", &a, &b, &k);
int ta = 0, tb = 0;
if(fmod(a/2,1)>0.5) ta = int(a/2+0.5);
else ta = int(a/2);
if(fmod(b/2,1)>0.5) tb = int(b/2+0.5);
else tb = int(b/2);
if(a<=0&&b<=0) { x = 0; y = 0; }
else if(a>0&&b<=0) { x = min(k,ta); y = 0; }
else if(a<=0&&b>0) { x = 0; y = min(k,tb); }
else if(a>0&&b>0)
{
if(ta+tb<=k) { x = ta; y = tb; }
else
{
double temp, max = -1e30;
int xx, yy;
for( x = 0; x <= k; ++x)
{
y = min(tb, k-x);
temp = calcu(a,b,x,y);
if(temp>max) { max = temp; xx = x; yy = y; }
}
x = xx; y = yy;
}
}
printf("%.2lf\n%d %d\n", calcu(a,b,x,y), x, y);
return 0;
}