ms 两个数组,从每个数组中取一个数相加,求最大的前k个和

本文介绍了一个算法问题,即从多个序列中选取一个元素组成新序列,求这些序列中和最小的前n个序列的和。文章详细阐述了解决方案的步骤,包括数据排序、优先队列的使用以及如何迭代更新序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两个数组,从每个数组中取一个数相加,求最大的前k个和 
比如: 
数组A:1,2,3 
数组B:4,5,6 
则最大的前2个和:9,8。 
ps:结果放到数组C[k]中 

http://www.cnblogs.com/372465774y/archive/2012/07/09/2583866.html

Sequence
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 5137   Accepted: 1572

Description

Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's clear that we may get n ^ m this kind of sequences. Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What we need is the smallest n sums. Could you help us?

Input

The first line is an integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains two integers m, n (0 < m <= 100, 0 < n <= 2000). The following m lines indicate the m sequence respectively. No integer in the sequence is greater than 10000.

Output

For each test case, print a line with the smallest n sums in increasing order, which is separated by a space.

Sample Input

1
2 3
1 2 3
2 2 3

Sample Output

3 3 4



题意:给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列,

让你求出序列和最小的前n个序列的序列和。

解题步骤:

1.将第一序列读入data1向量中,并按升序排序。

2.将数据读入data2向量中,并按升序排序。

    将data2[0] + data1[i] ( 0<=i<=n-1)读入dataq向量中

    建堆。

    然后data2[1] + data1[i] (0<=i<=n-1),如果data2[1] + data1[i]比堆dataq的顶点大,则退出,否则删除

    堆的顶点,插入data2[1] + data1[i]。然后是data2[2],...data2[n - 1]

3.将dataq的数据拷贝到data1中,并对data1按升序排序

4.循环2,3步,直到所有数据读入完毕。

5.打印data1中的数据即为结果。

[cpp]  view plain copy
  1. #include <cstdio>  
  2. #include <cstring>  
  3. #include <string>  
  4. #include <vector>  
  5. #include <queue>  
  6. #include <algorithm>  
  7. #include <iostream>  
  8. using namespace std;  
  9. int main()  
  10. {  
  11.     int t;  
  12.     int n,m;  
  13.     int num1[2010];  
  14.     int num2[2010];  
  15.     priority_queue<int,deque<int>,less<int> > big;  
  16.     scanf("%d",&t);  
  17.     while(t--)  
  18.     {  
  19.         scanf("%d%d",&m,&n);  
  20.         for(int i=0;i<n;i++)  
  21.         scanf("%d",&num1[i]);  
  22.         sort(num1,num1+n);  
  23.         for(int i=1;i<m;i++)  
  24.         {  
  25.             for(int j=0;j<n;j++)  
  26.             {  
  27.                 scanf("%d",&num2[j]);  
  28.                 big.push(num1[0]+num2[j]);  
  29.             }  
  30.             sort(num2,num2+n);  
  31.             for(int k=1;k<n;k++)  
  32.             for(int l=0;l<n;l++)  
  33.             {  
  34.                 if(num1[k]+num2[l]>big.top())  
  35.                     break;  
  36.                     big.pop();  
  37.                     big.push(num1[k]+num2[l]);  
  38.             }  
  39.             for(int k=0;k<n;k++)  
  40.             {  
  41.                 num1[n-k-1]=big.top();  
  42.                 big.pop();  
  43.             }  
  44.         }  
  45.         printf("%d",num1[0]);  
  46.         for(int i=1;i<n;i++)  
  47.         printf(" %d",num1[i]);  
  48.         puts("");  
  49.     }  
  50. }  


 




开始忘记给他们排序、WA了一次、郁闷!
 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#define N 2003
using namespace std;
struct node
{
    int i;
    int n;
};
struct cmp
{
  bool operator ()(const node&a,const node&b)
  {
      return a.n>b.n;
  }
};
int a[N],b[N],c[N];
int m,n;
int d[N];
void del()
{
    int i;
    for(i=0;i<n;i++)
         d[i]=0;
    node t;
    priority_queue<node,vector<node>,cmp> Q;
    for(i=0;i<n;i++)
    {
        t.i=i;
        t.n=a[i]+b[d[i]];
        Q.push(t);
    }
    int te=n;i=0;
    while(te--)
    {
       t=Q.top();Q.pop();
       c[i++]=t.n;
       t.n=a[t.i]+b[++d[t.i]];
       Q.push(t);
    }
    for(i=0;i<n;i++)
     a[i]=c[i];
}
int main()
{
    int T;
    int i;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&m,&n);
        for(i=0;i<n;i++)
          scanf("%d",&a[i]);
        sort(a,a+n);
        while(--m)
        {
            for(i=0;i<n;i++)
              scanf("%d",&b[i]);
            sort(b,b+n);
            del();
        }
        n--;
     for(i=0;i<n;i++)
       printf("%d ",a[i]);
     printf("%d\n",a[i]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值