题意就是3个架子,一堆书,放入后,要求每层宽度最大值乘以总高度最小。(每层必须要放书)
此题就属于提前规定一些方向简化状态的典型,比如把书的高度从高到低排序,这样的话,如果你要放的这层已经放书了,你要放的这本一定比这层放的矮,这样就不用纠结高度的问题了,dp[i][j][k]代表第二层宽度为j,第三层宽度为k的总高度。事实上这题完全可以用二维来做,为何?因为递推的方向都是单向的,比如第二层放书了,要更新的是dp[i][j+x][k],比如第三层放书了,要更新的是dp[i][j][k+x],所以只要第二维和第三维的递推都是从大到小就可以了。也不需要什么滚动数组了。
AC代码:
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 201000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
int dp[2105][2105],sum[75];
struct node
{
int h,w;
bool operator < (const node& t) const
{
return h>t.h;
}
};
node book[75];
void update(int& a, int b)
{
if(a < 0 || a > b)
a = b;
}
int main()
{
#ifdef GLQ
freopen("input.txt","r",stdin);
// freopen("o3.txt","w",stdout);
#endif
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%d%d",&book[i].h,&book[i].w);
sort(book,book+n);
memset(dp,-1,sizeof(dp));
dp[0][0] = book[0].h;
sum[0] = book[0].w;
for(int i = 1; i < n; i++)
sum[i] = sum[i-1]+book[i].w;
for(int i = 1; i < n; i++)
{
for(int j = sum[n-1]; j >= 0; j--)
{
if(j == 0)
{
// update(dp[0][0], dp[0][0]);
update(dp[book[i].w][0], dp[0][0]+book[i].h);
continue;
}
// int lim = sum-book[0].w-j > j+30 ? j+30 : sum-book[0].w-j;
int lim = sum[i-1]-book[0].w-j;
for(int k = lim; k >= 0; k--) if(dp[j][k] > 0)
{
// if(sum-k < 2*j-30) break;
int ho = j==0 ? book[i].h : 0, ha = k==0 ? book[i].h : 0;
// update(dp[j][0], dp[j][0]);
update(dp[j+book[i].w][k], dp[j][k]+ho);
update(dp[j][k+book[i].w], dp[j][k]+ha);
}
}
}
int ans = -1;
for(int i = 1; i < n*30; i++)
for(int j = 1; j < n*30; j++) if(dp[i][j] > 0)
{
int maxw = max(i,max(j,sum[n-1]-i-j));
// cout<<sum-i-j<<" "<<i<<" "<<j<<" "<<dp[0][i][j]<<" "<<maxw*dp[0][i][j]<<endl;
update(ans, maxw*dp[i][j]);
}
printf("%d\n",ans);
}
return 0;
}