Pro. 1

给定k个有序表,取其中前n小的数字。组成一个新表,求该表?

算法:

由于  a1[1] < a1[2] < a1[3] ... <a1[n]

a2[1] < a2[2] < a2[3] ... < a2[n]

.........

ak[1] < ak[2]<ak[3]...... < ak[n]

首先每个有序表的第一个元素入堆,然后最小元素出堆。该元素入新表L,相应线性表的下一个元素入堆。

例如:如果出堆得是a2[2],那么a2[3]入堆。所以给每一个表增设一个标记。循环n次得到新表.

Pro. 2

给定两个长为n的有序表A,B,根据两表中任意两元素和得到一个大小为N^2的表,得到的新表的前n小的元素。(HDU上的一个。但是那个题目直接Hash做)

根据题意:

A[1] + B[1] < A[1] + B[2] .. < A[1] + B[n]

A[2] + B[1] < A[1] + B[2] .. < A[1] + B[n]

.......

A[n] + B[1] < A[1] + B[2] .. < A[1] + B[n]

这样就得到了n个有序表。  转化为pro 1 得到答案

Pro. 3

说到正题了。 给定m 个长为n 的有序表。 每个表取一个元素。得到m个元素。求和。 要求,求前n小的和。

每次考虑两个表, 根据 a[1][1 ...n] 与a[2][1...n] ,转化为pro.2 。求出一个大小为n的表 L

再考虑表L与a[3][1...n] 得到新的表L'

接着考虑a[4][1....n]

...... 最后得到的本题的答案

                                                         Sequence
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 6259   Accepted: 1953

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

Source

POJ Monthly,Guang Lin

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

int t,n,m,A[2222],Q[2222];

struct node
{
    int arr,id;
    bool operator < (const node b) const
    {
        return arr>b.arr;
    }
};

int matrix[2222][2222];

void fuuuuuc()
{
    int cur=0;
    int nowfrom[2222];
    memset(nowfrom,0,sizeof(nowfrom));
    priority_queue<node> q;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            matrix[i][j]=Q[i]+A[j];
            if(j==0)
            {
                node x;
                x.arr=matrix[i][0]; x.id=i;
                q.push(x);
            }
        }
    }
/*
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cout<<matrix[i][j]<<" ";
        }
        cout<<endl;
    }
*/
    for(cur=0;cur<n;cur++)
    {
        node u=q.top();  q.pop();
     //   cout<<" -->: "<<u.id<<" "<<u.arr<<endl;
        int ID=u.id;
        Q[cur]=u.arr;
        nowfrom[ID]++;
        u.arr=matrix[ID][nowfrom[ID]];
        q.push(u);
    }
}

int main()
{
    int T;
    scanf("%d",&T);
while(T--)
{
    scanf("%d%d",&m,&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&Q[i]);
    }
    sort(Q,Q+n);
    for(int i=1;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",A+j);
        }
        sort(A,A+n);
        fuuuuuc();
    }
    for(int i=0;i<n;i++)
    {
        if(i) putchar(' ');
        printf("%d",Q[i]);
    }
    putchar(10);
}
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 2442 Sequence的更多相关文章

  1. POJ 2442 Sequence(堆的使用练习)

    题目地址:id=2442">POJ 2442 真心没想到这题的思路. .原来是从第一行逐步向下加,每次都仅仅保存前n小的数.顺便练习了下堆.. 只是感觉堆的这样的使用方法用的不太多啊. ...

  2. poj 2442 Sequence (Priority Queue)

    2442 -- Sequence 真郁闷,明明方法是对的,为什么我的代码老是那么的慢._(:з」∠)_ 这题要想考虑两列的情况,然后逐列拓展. 代码如下: #include <cstdio> ...

  3. POJ 2442 - Sequence - [小顶堆][优先队列]

    题目链接:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequ ...

  4. poj 2442 Sequence(优先队列)

    题目:http://poj.org/problem?id=2442 题意:给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列, 让你求出序列和最小的前n个序列的序列和 ...

  5. POJ 2442 Sequence 优先队列

    题目: http://poj.org/problem?id=2442 #include <stdio.h> #include <string.h> #include <q ...

  6. POJ 2442 Sequence【堆】

    题目链接:http://poj.org/problem?id=2442 题目大意:给出一个m*n的矩阵,从每一行中取出一个数相加.能得到n^m个不同的结果.要求输出当中前n项. 建立一个以n元数组为底 ...

  7. POJ 2442 Sequence堆 优先队列

    题目描述 给定m个序列,每个序列包含n个非负整数.现在我们可以从每个序列中选择一个数字以形成一个具有m个整数的序列.显然,我们可以得到n ^ m种这种序列.然后,我们可以计算每个序列中的数字总和,并获 ...

  8. Sequence POJ - 2442

    Sequence POJ - 2442 口胡一个结论:就是前i行产生的最小的n个和,一定可以在"前i-1行产生的最小n个和,每一个加上这一行的任意一个数,产生的n2个数"中找到.( ...

  9. [POJ 3581]Sequence

    [POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...

随机推荐

  1. 你的C#代码是怎么跑起来的(一)

    写了那么多C#代码,大家有没有想过自己写的代码编译后的可执行文件内部是什么样子,是怎样在系统上运行的? 编译成exe,然后双击exe文件运行,这中间到底发生了些什么呢,这篇先来剖析下exe内部的样子: ...

  2. 如何把apk编译时间和最后次git commit的sha值,写入到app中

    需求背景:我们修复Bug的时候,频繁提交APK包,导致测试同学搞不清哪个包才是最新的 比如一个版本3.0.1,我们可能后续基于这个版本陆续提交了好几个修复包 同时,如果服务端ip地址能在界面上配置的话 ...

  3. 成都普华永道税务开发的offer

    首先这是一个.net税务开发的offer,我是做开发的. 有没有人在成都普华永道的,最近收到普华永道的offer,如果有的话请联系我.想知道里面的情况.最想知道里面的加班情况,薪资还是有点诱惑的.毕竟 ...

  4. 基于int的Linux的经典系统调用实现

     先说明两个概念:中断和系统调用 一 系统调用: 是应用程序(运行库也是应用程序的一部分)与操作系统内核之间的接口,它决定了应用程序是如何和内核打交道的. 1,  Linux系统调用:2.6.19版内 ...

  5. Zigbee技术特点

    ZigBee工作原理 基于 ZigBee 的无线设备工作在 868MHZ, 915MHZ 和 2.4Z 频带.其最大数据速 率是 250Kbps. ZigBee 技术主要针对以电池为电源的应用,这些应 ...

  6. NuGet更新引用Dll

    第一种 通过 "Add Library Package Reference..." 添加 点击 ‘Add Library Package Reference...’ , 搜索你要添 ...

  7. JavaScript基础系列目录(2014.06.01~2014.06.08)

    下列文章,转载请亲注明链接出处,谢谢! 链接地址: http://www.cnblogs.com/ttcc/tag/JavaScript%20%E5%9F%BA%E7%A1%80%E7%9F%A5%E ...

  8. Kettle_设置全局变量

    使用全局变量的目的是为了避免反复修改[作业]和[转换]中变量到实际值 步骤: 1.打开全局配置文件 目录:C:\Users\Administrator\.kettle\kettle.propertie ...

  9. 【BZOJ 3188】【Coci 2011】Upit Splay模板题

    转啊转终于转出来了,然而我的模板跟陈竞潇学长的模板一模一样,还是太弱啊,第一次用指针. #include<cstdio> #include<cstring> #include& ...

  10. poj3237 树链剖分 暴力

    NEGATE a,b 将a b间的线段取反,这题应该用线段树+成段更新.我成段更新写的挫了,试了暴力修改过了(数据水). 也是简单的题目.不过要注意点和边的区别. #include<queue& ...