Temple Build Gym - 101656J

题目连接:https://vjudge.net/problem/Gym-101656J

题意: 给一个方台容器的 高 上底 下底,然后是3个正方形容器的边长,问最多可以放置的正方形的体积是多少,每种可以无限用

思路: 在一个容器里面放东西,问最多可以放多少,有点类似于背包问题。这里让我们放的东西是不限制的,因此,可以得到的dp公式是: dp(i) = MAX(dp[i], dp[ i - V_h[i]])  ( 1<= i <= h) 我们需要保存最大的值,因为不一定可以放满。

还有一个要注意的就是公式的应用了 一定要乘1.0才可以,因为上底减下底一定是个负数,不乘1.0的话,会损失精度 这个scanf要比cin快100ms左右

之前一直觉得跟0 1背包有什么相似之处,但是仔细想想,背包是逆序的,这个不需要考虑逆序,因为可以用到每个时刻的值

#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>

using namespace std;
typedef long long LL;
const int maxn = 1e6 + 10;

LL ans, height, bottom, top;
LL dp[maxn], shape[5];

LL cnt(LL h, LL r) {
    return  (bottom + h * (top - bottom) * 1.0 / height) / r; //如果不乘1.0 减法运算会损失精度
}

int main() {
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    while(~scanf("%lld%lld%lld",&height,&bottom,&top)) {
        for(int i = 0; i < 3; i++) scanf("%lld",&shape[i]);
        memset(dp, -1, sizeof(dp));
        dp[0] = 0, ans = 0;
        for(LL i = 1; i <= height; i++) {
            for(int j = 0; j < 3; j++) {
                if(shape[j] > i || dp[i - shape[j]] == -1) continue;
                LL c = cnt(i,shape[j]);
                dp[i] = max(dp[i],dp[i - shape[j]] + c * c * shape[j] * shape[j] * shape[j]);
            }
            ans = max(dp[i], ans);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值