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的更多相关文章

  1. POJ-2442-Sequence(二叉堆)

    POJ-2442 Description Given m sequences, each contains n non-negative integer. Now we may select one ...

随机推荐

  1. linux 线程函数大全

    Technorati 标签: Linux thread 索引: 1.创建线程pthread_create 2.等待线程结束pthread_join 3.分离线程pthread_detach 4.创建线 ...

  2. C++中extern “C”含义深层探索

    C++中extern “C”含义深层探索 extern “C” 是一个双向都需要用到的语法表示,就是说在cpp引用c头文件,或者c引用cpp文件时都需要用到.但extern “C” 永远只能在cpp引 ...

  3. Google面试题:计算从1到n的正数中1出现的次数

    题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. 找工作,准备看写题目,题目说是Goo ...

  4. C++ 实现不能被继承的类

    方法一: #include <iostream> using namespace std; class A { public: static A* getInstance(); stati ...

  5. java集合学习一

    首先看一下java集合的关系图 1.1从全面了解Java的集合关系图.常见集合  list  set map等其中我们最常用的 list  map 结合.几天说一下常见的map.map在我工作的两年里 ...

  6. [windows phone开发]新生助手的开发过程与体会三

    由于网络原因,新生助手开发介绍的博客近期一直没有更新,请大家见谅.今天向大家介绍一下新生助手中动态磁帖的实现. 在PhoneApplicationPage中添加如下引用 xmlns:toolkit=& ...

  7. smarty框架块函数

    块函数的形式是这样的:{func} .. {/func}.换句话说,它们被封闭在一个模板区域内,然后对该区域的内容进行操作.默认地,你的函数实现会被Smarty调用两次:一次是在开始标签,另一次是在闭 ...

  8. Delphi XE5教程8:使用Delphi命名空间

    // Project file declarations... //项目文件声明… program MyCompany.ProjectX.ProgramY; // Unit source file d ...

  9. 使用CSS画一个三角形

    <div style="width:0px;height:0px;border-width:40px;border-style:solid;border-color:transpare ...

  10. OBIEE 11g:Error:nQSError 36010 Server version 318 cannot read the newer version of the repository

    biee11g升级到最新版以后,发现了一些bug,需要回退到原来的版本,卸载掉升级包以后,启动BI服务,会报上述错误.这是因为资料库文件已经升级为了最新版本.这时候我们需要将资料库文件进行降版本操作. ...