zoukankan      html  css  js  c++  java
  • 积木(DP)问题

    问题:Do you remember our children time? When we are children, we are interesting in almost everything around ourselves. A little thing or a simple game will brings us lots of happy time! LLL is a nostalgic boy, now he grows up. In the dead of night, he often misses something, including a simple game which brings him much happy when he was child. Here are the game rules: There lies many blocks on the ground, little LLL wants build "Skyscraper" using these blocks. There are three kinds of blocks signed by an integer d. We describe each block's shape is Cuboid using four integers ai, bi, ci, di. ai, bi are two edges of the block one of them is length the other is width. ci is
    thickness of the block. We know that the ci must be vertical with earth ground. di describe the kind of the block. When di = 0 the block's length and width must be more or equal to the block's length and width which lies under the block. When di = 1 the block's length and width must be more or equal to the block's length which lies under the block and width and the block's area must be more than the block's area which lies under the block. When di = 2 the block length and width must be more than the block's length and width which lies under the block. Here are some blocks. Can you know what's the highest "Skyscraper" can be build using these blocks?
     

    Input
    The input has many test cases.
    For each test case the first line is a integer n ( 0< n <= 1000) , the number of blocks.
    From the second to the n+1'th lines , each line describing the i‐1'th block's a,b,c,d (1 =< ai,bi,ci <= 10^8 , d = 0 or 1 or 2).
    The input end with n = 0.
     

    Output
    Output a line contains a integer describing the highest "Skyscraper"'s height using the n blocks.
     

    Sample Input
    3
    10 10 12 0
    10 10 12 1
    10 10 11 2
    2
    10 10 11 1
    10 10 11 1
    0

     
    Sample Output
    24
    11

    回答:题意大概是给你些积木,分三类,0:只能放在小于等于它的长和宽的积木上,1:只能放在长和宽小于等于它且面积小于他的木块上,2:只能放在长和宽偶小于它的木块上。求积木的最高高度。

    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    struct block
    {
        int x,y,z,d;
    }b[1005];
    long long dp[1005];
    int n;
    bool cmp(block a,block b)
    {
        if(a.x!=b.x)return a.x<b.x;
        if(a.y!=b.y)return a.y<b.y;
        return a.d>b.d;
    }
    void DP()
    {
        long long ans=b[0].z;
        for(int i=0;i<n;i++)
        {
            dp[i]=b[i].z;
            ans=max(ans,dp[i]);
        }
        for(int i=1;i<n;i++)
        {
            if(b[i].d==0)
            {
                for(int j=0;j<i;j++)
                {
                    if(b[j].x<=b[i].x&&b[j].y<=b[i].y)
                        dp[i]=max(dp[i],dp[j]+b[i].z);
                }
            }
            if(b[i].d==1)
            {
                for(int j=0;j<i;j++)
                {
                    if(b[j].x<=b[i].x&&b[i].y>=b[j].y&&(b[i].y*b[j].y||b[i].x>b[j].x))
                        dp[i]=max(dp[i],dp[j]+b[i].z);
                }
            }
            if(b[i].d==2)
            {
                for(int j=0;j<i;j++)
                {
                    if(b[i].x>b[j].x&&b[i].y>b[j].y)
                    {
                        dp[i]=max(dp[i],dp[j]+b[i].z);
                    }
                }
            }
            ans=max(ans,dp[i]);  
        }
        cout<<ans<<' ';
    }

    int main()
    {
        while(1)
        {
            scanf("%d",&n);
            if(n==0)break;
            for(int i=0;i<n;i++)
            {
                scanf("%d%d%d%d",&b[i].x,&b[i].y,&b[i].z,&b[i].d);
                if(b[i].x<b[i].y)
                    swap(b[i].x,b[i].y);
            }
            sort(b,b+n,cmp);
            DP();
        }
        return 0;
    }

  • 相关阅读:
    【Azure Redis Cache】对StackExchange.Redis IOCP错误消息的解读
    【Azure Developer】使用REST API获取Activity Logs、传入Data Lake的数据格式问题
    【Azure 存储服务】Blob中数据通过Stream Analytics导出到SQL/Cosmos DB
    【Azure Redis 缓存】Linux VM使用6380端口(SSL方式)连接Azure Redis (redis-cli & stunnel)
    【Azure 应用服务】在Azure App Service for Windows 中部署Java/NodeJS/Python项目时,web.config的配置模板内容
    【Azure Service Bus】 Service Bus如何确保消息发送成功,发送端是否有Ack机制 
    领域驱动实践总结(基本理论总结与分析+架构分析与代码设计+具体应用设计分析V)
    Java三元表达式中的陷阱
    Java有陷阱——慎用入参做返回值
    Eclipse中安装反编译工具Fernflower(Enhanced Class Decompiler)
  • 原文地址:https://www.cnblogs.com/benchao/p/4606309.html
Copyright © 2011-2022 走看看