地址:
点击打开链接
emmmm,需要总结一波了,看到这个题第一眼的确有思路,但是码代码的时候还是犹豫了好一会。就是最佳状态的递推。
dy【这次状态】 = dy【上一状态】+data【跳到这一状态的增加值】
感觉和前面几篇加起来已经快把动态规划的简单问题都涵盖了吧。
这个是动态规划严格递增序列的最大和,与之前求最长之类的不同。因此也mark一下。总结整理一下。
#include <iostream>
#include <iomanip>
#include<queue>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<iomanip>
#include<string.h>
#include<sstream>
#include<string>
//¶¨Ò庯Êý¶Î
#define repf(i,a,b) for(int i =(a);i<(b);i++)
#define repfe(i,a,b) for(int i =(a);i<=(b);i++)
using namespace std;
struct Rec{
public :
int width;
int Length;
int height;
}recs[91];
int dp[91] ;
bool cmp(Rec a,Rec b)
{
if(a.Length== b.Length)
{
return a.width>b.width;
}
return a.Length>b.Length;
}
int main() {
int num,a,b,c,index ,times = 1;
while(cin>>num&&num!=0)
{
index = 0 ;
while(num--)
{
cin >> a>> b >>c ;
recs[index].height = a;
recs[index].Length = max(b,c);
recs[index].width = min(b,c);
index++;
recs[index].height = c;
recs[index].Length = max(a,b);
recs[index].width = min(a,b);
index++;
recs[index].height = b;
recs[index].Length = max(a,c);
recs[index].width = min(a,c);
index++;
}
sort(recs,recs+index,cmp);
memset(dp,0,sizeof(dp));
int res = 0;
for(int i = 0 ; i < index ; i ++)
{
dp[i]=recs[i].height;
for(int j = 0 ; j < i; j ++)
{
if(recs[j].Length>recs[i].Length&&recs[j].width>recs[i].width)
{
dp[i] = max(dp[i],recs[i].height+dp[j]);
}
}
res= max(res,dp[i]);
}
cout <<"Case "<< times++<<": maximum height = "<<res<<endl;
}
return 0;
}