标签:des style blog http os io for art
Description
Input
Output
Sample Input
1 2 3 1 2 3 2 2 3
Sample Output
3 3 4
题意:从m个序列中每个选出一个,选出m个的和的最小的前n个
思路:跟UVA - 11997 K Smallest Sums
思路是一样的
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> using namespace std; const int maxn = 2005; struct node { int s, b; node(int _s, int _b) { s = _s; b = _b; } bool operator <(const node &a) const { return s > a.s; } }; int n, m; int A[maxn][maxn]; void merge(int a[], int b[], int c[]) { priority_queue<node> q; for (int i = 0; i < n; i++) q.push(node(a[i]+b[0], 0)); for (int i = 0; i < n; i++) { node cur = q.top(); q.pop(); c[i] = cur.s; int cnt = cur.b; if (cnt + 1 < n) q.push(node(cur.s-b[cnt]+b[cnt+1], cnt+1)); } } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d", &m, &n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) scanf("%d", &A[i][j]); sort(A[i], A[i]+n); } for (int i = 1; i < m; i++) merge(A[0], A[i], A[0]); printf("%d", A[0][0]); for (int i = 1; i < n; i++) printf(" %d", A[0][i]); printf("\n"); } return 0; }
POJ - 2442 Sequence,布布扣,bubuko.com
标签:des style blog http os io for art
原文地址:http://blog.csdn.net/u011345136/article/details/38438823