0
点赞
收藏
分享

微信扫一扫

B. RPG Protagonist(贪心+思维) Educational Codeforces Round 94 (Rated for Div. 2)

原题链接: ​​https://codeforces.com/contest/1400/problem/B​​

B. RPG Protagonist(贪心+思维)  Educational Codeforces Round 94 (Rated for Div. 2)_思维
测试样例:

input
3
33 27
6 10
5 6
100 200
10 10
5 5
1 19
1 3
19 5
output
11
20

Note:

In the first test case:
you should take 3 swords and 3 war axes: 3⋅5+3⋅6=33≤33
and your follower — 3 swords and 2 war axes: 3⋅5+2⋅6=27≤27.
3+3+3+2=11 weapons in total.

In the second test case, you can take all available weapons even without your follower’s help, since 5⋅10+5⋅10≤100.

In the third test case, you can’t take anything, but your follower can take 3 war axes: 3⋅5≤19.

题意: 你和你的随从都有各自的最大携带空间。先店铺中有若干数量的剑和战斧,它们都有着各自的空间占量。问你们最多能带走多少把武器?

解题思路: 这道题显然是利用贪心算法实现,可我们似乎不知道该怎么贪,我们只知道要让我们的背包利用率达到最大,好像也知道要得到最优策略先把体积小的装完 。在这种情况下,我们就可以枚举所有情况来贪心解决。即你装多少的体积小的,剩下的留给你的随从。这即是所有的情况。(因为我们一旦确定了一个东西的数量,另一个随着最大体积的填充和优先选择顺序已经确定。) 所以这道题我们先要预处理使得我们要处理的元素始终最小(即进行值交换。),然后我们枚举情况并利用空间最大的利用思想来实现我们能够带走的最多武器,之后再比对找出最优解,这即是答案。具体看代码。

AC代码:

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,p,f,cntw,cnts,w,s;
void solve(){
if(w>s){
swap(w,s),swap(cntw,cnts);
}
int ans=0,sum,v,temp1,temp2,cnt1,cnt2;
//我们始终认为w是体积小的。
rep(i,0,cntw){
//开始枚举
sum=0;temp1=p,temp2=f,cnt1=cntw,cnt2=cnts;
v=min(i,temp1/w);
sum+=v,cnt1-=v;
temp1-=v*w;
v=min(cnt2,temp1/s);
sum+=v,cnt2-=v;
v=min(cnt1,temp2/w);
sum+=v,temp2-=v*w;
v=min(cnt2,temp2/s);
sum+=v;
ans=max(sum,ans);
}
cout<<ans<<endl;
}
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>p>>f;
cin>>cnts>>cntw>>s>>w;
solve();
}
}
return 0;
}


举报

相关推荐

0 条评论