POJ 2442-Sequence(優先佇列-m組n個數每組取一個求n個最小值)
Sequence
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 9285 | Accepted: 3097 |
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
1 2 3 1 2 3 2 2 3
Sample Output
3 3 4
Source
POJ Monthly,Guang Lin
題目意思:
有m組序列,每組有n個數。從每組中取一個數並求和,輸出最小的n個和。
解題思路:
輸入一組處理一組,先求出前兩組數的n個最小和,再輸入一組數,將這組數與前面的n個最小和一起處理,再求出這兩組數的n個最小和…………直到m組數全部輸入處理完畢。
處理過程如下:
①第一組數輸入A陣列中後降序排列,第二組數輸入B陣列後也降序排列
②將A[0]+B[i]這n個數先入隊,再依次遍歷A陣列剩下的n-1個數與B陣列的n個數之和,每次比較將較小的和入隊。
③遍歷完畢後,將佇列中元素賦給A陣列,同時佇列清空。
④輸入下一組數到B陣列,重複②,直到m組數輸入處理完畢。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
const int MAXN=30030;
int a[MAXN],b[MAXN];
using namespace std;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
priority_queue<int>q;//最大值優先
int m,n;
cin>>m>>n;
for(int i=0; i<n; ++i)
cin>>a[i];
sort(a,a+n);//升序排列
--m;
while(m--)
{
for(int i=0; i<n; ++i)
cin>>b[i];
sort(b,b+n);
for(int i=0; i<n; ++i)//入隊
q.push(a[0]+b[i]);
for(int i=1; i<n; ++i)//依次比較將較小的和入隊
for(int j=0; j<n; ++j)
{
int temp=a[i]+b[j];
if(temp<q.top())
{
q.pop();
q.push(temp);
}
}
for(int i=0; i<n; ++i)//將佇列中的數賦給a陣列,同時佇列清空
{
a[i]=q.top();
q.pop();
}
sort(a,a+n);
}
for(int i=0; i<n-1; ++i)//輸出結果
cout<<a[i]<<" ";
cout<<a[n-1]<<endl;
}
return 0;
}
/*
1
2 3
1 2 3
2 2 3
*/
測試資料:
2
2 3
1 2 3
2 2 3
10 10
21 12 123 3 21 123 32 143 43 56
2 32 43 34 54 56 656 76 43 234
234 45 5 65 56 76 43 23 435 57
32 324 435 46 56 76 87 78 43 23
3 32 324 45 56 57 34 23 54 565
23 32 34 342 324 232 2 432 324 12
234 324 4 45 65 67 435 23 5 654
34 3245 345 56 56 657 67 456 345 325
234 234 546 65 88 66 53 654 65 765
5 3 34 34 34 56 345 234 2 34
執行結果:
3 3 4
131 132 132 133 134 135 140 140 141 141
相關文章
- hdu 4546 優先佇列 數列組合和第m小佇列
- 不能使用for迴圈,傳入n和m, 生成一個長度為n,每一項都是m的陣列陣列
- 動態生成一個m行n列的表格
- 找最小的k個數(優先佇列)佇列
- 面試中被問到一組有序序列(從小到大),求這組序列中的前n個面試
- 改進,從一個陣列中找出 N 個數,其和為 M 的所有可能陣列
- javascript將陣列的元素每兩個一組存入一個新陣列JavaScript陣列
- 分組求TOP N記錄
- Python有 n 個物品和一個大小為 m 的揹包. 給定陣列 A 表示每個物品的大小和陣列 V 表示每個物品的價值.。。Python陣列
- 求陣列中k個數的所有組合陣列
- POJ 3253-Fence Repair(哈夫曼樹-最小值優先佇列)AI佇列
- 從陣列中找出N個數,其和為M的所有可能陣列
- 獲取陣列第N個元素的方法陣列
- 洗牌演算法擴充(從n個數中隨機m個數)演算法隨機
- MySQL 分組排序後 → 如何取前N條或倒數N條MySql排序
- POJ 3253 Fence Repair 優先佇列AI佇列
- java基礎:遍歷m取n的所有組合(轉)Java
- YT14-HDU-求N^N的個位數(暴力破解版)
- mysql 分組取每個組的前幾名的問題MySql
- [PY3]——實現一個優先順序佇列佇列
- POJ 1724 ROADS(優先佇列+spfa)佇列
- POJ2431 Expedition (優先佇列)佇列
- POJ 2051(最小堆/優先佇列)佇列
- n從1開始,每一個可以選擇對n加1或者對n加倍,若想獲得整數2013,最少需要多少個操作
- [整] JavaScript m選n組合演算法JavaScript演算法
- 從一個無序,不相等的陣列中,選取N個數,使其和為M實現演算法(javascript實現)陣列演算法JavaScript
- 指標-n個數的排序指標排序
- 對N個數進行排序排序
- 求n個排序連結串列的交集排序
- JavaScript 獲取第n個li元素JavaScript
- 定義一個求n的階乘的函式函式
- 杭電OJ 2028求n個數的最小公倍數
- 建立包含N個空物件的陣列物件陣列
- Nth Digit 第N個數字Git
- HDU 2582 f(n) (組合數的gcd)GC
- 從面試題中學演算法(2)---求陣列中唯一n個出現1次的數字(n=1,2,3)面試題演算法陣列
- SQL 如何計算每個分組的中位數SQL
- 輸入n個元素組成的序列s,你需要找出一個乘積最大的連續子序列