题目连接: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;
}