hdu 3127 矩形分割 值得思考的完全背包

Problem Description
There are many pretty girls in Wuhan University, and as we know, every girl loves pretty clothes, so do they. One day some of them got a huge rectangular cloth and they want to cut it into small rectangular pieces to make scarves. But different girls like different style, and they voted each style a price wrote down on a list. They have a machine which can cut one cloth into exactly two smaller rectangular pieces horizontally or vertically, and ask you to use this machine to cut the original huge cloth into pieces appeared in the list. Girls wish to get the highest profit from the small pieces after cutting, so you need to find out a best cutting strategy. You are free to make as many scarves of a given style as you wish, or none if desired. Of course, the girls do not require you to use all the cloth.
 

Input
The first line of input consists of an integer T, indicating the number of test cases.
The first line of each case consists of three integers N, X, Y, N indicating there are N kinds of rectangular that you can cut in and made to scarves; X, Y indicating the dimension of the original cloth. The next N lines, each line consists of two integers, xi, yi, ci, indicating the dimension and the price of the ith rectangular piece cloth you can cut in.
 

Output
Output the maximum sum of prices that you can get on a single line for each case.

Constrains
0 < T <= 20
0 <= N <= 10; 0 < X, Y <= 1000
0 < xi <= X; 0 < yi <= Y; 0 <= ci <= 1000
 
题目大意是有一个大的矩形,让你分成小的矩形,现在给出小的矩形边长和价值,问可以分到的最大价值是多少。
本题死活想不出来怎么动态规划,后来看到别人的题解微微有点明白。现在总结一下:
动态规划最重要的是状态转移方程。状态转移方程怎么找呢,应该是模拟分了一块,然后看剩下的怎么表示。推导出方程。
本题方程就不再叙述,就是看那个小矩形横着放还是竖着放,横着有两种切法,竖着也有两种切法。找出最大的。
借用大神的图片:

只是有点不明白,这题竟然和for循环顺序有关。小的布放到外层循环就wrong,放到内层循环就Ac。不知道为什么。
摘抄一段杭电大神的解释:
一开始是先循环的布块,怎么做都不对,后来终于发现,原来与顺序有关,想了想,理解如下:
因为放的方法,题目是要a machine which can cut one cloth into exactly two smaller rectangular pieces horizontally or vertically,大体意思就是,如果你要切的话,不是从里面抠出来,而是沿着边缘延伸,把它所在的那一块大的也切下来。因此,如果是先循环布块的话,是在整个布块中一块一块地切,那样的切法就是从里面抠。
而题目的切法,既然是顺着边沿,不妨把布块就放在右下角,然后扩展整个大的背景布块。小布块始终在右下角。
可是我对这种解释有不一样的看法, 我认为怎么切是状态转移方程决定的,状态转移方程那么写,就是表明咱是从边缘切算的,应该和for循环没有关系。

又找到一个大神的说法:
注意: 完全背包问题限制条件的维度j和物品编号的维度i的循环先后顺序是可以互换的. 但是此题必须先循环X和Y的维度, 然后才是物品编号的维度. 因为一般的完全背包问题的最优解对于物品的选取顺序没有要求, 可以先区任何物品. 但是此题对于原始矩形来说, 你在它的顶角边缘先切割那个小矩形是明显不同的,有可能你先切割1号矩形就得不到最优解, 但是你先切割3号矩形才能得到最优解(仔细想想是不是).
我认为这种说法是对的,有点类似于原来做的那个01背包,需要消除后效性,因为这题也有后效性。比如说当前切割一个最小的矩形,在当前是最优解。但是切另一个后还能切这个的话,而切了这个就不能切另一个。假如矩形都是d[i][j],这时切得方法不同,最优解就不同,所以这题有后效性。被切的布从最小开始,小矩形放到里面,每种情况都切一下,消除后效性。如果不这样,小矩形的for循环放到外面,假如先切1号得不到最优解,当你反过来切3号时候是依赖一号的值的,自然也得不到最优解。这种题一定要慎重考虑~~~
代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int dp[1001][1001];

int row[11];
int col[11];
int val[11];

int max(int a, int b)
{
    return a > b ? a :b;
}

int main()
{
    int t, n, x, y;
    int i, j, k;

    scanf("%d", &t);
    while (t--)
    {
        scanf("%d %d %d", &n, &x, &y);

        for (i = 1; i <= n; i++)
        {
            scanf("%d %d %d", &row[i], &col[i], &val[i]);
        }

        memset(dp, 0, sizeof(dp));


        for (j = 1; j <= x; j++)
        {
            for (k = 1; k <= y; k++)
            {
                for (i = 1; i <= n; i++)
                {
                    if (j - row[i] >= 0 && k - col[i] >= 0)
                    {
                        dp[j][k] = max(dp[j][k], max(dp[j - row[i]][k] + dp[row[i]][k - col[i]], dp[j - row[i]][col[i]] + dp[j][k - col[i]]) + val[i]);
                    }
                    if (j - col[i] >= 0 && k - row[i] >= 0)
                    {
                        dp[j][k] = max(dp[j][k], max(dp[j-col[i]][k] + dp[col[i]][k - row[i]], dp[j][k - row[i]] + dp[j - col[i]][row[i]]) + val[i]);
                    }
                }
            }
        }

        printf("%d\n", dp[x][y]);
    }
    return 0;
}

错误代码也粘贴一下,以后方便查看:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int dp[1001][1001];

int row[11];
int col[11];
int val[11];

int max(int a, int b)
{
    return a > b ? a :b;
}

int main()
{
    int t, n, x, y;
    int i, j, k;

    scanf("%d", &t);
    while (t--)
    {
        scanf("%d %d %d", &n, &x, &y);

        for (i = 1; i <= n; i++)
        {
            scanf("%d %d %d", &row[i], &col[i], &val[i]);
        }

        memset(dp, 0, sizeof(dp));

        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= x; j++)
            {
                for (k = 1; k <= y; k++)
                {
                    if (j - row[i] >= 0 && k - col[i] >= 0)
                    {
                        dp[j][k] = max(dp[j][k], max(dp[j - row[i]][k] + dp[row[i]][k - col[i]], dp[j - row[i]][col[i]] + dp[j][k - col[i]]) + val[i]);
                    }
                    if (j - col[i] >= 0 && k - row[i] >= 0)
                    {
                        dp[j][k] = max(dp[j][k], max(dp[j-col[i]][k] + dp[col[i]][k - row[i]], dp[j][k - row[i]] + dp[j - col[i]][row[i]]) + val[i]);
                    }
                }
            }
        }

        printf("%d\n", dp[x][y]);
    }
    return 0;
}






在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字前面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值