• Sequence 分类: 栈和队列 2015-08-05 10:10 2人阅读 评论(0) 收藏


    Sequence
    Time Limit: 6000MS Memory Limit: 65536K
    Total Submissions: 8277 Accepted: 2708

    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
    题意:给你一个m*n的矩阵,从每一行中选一个元素,组成m个元素的和,问和最小的n个数;
    方法:用的优先队列,优先队列中保存n个元素是前i-1个行中和最小的n个数,队头是和最大的,然后让这n个和与第i行的元素从小到大相加,如果大于队头元素,则说明队头不是最小的,则去掉队头元素,加入更小的,直到将m行都加完,队列中的n个元素就是最小的你个数.

    #include <map>
    #include <list>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define eps 1e-9
    #define LL long long
    #define PI acos(-1.0)
    #define INF 0x3f3f3f3f
    #define CRR fclose(stdin)
    #define CWW fclose(stdout)
    #define RR freopen("input.txt","r",stdin)
    #pragma comment(linker, "/STACK:102400000")
    #define WW freopen("output.txt","w",stdout)
    
    const int MAX = 6000000+5;
    int Arr[110][2110];
    int b[2110];
    priority_queue<int >Q;
    int main()
    {
        int T;
        int n,m;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d",&n,&m);
            for(int i=0; i<n; i++)
            {
                for(int j=0; j<m; j++)
                    scanf("%d",&Arr[i][j]);
                sort(Arr[i],Arr[i]+m);//进行排序
            }
            for(int i=0; i<m; i++)
            {
                Q.push(Arr[0][i]);//以第一行为基准
            }
            for(int i=1; i<n; i++)
            {
                for(int j=0; j<m; j++)
                {
                    b[j]=Q.top();//先用数组储存起来
                    Q.pop();
                }
                for(int j=0; j<m; j++)//Arr[i]从小到大
                {
                    for(int k=m-1; k>=0; k--)//b[]从小到大
                    {
                        if(j==0)
                        {
                            Q.push(Arr[i][j]+b[k]);//先是Arr[i][j]与前i-1个最小的n个数相加
                        }
                        else
                        {
                            if(Arr[i][j]+b[k]<Q.top())//如果小于队头,说明队头不是n个最小的和,去掉,加入更小的
                            {
                                Q.pop();
                                Q.push(Arr[i][j]+b[k]);
                            }
                            else//跳出,因为后面的更大,就没有必要判断
                            {
                                break;
    
                            }
                        }
                    }
                }
            }
            for(int i=m-1; i>=0; i--)
            {
                b[i]=Q.top();
                Q.pop();
            }
            for(int i=0; i<m; i++)
            {
                if(i)
                    printf(" ");
                printf("%d",b[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Kibana安装
    25.Spring Cloud Sleuth与ELK
    Spring Cloud Sleuth综合整理
    26.Spring Cloud Sleuth与Zipkin
    算法与数据结构实验题 1.3 寻找幸运值
    算法与数据结构实验题 1.1 互质序列
    课程作业八
    课程作业七
    课程作业六
    课程作业五
  • 原文地址:https://www.cnblogs.com/juechen/p/4721930.html
Copyright © 2020-2023  润新知