hdu5887Herbs Gathering

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5887

题意:n个物品的01背包,n<=100,其他数据为1e9以内。

分析:比赛的时候看到这题没想法,以为要什么奇淫技巧才能过。虽然看到了出题人说的是随机的数据但是TM加个最优解优化就能将2^n优化到15ms也是没脾气。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=60010;
const int M=50010;
const int mod=1000000007;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double eps=1e-5;
const double inf=~0u>>1;
const double pi=acos(-1.0);
typedef long double db;
typedef unsigned int uint;
typedef unsigned long long ull;
struct node {
    ll x,y;
    bool operator < (const node b) const {
        return y*b.x<x*b.y;
    }
}a[110];
ll m,ans;
void dfs(int n,ll val,ll sum) {
    ans=max(ans,val);
    if (n==0) return ;
    if ((m-sum)*a[n].y*1.0/a[n].x+val<1.0*ans) return ;
    if (sum+a[n].x<=m) dfs(n-1,val+a[n].y,sum+a[n].x);
    dfs(n-1,val,sum);
}
int main()
{
    int i,n;
    while (scanf("%d%I64d", &n, &m)!=EOF) {
        ans=0;
        for (i=1;i<=n;i++) scanf("%I64d%I64d", &a[i].x, &a[i].y);
        sort(a+1,a+n+1);
        dfs(n,0ll,0ll);
        printf("%I64d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值