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

B. Minimum Product

题目链接入口 Click here~~
在这里插入图片描述

题目描述
给定5个数, a , b , x , y , n a,b,x,y,n a,b,x,y,n,要求满足: a ≥ x , b ≥ y a≥x,b≥y axby,做 n n n a − 1 a-1 a1 或者 b − 1 b-1 b1 的操作,使得 a × b a×b a×b 的结果最小。
输入
第一行输入案例个数
第二行一次输入 a , b , x , y , n a,b,x,y,n a,b,x,y,n
输出
输出在n次 a − 1 a-1 a1 或者 b − 1 b-1 b1 的操作后,a×b 最小的结果。
案例
输入案例

7
10 10 8 5 3
12 8 8 7 2
12343 43 4543 39 123212
1000000000 1000000000 1 1 1
1000000000 1000000000 1 1 1000000000
10 11 2 1 5
10 11 9 1 10

输出案例

70
77
177177
999999999000000000
999999999
55
10

题解:
根据数学知识,两个数的和保持不变,两个数之差越大,则两个数乘积越小,因为a必须匹配x,b必须匹配y,所以只需要写一个比较函数cmp( ),一次先用a- -,如果a==x,且执行次数不够n,再执行b- -,一次先b再a,再比较两次的结果,取小的即可。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>

#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define int ll
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int read()
{
	int w = 1, s = 0;
	char ch = getchar();
	while (ch < '0' || ch>'9') { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0';    ch = getchar(); }
	return s * w;
}
//------------------------ 以上是我常用模板与刷题几乎无关 ------------------------//
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()
{
	int t = read();
	while (t--)
	{
		int a, b, x, y, n;
		scanf("%lld%lld%lld%lld%lld", &a, &b, &x, &y, &n);
		int res1 = cmp(a, b, x, y, n);
		int res2 = cmp(b, a, y, x, n);
		if (res1 > res2)
			printf("%lld\n", res2);
		else
			printf("%lld\n", res1);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值