日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/4/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ms 两个数组,从每个数组中取一个数相加,求最大的前k个和 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

兩個(gè)數(shù)組,從每個(gè)數(shù)組中取一個(gè)數(shù)相加,求最大的前k個(gè)和?
比如:?
數(shù)組A:1,2,3?
數(shù)組B:4,5,6?
則最大的前2個(gè)和:9,8。?
ps:結(jié)果放到數(shù)組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的矩陣,然后每行取一個(gè)元素,組成一個(gè)包含n個(gè)元素的序列,一共有n^m種序列,

讓你求出序列和最小的前n個(gè)序列的序列和。

解題步驟:

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

2.將數(shù)據(jù)讀入data2向量中,并按升序排序。

??? 將data2[0] + data1[i] ( 0<=i<=n-1)讀入dataq向量中

??? 建堆。

??? 然后data2[1] + data1[i] (0<=i<=n-1),如果data2[1] + data1[i]比堆dataq的頂點(diǎn)大,則退出,否則刪除

??? 堆的頂點(diǎn),插入data2[1] + data1[i]。然后是data2[2],...data2[n - 1]

3.將dataq的數(shù)據(jù)拷貝到data1中,并對data1按升序排序

4.循環(huán)2,3步,直到所有數(shù)據(jù)讀入完畢。

5.打印data1中的數(shù)據(jù)即為結(jié)果。

[cpp]?view plaincopy
  • #include?<cstdio>??
  • #include?<cstring>??
  • #include?<string>??
  • #include?<vector>??
  • #include?<queue>??
  • #include?<algorithm>??
  • #include?<iostream>??
  • using?namespace?std;??
  • int?main()??
  • {??
  • ????int?t;??
  • ????int?n,m;??
  • ????int?num1[2010];??
  • ????int?num2[2010];??
  • ????priority_queue<int,deque<int>,less<int>?>?big;??
  • ????scanf("%d",&t);??
  • ????while(t--)??
  • ????{??
  • ????????scanf("%d%d",&m,&n);??
  • ????????for(int?i=0;i<n;i++)??
  • ????????scanf("%d",&num1[i]);??
  • ????????sort(num1,num1+n);??
  • ????????for(int?i=1;i<m;i++)??
  • ????????{??
  • ????????????for(int?j=0;j<n;j++)??
  • ????????????{??
  • ????????????????scanf("%d",&num2[j]);??
  • ????????????????big.push(num1[0]+num2[j]);??
  • ????????????}??
  • ????????????sort(num2,num2+n);??
  • ????????????for(int?k=1;k<n;k++)??
  • ????????????for(int?l=0;l<n;l++)??
  • ????????????{??
  • ????????????????if(num1[k]+num2[l]>big.top())??
  • ????????????????????break;??
  • ????????????????????big.pop();??
  • ????????????????????big.push(num1[k]+num2[l]);??
  • ????????????}??
  • ????????????for(int?k=0;k<n;k++)??
  • ????????????{??
  • ????????????????num1[n-k-1]=big.top();??
  • ????????????????big.pop();??
  • ????????????}??
  • ????????}??
  • ????????printf("%d",num1[0]);??
  • ????????for(int?i=1;i<n;i++)??
  • ????????printf("?%d",num1[i]);??
  • ????????puts("");??
  • ????}??
  • }??

  • ?




    開始忘記給他們排序、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;
    }

    總結(jié)

    以上是生活随笔為你收集整理的ms 两个数组,从每个数组中取一个数相加,求最大的前k个和的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。