DP训练第二日(2019.4.13)
以下各题来自[kuangbin带你飞]专题十二 基础DP1
前言
隔的时间确实有点长的,中间还因为没保存丢了几次,以后要注意了。
C HDU 1069 Monkey and Banana
题意
给定一堆箱子(个数不限)的长宽高,规定箱子只能堆在长宽严格大于他的箱子的上面,问最多能堆多高
做法
我们很容易可以得到,同一种箱子我们最多用3次,因此,我们可以建立一个结构体数组,将六种不同的长宽组合存在里面(之所以不是三种是因为这样就可以不不用去判断长宽高的大小关系了),然后按照长、宽、排序,然后就是背包了。
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
struct node
{
int l,w,h;
}po[185];
int dp[185];
bool cmp(node a,node b)
{
if(a.l==b.l) return a.w<b.w;
return a.l<b.l;
}
int main()
{
int n,cnt=0;
int t=1;
while(scanf("%d",&n)!=EOF&&n)
{
cnt=0;
for(int i=0;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
po[cnt].l = a;po[cnt].w = b;po[cnt++].h = c;
po[cnt].l = b;po[cnt].w = a;po[cnt++].h = c;
po[cnt].l = a;po[cnt].w = c;po[cnt++].h = b;
po[cnt].l = c;po[cnt].w = a;po[cnt++].h = b;
po[cnt].l = c;po[cnt].w = b;po[cnt++].h = a;
po[cnt].l = b;po[cnt].w = c;po[cnt++].h = a;
}
sort(po,po+cnt,cmp);
dp[0] = po[0].h;
for(int i=1;i<cnt;i++)
{
dp[i] = po[i].h;
for(int j=0;j<i;j++)
{
if(po[j].l<po[i].l&&po[j].w<po[i].w)
{
dp[i] = max(dp[i],dp[j]+po[i].h);
}
}
}
int maxn = 0;
for(int i=0;i<cnt;i++)
{
//printf("%d ",dp[i]);
maxn = max(maxn,dp[i]);
}//printf("\n");
printf("Case %d: maximum height = %d\n",t++,maxn);
}
}
D HDU 1074 Doing Homework(状压DP)
题意
给定一些作业的完成所需时间和截止时间,超出截止时间提交的作业会有相应的扣分,问怎样可以使得扣分最少。
做法
声明:在写这个题之前,我并没有很多地接触到状压DP的思想,这个题也是在学长的帮助下和别人的代码的基础上进行理解的,在这里仅仅作为一种记录,侵删。
考虑到n的大小,我们利用一个不超过16位的数来表示完成的情况,每一位表示一个作业,对于只有一位不同的数而言,他们存在着一种先后关系,例如100和101,我们可以利用这样的一个关系进行状态转移,同时记录下先后的顺序。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=(1<<15)+10;
const int INF = 0x7f7f7f7f;
int n;
string name[16];
int dl[16];
int cost[16];
int d[N],f[N],op[N];
int dp(int u)
{
if(d[u]!=-1) return d[u];
d[u] = INF;
int vis[16],t=0,temp = u;
for(int i=n-1;i>=0;i--)
{
vis[i] = temp%2;
temp/=2;
if(vis[i]) t+=cost[i];
}
for(int i=n-1;i>=0;i--)
{
if(vis[i])
{
int v = dp(u-(1<<(n-i-1)));
if(t>dl[i]) v+=t-dl[i];
if(v<d[u])
{
d[u] = v;
f[u] = u - (1<<(n-i-1));
op[u] = i;
}
}
}
return d[u];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
cin>>name[i];
scanf("%d%d",&dl[i],&cost[i]);
}
memset(d,-1,sizeof(d));
d[0] = 0;
printf("%d\n",dp((1<<n)-1));
stack <int> s;
int u = (1<<n) -1;
while(u)
{
s.push(op[u]);
u = f[u];
}
while(!s.empty())
{
cout<<name[s.top()]<<endl;
s.pop();
}
}
}
E HDU 1087 Super Jumping! Jumping! Jumping!
做法
简单的最长上升子序列,不再赘述
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
int a[1005];
int dp[1005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n)
{
memset(dp,0,sizeof(dp));
for(int i =1;i<=n;i++)
{
scanf("%d",&a[i]);
dp[i] = a[i]; // WAµã
}
int ans = -1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
if(a[j]<a[i])
{
dp[i] = max(dp[j]+a[i],dp[i]);
}
}
ans = max(ans,dp[i]);
}
printf("%d\n",ans);
}
}
F HDU 1114 Piggy-Bank
题意
存钱罐里放金币,放满的情况下最小的价值
做法
完全背包(滚动数组优化)
代码
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
int dp[100005];
int cost[505];
int weight[505];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int e,f;
scanf("%d%d",&e,&f);
int w = f-e;
int n;
memset(dp,0x7f7f7f7f,sizeof(dp));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&cost[i],&weight[i]);
}
dp[0] = 0;
for(int i=0;i<n;i++)
{
for(int j=1;j<=w;j++)
{
if(j>=weight[i])
{
dp[j] = min(dp[j-weight[i]]+cost[i],dp[j]);
//printf("%d %d %d %d %d %d\n",dp[i][0],weight[i],j,dp[i][j-weight[i]],cost[i],dp[i][j]);
}
}
}
if(dp[w]==0x7f7f7f7f)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[w]);
}
}
结语
明天继续,咳咳