POJ 2442 Sequence 堆和堆排序

Sequence
Time Limit: 6000MS
Memory Limit: 65536K
Total Submissions: 7697
Accepted: 2543

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

實在不想說什麼,瞎搞就給AC了,說句實在的,這題搞的我也是昏昏沉沉的。。。。。。


關於堆和堆排序,請看我的文章:http://www.ihypo.net/1648.html


#include <stdio.h>
#include <string.h>
#include <algorithm>

int s1[2010];//建立堆用的數組
int s2[2010];//輸入數的數組
int s3[2010];//第一次輸入用,後面都是當臨時數組用

void adjust(int len,int i)//堆的調整函數
{
    int mx = i;
    int lt = mx << 1;
    int rt = mx << 1 | 1;
    while(lt <= len || rt <= len)
    {
        if(lt <= len && s1[lt] > s1[mx])
            mx = lt;
        if(rt <= len && s1[rt] > s1[mx])
            mx = rt;
        if(mx != i)
        {
            int tmp = s1[mx];
            s1[mx] = s1[i];
            s1[i] = tmp;

            i = mx;
            lt = mx << 1;
            rt = mx << 1 | 1;
        }
        else
            break;
    }
}

void build(int len)//建立堆的函數
{
    for(int i = len >> 1; i >= 1; i--)
        adjust(len,i);
}

void sortheap(int len)//堆排序函數
{
    while(len)
    {
        int tmp = s1[1];
        s1[1] = s1[len];
        s1[len] = tmp;
        len--;
        adjust(len,1);
    }
}

int main()
{
    int m,n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&s3[i]);
            s1[i] = s3[i];//這一操作是用來防止只有一行數的
        }//先把第一組數據讀入
        if(m == 1)
            build(n);//如果只有一行數的話,那麼這行數從小到大排序就是答案,這裏建立堆,後面sortheap排序,就可以輸出了
        for(int i = 1; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                scanf("%d",&s2[j]);//讀入數據,放入數s2中
            }
            std::sort(s2,s2 + n);//對s2排序
            for(int j = 1; j <= n; j++)
                s1[j] = s3[j] + s2[0];//s2[0]是s2中最小的一個,讓s3加上它,就保證當前最小
            build(n);//建最大堆
            for(int j = 1; j <= n; j++) //這兩個循環使用來找當前前n個最小值加上新來的s2的值是否有比最大堆的堆頂小的,如果有,更新最大堆堆頂,否則,跳出
            {
                for(int k = 1; k < n; k++)
                {
                    if(s3[j] + s2[k] < s1[1])
                    {
                        s1[1] = s3[j] + s2[k];
                        adjust(n,1);
                    }
                    else
                        break;
                }

            }
            for(int j = 1; j <= n; j++)
                s3[j] = s1[j];//把堆中在線更新的的前n個最小值賦值給s3,下次循環還要用
        }
        sortheap(n);//堆排序
        for(int i = 1; i <= n; i++)
        {
            printf("%d",s1[i]);
            if(i <= n - 1)
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章