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 ...
随机推荐
- mysql颠覆实战笔记(一)--设计一个项目需求,灌入一万数据先
版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸 ...
- 启动 mysql 失败 Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'
Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql' 这应该是某种情况下导致/usr/ ...
- 重拾C,一天一点点_6
break与continuecontinue只能用于循环语句goto最常见的用法是终止程序在某些深度嵌套的结构中的处理过程,例如一次跳出两层或多层循环.break只能从最内层循环退出到上一级的循环. ...
- Lucene 3.0
http://www.cnblogs.com/forfuture1978/archive/2010/02/22/1671487.html http://www.cnblogs.com/jiekzou/ ...
- 在 SQL Server 中的网络数据库文件的支持说明
其实就是一个学员问SQL Server 是否能存放的于NAS(UAC 的路径下). 官方的回答简略版本为:可以,需要满足一些强制性的硬件要求.但需要考虑一系列的性能的问题. http://suppor ...
- SQL统计——按照各种维度
在SQLserver中可以按照各种维度进行统计,实现与EXCLE一样强大的功能. --========================== --Blog:<奔跑的金鱼> --Desc:&l ...
- MySQL监控工具-orztop
先安装orzdba,链接:http://blog.itpub.net/28939273/viewspace-1875895/ 安装依赖的包:[root@hank-yoon servers]# yum ...
- CentOS 7.0安装MPlayer
自带的播放器不管rmvb还是mp4都不能播放,也搜索不到解码器. CentOS7epel装不上所以也没有rpmfusion,所以决定自己编译mplayer 首先是要获取源代码. 首先是主程序的源代码. ...
- 在Debian中安装VMware Workstatption 12
在Debian中安装VMware Workstatption 12-----------------------------------------------> 下载文件: *vmwar ...
- mysql federated engine
mysql)) -> engine=federated -> connection='mysql://root@localhost:3306/t1/t';