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. 使用AChartEngine第一步:在项目中配置AChartEngine环境

    1. 从AChartEngine官网上下载acharengine的jar文件包. 地址:https://code.google.com/p/achartengine/downloads/list 2. ...

  2. Shadowsock搭建

    搭建Shadowsocks服务端: 搭建Shadowsocks之前首先必须购买一个VPS.一般VPS提供商会给一个测试地址,购买之前最好先ping一下速度.也可以通过以下网址测试下vps网络速度: h ...

  3. [BZOJ 2186][Sdoi2008]沙拉公主的困惑(欧拉函数)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2186 分析: 就是要求1~n!中与m!互质的数的个数 首先m!以内的就是φ(m!) 关 ...

  4. Linq之Linq to Sql

    目录 写在前面 系列文章 Linq to sql 总结 写在前面 上篇文章介绍了linq to xml的相关内容,linq to xml提供一种更便捷的创建xml树,及查询的途径.这篇文章将继续介绍l ...

  5. WCF 入门 (18)

    前言 感冒了呀...but 不忌油炸,不忌辛辣,o(∩_∩)o . 第18集 WCF服务应该抛出fault 异常 Throwing fault exceptions from a WCF servic ...

  6. linux编译ruby1.8.7 出现OPENSSL错误

    安装ruby-1.8.7出现编译错误.如下: ossl_pkey_ec.c:815: error: ‘EC_GROUP_new_curve_GF2m’ undeclared (first use in ...

  7. java.lang.NoClassDefFoundError: org/objectweb/asm/Type

    Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/ ...

  8. mysql-分页查询方案

    一.直接使用limit最简单查询方法: , 在中小数据量的情况下,这样的SQL足够用了,唯一需要注意的问题就是确保使用了索引. 随着数据量的增加,页数会越来越多,查看后几页的SQL就可能类似: , 言 ...

  9. SQL Server之存储过程基础知识

    什么是存储过程呢?存储过程就是作为可执行对象存放在数据库中的一个或多个SQL命令. 通俗来讲:存储过程其实就是能完成一定操作的一组SQL语句. 那为什么要用存储过程呢?1.存储过程只在创造时进行编译, ...

  10. Mac OS X终端下apache操作

    重启apache:sudo /usr/sbin/apachectl restart 关闭apache:sudo /usr/sbin/apachectl stop 开启apache:sudo /usr/ ...