Sequence用堆排序
2014-05-26 21:05
10 查看
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
Sample Output
View Code
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
1 2 3 1 2 3 2 2 3
Sample Output
3 3 4
#include"iostream" #include"algorithm" #include"ctime" #include"cstdio" #include"cctype" using namespace std; #define maxx 2008 int a[maxx],b[maxx],sum[maxx]; int main() { int i,j,k,t,n,m,temp; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=1;i<m;i++) { sort(a,a+n); for(j=0;j<n;j++) scanf("%d",&b[j]); for(j=0;j<n;j++) sum[j]=a[j]+b[0]; make_heap(sum,sum+n); for(j=1;j<n;j++) for(k=0;k<n;k++) { temp=b[j]+a[k]; if(temp>=sum[0]) break; pop_heap(sum,sum+n); sum[n-1]=temp; push_heap(sum,sum+n); } for(j=0;j<n;j++) a[j]=sum[j]; } sort(a,a+n); printf("%d",a[0]); for(j=1;j<n;j++) printf(" %d",a[j]); printf("\n"); printf("time :%d\n",clock()); } return 0; }
View Code
相关文章推荐
- db2 SEQUENCE
- 白话经典算法系列之七 堆与堆排序
- Sequence Training in DNN/CNN for ASR
- Java实现---堆排序 Heap Sort
- hibernate注解方式用sequence生成主键
- HDU 4915 Parenthese sequence
- 微软编程一小时 题目2 : Longest Repeated Sequence
- 【Codeforces】-612C-Replace To Make Regular Bracket Sequence(括号配对,栈)
- Solaris SPARC Boot Sequence
- poj 2388 堆排序
- Longest Consecutive Sequence
- UVA1584 UVALive3225 Circular Sequence【水题】
- 【数据结构】堆排序
- leetcode Permutation Sequence
- 链表排序(冒泡、选择、插入、快排、归并、希尔、堆排序)
- 【HDU】5783 Divide the Sequence
- 一.排序算法大全之堆排序
- 堆排序
- Codeforces #24 C. Sequence of points
- Best Coder #86 1002 NanoApe Loves Sequence