题目链接:B. RPG Protagonist
题意
有两个人A能拿p单位的原料,B能拿f单位的原料。有两种武器剑和斧头,一把剑需要s个原料,储备间只能做cnts把剑;一把斧头需要w个原料,储备间只能做cntw把斧头。问A和B最多拿多少把武器。
题解
其实这个用到了高中数学的方法——列不等式组。
首先很容易想到哪个花费的材料最少就先制作这个武器。
我们设A制作了s1把剑和w1把斧头,B制作了s2把剑和w2把斧头。
根据题意可以列出不等式方程组。
{ s ∗ s 1 + w ∗ w 1 ≤ p , s ∗ s 2 + w ∗ w 2 ≤ f , s 1 + s 2 ≤ c n t s , w 1 + w 2 ≤ c n t w , { \begin{cases} s*s_1+w*w_1≤p, \\ s*s_2+w*w_2≤f, \\ s_1+s_2≤cnts,\\ w_1+w_2≤cntw,\end{cases}} ⎩⎪⎪⎪⎨⎪⎪⎪⎧s∗s1+w∗w1≤p,s∗s2+w∗w2≤f,s1+s2≤cnts,w1+w2≤cntw,
我们取s≥w
根据贪心,我们可以肯定先用花费少的w,所以枚举 w 1 {w_1} w1,继而得出其它的 s 1 , w 2 , s 2 {s_1,w_2,s_2} s1,w2,s2,取答案最小值即可。
{ s 1 = m i n ( c n t s , ( p − w ∗ w 1 ) / s ) , w 2 = m i n ( c n t w − w 1 , f / w ) , s 2 = m i n ( c n t s − s 2 , ( f − w ∗ w 2 ) / s ) , { \begin{cases} s_1=min(cnts,(p-w*w_1)/s), \\ w_2=min(cntw-w_1,f/w), \\ s_2=min(cnts-s_2,(f-w*w_2)/s),\end{cases}} ⎩⎪⎨⎪⎧s1=min(cnts,(p−w∗w1)/s),w2=min(cntw−w1,f/w),s2=min(cnts−s2,(f−w∗w2)/s),
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x
const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int main()
{
int t;
cin >> t;
while(t--)
{
ll p,f,cnts,cntw,s,w;
cin >> p >> f >> cnts >> cntw >> s >> w;
if(s<w) swap(s, w),swap(cnts, cntw);
ll ans=0;
for(ll w1=0;w1<=cntw && w1*w<=p;w1++)
{
ll s1=min(cnts,(p-w*w1)/s);
ll w2=min(cntw-w1,f/w);
ll s2=min(cnts-s1,(f-w*w2)/s);
ans=max(ans,w1+w2+s1+s2);
}
cout << ans << endl;
}
}