D. Colored Rectangles

本文探讨了一个涉及动态规划(DP)的问题,即如何通过不同类型的木棒组合成矩形,以求得所有矩形面积之和的最大值。文章详细介绍了状态转移方程的构建,并分享了一段实现这一算法的C++代码,强调了数组排序的重要性。

传送门

分析

个人感觉这道题比C简单(一个DP选手最后的尊严)

题目大意是说有三种不同的木棒,第一种木棒有a对,第二种b对,第三种c对,每次取两对不同的木棒组成一个矩形,问最后组成的若干个矩形的面积之和最大是多少

首先我们假设已经选了i个第一种,j个第二种,k个第三种,那么我们可以列成三个状态转移方程

//取第一个和第三个
dp[i + 1][j][k + 1] = max(dp[i + 1][j][k + 1],dp[i][j][k] + 1ll * x[i + 1] * z[k + 1]);
//取第二个和第三个
dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1],dp[i][j][k] + 1ll * y[j + 1] * z[k + 1]);
//取第一个和第二个
dp[i + 1][j + 1][k] = max(dp[i + 1][j + 1][k],dp[i][j][k] + 1ll * x[i + 1] * y[j + 1]);

最后需要注意一下要让最大的数字相互组合,所以一开始的时候需要将数组sort一下

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
// #pragma GCC option("arch=native","tune=native","no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int T;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 210;
int a[N];
int r,g,b;
int x[N],y[N],z[N];
ll dp[N][N][N];

bool cmp(int x,int y){
    return x > y;
}

int main(){
    scanf("%d%d%d",&r,&g,&b);
    for(int i = 1;i <= r;i++) scanf("%d",&x[i]);
    for(int i = 1;i <= g;i++) scanf("%d",&y[i]);
    for(int i = 1;i <= b;i++) scanf("%d",&z[i]);
    sort(x + 1,x + 1 + r,cmp);
    sort(y + 1,y + 1 + g,cmp);
    sort(z + 1,z + 1 + b,cmp);
    ll ans = 0;
    for(int i = 0;i <= r;i++)
        for(int j = 0;j <= g;j++){
            // dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0],dp[i][j][k] + 1ll * x[i + 1] * y[k + 1]);
            for(int k = 0;k <= b;k++){
                dp[i + 1][j][k + 1] = max(dp[i + 1][j][k + 1],dp[i][j][k] + 1ll * x[i + 1] * z[k + 1]);
                dp[i][j + 1][k + 1] = max(dp[i][j + 1][k + 1],dp[i][j][k] + 1ll * y[j + 1] * z[k + 1]);
                dp[i + 1][j + 1][k] = max(dp[i + 1][j + 1][k],dp[i][j][k] + 1ll * x[i + 1] * y[j + 1]);
                ans = max(dp[i + 1][j][k + 1],ans);
                ans = max(dp[i][j + 1][k + 1],ans);
                ans = max(dp[i + 1][j + 1][k],ans);
            }
        }
    printf("%lld",ans);
}



* This example program shows how to find pads in an image and how to * determine their position, rotation, and size robustly and accurately using * fit_rectangle2_contour_xld. dev_update_pc ('off') dev_update_window ('off') dev_update_var ('off') read_image (Image, 'die_pads') dev_close_window () get_image_size (Image, Width, Height) dev_open_window (0, 0, Width * 2, Height * 2, 'black', WindowHandle) dev_set_part (0, 0, Height - 1, Width - 1) * Find the pads in the image using blob analysis. fast_threshold (Image, Region, 180, 255, 20) connection (Region, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, ['area', 'anisometry'], 'and', [200, 1], [1200, 2]) * Construct a ROI for subpixel-accurate edge detection. fill_up (SelectedRegions, RegionFillUp) shape_trans (RegionFillUp, RegionTrans, 'convex') boundary (RegionTrans, RegionBorder, 'inner') dilation_circle (RegionBorder, RegionDilation, 2.5) union1 (RegionDilation, RegionUnion) * Perform the subpixel-accurate edge detection. reduce_domain (Image, RegionUnion, ImageReduced) edges_sub_pix (ImageReduced, Edges, 'sobel_fast', 0.5, 20, 40) * Select the edge fragments that belong to the pads. select_shape_xld (Edges, SelectedContours, 'contlength', 'and', 10, 200) * Merge adjacent edge fragments to obtain one contour per pad. union_adjacent_contours_xld (SelectedContours, UnionContours, 2, 1, 'attr_keep') * Fit rectangles robustly to the pads' edges. fit_rectangle2_contour_xld (UnionContours, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder) * Generate rectangles from the fitting result for visualization purposes. gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2) dev_display (Image) dev_set_colored (12) dev_display (Rectangle)解释一下这个代码
最新发布
09-08
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值