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("\n");
}
return 0;
}

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

Sequence 分类: 栈和队列 2015-08-05 10:10 2人阅读 评论(0) 收藏的更多相关文章

  1. DateTime日期格式获取 分类: C# 2014-04-15 10:36 233人阅读 评论(0) 收藏

    c#.net 获取时间年月日时分秒格式 //获取日期+时间 DateTime.Now.ToString();            // 2008-9-4 20:02:10 DateTime.Now. ...

  2. 博弈论入门小结 分类: ACM TYPE 2014-08-31 10:15 73人阅读 评论(0) 收藏

    文章原地址:http://blog.csdn.net/zhangxiang0125/article/details/6174639 博弈论:是二人或多人在平等的对局中各自利用对方的策略变换自己的对抗策 ...

  3. 全面解析sizeof(下) 分类: C/C++ StudyNotes 2015-06-15 10:45 263人阅读 评论(0) 收藏

    以下代码使用平台是Windows7 64bits+VS2012. sizeof作用于基本数据类型,在特定的平台和特定的编译中,结果是确定的,如果使用sizeof计算构造类型:结构体.联合体和类的大小时 ...

  4. 全面解析sizeof(上) 分类: C/C++ StudyNotes 2015-06-15 10:18 188人阅读 评论(0) 收藏

    以下代码使用平台是Windows7 64bits+VS2012. sizeof是C/C++中的一个操作符(operator),其作用就是返回一个对象或者类型所占的内存字节数,使用频繁,有必须对齐有个全 ...

  5. Financial Management 分类: POJ 2015-06-11 10:51 12人阅读 评论(0) 收藏

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 164431   Accepted: ...

  6. C#多线程(上) 分类: C# 线程 2015-03-09 10:35 174人阅读 评论(0) 收藏

    一.多线程的相关概念 什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源. 而一个进程又是由多个线程所组成的. 什么是线程? 线程是程序中的一个执行 ...

  7. C#中的线程(上)-入门 分类: C# 线程 2015-03-09 10:56 53人阅读 评论(0) 收藏

    1.     概述与概念 C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为"主线 ...

  8. C#多线程(下) 分类: C# 线程 2015-03-09 10:41 153人阅读 评论(0) 收藏

    四.多线程的自动管理(线程池) 在多线程的程序中,经常会出现两种情况: 一种情况: 应用程序中,线程把大部分的时间花费在等待状态,等待某个事件发生,然后才能给予响应 这一般使用ThreadPool(线 ...

  9. iOS8 UISearchViewController搜索功能讲解 分类: ios技术 2015-07-14 10:23 76人阅读 评论(0) 收藏

    在iOS8以前我们实现搜索功能需要用到UISearchbar和UISearchDisplayController, 在iOS8之后呢, UISearchController配合UITableView的 ...

  10. APP被苹果APPStore拒绝的各种原因 分类: ios相关 app相关 2015-06-25 17:27 200人阅读 评论(0) 收藏

    APP被苹果APPStore拒绝的各种原因 1.程序有重大bug,程序不能启动,或者中途退出. 2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币. 3.游戏里有实物奖励的话,一定要说清楚,奖励 ...

随机推荐

  1. Cocoapods注意点

    1 安装和升级$ sudo gem install cocoapods $ pod setup 2 更换为taobao的源 $ gem sources -r https://rubygems.org/ ...

  2. ios app 解决微信扫二维码不能跳转问题

    <script> (function(){ // Setup GA (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i ...

  3. [Reprint]c++中typename和class的区别介绍

    在c++Template中,很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢?   相信学习C++的人对class这个关键字都非常明白,clas ...

  4. fzuoj Problem 2177 ytaaa

    http://acm.fzu.edu.cn/problem.php?pid=2177 Problem 2177 ytaaa Accept: 113    Submit: 265Time Limit: ...

  5. BackgroundWorker的使用

    一个程序中需要进行大量的运算,并且需要在运算过程中支持用户一定的交互,为了获得更好的用户体验,使用BackgroundWorker来完成这一功能.   基本操作: bgw.RunWorkerAsync ...

  6. 14---Net基础加强

    更新中,敬请期待............ 复习-匿名类型 Xml介绍

  7. 12.了解或熟悉 C, C++, .NET

    挂起,暂不了解,不过C++略懂,.net虽不懂但是做过实际项目,有能力担当

  8. zw版【转发·台湾nvp系列例程】HALCON EquHistoImage(Delphi)

    zw版[转发·台湾nvp系列例程]HALCON EquHistoImage(Delphi) zw版[转发·台湾nvp系列例程]HALCON EquHistoImage(Delphi) (Delphi ...

  9. form 表单jquery验证插件使用

    第一部分:表单样式 <form action="#" method="post" id="regist">   <tabl ...

  10. SQL UNION 操作符

    转由http://www.w3school.com.cn/sql/sql_union.asp 这个网址的数据库知识,个人推荐,因为有实例,理解更透彻一些.非广告啊,个人感觉好啊 SQL UNION 操 ...