Problem K

K Smallest Sums

You're given k arrays, each array has k integers. There are kk ways to pick exactly one element in each array and calculate the sum of the integers. Your task is to find the k smallest sums among them.

Input

There will be several test cases. The first line of each case contains an integer k (2<=k<=750). Each of the following k lines contains k positive integers in each array. Each of these integers does not exceed 1,000,000. The input is terminated by end-of-file (EOF). The size of input file does not exceed 5MB.

Output

For each test case, print the k smallest sums, in ascending order.

Sample Input

3
1 8 5
9 2 5
10 7 6
2
1 1
1 2

Output for the Sample Input

9 10 12
2 2

Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
int ans[MS],a[MS],n;
struct node
{
int s,b;
node(int s,int b):s(s),b(b){}
bool operator <(const node &a)const
{
return s>a.s;
}
}; void merge(int *A,int *B,int *C,int n)
{
priority_queue<node> pq;
for(int i=;i<n;i++)
pq.push(node(A[i]+B[],));
for(int i=;i<n;i++)
{
node t=pq.top();
pq.pop();
C[i]=t.s;
int b=t.b;
if(b+<n)
pq.push(node(t.s-B[b]+B[b+],b+));
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)
scanf("%d",&ans[i]);
sort(ans,ans+n);
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
scanf("%d",&a[j]);
sort(a,a+n);
merge(ans,a,ans,n);
}
for(int i=;i<n;i++)
{
if(i)
printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
return ;
}

D - K Smallest Sums(多路归并+贪心)的更多相关文章

  1. UVa 11997 K Smallest Sums 优先队列&amp;&amp;打有序表&amp;&amp;归并

    UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...

  2. 【暑假】[实用数据结构]UVa11997 K Smallest Sums

    UVa11997 K Smallest Sums  题目: K Smallest Sums You're given k arrays, each array has k integers. Ther ...

  3. UVA-11997 K Smallest Sums

    UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & ...

  4. 11997 - K Smallest Sums(优先队列)

    11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...

  5. uva 11997 K Smallest Sums 优先队列处理多路归并问题

    题意:K个数组每组K个值,每次从一组中选一个,共K^k种,问前K个小的. 思路:优先队列处理多路归并,每个状态含有K个元素.详见刘汝佳算法指南. #include<iostream> #i ...

  6. UVA 11997 K Smallest Sums (多路归并)

    从包含k个整数的k个数组中各选一个求和,在所有的和中选最小的k个值. 思路是多路归并,对于两个长度为k的有序表按一定顺序选两个数字组成和,(B表已经有序)会形成n个有序表 A1+B1<=A1+B ...

  7. UVA 11997 K Smallest Sums 优先队列 多路合并

    vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一 ...

  8. UVA - 11997:K Smallest Sums

    多路归并 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> ...

  9. UVa 11997 (优先队列 多路归并) K Smallest Sums

    考虑一个简单的问题,两个长度为n的有序数组A和B,从每个数组中各选出一个数相加,共n2中情况,求最小的n个数. 将这n2个数拆成n个有序表: A1+B1≤A1+B2≤... A2+B1≤A2+B2≤. ...

随机推荐

  1. index structure

    1. wordlist 0) 0, 1byte 1) token-id(delta), 8byte 2) doclist-offset(delta), 8byte 3) doc_count, 4byt ...

  2. Struts – Wildcards example

    Struts wildcards can helps to reduce the repetition in your struts-config.xml file, as long as your ...

  3. java getEnv不区分大小写 getProperty区分大小写

    System.out.println(System.getenv("JAVA_HOME")); System.out.println(System.getenv("Pat ...

  4. spring properties resolve 问题

    在stackoverflow上看到一个问题 配置如下: <context:property-placeholder location="/WEB-INF/application-cus ...

  5. IMAQdx和IMAQ

    NI-IMAQdx driver software gives you the ability to acquire images with IEEE 1394 and GigE Vision cam ...

  6. [iOS微博项目 - 3.0] - 手动刷新微博

    github: https://github.com/hellovoidworld/HVWWeibo   A.下拉刷新微博 1.需求 在“首页”界面,下拉到一定距离的时候刷新微博数据 刷新数据的时候使 ...

  7. SQL嵌套查寻初识,以及SOME ANY EXISTS的基础常识

    定义: 1 .指在一个外层查询中包含有另一个内层查询.其中外层查询称为主查询,内层查询称为子查询. 2 .SQL允许多层嵌套,由内而外地进行分析,子查询的结果作为主查询的查询条件 3 .子查询中一般不 ...

  8. HDU 3586 Information Disturbing (二分+树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...

  9. 待整理 - BAT文件编写

    BAT文件语法和技巧(bat文件的编写及使用)  http://www.jb51.net/article/5828.htm

  10. word2013中取消句首字母自动大写

    经常使用word的朋友都知道word中一行的首字母会自动大写,这给用户带来方便的同时,也产生了问题,因为有时候我们并不希望每行开头的首字母大写.要取消首字母自动大写可以取消勾选"首句字母大写 ...