Codeforces Round #667 (Div. 3) B. Minimum Product

本文探讨了如何通过调整两个数来最大化它们之间的差值,提供了详细的算法思路和C++实现代码,适用于需要解决此类数学优化问题的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

思路:

要确保两个数之间的差值最大,那么我们可以从一个一直减小然后再从另一个开始,讨论两种情况谁的收益最大即可。

代码:

#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define ll long long
const int N=2e5+5;
const int M=2e4+5;
const double eps=1e-8;
const int mod=1e9+7;
const int inf=0x7fffffff;
const double pi=3.1415926;
using namespace std;
int cmp(int a,int b,int x,int y,int n)
{
	int cnt=0;
	if((a-x)+(b-y)<n)
    {
        return x*y;
    }
	if(b-y<=n)
	{
		cnt+=b-y;
		b=y;
	}
	else
	{
		b=b-n;
		return a*b;
	}
	if (a-x>=n-cnt)
	{
		a=a-(n-cnt);
		return a*b;
	}
}
signed main()
{
    IOS;
	int t;
	cin>>t;
	while (t--)
	{
		int a,b,x,y,n;
		cin>>a>>b>>x>>y>>n;
		int res1=cmp(a,b,x,y,n),res2=cmp(b,a,y,x,n);
		if (res1>res2)
        {
            cout<<res2<<endl;
        }
		else
        {
            cout<<res1<<endl;
        }
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值