F - Sequence

Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%I64d
& %I64u

Submit Status

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

感谢http://www.cnblogs.com/372465774y/archive/2012/07/09/2583866.html

做这个题首先思考两个问题

由这两个得出,要求n个数组每一个数组m个值。数组1和数组2的和找出最小的m个,再用来和数组3求和,找到最小的m个,终于得到全部的数组中的最小的m个

因为每一个数组都是有序的,并且我们要求的最小的m个。数组a[i][j]+队列中的值 > 队首的值,那么a[i][j]加上队列中以后的值都会大于队首。对于我们要求解的最小的m个值无意义。队列中保存了当前数组到之前全部数组的最小的m个和,不断更新队列

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int a[110][2100] , b[2100] ;
priority_queue <int> p ;
int main()
{
int i , j , k , n , m , t ;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &n, &m);
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < m ; j++)
scanf("%d", &a[i][j]);
sort(a[i],a[i]+m);
}
for(i = 0 ; i < m ; i++)
p.push(a[0][i]) ;
for(i = 1 ; i < n ; i++)
{
for(j = 0 ; j < m ; j++)
{
b[j] = p.top();
p.pop();
}
for(j = 0 ; j < m ; j++)
{
for(k = m-1 ; k >= 0 ; k--)
{
if(j == 0)
p.push( a[i][j]+b[k] );
else
{
if( a[i][j] + b[k] < p.top() )
{
p.pop();
p.push(a[i][j]+b[k]);
}
else
break;
}
}
}
}
for(j = 0 ; j < m ; j++)
{
b[j] = p.top();
p.pop();
}
for(j = m-1 ; j >= 0 ; j--)
{
if(j == 0)
printf("%d\n", b[j]);
else
printf("%d ", b[j]);
}
}
return 0;
}

版权声明:转载请注明出处:http://blog.csdn.net/winddreams

STL--F - Sequence(n*m-&gt;之前的最低要求m个月)的更多相关文章

  1. 线段树 区间合并 F - Sequence operation

    F - Sequence operation 题解:这个题目不是一个特别难的题目,但是呢,写了好久,首先线段树难敲,其次就是bug难找,最后这个代码都被我改的乱七八糟的了,这个有两个地方要注意一下,一 ...

  2. The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest - F.Sequence(打表+线段树)

    题意:给你一个长度为$n$的数组,定义函数$f(l,r)=a_{l} \oplus a_{l+1} \oplus...\oplus a_{r}$,$F(l,r)=f(l,l)\oplus f(l,l+ ...

  3. 南理第八届校赛同步赛-F sequence//贪心算法&二分查找优化

    题目大意:求一个序列中不严格单调递增的子序列的最小数目(子序列之间没有交叉). 这题证明贪心法可行的时候,可以发现和求最长递减子序列的长度是同一个方法,只是思考的角度不同,具体证明并不是很清楚,这里就 ...

  4. 从零开始写STL—set/map

    这一部分只要把搜索树中暴露的接口封装一下,做一些改动. set源码剖析 template<typename T> class set { public: typedef T key_typ ...

  5. Purfer Sequence

    原文地址:http://www.cnblogs.com/zhj5chengfeng/archive/2013/08/23/3278557.html 我们知道,一棵树可以用括号序列来表示,但是,一棵顶点 ...

  6. Scalaz(26)- Lens: 函数式不可变对象数据操作方式

    scala中的case class是一种特殊的对象:由编译器(compiler)自动生成字段的getter和setter.如下面的例子: case class City(name:String, pr ...

  7. 泛函编程(19)-泛函库设计-Parallelism In Action

    上节我们讨论了并行运算组件库的基础设计,实现了并行运算最基本的功能:创建新的线程并提交一个任务异步执行.并行运算类型的基本表达形式如下: import java.util.concurrent._ o ...

  8. 学习multiprocessing

    1. multiprocessing.Pool from multiprocessing.pool import Pool def gen_row(): ...return rows def main ...

  9. 【DG】[三思笔记]一步一步学DataGuard

    [DG][三思笔记]一步一步学DataGuard 它有无数个名字,有人叫它dg,有人叫它数据卫士,有人叫它data guard,在oracle的各项特性中它有着举足轻理的地位,它就是(掌声)..... ...

随机推荐

  1. ios UITableView 相关

    1.tableView 实现的方法 无分组的cell #pragma mark - Table view data source - (NSInteger)tableView:(UITableView ...

  2. JS验证身份证的合法性

    //验证身份证的合法性 function IdentityCodeValid(code) { var city={11:"北京",12:"天津",13:&quo ...

  3. U11认识与学习bash

    1.使用命令clear来清除界面. 2.命令别名设置alias和unalias: 例如: alias lm='ls -l | more' 查看当前的别名设置有哪些: alias unalias lm ...

  4. Codeforces #252 (Div. 2) B. Valera and Fruits

    题目倒是不难,可是读起来非常恶心 依据题目的描写叙述不easy找到适合存储的方法 后来我就想不跟着出题人的思路走 我自己开一个数组c 令c[a[i]] = b[i] 则c[i] == [j] 代表第i ...

  5. Oracle ORA-01034,ORA-27101,ORA-00600

    本机IP地址:192.168.1.163 [oracle@rtest ~]$ sqlplus /nolog SQL*Plus: Release 10.2.0.2.0 - Production on S ...

  6. C++头文件保护符和变量的声明定义

    1.#ifndef #define #endif头文件保护符 在编译的过程中,每个.cpp文件被看成一个单独的文件来编译成单独的编译单元,#ifndef 保证类的头文件在同一个.cpp文件里被多次引用 ...

  7. 如何检测被锁住的Oracle存储过程及处理办法汇总(转)

    1.查看是哪一个存储过程被锁住查V$DB_OBJECT_CACHE视图select * from V$DB_OBJECT_CACHE where owner='过程的所属用户' AND LOCKS!= ...

  8. Activity详细解释(生命周期、以各种方式启动Activity、状态保存,等完全退出)

    一.什么是Activity? 简单的说:Activity或者悬浮于其它窗体上的交互界面. 在一个应用程序中通常由多个Activity构成.都会在Manifest.xml中指定一个主的Activity, ...

  9. 实现strcmp非常easy的思维

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void strcom(char *s ...

  10. Codeforces 432E Square Tiling(结构体+贪婪)

    题目连接:Codeforces 432E Square Tiling 题目大意:给出一个n∗m的矩阵,要求对该矩阵进行上色,用大写字母,可是每次上色的区域必须是正方形,不求相邻的上色区域不能有同样的颜 ...