• 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;
    }
  • 相关阅读:
    浅谈网站旁注入侵思路及需要的条件
    黑客破解网络密码的10种方法
    管理员必须掌握的八个cmd命令
    浅谈无线攻击技术思路
    windows下部署免费ssl证书(letsencrypt)
    用Let's Encrypt实现Https(Windows环境+Tomcat+Java)
    从 HTTP 到 HTTPS
    三种web性能压力测试工具
    ECS Windows系统使用自带监视器查看IIS并发连接数
    Windows系统服务器IIS7.5 Asp.net支持10万请求的设置方法
  • 原文地址:https://www.cnblogs.com/jackge/p/3223215.html
Copyright © 2020-2023  润新知