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 ...
随机推荐
- 3月3日(5) Roman to Integer
原题 Roman to Integer 题意很简单,把Roman字母翻译成int. 实现方式也不难,针对每个字符转成int,从右往左,依次判断,如果当前值比上一个值大则相加,小则相减. 什么,你问我怎 ...
- Python's Exception 层级结构
BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration ...
- String 两种实例化方式的区别
package com.java1234.chap03.sec08; public class Demo3 { public static void main(String[] args) { //1 ...
- jquery.js有红叉
使用Eclipse 3.7及以上版本时,工程中加入jquery.min.js文件,发现该文件出现错误提示(红×),但使用Eclipse 3.7以前的版本就不会出现这种提示.是因为Eclipse 3.7 ...
- 【转】JavaScript里的this指针
用自然语言的角度理解JavaScript中的this关键字 <script type="text/javascript"> function ftn03(){ var ...
- ubuntu 停在开机界面
今天有解决了一个问题.我在win7虚拟机上装的64位的Ubuntu 12.04.忘了怎么个情况了,反正就是系统进不去了,停在了开机界面,5个点的那个. 解决方法如下: 开机的时候按住shift键,进入 ...
- python: 实现sha1小工具
File1: sha1.py File2: sha1.bat ------------------ File1: sha1.py import hashlib import os,sys def Ca ...
- How to running Job from a Form
For Example we wanna run a Job with name "FAN_TableList_CSV". So you must create a button ...
- [ios]ios的延迟执行方法
1.最直接的方法performSelector:withObject:afterDelay: 这种方法的缺点:每次要为延时写一个方法 2.使用类别,用BOLCK执行 [代码]c#/cpp/oc代码 ...
- JAVA算术运算符、关系运算符和位运算符
算术运算符 1.java的算数运算符包括+(加).-(减).*(乘)./(除).%(取余),在运算过程中出现的隐式转换原则和C语言一样:2. 高位数据向低位数据转化要使用强制转化: 关系运算符 1.j ...