題目描述
Farmer John and his herd are playing frisbee. Bessie throws the
frisbee down the field, but it's going straight to Mark the field hand
on the other team! Mark has height H (1 <= H <= 1,000,000,000), but
there are N cows on Bessie's team gathered around Mark (2 <= N <= 20).
They can only catch the frisbee if they can stack up to be at least as
high as Mark. Each of the N cows has a height, weight, and strength.
A cow's strength indicates the maximum amount of total weight of the
cows that can be stacked above her.
Given these constraints, Bessie wants to know if it is possible for
her team to build a tall enough stack to catch the frisbee, and if so,
what is the maximum safety factor of such a stack. The safety factor
of a stack is the amount of weight that can be added to the top of the
stack without exceeding any cow's strength.
FJ將飛盤拋向身高爲H(1 <= H <= 1,000,000,000)的Mark,但是Mark
被N(2 <= N <= 20)頭牛包圍。牛們可以疊成一個牛塔,如果疊好後的高度大於或者等於Mark的高度,那牛們將搶到飛盤。
每頭牛都一個身高,體重和耐力值三個指標。耐力指的是一頭牛最大能承受的疊在他上方的牛的重量和。請計算牛們是否能夠搶到飛盤。若是可以,請計算牛塔的最大穩定強度,穩定強度是指,在每頭牛的耐力都可以承受的前提下,還能夠在牛塔最上方添加的最大重量。
輸入輸出格式
輸入格式:INPUT: (file guard.in)
The first line of input contains N and H.
The next N lines of input each describe a cow, giving its height,
weight, and strength. All are positive integers at most 1 billion.
輸出格式:OUTPUT: (file guard.out)
If Bessie's team can build a stack tall enough to catch the frisbee, please output the maximum achievable safety factor for such a stack.
Otherwise output "Mark is too tall" (without the quotes).
輸入輸出樣例
4 10 9 4 1 3 3 5 5 5 10 4 4 5
2
題解
由於數據較小,所以想到狀壓dp,在當前狀態加一頭牛看最優解。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll H,dp[1<<21],f[1<<21];//f[i]表示i這種狀態的最大穩定強度 dp[i]表示最大高度
ll h[30],w[30],s[30],ans=-1;
int main(){
int n;
scanf("%d%lld",&n,&H);
for(int i=0;i<n;i++){
scanf("%lld%lld%lld",&h[i],&w[i],&s[i]);
}
memset(f,-1,sizeof(f));
f[0]=2147483647;
int end=(1<<n)-1;
for(int i=0;i<=end;i++){
for(int j=0;j<n;j++){
if(f[i]>=w[j]){
if((i>>j)&1)
continue;
int k=i^(1<<j);
f[k]=max(f[k],min(s[j],f[i]-w[j]));
dp[k]=dp[i]+h[j];
if(dp[k]>=H){
ans=max(ans,f[k]);
}
}
}
}
if(ans<0) printf("Mark is too tall\n");
else printf("%lld\n",ans);
system("pause");
return 0;
}