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. 大仙说道之Android studio实现Service AIDL

    今天要开发过程中要用到AIDL的调用,之前用的eclipse有大量教程,用起来很方便,现在刚换了Android studio,不可否认studio真的很强大,只是很多功能还需要摸索. AIDL(And ...

  2. linux 编译内核[scripts/kconfig/dochecklxdialog] 错误

    administrator@ubuntu:~/linux-2.6.28-omap$ make menuconfig *** Unable to find the ncurses libraries o ...

  3. Andriod docs加载速度慢的问题解决

    网上找了个类, import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import ja ...

  4. Easyui datebox控件打开页面就验证解决方法

    问题描述: datebox时间控件有些场景下默认值需要为空,但是为空的情况下打开页面会自动验证,十分影响美观. 实现原理: <input class="easyui-databox&q ...

  5. 省市区联动(MVC分布视图)

    1.调用分布视图 //Html辅助方法 返回参数的值 存储在ProvinceId.CityId.DistrictId中 @{Html.RenderAction("GetProvince&qu ...

  6. git记住用户名密码

    git config --global credential.helper store

  7. 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏

    function orient() {if (window.orientation == 90 || window.orientation == -90) {//ipad.iphone竖屏:Andri ...

  8. SQL中的模糊查询

    写个标题先.先来一篇大神的文章:http://www.cnblogs.com/GT_Andy/archive/2009/12/25/1921914.html 练习代码如下: 1.百分号:%   表示任 ...

  9. Oracle中的注释

    注释用于对程序代码的解释说明,它能够增强程序的可读性,是程序易于理解. 单行注释: 用“--”,后面跟上注释的内容 Declare Num_sal number; --声明一个数字类型的变量 Var_ ...

  10. pt-query-digest分析mysql查询日志

    [root@hank-yoon log]# pt-query-digest slowq.log # 200ms user time, 10ms system time, 24.39M rss, 205 ...