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. 删除 windows 下 node_modules 过深的目录

    本文同步自我的个人博客:http://www.52cik.com/2015/11/13/node-modules-del.html 说到 node 的模块,确实既好用又蛋疼.相信无数人吐槽 node_ ...

  2. IE firefox 兼容性整理

    1.尽量用jquery操作. 2.jquery取值时要用准确的方法,attr(), val(), text(), html(). 例如: <span value="a"> ...

  3. zoeDylan.js框架-数据底层

    zoeDylan.js是墨芈自己写的一套前端框架,不过由于墨芈经验不足,所以框架内部代码有些混乱. 墨芈写这套框架的目的是为了存储以后做前端开发过程中的一些代码,简单的说这套框架就是一个大杂烩. 这套 ...

  4. requirejs自己的学习

    1.最新版本的RequireJS压缩后只有14K. 2.模块化,不在使用全局变量,都用块级作用域包装. 3.防止js加载阻止页面渲染. 4.避免出现多个javascript的标签.

  5. [工具类]文件或文件夹xx已存在,则重命名为xx(n)(2)

    写在前面 最近一直在弄文件传输组件,其中一个功能就是,在接收端接收文件时,如果文件已经存在了,则对其进行文件名+索引的方式进行自动重命名,之前也写个类似的工具类,总感觉代码太冗余,每回头想想,总觉得心 ...

  6. CSS3——3D效果

    1.效果1 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" c ...

  7. maven_创建quickstart模板时异常

    错误信息: Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1.1 from an ...

  8. JS模式:jq中简单的模式--》采摘自js设计(tomxu_version)

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  9. Java-小数点控制

    package 运算及类型转换类; import java.text.DecimalFormat; public class 控制小数点类 { public static double decimal ...

  10. 【POJ 2250】Compromise(最长公共子序列LCS)

    题目字符串的LCS,输出解我比较不会,dp的时候记录从哪里转移来的,之后要一步一步转移回去把解存起来然后输出. #include<cstdio> #include<cstring&g ...