如何最大化两个数组的总和 [英] How do I maximize the sum of two arrays

查看:57
本文介绍了如何最大化两个数组的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最大化两个数组的乘积之和

您将获得两个大小为N的整数数组A和B.

让我们定义数组A和B的交互对于从1到N的每个i,A [i] * B [i]的总和是



你想要最大化
数组。您可以获得最多K(可能为零)

以下类型的操作。

在一次操作中,您可以增加或减少任何
$ b数组A或数组B的元素$ b乘以1.

找出阵列交互的最大值

你可以获得。

输入

第一行包含两个空格分隔的整数N. k

第二行包含N个空格分隔的整数

表示数组A.

第三行包含N个空格分隔的整数

表示数组B.

输出

输出一个整数,表示

问题的答案。

约束

1≤N≤10^ 5< br /> 
-10 ^5≤| A [i] |,| B [i] | ≤10^ 5< br />
0≤K≤10^ 4



样本输入0

 2 2 
1 1
-1 -1



样本输出0

 0 



解释0

通常的乘法我们得到 1 *( - 1)+ 1 *( -1)= - 2
最大化数组B后的
b [0] = 0 b [1] = 0

然后总和 1 * 0 + 1 * 0 = 0

样本输出1

 2 2 
-1 2
0 2



样本输出 8

解释1

 a [] = { -  1,2}< br /> 
b [] = {0,2}



最大化 k = 2

b [1] = 4

然后 -1 * 0 + 2 * 4 = 8



我的尝试:



只是暴力技术但是给我错误的答案

解决方案

引用:

只是暴力技术是的,但是给了我错误的答案

可以猜到你在某处犯了错误。但没有代码...



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候您接近一个错误。



建议:拿一张纸并尝试手工完成,您的程序应该使用相同的程序。


maximizing the sum of product of two array
You are given two integer arrays A and B each of size N .
Let us define interaction of arrays A and B to be the sum
of A[i] * B[i] for each i from 1 to N .
You want to maximize the value of interaction of the
arrays. You are allowed to make at most K (possibly zero)
operations of following kind.
In a single operation, you can increase or decrease any
of the elements of array A or array B by 1.
Find out the maximum value of interaction of the arrays
that you can get.
Input
First line contains two space separated integers N . k
Second line contains N space separated integers
denoting array A .
Third line contains N space separated integers
denoting array B .
Output
output a single integer denoting the answer of the
problem.
Constraints

1 ≤ N ≤ 10^5<br />
-10^5 ≤ |A[i]|, |B[i]| ≤ 10^5<br />
0 ≤ K ≤ 10^4


sample Input 0

2 2
1 1
-1 -1


sample Output 0

0


explanation 0
on usual multiplication we get 1*(-1)+1*(-1)=-2
after maximizing array B b[0]=0 b[1]=0
then the sum is 1*0+1*0= 0
sample output 1

2 2
-1 2
0 2


sample Output 8
explationation 1

a[]={-1,2}<br />
b[]={0,2}


on maximizing for k=2
b[1]=4
then -1*0+2*4=8

What I have tried:

just the bruteforce technique but is giving me wrong answer

解决方案

Quote:

just the bruteforce technique but is giving me wrong answer

Just can guess that you have a mistake somewhere. but without code ...

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.


这篇关于如何最大化两个数组的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆