Fruit Feast(暴力)(动态规划)

Fruit Feast

时间限制: 1 Sec  内存限制: 64 MB
提交: 64  解决: 18
[提交][状态][讨论版]

题目描述

Bessie has broken into Farmer John's house again! She has discovered a pile of lemons and a pile of oranges in the kitchen (effectively an unlimited number of each), and she is determined to eat as much as possible.

Bessie has a maximum fullness of T(1≤T≤5,000,000). Eating an orange increases her fullness by A, and eating a lemon increases her fullness by B (1≤A,B≤T). Additionally, if she wants, Bessie can drink water at most one time, which will instantly decrease her fullness by half (and will round down).

Help Bessie determine the maximum fullness she can achieve!

输入

The first (and only) line has three integers T, A, and B.

输出

 A single integer, representing the maximum fullness Bessie can achieve.

样例输入

8 5 6

样例输出

8
【分析】在此提供两种做法,一种暴力,一种动态规划。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=5000005;
const int M=15005;
ll sum=1;
int n,m;
int vis[N];
int maxn=0;
struct man
{
  int num,use;
};
int main()
{
    int a,b;
    memset(vis,0,sizeof(vis));
    scanf("%d%d%d",&n,&a,&b);
    queue<man>q;
    man A;A.num=a;A.use=0;
    man B;B.num=b;B.use=0;
    q.push(A);q.push(B);
    vis[a]=1;vis[b]=1;
    while(!q.empty()){
        man t=q.front();
        q.pop();
        if(t.num+a>n)maxn=max(maxn,t.num);
        if(t.num+a<=n&&vis[t.num+a]==0){
            vis[t.num+a]=1;
            man k;k.num=t.num+a;k.use=t.use;
            q.push(k);
        }
        if(t.num+b>n)maxn=max(maxn,t.num);
        if(t.num+b<=n&&vis[t.num+b]==0){
            vis[t.num+b]=1;
                man k;k.num=t.num+b;k.use=t.use;
                q.push(k);
        }
        if(t.use==0&&vis[t.num/2]==0){
            vis[t.num/2]=1;
        man k;k.num=t.num/2;k.use=1;
            q.push(k);
        }
    }
    printf("%d\n",maxn);
    return 0;
}
暴力

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=5000005;
const int M=15005;
ll sum=1;
int n,m;
int vis[N];
int maxn=0;
int dp[N];
int main()
{
    int a,b;
    scanf("%d%d%d",&n,&a,&b);
    dp[0]=1;
    for(int i=a;i<=n;i++)dp[i]|=dp[i-a];
    for(int i=b;i<=n;i++)dp[i]|=dp[i-b];
    for(int i=0;i<=n;i++)dp[i/2]|=dp[i];
    for(int i=a;i<=n;i++)dp[i]|=dp[i-a];
    for(int i=b;i<=n;i++)dp[i]|=dp[i-b];
    for(a =n;dp[a]==0;a--);
    cout<<a<<endl;
    return 0;
}
动态规划

 

转载于:https://www.cnblogs.com/jianrenfang/p/5760362.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值