B. Minimum Product
题目描述
给定5个数,
a
,
b
,
x
,
y
,
n
a,b,x,y,n
a,b,x,y,n,要求满足:
a
≥
x
,
b
≥
y
a≥x,b≥y
a≥x,b≥y,做
n
n
n次
a
−
1
a-1
a−1 或者
b
−
1
b-1
b−1 的操作,使得
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
a−1 或者
b
−
1
b-1
b−1 的操作后,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;
}