一.题目链接:
HDU-3244
二.题目大意:
有 n 种原料,每种原料有 6 个属性.
x:每个人需要此原料的量
y:已有的量
s1:小包原料的数量
p1:小包原料的价钱
s2:大包原料的数量
p2:大包原料的价钱
现有 m 元,求最多的人数.
三.分析:
二分人数,check(mid) 函数里面对每种原料完全背包.
其中背包容量应设为 mid * x - y + s2 (+ s2 是因为可能买完之后不正好,但价格更低)
还有要各种控制边界,一不小心就会 TLE.
详见代码.
四.代码实现:
#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long int
using namespace std;
const int M = (int)1e2;
const ll mod = (ll)1e9 + 7;
const int inf = 0x3f3f3f3f;
struct node
{
int x, y;
int s1, p1;
int s2, p2;
}s[M + 5];
int dp[(int)6e6 + 5];
int n, m;
int cal_c(int k, int need)
{
int w[2] = {s[k].s1, s[k].s2};
int v[2] = {s[k].p1, s[k].p2};
dp[0] = 0;
int V = need + s[k].s2;
for(int i = 1; i <= V; ++i)///这里只能赋初值到 V,不然就T了~
dp[i] = inf;
for(int i = 0; i < 2; ++i)
{
for(int j = w[i]; j <= V; ++j)
dp[j] = min(dp[j], dp[j - w[i]] + v[i]);
}
int ans = inf;
for(int i = need; i <= V; ++i)
ans = min(ans, dp[i]);
return ans;
}
bool check(int mid)
{
int c = 0;
int num;
for(int i = 1; i <= n; ++i)
{
num = mid * s[i].x - s[i].y;
if(num <= 0)
continue;
c += cal_c(i, num);
if(c > m)
return 0;
}
return 1;
}
int main()
{
while(~scanf("%d %d", &n, &m) && (n + m))
{
for(int i = 1; i <= n; ++i)
scanf("%d %d %d %d %d %d", &s[i].x, &s[i].y, &s[i].s1, &s[i].p1, &s[i].s2, &s[i].p2);
int l = 0;
int r = (int)1e5;
while(l < r)
{
int mid = (l + r + 1) >> 1;
if(check(mid))
l = mid;
else
r = mid - 1;
}
printf("%d\n", r);
}
return 0;
}