7-5 Hand-made Cream

Suppose you are a baker planning to bake some hand-made cream breads.

To bake a cream bread, we need to use one slice of bread and one kind of cream. Each hand-made cream bread has a taste score to describe how delicious it is, which is obtained by multiplying the taste score for bread and the taste score for cream. (The taste scores could be negative, howerver, two negative tast scores can still produce delicious cream bread.)

The bread and cream are stored separately.

The constraint is that you need to examine the breads in a given order, and for each piece of bread, you have to decide immediately (without looking at the remaining breads) whether you would like to take it.

After you are finished with breads, you will take the same amount of cream in the same manner. The breads and creams you take will be paired in the same order as you take them.

Given N taste scores for bread and M taste scores for cream, you are supposed to calculate the maximum total taste scores for all hand-made cream bread.

Input Specification:

Each input file contains one test case. For each case, the first line contains two integers N and M (1≤N,M≤10^3), which are the numbers of bread and cream, respectively.

The second line gives N taste scores for bread.

The third line gives M taste scores for cream.

The taste scores are integers in [−10^3,10^3].

All the numbers in a line are separated by a space.

Output Specification:

Print in a line the maximum total taste score.

Sample Input:

3 4
-1 10 8
10 8 11 9

Sample Output:

188

Hint:

The maximum total taste score for the sample case is 10×10+8×11=188.

 题目大意:

假设你是一个面包师,打算烤一些手工制作的奶油面包。

要烤奶油面包,我们需要用一片面包和一种奶油。
每一个手工制作的奶油面包都有一个味道分数来描述它的美味程度,它是由面包的味道分数和奶油的味道分数相乘得到的。
(味道得分可以是负的,然而,两个负的味道得分仍然可以生产出美味的奶油面包。)

面包和奶油分开存放。

限制是,你需要按照给定的顺序检查面包,对于每一片面包,你必须立即决定(不需要看剩下的面包)你是否想要它。

用完面包后,用同样的方法加入等量的奶油。你拿的面包和面霜会按照你拿的顺序来搭配。

给定面包的N分和奶油的M分,你应该计算出所有手工奶油面包的最大总味道分数。

输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含两个整数N和M(1≤N,M≤10^3)
,分别是面包和奶油的数量。

第二行给出了面包的N个味道得分。

第三条线给出了奶油的M分。

口味评分为[−10^3,10^3]中的整数

一行中的所有数字都用空格隔开。

输出规范:
在一行中打印最大总味道分数。

这个题坑点感觉蛮多,试了好多种方法都不能拿满分,最后索性摆烂:

#include <bits/stdc++.h>
using namespace std;
int N, M;
int bread[1001], cream[1001];

int calcMaxSumOfProd(int arr1[], int arr2[], int n) {
	int maxSum = 0;
	sort(arr1, arr1 + n, greater<int>());
	sort(arr2, arr2 + n, greater<int>());
	for (int i = 0; i < n; i++)
		maxSum += (arr1[i] * arr2[i]);
	return maxSum;
}

int main() {
	cin >> N >> M;
	for (int i = 0; i < N; i++) {
		cin >> bread[i];
	}
	for (int j = 0; j < M; j++) {
		cin >> cream[j];
	}
	int n = sizeof(bread) / sizeof(bread[0]);
	cout << calcMaxSumOfProd(bread, cream, n);
	return 0;
}

大家有做过这道题吗,有没有大神能贡献更完美的题解呢,若有的话欢迎分享,谢谢啦~ 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值