POJ 2442 Sequence(堆的使用练习)

题目地址:POJ 2442

真心没想到这题的思路。。原来是从第一行逐步向下加,每次都只保存前n小的数。顺便练习了下堆。。不过感觉堆的这种用法用的不太多啊。。

又是手残。。把j写成了i,于是就改啊改。。改的跟题解上的几乎一样了= = !。。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
const int INF=0x3f3f3f3f;
int q[3000], b[3000], sum[3000];
int main()
{
    int t, n, m, i, j, x, tmp, k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&sum[i]);
        }
        sort(sum,sum+n);
        for(i=1;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%d",&b[j]);
            }
            sort(b,b+n);
            for(j=0;j<n;j++)
            {
                q[j]=sum[0]+b[j];
            }
            make_heap(q,q+n);
            for(j=1;j<n;j++)
            {
                for(k=0;k<n;k++)
                {
                    tmp=sum[j]+b[k];
                    if(tmp>=q[0])
                        break;
                    pop_heap(q,q+n);
                    q[n-1]=tmp;
                    push_heap(q,q+n);
                }
            }
            for(j=0;j<n;j++)
            {
                sum[j]=q[j];
            }
            sort(sum,sum+n);
        }
        for(i=0;i<n-1;i++)
        {
            printf("%d ",sum[i]);
        }
        printf("%d\n",sum[n-1]);
    }
    return 0;
}

POJ 2442 Sequence(堆的使用练习)

时间: 2024-08-15 15:33:28

POJ 2442 Sequence(堆的使用练习)的相关文章

[ACM] POJ 2442 Sequence (堆的性质)

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7011   Accepted: 2262 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

POJ 2442 Sequence【堆】

题目链接:http://poj.org/problem?id=2442 题目大意:给出一个m*n的矩阵,从每一行中取出一个数相加,能得到n^m个不同的结果,要求输出其中前n项. 建立一个以n元数组为底层数组的堆,在这里,利用stl中的make_heap,pop_heap,push_heap等函数解决. 1.将第一组数据输入arr1数组,升序排序. 2.将接下来的数据输入到arr2数组中,并且heap[i]=arr1[0]+arr2[0...n-1],make_heap(heap,heap+n).

poj 2442 Sequence 优先队列的运用

题意: 给m行,每行n个数,从每行取一个数计算和,求前n小的和. 分析: 优先队列的运用,主要是make_heap,pop_heap,push_heap三个STL函数的用法. 代码: //poj 2442 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=2048; int a[maxN],b[maxN],sum[maxN]; int main() { int ca

poj 2442 Sequence

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7019   Accepted: 2266 题目大意:给出n个序列每一个序列有每一个元素在每一个序列里取出一个元素求和  输出前n个最小和 优先队列 #include<cstring> #include<cstdio> #include<cstring> #include<queue> #include<iostr

【POJ 2442】Sequence

[POJ 2442]Sequence 优先队列 m个序列 每个序列n个数 从每个序列中取一个数 可以组成一个长为m的序列 这样一共有n^m种组法 把所有组合的加和排序后输出前n小的和 乍一看听高深的一个问题 其实想清楚了很简单 每一组中取一个数相加 第一组可以有n种取法 假设当前只有两组 按题意组合就是将第一组中n个数分别与第二组n个数相加 取出前n小的和 那么现在再来一组 前两组一共有n*n种组合 每种组合与第三组中n个数再组合 前n小的和就是结果 这三组一共有n^3种组合 然而答案只需要前n

【POJ 2442】 Sequence

[题目链接] http://poj.org/problem?id=2442 [算法] 堆 [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include

(堆的应用) poj 2442

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7762   Accepted: 2565 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

poj 2623 Sequence Median 堆的灵活运用

I - Sequence Median Time Limit:1000MS     Memory Limit:1024KB     64bit IO Format:%I64d & %I64u Submit Status Description Given a sequence of N nonnegative integers. Let's define the median of such sequence. If N is odd the median is the element with

POJ 1019- Number Sequence(规律定位)

Number Sequence Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1019 Appoint description:  System Crawler  (2015-05-22) Description A single positive integer i is given. Write a program to find