Educational Codeforces Round 93 (Rated for Div. 2) D. Colored Rectangles ###K ###K ###K //K
题目链接:https://codeforces.ml/contest/1398/problem/D
题意:给3种集合 每种集合有 n对棍子 每次取任意两个集合的一对棍子构成矩形, 矩形的面积为贡献值, 问如何使得贡献值最大
思路: 不能确定的贪心 并且看到数据范围是允许dp 的 但是 直接dp选和不选是不对,因为有些数是没法组合的
所以先排序最大到小 使得当前顺序必定是最优中的集合后 然后再dp 即可
不合法状态直接置负无穷然后一直用ans 记录最大的dp[i][j][k] 即可
因为最终的dp[r][g][b] 不一定是有效状态,但最优解一定出现在所有的状态之中
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=2e2+10; 4 const int mod=1e9+7; 5 #define ll long long 6 #define pi pair<int,int> 7 #define fi first 8 #define sc second 9 #define pb push_back 10 11 int dp[maxn][maxn][maxn]; 12 int r[maxn],g[maxn],b[maxn]; 13 int R,G,B; 14 15 16 int main() 17 { 18 ios::sync_with_stdio(0); 19 cin.tie(0); 20 cin>>R>>G>>B; 21 for(int i=1;i<=R;i++) cin>>r[i]; 22 for(int i=1;i<=G;i++) cin>>g[i]; 23 for(int i=1;i<=B;i++) cin>>b[i]; 24 sort(r+1,r+1+R,greater<int>()); 25 sort(g+1,g+1+G,greater<int>()); 26 sort(b+1,b+1+B,greater<int>()); 27 memset(dp,-0x3f,sizeof(dp)); 28 dp[0][0][0]=0; 29 int ans=0; 30 for(int i=0;i<=R;i++) 31 { 32 for(int j=0;j<=G;j++) 33 { 34 for(int k=0;k<=B;k++) 35 { 36 int &v=dp[i][j][k]; 37 if(i&&j) v=dp[i-1][j-1][k]+r[i]*g[j]; 38 if(i&&k) v=max(v,dp[i-1][j][k-1]+r[i]*b[k]); 39 if(j&&k) v=max(v,dp[i][j-1][k-1]+g[j]*b[k]); 40 ans=max(ans,v); 41 } 42 } 43 } 44 cout<<ans<<'\n'; 45 46 47 48 49 50 }

浙公网安备 33010602011771号