zoukankan      html  css  js  c++  java
  • 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;
    }
    动态规划
  • 相关阅读:
    大数据Hadoop第二周——配置新的节点DataNode及ip地址
    vue环境搭建详细步骤
    苹果电脑Mac系统如何下载安装谷歌Chrome浏览器
    点云的基本特征和描述
    ModuleNotFoundError: No module named 'rospkg'
    ROS的多传感器时间同步机制Time Synchronizer
    Spring Cloud 2020 版本重大变革,更好的命名方式!
    Spring MVC 接收请求参数所有方式总结!
    阿里为什么不用 Zookeeper 做服务发现?
    微服务之间最佳调用方式是什么?
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5760362.html
Copyright © 2011-2022 走看看