如何在C或C ++或java中解决这个问题,请尽快回答这个问题 [英] How to do this problem in C or C++ or java, please answer this question as soon as possible
问题描述
任务是找到两个相同大小的数组的最小产品总和,因为在第一个数组上允许k个修改。在每次修改中,第一个数组的一个数组元素可以增加或减少2.
注意 - 乘积和是求和(A [i] * B [i])对于从1到n的所有i,其中n是两个数组的大小
输入格式:
输入的第一行包含由空格分隔的n和k
第二行包含数组A(可修改的数组),其值由空格分隔
第三行包含数组B(不可修改的数组),其值由空格分隔
输出格式:
输出两个阵列的最小产品总和
约束条件:
1≤N≤10^ 5
0≤| A [i] |,| B [i] | ≤10^ 5
0≤K≤10^ 9
样本输入和输出
SNo。输入输出
1.
3 5
1 2 -3
-2 3 -5
-31
2.
5 3
2 3 4 5 4
3 4 2 3 2
25
我尝试过:
The task is to find the minimum sum of Products of two arrays of the same size, given that k modifications are allowed on the first array. In each modification, one array element of the first array can either be increased or decreased by 2.
Note- the product sum is Summation (A[i]*B[i]) for all i from 1 to n where n is the size of both arrays
Input Format:
First line of the input contains n and k delimited by whitespace
Second line contains the Array A (modifiable array) with its values delimited by spaces
Third line contains the Array B (non-modifiable array) with its values delimited by spaces
Output Format:
Output the minimum sum of products of the two arrays
Constraints:
1 ≤ N ≤ 10^5
0 ≤ |A[i]|, |B[i]| ≤ 10^5
0 ≤ K ≤ 10^9
Sample Input and Output
SNo. Input Output
1.
3 5
1 2 -3
-2 3 -5
-31
2.
5 3
2 3 4 5 4
3 4 2 3 2
25
What I have tried:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
long long int A[10^5],B[10^5];
int N;
int pro,temp,diff;
int maxDiff=0,minSum=0;
int K;
cin>>N>>K;
for(int i=0;i<n;i++)>
{
cin>>A[i];
cin>>B[i];
}
for(int i=0;i<n;i++)>
{
pro=A[i]*B[i];
if(pro<0 && B[i]<0)
temp=(A[i]+2 * K)* B[i];
else if(pro<0 && A[i]<0)
temp=(A[i]-2 * K)* B[i];
else if(pro>0 && A[i]<0)
temp=(A[i]+ 2* K)* B[i];
else if(pro>0 && A[i]>0)
temp=(A[i]- 2*K) *B[i];
diff=abs(pro-temp);
if(diff>maxDiff)
maxDiff=diff;
minSum=minSum+pro;
}
minSum=minSum-maxDiff;
cout<<"\n"<<minSum;
}
推荐答案
long long int A[10^5],B[10^5];
以上甚至不会编译,如果确实如此,你将有两个数组,每个元素都是零。我建议阅读 C ++内置运算符,优先级和相关性 [ ^ ]。您还声明了一个变量 N
,但是然后尝试在中使用名为
循环。对变量使用适当有意义的名称有助于避免诸如此类的简单错误。 n
的变量for
The above will not even compile, and if it did you would have two arrays each of zero elements. I suggest a reading of C++ Built-in Operators, Precedence and Associativity[^]. You also declare a variable N
, but then try to use a variable named n
in your for
loop. Using proper meaningful names for variables helps to avoid simple errors such as this.
这篇关于如何在C或C ++或java中解决这个问题,请尽快回答这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!