且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何最大化两个数组的总和

更新时间:2023-02-10 10:53:54

引用:

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

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



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



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

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

掌握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.