题意: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;
}