POJ2442Sequence
http://poj.org/problem?id=2442
题意 :就是输入m个数集,每个含n个数,求从每个集合取一个数后,按非降序输出前n小的和。
思路 : 本来打算是用几个for循环的,后来觉得要是真这么简单就不会在堆里分类了,所以,经过会神详细指导讲解,略懂略懂,弄俩优先队列,正好搞定
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
using namespace std ;
const int maxn = ;
int main()
{
int n ;
scanf("%d",&n);
int m,t,s;
int a[maxn] ;
for(int i = ; i <= n ; i++)
{
priority_queue<int ,vector<int>,less<int> > Q;//大根堆是用来找和最小的值的
priority_queue<int ,vector<int>,greater<int> > P;//小根堆是将输入的每行数从小到大排序
scanf("%d %d",&m,&t);
for(int h = ; h < t ; h++)
{
scanf("%d",&s);//先输入第一行
P.push(s);
}
for(int h = ; h < m ; h++)
{
for(int j = ; j < t ; j++)
scanf("%d",&a[j]) ;
while(!P.empty())
{
int b = P.top();
P.pop();
for(int j = ; j < t ; j++)
{
if(Q.size() == t && b+a[j] < Q.top())
{
//如果大根堆里已经有了t个数了,那就判断首元素与b+a[j]谁大,若是大,就删掉,加入新的
Q.pop();
Q.push(b+a[j]);
}
else if(Q.size() < t)
Q.push(b+a[j]) ;
}
} while(!Q.empty())
{
P.push(Q.top());
Q.pop();
}
}
printf("%d",P.top()) ;
P.pop();
for(int k = ; k < t ; k++)
{
printf(" %d",P.top()) ;
P.pop();
}
printf("\n");
//memset(sh,0,sizeof(sh));
}
return ;
}
POJ2442Sequence的更多相关文章
- POJ-2442-Sequence(二叉堆)
POJ-2442 Description Given m sequences, each contains n non-negative integer. Now we may select one ...
随机推荐
- 查看某个模块的Tables
在SE11 中 关于table的F4 help 有一个筛选条件是Package 同时由于不同的模块放在不同的Package中 很容易根据这个条件 获得某个模块的所有Tables 亲测有效 1 ...
- wordpress使用video.js与七牛云存储实现无广告视频分享应用
video.js是一款极受欢迎的基于HTML5的开源WEB视频播放器,其充分利用了HTML5的视频支持特性,可以实现全平台的无视频插件播放功能,对于现在流行的手机.PAD等移动智能终端有极佳的应用体验 ...
- 【Qt】Qt之启动外部程序【转】
简述 QProcess可以用来启动外部程序,并与它们交互. 要启动一个进程,通过调用start()来进行,参数包含程序的名称和命令行参数,参数作为一个QStringList的单个字符串. 另外,也可以 ...
- 如何在ARC代码中混编非ARC代码
“ios中如果arc和非arc文件混编,可以在build parses中指定compile flags,如果arc文件设为"-fobjc-arc",非arc文件设为"-f ...
- Operating Cisco Router
Operating Cisco Router consider the hardware on the ends of the serial link, in particular where the ...
- Java中resourceBundle和Properties的区别
第一种办法InputStream is = Test.class.getResourceAsStream("DbConfig.properties");Properties p = ...
- mapreduce 实现矩阵乘法
import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs ...
- Fat-tree 胖树交换网络
胖树架构下,网络带宽不收敛 传统的树形网络拓扑中,带宽是逐层收敛的,树根处的网络带宽要远小于各个叶子处所有带宽的总和. 而胖树网络则更像是真实的树,越到树根,枝干越粗,即:从叶子到树根,网络带宽不收敛 ...
- JNI-入门之二
Android中JNI编程的那些事儿 首先说明,Android系统不允许一个纯粹使用C/C++的程序出现,它要求必须是通过Java代码嵌入Native C/C++——即通过JNI的方式来使用本地(Na ...
- CentOS 6.0 设置IP地址、网关、DNS
切忌: 在做任何操作之前先备份原文件,我们约定备份文件的名称为:源文件名称+bak,例如原文件名称为:centos.txt 那么备份文件名称为:centos.txtbak 引言:linux ...