zoukankan      html  css  js  c++  java
  • POJ 2442 Sequence

    Sequence
    Time Limit: 6000MS   Memory Limit: 65536K
    Total Submissions: 6120   Accepted: 1897

    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-1个有序表的qian那个最小和与第m个有序表形成。以此类推,可先参考此题:http://www.cnblogs.com/jackge/archive/2013/04/28/3049766.html

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    
    using namespace std;
    
    const int N=2010;
    
    int m,n,a[N],b[N],c[N],d[N];
    
    struct node{
        int id,x;
        bool operator < (const node &a) const{
            return a.x<x;
        }
    };
    
    void solve(){
        for(int i=0;i<n;i++)
            d[i]=0;
        //priority_queue<node,vector<node>,cmp> q;
        priority_queue<node> q;
        node cur;
        for(int i=0;i<n;i++){
            cur.id=i;
            cur.x=a[i]+b[d[i]];
            q.push(cur);
        }
        int k=n,cnt=0;
        while(k--){
            cur=q.top();
            q.pop();
            c[cnt++]=cur.x;
            cur.x=a[cur.id]+b[++d[cur.id]];
            q.push(cur);
        }
        for(int i=0;i<n;i++)
            a[i]=c[i];
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&m,&n);
            for(int i=0;i<n;i++)
                scanf("%d",&a[i]);
            sort(a,a+n);
            for(int i=1;i<m;i++){
                for(int j=0;j<n;j++)
                    scanf("%d",&b[j]);
                sort(b,b+n);
                solve();
            }
            for(int i=0;i<n-1;i++)
                printf("%d ",a[i]);
            printf("%d
    ",a[n-1]);
        }
        return 0;
    }
  • 相关阅读:
    IE7下元素的 'paddingtop' 遇到 'clear' 特性在某些情况下复制到 'paddingbottom'
    Foundation HTML5 Canvas中的2处错误
    近期学习技术安排
    2011年工作总结和展望(上篇)
    详解ObjectiveC消息传递机制
    ObjectiveC 2.0的运行时编程消息转发
    c# Pdf 转换图片
    c语言指针用法难点
    C# web实现word 转Html、office转Html、pdf转图片 在线预览文件
    ObjectiveC中什么是类
  • 原文地址:https://www.cnblogs.com/jackge/p/3223215.html
Copyright © 2011-2022 走看看