第三场-J-Temple Build

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xbb224007/article/details/79957118

题目链接:点击打开链接

(一)题面:

Description

The Dwarves of Middle Earth are renowned for their delving and smithy ability, but they are also master builders. During the time of the dragons, the dwarves found that above ground the buildings that were most resistant to attack were truncated square pyramids (a square pyramid that does not go all the way up to a point, but instead has a flat square on top). The dwarves knew what the ideal building shape should be based on the height they wanted and the size of the square base at the top and bottom. They typically had three different sizes of cubic bricks with which to work. Their goal was to maximize the volume of such a building based on the following rules:

The building is constructed of layers; each layer is a single square of bricks of a single size. No part of any brick may extend out from the ideal shape, either to the sides or at the top. The resulting structure will have jagged sides and may be shorter than the ideal shape, but it must fit completely within the ideal design. The picture at the right is a vertical cross section of one such tower. There is no limit on how many bricks of each type can be used.

Input

Each line of input will contain six entries, each separated by a single space. The entries represent the ideal temple height, the size of the square base at the bottom, the size of the square base at the top (all three as non-negative integers less than or equal to one million), then three sizes of cubic bricks (all three as non-negative integers less than or equal to ten thousand). Input is terminated upon reaching end of file.

Output

For each line of input, output the maximum possible volume based on the given rules, one output per line.

Sample Input

500000 800000 300000 6931 11315 5000

Sample Output

160293750000000000

(二)题目大意:

给定一个正四棱台,已知其下底边长、上底边长及高。再给出三个正方体,已知其边长。

求:正四棱台中最多能放的给定正方体的体积总和。


(三)解题思路:

  1. 静下来一看不就是求一定空间内放物体的最大体积吗,就比较容易想到背包问题。
  2. 我们设dp[i]表示高度为i时候的最优解;
    则有dp[i]=max(dp[i],dp[i-r[i]]+w),w表示高度为i-r[i]的时候可以放的方块的体积,如下图;

    当我们求高度为i的正四棱台的时候,求出对应高度的宽度width,那么我们就可以求出目标区域能放的正方体的总体积w,再由dp[i-r[i]]转移过来取一个最大值即可。
  3. 我们不一定可以恰好满足最高高度,高度最高的时候解也不一定最优,即dp[heigth]可能不存在,也不一定最优,故我们应该取各个高度的最优值。

(四)具体代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=1e6+10;
LL down,up,heigth,ans=0,r[3];
LL dp[maxn];
LL width(LL h,LL r){return (up+(down-up)*(heigth-h)*1.0/heigth)/r;}
int main(){
    freopen("in.txt","r",stdin);
    while(~scanf("%lld%lld%lld%lld%lld%lld",&heigth,&down,&up,&r[0],&r[1],&r[2])){
        memset(dp,-1,sizeof dp);
        dp[0]=0;ans=0;
        for(LL i=1;i<=heigth;i++){
            for(LL j=0;j<3;j++){
                if(i<r[j]||dp[i-r[j]]==-1)continue;
                LL w=width(i,r[j]);
                dp[i]=max(dp[i],dp[i-r[j]]+w*w*r[j]*r[j]*r[j]);
            }
            ans=max(ans,dp[i]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

(五)题后总结:
这个题不知道当时是怎么想的,想成了:求高度为h的四棱台时需要从高度为1~h-1的解转移过来...。后来看到过题人数比较少就没怎么想写了。现在想想当时可能还是可以做的。

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页