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

题意:有m行数字,每行均有n个数字.从每行均取出一个数字相加,共有m^n个解,输出解最小的n个.

题解:建一个大根堆,先将第一行的全部扔进去,在全部扔回一个数组里,那么此时数组中的数是从大到小排列的.

然后这个数组,我们给它中的每一个数取出,再加上第二行的第一个数,全部扔进堆里,接着对于第2~n个数中的每一个,我们将它与数组中最小的,次小的……(数组倒过来)相加,如果和比大根堆的堆顶大,这就是我们要找的了,将堆顶弹出,将这个值插入.然而我并不知道这玩意的复杂度,居然就这么A了?真是奇怪,有信仰的话可一定要用手写堆啊!

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define hi puts("hi");
using namespace std; int map[][]; struct Heap
{
int a[],sz; void init()
{
sz=;
} void add(int x)
{
int now=sz++;
while(now>)
{
int fa=now>>;
if(a[fa]>x)
{
break;
}
a[now]=a[fa];
now=fa;
}
a[now]=x;
} int pop()
{
int ans=a[];
int now=;
int x=a[--sz];
while((now<<)<sz)
{
int ls=now<<,rs=now<<|;
if(a[ls]<a[rs]&&rs<sz)
{
ls=rs;
}
if(a[ls]<x)
{
break;
}
a[now]=a[ls];
now=ls;
}
a[now]=x;
return ans;
} int top()
{
return a[];
} int empty()
{
return sz==;
}
} heap; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m,n,b[],cnt=;
heap.init();
scanf("%d%d",&m,&n); for(int i=; i<=m; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&map[i][j]);
}
}
for(int i=; i<=n; i++)
{
heap.add(map[][i]);
}
for(int i=; i<=m; i++)
{
cnt=;
while(!heap.empty())
{
b[++cnt]=heap.pop();
}
for(int j=; j<=cnt; j++)
{
heap.add(b[j]+map[i][]);
}
for(int j=; j<=n; j++)
{
for(int k=cnt; k>=; k--)
{
if(b[k]+map[i][j]<heap.top())
{
heap.pop();
heap.add(b[k]+map[i][j]);
}
else
{
break;
}
}
}
}
cnt=;
while(!heap.empty())
{
b[++cnt]=heap.pop();
}
for(int i=cnt; i>=; i--)
{
printf("%d ",b[i]);
}
printf("%d\n",b[]);
} }

POJ2442 Sequence(堆的骚操作)的更多相关文章

  1. $POJ2442\ Sequence$ 堆

    正解:堆 解题报告: 传送门$QwQ$ 全场除了我都切了系列$kk$ 首先看$n=2$的情况. 首先暴力不说?就记录一个$sum$再分别记录$xy$两维的下标存到堆里面每次取队头并继续扩展就完事$Qw ...

  2. C#中的9个“黑魔法”与“骚操作”

    C#中的9个"黑魔法"与"骚操作" 我们知道C#是非常先进的语言,因为是它很有远见的"语法糖".这些"语法糖"有时过于好 ...

  3. Typescript骚操作,在TS里面直接插入HTML

    Typescript骚操作,在TS里面直接插入HTML,还有语法提示 先给大家看一个图 因为我不喜欢用很重的框架,主要是并非专业UI,但是偶尔会用到,还是觉得直接element组装受不了,想想能在ts ...

  4. 闪电侠 Netty 小册里的骚操作

    前言 即使这是一本小册,但基于"不提笔不读书"的理念,仍然有必要总结一下.此小册对于那些"硬杠 Netty 源码 却不曾在千万级生产环境上使用实操"的用户非常有 ...

  5. awk骚操作

    一.awk自加 [root@168web3 ~]# head /data/logs/cloud_monitor_rds_cpu.log |awk '{sum+=$NF}END{print sum}' ...

  6. 如何在命令长度受限的情况下成功get到webshell(函数参数受限突破、mysql的骚操作)

    0x01 问题提出 还记得上篇文章记一次拿webshell踩过的坑(如何用PHP编写一个不包含数字和字母的后门),我们讲到了一些PHP的一些如何巧妙地绕过数字和字母受限的技巧,今天我要给大家分享的是如 ...

  7. UOJ 117 欧拉回路(套圈法+欧拉回路路径输出+骚操作)

    题目链接:http://uoj.ac/problem/117 题目大意: 解题思路:先判断度数: 若G为有向图,欧拉回路的点的出度等于入度. 若G为无向图,欧拉回路的点的度数位偶数. 然后判断连通性, ...

  8. 关于map 及 map 骚操作

    关于map这个东西   很冷门..................   但是,这个博客带你稍微了解一下map:   map用法:一般当作一个下表无穷大的数组   关于它的骚操作:map的鬼畜用法,可以 ...

  9. 通过HTTP的HEADER完成各种骚操作

    作为一名专业的切图工程师,我从来不care网页的header,最多关心Status Code是不是200.但是HEADER真的很重要啊,客户端从服务器端获取内容,首先就是通过HEADER进行各种沟通! ...

随机推荐

  1. 让Delphi XE5跟其他版本的Delphi共存

    找到Delphi XE5的安装根目录  ....  \Program Files (x86)\Embarcadero\RAD Studio\12.0\bin下的cglm.ini文件, 打开cglm.i ...

  2. MSSQL使用sqlbulkcopy批量插入数据

    具体代码如下: /// <summary> /// 批量插入数据到BayonetZipFailedPic表 /// </summary> /// <param name= ...

  3. Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

    python3.6.3  我在处理爬虫时候使用BeautifulSoup中遇到报错 “ bs4.FeatureNotFound: Couldn't find a tree builder with t ...

  4. PhantomJS 一个隐形的浏览器

    下载地址: http://phantomjs.org/download.html 使用方法: 下载压缩包解压出来找到phantomjs.exe 放到python的根目录下

  5. java代码实现点击鼠标从控制台输出信息

    总结:最难的就是当我们需要点击按钮时去实现某个功能-----------因为那个我没有理解透,是涉及整个程序的 package com.a.b; import javax.swing.*; impor ...

  6. shulti模块简述

    #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import shutil shutil.copyfileobj('D:\\3. ...

  7. getpass密码隐藏

    使用getpass模块对输入的字符进行隐藏输入 #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import getpass u ...

  8. Dev TreeList基本用法

    public partial class treelist_shijian : DevExpress.XtraEditors.XtraForm      {          public treel ...

  9. Python内置函数二 (递归函数,匿名函数,二分法)

    匿名函数 lambda() 语法: lambad  参数 : 返回值 def func(a,b): return a * b print(func(2,5)) a = lambda a ,b : a* ...

  10. 「小程序JAVA实战」微信小程序简介(一)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-01/ 一直想学习小程序,苦于比较忙,加班比较多没时间,其实这都是理由,很多时候习惯了搬砖,习惯了固 ...