C. A and B and Team Training【1300 / 思维 贪心 枚举】

这篇博客探讨了两种方法来解决一个组合优化问题。方法一是采用贪心策略,优先消耗数量较多的元素;方法二是通过枚举所有可能的方案,找到最优解。代码实现分别展示了两种方法,并在C++中进行演示。文章强调了如何在无法继续执行操作时终止循环,并计算出最大执行次数。

在这里插入图片描述
https://codeforces.com/problemset/problem/519/C
方法一: 贪心,哪个当前多,用哪个

#include<bits/stdc++.h>
using namespace std;

int main(void)
{
	int n,m; cin>>n>>m;
	int cnt=0;
	while(1)
	{
		while(n>=m&&n>=2&&m) cnt++,n-=2,m--;//n多先执行n 
		while(m>=n&&m>=2&&n) cnt++,m-=2,n--;//m多先执行m 
		if(! ((m>=2&&n)||(n>=2&&m)) ) break;//无法执行了 
	}
	cout<<cnt<<endl;
	return 0;
}

方法二: 枚举,本质问的就是这两种方案各多少个。我们可以直接枚举一种方案数,再计算另一种方案数即可。所有的组合取一个max

#include<bits/stdc++.h>
using namespace std;

int main(void)
{
	int n,m; cin>>n>>m;
	int cnt=0;
	for(int i=0;i<=max(n,m);i++)
	{
		int x=i*2,y=i;
		int tempx=n-x,tempy=m-y;
		if(tempx>=0&&tempy>=0)
		{
			int len=min(tempx,tempy/2);
			cnt=max(cnt,i+len);
		}
	}
	cout<<cnt<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值