ACM模版

描述

题解

简单说,就是暴力枚举,可是也不是毫无技巧可言,一开始我直接先求出价值最大的糖果的最大食用量,然后开始递减,但是由于数据范围太大,卒~~~

最后,只能从两头枚举,将红糖果从 0 枚举到 sqrt(C) + 1,蓝糖果也是如此,依次求 res,更新 ans,至于为何,则是因为最优解中,较少的糖果数目不会超过 sqrt(C) + 1。

代码

#include <iostream>
#include <cmath>

using namespace std;

typedef long long ll;

struct candy
{
    int w;
    int h;
    float v;
};

int main(int argc, const char * argv[])
{
// freopen("/Users/zyj/Desktop/input.txt", "r", stdin);
// freopen("/Users/zyj/Desktop/output.txt", "w", stdout);

    int C;
    candy R, B;
    cin >> C >> R.h >> B.h >> R.w >> B.w;

    ll ans = 0, res;
    ll temp = sqrt(C) + 1;

    for (ll i = 0; i < temp; i++)
    {
        if (i * R.w <= C)
        {
            res = i * R.h + (C - i * R.w) / B.w * B.h;
            if (res > ans)
            {
                ans = res;
            }
        }
        if (i * B.w <= C)
        {
            res = i * B.h + (C - i * B.w) / R.w * R.h;
            if (res > ans)
            {
                ans = res;
            }
        }
    }

    cout << ans << '\n';

    return 0;
}