Educational Codeforces Round 93 (Rated for Div. 2) D. Colored Rectangles

一上来看完题目,这很贪心,马上写了发贪心,wa7!
hack样例,当面临选择的数有两个相同,然后选择了某一个,可能后序的操作,得到的值不如选另一个数的价值大。

1 2 2
100 
90 90
90 30

1 2 2 
100
90 30
90 90 

所以正解该是dp,记录了每个状态的最优解。
很容易想到状态 d p [ i ] [ j ] [ z ] dp[i][j][z] dp[i][j][z] 表示分别选择了 i , j , z i,j,z i,j,z 个棍子的最大价值。
然后转移的话可以从 d p [ i − 1 ] [ j − 1 ] [ z ] , d p [ i − 1 ] [ j ] [ z − 1 ] , d p [ i ] [ j − 1 ] [ z − 1 ] dp[i-1][j-1][z],dp[i-1][j][z-1],dp[i][j-1][z-1] dp[i1][j1][z],dp[i1][j][z1]dp[i][j1][z1] 三个状态转移过来。
转移方程:

dp[i][j][z] = max(dp[i][j][z],dp[i-1][j-1][z]+a[i]*b[j]);
dp[i][j][z] = max(dp[i][j][z],dp[i-1][j][z-1]+a[i]*c[z]);
dp[i][j][z] = max(dp[i][j][z],dp[i][j-1][z-1]+b[j]*c[z]);

对每组给定的数从大到小排个序,然后选择。

参考代码

#pragma GCC optimize(2)
#include
using namespace std;
const int man = 2e5+10;
#define IOS ios::sync_with_stdio(0)
#define ull unsigned ll
#define uint unsigned
#define pai pair
#define pal pair
#define IT iterator
#define pb push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);++i)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);--i)
#define endl '\n'
#define ll long long
const ll mod = 1e9+7;
int a[man],b[man],c[man];
ll dp[205][205][205];

signed main() {
	#ifndef ONLINE_JUDGE
		//freopen("in.txt", "r", stdin);
		//freopen("out.txt","w",stdout);
	#endif
	int n,m,k;cin >>n>>m>>k;
	for(int i = 1;i <= n;++i)cin >> a[i];
	for(int i = 1;i <= m;++i)cin >> b[i];
	for(int i = 1;i <= k;++i)cin >> c[i];
	sort(a+1,a+1+n,[](const int a,const int b){return a>b;});
	sort(b+1,b+1+m,[](const int a,const int b){return a>b;});
	sort(c+1,c+1+k,[](const int a,const int b){return a>b;});
	ll ans = 0;
	for(int i = 0;i <= n;++i){
		for(int j = 0;j <= m;++j){
			for(int z = 0;z <= k;++z){
				if(i>=1&&j>=1)dp[i][j][z] = max(dp[i][j][z],dp[i-1][j-1][z]+a[i]*b[j]);
				if(i>=1&&z>=1)dp[i][j][z] = max(dp[i][j][z],dp[i-1][j][z-1]+a[i]*c[z]);
				if(j>=1&&z>=1)dp[i][j][z] = max(dp[i][j][z],dp[i][j-1][z-1]+b[j]*c[z]);
				ans = max(ans,dp[i][j][z]);
			}
		}
	}
	cout<<ans<<endl;	
	return 0;
}

/*
1 2 2
100 
90 90
90 30

1 2 2 
100
90 30
90 90 
*/

你可能感兴趣的:(codeforce,dp,思维)