CF-Educational Codeforces Round 93 (Rated for Div. 2)-1398D. Colored Rectangles【DP】

题目链接
题意:给定三种颜色的棍子,它们分别有R、G、B对,同时给出每种颜色每一对的长度,现在可以选择两种不同颜色的棍子组成矩形,问你用以上木棒能够组成的矩形面积之和的最大值。
思路:
d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k]表示当前每种颜色选的个数
当然为了使结果最大,三个数组都要升序排序后从后面遍历
d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k]的上一个状态有三种情况
d p [ i + 1 ] [ j + 1 ] [ k ] dp[i+1][j+1][k] dp[i+1][j+1][k]//选RG
d p [ i + 1 ] [ j ] [ k + 1 ] dp[i+1][j][k+1] dp[i+1][j][k+1]//选RB
d p [ i ] [ j + 1 ] [ k + 1 ] dp[i][j+1][k+1] dp[i][j+1][k+1]//选GB
上述状态取 m a x max max即可
AC代码:

#include 

#define ll long long
#define inf 1000000000
#define mod 1000000007
#define N 100005
#define rep(i, a, b) for(int i=a;i<=b;i++)
#define per(i, a, b) for(int i=a;i>=b;i--)
using namespace std;
int T = 1;
ll dp[205][205][205];
int main() {
    //cin >> T;
    while (T--) {
        ll R,G,B;
        cin>>R>>G>>B;
        vector<ll> r(R+3,0),g(G+3,0),b(B+3,0);
        rep(i,1,R) cin>>r[i];
        rep(i,1,G) cin>>g[i];
        rep(i,1,B) cin>>b[i];
        sort(r.begin()+1,r.begin()+R+1);
        sort(g.begin()+1,g.begin()+G+1);
        sort(b.begin()+1,b.begin()+B+1);
        ll ans=0;
        per(i,R,0)per(j,G,0)per(k,B,0){
            dp[i][j][k]=max(dp[i+1][j+1][k]+r[i+1]*g[j+1],
                            max(dp[i+1][j][k+1]+r[i+1]*b[k+1],
                                max(dp[i][j+1][k+1]+g[j+1]*b[k+1],0ll)));
            ans=max(ans,dp[i][j][k]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

你可能感兴趣的:(codeforces,算法,动态规划)