这题主要在于不能一次性的求出贪心结果,必须遍历第一个角色然后求出第二个角色的贪心结果。
时间复杂度为O(n)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl
ll ans;
int main(){
int t;
//freopen("input.txt","r",stdin);
scanf("%d",&t);
while(t--){
ll p,f,cnts,cntw,s,w;
ans = 0;
scanf("%lld%lld%lld%lld%lld%lld",&p,&f,&cnts,&cntw,&s,&w);
if(p < f)swap(p,f);
if(s > w){
swap(s,w);
swap(cnts,cntw);
}
for(int i = 0;i <= cnts;i++){
if(i * s > p)break;
ll tcnts = cnts,tcntw = cntw,tp = p,tf = f;
ll consum_w = min(tcntw,(tp - i * s) / w);
ll t_ans = i + consum_w;
tcnts -= i;
tcntw -= consum_w;
ll f_consum_s = min(tcnts,tf / s);
ll f_consum_w = min(tcntw,(tf - f_consum_s * s) / w);
t_ans += f_consum_s + f_consum_w;
ans = max(ans,t_ans);
}
cout<<ans<<endl;
}
return 0;
}