C++ 数的最大K积

C++ 数的最大K积,c++,algorithm,max,product,modulo,C++,Algorithm,Max,Product,Modulo,任务-数字的最大K积 时限:1 内存限制:64米 给定一个整数序列N(1)≤ N≤ 年5月10日≤ 2.10.9)和K(1)的数量≤ K≤ N) 。查找乘积最大的K个序列号 输入数据: 第一行包含两个整数N和K。 第二行列出了序列A的N个元素 输出数据: 求出最大乘积。所以答案可能相当大,以10^9+7的模输出 示例 输入数据的结果 3.2 -2-3 3 答复-6 下面是我的尝试。这是一个错误,这是我不知道的。你能帮我找出我的决定中的错误吗 #include <stdio.h> #i

任务-数字的最大K积
时限:1
内存限制:64米
给定一个整数序列N(1)≤ N≤ 年5月10日≤ 2.10.9)和K(1)的数量≤ K≤ N) 。查找乘积最大的K个序列号

输入数据:
第一行包含两个整数N和K。
第二行列出了序列A的N个元素

输出数据:
求出最大乘积。所以答案可能相当大,以10^9+7的模输出

示例
输入数据的结果
3.2
-2-3 3

答复-6

下面是我的尝试。这是一个错误,这是我不知道的。你能帮我找出我的决定中的错误吗

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> v1;
const int mod = 1000000007;
int n, k, pos1 = 0, pos2 = 0, negative = 0;
long long res = 1;
void QuickSort(v1 &a, int l, int r) {
  int i = l, j = r, pivot = abs(a[l + ((r - l) >> 1)]);
  do {
        while (abs(a[i]) < pivot) i++;
        while (abs(a[j]) > pivot) j--;
        if (i <= j) {
            int temp = a[i];
            a[i++] = a[j];
            a[j--] = temp;
        }
    } while (i < j);
    if (l < j) QuickSort(a, l, j);
    if (i < r) QuickSort(a, i, r);
}

long long product(v1 &a, v1 &b, int q, int j, char flag) {
    long long res = 1; int temppos;
    if (flag == 0 && j) {
        temppos = b[pos1];
        b[pos1] = j;
    }
    if (flag == 1 && q) {
        temppos = b[pos2];
        b[pos2] = q;
    }
    if (!pos2 && (k & 1)) {
        for (int i = 1; i <= k; i++)
            res = (long long)((res % mod)*1ll*(a[i] % mod)) % mod;
    } else {
        for (int i = 1; b[i] != 0; i++)
            res = (long long)((res % mod)*1ll*(a[b[i]] % mod)) % mod;
    }
    if (flag == 0 && j) b[pos1] = temppos;
    if (flag == 1 && q) b[pos2] = temppos;
    return res;
}

int main()
{
      v1 a(100002, 0);
        v1 b(100002, 0);  //index multiplied to the elements
        cin >> n >> k;
        for (int i = 1; i <= n; i++) cin >> a[i];
        QuickSort(a, 1, n);
        for (int i = n, j = 1; i > n - k; i--) {
                b[j] = i;
                if (a[i] < 0) {                     
                    pos1 = j;    //index last positive number 
                    negative++;  //increase the counter negative numbers
                }
                  else pos2 = j;  //index last positive number                
                j++;
        }
        int j = n - k, q = j;
        if (negative & 1) { //If an odd number of negative numbers
            while (j > 0 && a[j] < 0) j--;
            while (q > 0 && a[q] > 0) q--;
            res = max(product(a, b, q, j, 0), product(a, b, q, j, 1));
        } else res = product(a, b, q, j, 3);
        cout << res << endl;
    cin >> res;
    return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
typedef向量v1;
常数整数模=100000007;
int n,k,pos1=0,pos2=0,负=0;
长res=1;
无效快速排序(v1&a、整数l、整数r){
int i=l,j=r,pivot=abs(a[l+((r-l)>>1)];
做{
而(abs(a[i])pivot)j--;
如果(in>>k;
对于(int i=1;i>a[i];
快速排序(a,1,n);
对于(int i=n,j=1;i>n-k;i--){
b[j]=i;
如果(a[i]<0){
pos1=j;//索引最后一个正数
负+++;//增加计数器的负数
}
else pos2=j;//索引最后一个正数
j++;
}
int j=n-k,q=j;
if(negative&1){//if负数的奇数
而(j>0&&a[j]<0)j--;
而(q>0&&a[q]>0)q--;
res=最大值(乘积(a,b,q,j,0),乘积(a,b,q,j,1);
}else res=乘积(a,b,q,j,3);
法院;
返回0;
}

首先:您的代码非常密集和混乱,因此我将在这里给出我自己的方法和思考过程

所以我的第一个想法是关于所有输入整数都为正的情况。将我们的源数组(包含N个整数的数组)称为A

  • 初始化包含A中前K个元素的数组B

  • 对于每个i-K,您的尝试的高级描述将是有用的(除了您的代码之外,或者只是高级描述)。