codeforces C. Om Nom and Candies

Meaning of the questions: to two types of sugar, sugar by weight, respectively: Happy value Wr, Wb, sugar respectively: Hr, Hb. Seeking eat no more than the weight of C sugar, the greatest joy is how much value.

Ideas: If Hr / Wr> Hb / Rb, assuming Wr * Wb <= C, if taking a r a candy Wb, spent weight Wr * Wb, be happy value Wb * Hr. If we take a b Wr candy, spent the same weight, the value to be happy Wr * Hb <Wb * Hr, so that the number need not exceed the candy enumeration b Wr, when Wr <sqrt (C), is clearly smaller than the enumeration sqrt (C ) the number can be. When Wr> sqrt (C), the number of cases is to enumerate all C / Wr <sqrt (C), so only need to enumerate smaller than sqrt (C) times. So overall complexity sqrt (C) can solve the problem.

#include <bits/stdc++.h>
using namespace std;
#define maxn 10010
typedef long long ll;
 
int main()
{
    ll c,hr,hb,wr,wb;
    cin >> c >> hr >> hb >> wr >> wb;
    ll ans = 0;
    ll nums = c / wr;
    for(int i = 1;i <= 100000;i++){
        if(nums < 0)break;
        ans = max(ans, nums * hr + ((c - nums * wr) / wb) * hb);
        nums--;
    }
    nums = (c / wb);
    for(int i = 1;i <= 100000;i++){
        if(nums < 0)break;
        ans = max(ans, nums * hb + ((c - nums * wb) / wr) * hr);
        nums--;
    }
    cout<<ans<<"\n";
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/cleanerhgf/p/11951827.html