51Nod1548 ohm Noam and candy

Problem

One day, 欧姆诺诺姆 came to the friend's house, he found a lot of candy. There are blue and red. He knew every single red candy gram weight Wr, every single blue candy gram weight Wb. Eating a candy blue Hb values ​​will bring him joy of eating a red candy will bring him joy of the value of Hr.

Ohm Nome only eat up to C grams of candy, and candy every one can not eat half. Now he wants to get the maximum value by the joy of eating blue and red candies.

Sample explain: each type of candy you can eat two.

Solution

I am a past water directly Select the highest unit value, and then enumerate down.

Demonstrate insight title report.

Code

#include<stdio.h>
#include<set>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<string.h>
#define mem(ss) memset(ss,0,sizeof(ss))
#define rep(d, s, t) for(int d=s;d<=t;d++)
#define rev(d, s, t) for(int d=s;d>=t;d--)
typedef long long ll;
typedef long double ld;
typedef double db;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
ll w[10],h[10];
ll c;
int main() {
    io_opt;
    cin>>c>>h[1]>>h[2]>>w[1]>>w[2];
    db x1=(db)h[1]/w[1],x2=(db)h[2]/w[2];
    if(x1<x2){
        swap(h[1],h[2]);
        swap(w[1],w[2]);
    }
    ll x=c/w[1],y=(c-x*w[1])/w[2];
    ll ans=x*h[1]+y*h[2];
    for(int i=1;i<=100000&&x-i>=0;i++){
        ll xx=x-i,yy=(c-xx*w[1])/w[2];
        ans=max(ans,xx*h[1]+yy*h[2]);
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/sz-wcc/p/11704756.html