URAL 1200 - Horns and Hoofs(暴力+剪枝)

比赛时水过的,现在贴一下正规做法。


#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值