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
相關文章
- N個人,按M進行分組
- 3516 求n個整數的最小值 迴圈結構
- 不能使用for迴圈,傳入n和m, 生成一個長度為n,每一項都是m的陣列陣列
- 3069 求n個整數的和
- 改進,從一個陣列中找出 N 個數,其和為 M 的所有可能陣列
- Python有 n 個物品和一個大小為 m 的揹包. 給定陣列 A 表示每個物品的大小和陣列 V 表示每個物品的價值.。。Python陣列
- 統計整數區間[N,M](N,M<100000)中所以非偶數的合數個數,並輸出這個數。
- 輸入N,再輸入N個數,N
- 獲取陣列第N個元素的方法陣列
- 面試中被問到一組有序序列(從小到大),求這組序列中的前n個面試
- 從陣列中找出N個數,其和為M的所有可能陣列
- 寫一個方法,傳入數字x,從一個一維陣列裡找到兩個數字符合“n1 + n2 = x”陣列
- 洗牌演算法擴充(從n個數中隨機m個數)演算法隨機
- 求陣列中k個數的所有組合陣列
- MySQL 分組排序後 → 如何取前N條或倒數N條MySql排序
- n個骰子的點數
- 2022-07-17:1、2、3...n-1、n、n、n+1、n+2... 在這個序列中,只有一個數字有重複(n)。 這
- 從一個無序,不相等的陣列中,選取N個數,使其和為M實現演算法(javascript實現)陣列演算法JavaScript
- 杭電OJ 2028求n個數的最小公倍數
- 給定一個n,輸出從1到n的整數
- 每週一個 Python 模組 | copyPython
- 每週一個 Python 模組 | functoolsPython
- 每週一個 Python 模組 | jsonPythonJSON
- 每週一個 Python 模組 | stringPython
- 每週一個 Python 模組 | socketPython
- 每週一個 Python 模組 | heapqPython
- 每週一個 Python 模組 | enumPython
- 每週一個 Python 模組 | itertoolsPython
- 每週一個 Python 模組 | timePython
- 每週一個 Python 模組 | bisectPython
- 每週一個 Python 模組 | QueuePython
- 每週一個 Python 模組 | structPythonStruct
- 每週一個 Python 模組 | signalPython
- 每週一個 Python 模組 | unittestPython
- 每週一個 Python 模組 | linecachePython
- 每週一個 Python 模組 | pathlibPython
- 每週一個 Python 模組 | hashlibPython
- 每週一個 Python 模組 | globPython
- 每週一個 Python 模組 | contextlibPythonContext