B. RPG Protagonist(思维+贪心+模拟)

52 篇文章 1 订阅
本文介绍了一种解决RPG游戏中主角和随从抢劫铁匠铺获取资源的问题。通过枚举主角携带剑的数量,利用贪心策略确定随从最优选择,从而找出能携带的最大武器总数。代码实现包括了两种不同的算法思路,旨在优化资源分配策略。
摘要由CSDN通过智能技术生成

https://codeforces.com/problemset/problem/1400/B


You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.

You decided to rob a town's blacksmith and you take a follower with you. You can carry at most pp units and your follower — at most ff units.

In the blacksmith shop, you found cntscnts swords and cntwcntw war axes. Each sword weights ss units and each war axe — ww units. You don't care what to take, since each of them will melt into one steel ingot.

What is the maximum number of weapons (both swords and war axes) you and your follower can carry out from the shop?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains two integers pp and ff (1≤p,f≤1091≤p,f≤109) — yours and your follower's capacities.

The second line of each test case contains two integers cntscnts and cntwcntw (1≤cnts,cntw≤2⋅1051≤cnts,cntw≤2⋅105) — the number of swords and war axes in the shop.

The third line of each test case contains two integers ss and ww (1≤s,w≤1091≤s,w≤109) — the weights of each sword and each war axe.

It's guaranteed that the total number of swords and the total number of war axes in all test cases don't exceed 2⋅1052⋅105.

Output

For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.


思路:枚举主人拿多少剑,剩下全拿斧头。随从在剩下剑和斧头中先贪心花费小的,因为两者最后价值相同。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e5;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL solve(LL i,LL p,LL f,LL cnts,LL cntw,LL s,LL w){///x是枚举主任拿几把剑
        LL sum=0;
        if(p>=i*s){
            sum+=i;
            p-=i*s;
            cnts-=i;///余下的剑数量
        }
        else{
            return sum;
        }
        LL num1=p/w;///主人余下全拿斧头
        if(num1>cntw){///斧头不够
            sum+=cntw;
            p-=cntw*w;
            cntw=0;
        }
        else if(num1<=cntw){///斧头够了
            sum+=num1;
            p-=num1*w;
            cntw-=num1;
        }
        ///贪心判断剑便宜还是斧头便宜
        if(s<=w){///剑便宜,随从尽量拿剑
            LL num2=f/s;
            if(num2>cnts){///余下的剑不够
                sum+=cnts;
                f-=cnts*s;
                cnts=0;
            }
            else if(num2<=cnts){///余下的剑够了
                sum+=num2;
                f-=num2*s;
                cnts-=num2;
            }
            sum+=min(cntw,(f/w));///剩下体力能拿多少斧头是多少
        }
        else if(s>w){///斧头便宜,随从尽量拿斧头
            LL num2=f/w;
            if(num2>cntw){
                sum+=cntw;
                f-=cntw*w;
                cntw=0;
            }
            else if(num2<=cntw){
                sum+=num2;
                f-=num2*w;
                cntw-=num2;
            }
            sum+=min(cnts,(f/s));
        }
    return sum;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL t;t=read();
  while(t--){
    LL p,f;p=read();f=read();
    LL cnts,cntw;cnts=read();cntw=read();
    LL s,w;s=read();w=read();
    LL ans=0;
    for(LL i=0;i<=cnts;i++){///枚举主人拿i把剑
        LL sum=solve(i,p,f,cnts,cntw,s,w);
        ans=max(ans,sum);
    }
    cout<<ans<<"\n";
  }
return 0;
}

然后我的枚举和贪心还是麻烦了。

来看一个雨雨更彻底的代码。

先贪心swap更划算的状态。然后主任和随从都尽量去拿这个划算的状态。拿了之后再枚举主人拿多少不划算的状态,剩下的就都给随从拿不划算的状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值