洛谷P3112 [USACO14DEC]后卫马克Guard Mark
1 #include <bits/stdc++.h> 2 #define For(i,j,k) for(int i=j;i<=k;i++) 3 #define LL long long 4 using namespace std ; 5 6 const int N = 21 ; 7 int n,H,res ; 8 LL sum ; 9 int dp[1100000],bin[21] ; 10 struct cow{ 11 int height,weight,pow ; 12 }a[N]; 13 14 inline int read() 15 { 16 int x = 0 , f = 1 ; 17 char ch = getchar() ; 18 while(ch<'0'||ch>'9') { if(ch=='-') f = -1 ; ch = getchar() ; } 19 while(ch>='0'&&ch<='9') { x = x * 10+ch-48 ; ch = getchar() ; } 20 return x * f ; 21 } 22 23 int main() 24 { 25 n = read() ; H = read() ; 26 res = -1 ; 27 For(i,1,n) { 28 a[i].height = read() ; a[i].weight = read() ; a[i].pow = read() ; 29 } 30 memset(dp,-1,sizeof dp) ; 31 bin[0] = 1 ; 32 For(i,1,n) bin[i]=bin[i-1]*2 , dp[bin[i-1]] = a[i].pow ; 33 For(i,1,bin[n]-1) { 34 For(j,1,n) 35 if(!(i & bin[j-1])) 36 dp[i | bin[j-1] ] = max(dp[i | bin[j-1] ],min(dp[i]-a[j].weight,a[j].pow) ) ; 37 sum = 0 ; 38 For(j,1,n) 39 if(i & bin[j-1] ) sum=sum+ a[j].height ; 40 if(sum>=H) 41 if(res<dp[i]) 42 res = dp[ i ] ; 43 } 44 if(res>=0) printf("%d ",res) ; 45 else printf("Mark is too tall") ; 46 return 0 ; 47 }