[洛谷P3112][USACO14DEC]后卫马克Guard Mark:贪心+搜索

分析:

发现对于任何一种选牛方案,wei+cap越大的牛在越下面越优,所以可以先对所有牛根据wei+cap降序排序,然后dfs枚举方案,check()函数检查是否可行,还是很简单的(。・∀・)ノ゙。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
int n;LL h,ans;
bool flag;
struct Cow{
    LL hei,wei,cap;
}c[25];
vector<Cow> v;
bool cmp(Cow x,Cow y){
    return x.wei+x.cap>y.wei+y.cap;
}
bool check(){
    LL mincap=2e18,tot=0;
    for(int i=0;i<v.size();i++){
        if(v[i].wei>mincap) return 0;
        mincap=min(mincap-v[i].wei,v[i].cap);
        tot+=v[i].hei;
    }
    if(tot<h) return 0;
    ans=max(ans,mincap);
    return 1;
}
void dfs(int pos){
    if(pos==n+1){
        if(check()) flag=1;
        return;
    }
    dfs(pos+1);
    v.push_back(c[pos]);
    dfs(pos+1);
    v.pop_back();
}
int main(){
    scanf("%d%lld",&n,&h);
    for(int i=1;i<=n;i++){
        scanf("%lld%lld%lld",&c[i].hei,&c[i].wei,&c[i].cap);
    }
    sort(c+1,c+n+1,cmp);
    dfs(1);
    if(!flag){
        printf("Mark is too tall\n");
        return 0;
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ErkkiErkko/p/9388057.html