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).

3.arr1数组从1到n-1,比较temp=arr1[i]+arr2[0...n-1]与堆顶的元素,如果temp比较小,则将堆顶元素pop,添加temp到heap;否则跳出循环。

4.将heap中的元素全部赋值给arr1数组,升序排序,重复2,3两步,直到所有数据全部处理完。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#define M 111
#define N 2111
using namespace std;
int arr1[N],arr2[N],heap[N];
int m,n;
int main()
{
    int t;
    bool s=true;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i=0;i<n;i++)
            scanf("%d",&arr1[i]);
        sort(arr1,arr1+n);
        for(int i=1;i<m;i++)
        {
            for(int j=0;j<n;j++)
                scanf("%d",&arr2[j]);
            sort(arr2,arr2+n);
            for(int j=0;j<n;j++)
                heap[j]=arr1[0]+arr2[j];
            make_heap(heap,heap+n);
            for(int j=1;j<n;j++)
            {
                for(int k=0;k<n;k++)
                {
                    int temp=arr1[j]+arr2[k];
                    if(temp<heap[0])
                    {
                        pop_heap(heap,heap+n);//将heap里的最大值放在最后,并且重新修正heap
                        heap[n-1]=temp;
                        push_heap(heap,heap+n);
                        s=false;
                    }
                    else
                        break;
                }
                if(s)
                    break;
            }
            for(int j=0;j<n;j++)
                arr1[j]=heap[j];
            sort(arr1,arr1+n);
        }
        for(int i=0;i<n;i++)
            printf("%d ",arr1[i]);
        printf("\n");
    }
    return 0;
}

POJ 2442 Sequence【堆】,布布扣,bubuko.com

时间: 2024-07-30 16:56:41

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(堆的使用练习)

题目地址:POJ 2442 真心没想到这题的思路..原来是从第一行逐步向下加,每次都只保存前n小的数.顺便练习了下堆..不过感觉堆的这种用法用的不太多啊.. 又是手残..把j写成了i,于是就改啊改..改的跟题解上的几乎一样了= = !.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #inc

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