题解
- 最小值最大,显然二分
- 现在二分出来一个最低高度,思考一下怎么判断
- 考虑dp,设f[i][s]为当前到第i位前k个位用泥土的状态为s(是否有用)
- 那么就很显然了,每次转移前现算出前k个给当前位的贡献,设为num
- 转移就有两种一种是当前位不选,一种是当前位选
- 状态转移方程就很显然了
- ①f[i+1][j>>1]=min(f[i+1][j>>1],f[i][j]);
②f[i+1][(j>>1)|(1<<(k-1))]=min(f[i+1][(j>>1)|(1<<(k-1))],f[i][j]+c[i+1]);
代码
1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4 using namespace std;
5 int n,m,k,sum,mx,num,h[110],e[110],c[110],f[110][1<<12];
6 bool check(int x)
7 {
8 memset(f,127,sizeof(f)),f[0][0]=0;
9 for (int i=0;i<=n-1;i++)
10 for (int j=0;j<=(1<<k)-1;j++)
11 {
12 num=0;
13 for (int z=1;z<=k-1;z++) if (j&(1<<z)) num+=e[i-k+z+1];
14 if (num+h[i+1]>=x) f[i+1][j>>1]=min(f[i+1][j>>1],f[i][j]);
15 if (num+h[i+1]+e[i+1]>=x) f[i+1][(j>>1)|(1<<(k-1))]=min(f[i+1][(j>>1)|(1<<(k-1))],f[i][j]+c[i+1]);
16 }
17 for (int i=0;i<=(1<<k)-1;i++) if (f[n][i]<=m) return true;
18 return false;
19 }
20 int main()
21 {
22 freopen("cover.in","r",stdin),freopen("cover.out","w",stdout);
23 scanf("%d%d%d",&n,&m,&k);
24 for (int i=1;i<=n;i++) scanf("%d%d%d",&h[i],&e[i],&c[i]),sum+=e[i],mx=max(mx,h[i]);
25 int l=1,r=sum+mx;
26 while (l<r)
27 {
28 int mid=(l+r)>>1;
29 if (check(mid)) l=mid+1; else r=mid;
30 }
31 printf("%d",l-1);
32 }