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
",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