Sequence 优先队列模拟堆 poj 2442
来源:互联网 发布:广联达电力计价软件 编辑:程序博客网 时间:2024/04/24 19:56
Sequence
Time Limit: 6000MSMemory Limit: 65536KTotal Submissions: 6381Accepted: 1998
Description
Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's clear that we may get n ^ m this kind of sequences. Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What we need is the smallest n sums. Could you help us?
Input
The first line is an integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains two integers m, n (0 < m <= 100, 0 < n <= 2000). The following m lines indicate the m sequence respectively. No integer in the sequence is greater than 10000.
Output
For each test case, print a line with the smallest n sums in increasing order, which is separated by a space.
Sample Input
12 31 2 32 2 3
Sample Output
3 3 4
Source
这是用优先队列来模拟堆,这个题意是说用2行3列的矩阵,那么我们就会形成3^2个选择,我们先把第一行放入
num1中,然后把第二行的数据赋给num2,让num2[0]和num1[ ]相加得出的值放入优先队列big中,放入big中
再与num2[i](i>=1)+num1[ ]和比较,如果小于最大值,就把最大值直接替换,这样最后就可以得出来前n个序列
中最大的值了
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<algorithm>#include<vector>using namespace std;int main(){ int num1[3000]; int num2[3000]; priority_queue<int,vector<int>,less<int> >big; //´ int t,i,j,k; int m,n; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); for(i=0;i<n;i++) scanf("%d",&num1[i]); sort(num1,num1+n); int l,y; for(y=1;y<m;y++) { for(j=0;j<n;j++) { scanf("%d",&num2[j]); big.push(num1[0]+num2[j]); } sort(num2,num2+n); for(k=1;k<n;k++) { for(l=0;l<n;l++) { if(num1[k]+num2[l]>big.top()) { break; } big.pop(); big.push(num1[k]+num2[l]); } } for(k=0;k<n;k++) { num1[n-k-1]=big.top(); big.pop(); } } for(i=0;i<n-1;i++) printf("%d ",num1[i]); printf("%d\n",num1[i]); } return 0;}
0 0
- Sequence 优先队列模拟堆 poj 2442
- poj-2442-Sequence-优先队列||堆
- POJ 2442 Sequence(stl+优先队列||堆)
- POJ 2442 Sequence(优先队列)
- Sequence Poj2442 (堆/优先队列)
- poj 2442Sequence 优先队列的运用
- POJ 2442 Sequence (STL优先队列)
- poj 2442 优先队列/堆+dp
- 最小堆模拟优先队列
- poj 3253 优先队列 堆
- Sequence(优先队列,或堆)
- POJ 题目2442 Sequence(技巧,优先队列)
- 【POJ】 2442——Sequence【STL—优先队列】
- POJ 2442 Sequence k路归并,优先队列
- 优先队列priority_queue 用法详解 +poj 2442 Sequence
- 优先队列模拟最大堆和最小堆,poj 1442 Black Box
- POJ 2442 Sequence(堆)
- POJ 2442 Sequence【堆】
- ZOJ-1251
- CVS on Win7
- 开发笔记:解决安卓GestureOverlayView手势和ListView点击事件、文本框获取焦点冲突的问题
- ZOJ-1292
- Maximo学习笔记------关联字段显示详细描述
- Sequence 优先队列模拟堆 poj 2442
- UICollectionView-数据源
- quick-cocos2dx AppBase
- [推荐]AlloVince 的 VIM 配置方案
- 腾讯风铃怎么连接数据库的呢
- ZOJ-1331
- SqlServer中,用exec处理sql字符串中含有变量,提示:必须声明标量变量@(已解决!)
- SeekBar OpenGLRenderer GL_INVALID_VALUE
- 确保测试代码不会在发布版上运行