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;
}

动态规划

时间: 2024-10-11 11:28:18

Fruit Feast(暴力)(动态规划)的相关文章

bzoj4393: [Usaco2015 Dec]Fruit Feast

题意: T,A,B.T是上限.A和B可以随意吃但是不能超过T.有一次将吃的东西/2的机会.然后可以继续吃,不能超过T.问最多可以吃多少. =>我们先处理不能/2可以吃到哪些.然后弄个双指针扫一扫就可以了TAT #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> using namespace std; #define rep(i,s,t) for(int i=s

Fruit Feast

Fruit Feast 题目描述 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 maxi

BZOJ 4393 Fruit Feast

沉迷刷水无法自拔. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 5005000 using namespace std; int t,a,b; bool vis[maxn]; int main() { scanf("%d%d%d",&t,&a,&b); vis[0]=true; for

bzoj 4393 Usaco Fruit Feast

题目很明显的一道dp题目. 方程也不难推 dp[i][0]表示饱食度为i没喝水  dp[i][1]表示当前饱食度为i已经喝了水 #include <cstdio> #include <algorithm> using namespace std; int t,a,b,Max; bool dp[5000055][2]; int main(){ scanf("%d%d%d",&t,&a,&b); dp[0][0]=1; for(int i=0

区间Dp 暴力枚举+动态规划 Hdu1081

F - 最大子矩形 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit Status Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located withi

Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力)

Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算术序列: 1, 3, 5, 7, 9 7, 7, 7, 7 3, -1, -5, -9 但是这一个就不是: 1, 1, 2, 5, 7 求给定数组,能有多少个算术序列 测试样例 Input: [1, 2, 3, 4] Output: 3 有三个算术序列切片: [1,2,3], [2,3,4], [1

Codeforces 372B. Counting Rectangles is Fun【动态规划,暴力枚举】(lowbit()小用法)

题目大意: 给出一个由0,1构成的矩阵,询问(a,b)到(c,d)两个点之间的只含有0的矩形有多少个. 方法: 由于矩阵不大,最多40*40,而且询问量很大(10^5)由此我们考虑o(1)输出答案,首先用一个四维数组预处理出答案,最后直接输出即可. 令dp[a][b][c][d]为(a,b)到(c,d)两个点之间的只含有0的矩形的数量, 则递推的公式: dp[a][b][c][d]=dp[a][b][c][d-1]+dp[a][b][c-1][d]-dp[a][b][c-1][d-1] 每次计算

算法整理之动态规划

我现在介绍的这个版本,是从算法爱好者中看到的一个别人的漫画版本.题目:有一座高度是10级台阶的楼梯,从下往上走,每跨一步只能向上1级或者2级台阶.要求用程序来求出一共有多少种走法.比如,每次走1级台阶,一共走10步,这是其中一种走法.我们可以简写成 1,1,1,1,1,1,1,1,1,1.再比如,每次走2级台阶,一共走5步,这是另一种走法.我们可以简写成 2,2,2,2,2. 分析:这种问题是典型的使用动态规划解决的问题,在使用动态规划的方法之前,能想到的方法可能就是使用排列组合,这是一个非常复

动态规划刷题记录1(不定期更新~)

Dp刷(chao)题记录&题(fu)解(zhi) 1.bzoj1055 [HAOI2008]玩具取名 题目大意:字典中有四个字母,’w’\’i’\’n’\’g’,给出每个字母的转换关系,即某个单个字母可以转换为两个字母.给出一个文本,求最初的可能文本(刚开始有且仅有一个字母). 题解:明显是一道区间dp嘛~.设状态为文本[i,j]内的字母可以转化为字母[k],即f(i,j,k),要解状态的可能性.转移思路,自然是枚举i到j内的断点,再枚举关系.那么初始的状态转移方程就是 f[i][j][k]=f