UVA - 11997

Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

You’re given k arrays, each array has k integers. There are k^k 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.

The question is from 

id=18702" style="color:rgb(255,153,0); text-decoration:none">here.

My Solution

把每一个数组排序以后打个 有序表

表1: A1 + B1 <= A1+B2 <= A1+B3 <= ``````````

表2: A2 + B1 <= ``````

表3: A3 + B1 <= ``````

第啊张表里的元素形如 Aa + Bb。用(s = Aa+Bb, b) 表示,用s‘ = Aa + B(b+1) =Aa + Bb - Bb + B(b+1) = s - Bb + B(b+1);

#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 760;
int A[maxn],B[maxn]; struct Item {
int s, b;
Item(int s, int b): s(s), b(b) {}
bool operator < (const Item& rhs) const {
return s > rhs.s;
}
}; void merge_pro(int* A, int* b, int n)
{
priority_queue<Item> q;
for(int i = 0; i < n; i++)
q.push(Item(A[i]+B[0], 0));
for(int i = 0; i < n; i++){
Item item = q.top();q.pop();
A[i] = item.s;
int b = item.b;
if(b+1 < n) q.push(Item(item.s-B[b]+B[b+1], b+1)); //this "if" judge is really necessary!
}
} int main()
{
int n;
while(scanf("%d", &n) == 1){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == 0) scanf("%d", &A[j]);
else scanf("%d", &B[j]);
}
if(i == 0)sort(A, A+n);
else sort(B, B+n);
if(i != 0) merge_pro(A, B, n);
}
printf("%d", A[0]);
for(int i = 1; i < n; i++)
printf(" %d", A[i]);
printf("\n");
}
return 0;
}

Thank you!

UVa 11997 K Smallest Sums 优先队列&amp;&amp;打有序表&amp;&amp;归并的更多相关文章

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

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

  2. UVa 11997 K Smallest Sums - 优先队列

    题目大意 有k个长度为k的数组,从每个数组中选出1个数,再把这k个数进行求和,问在所有的这些和中,最小的前k个和. 考虑将前i个数组合并,保留前k个和.然后考虑将第(i + 1)个数组和它合并,保留前 ...

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

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

  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

    题目传送门 题意:训练指南P189 分析:完全参考书上的思路,k^k的表弄成有序表: 表1:A1 + B1 <= A1 + B2 <= .... A1 + Bk 表2:A2 + B1 &l ...

  6. 【UVA–11997 K Smallest Sums 】

    ·哦,这题要用优先队列?那大米饼就扔一个手写堆上去吧! ·英文题,述大意:       输入n个长度为n的序列(题中是k,2<=k<=750).一种结果定义为:从每个序列中都要挑选一个数加 ...

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

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

  8. 【UVA 11997 K Smallest Sums】优先级队列

    来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定 ...

  9. uva_11997,K Smallest Sums优先队列

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> #inclu ...

随机推荐

  1. 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树|可并堆-左偏树)

    2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...

  2. 【费用流】【Next Array】费用流模板(spfa版)

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> using ...

  3. Eclipse的workspace中放入Ext JS卡死或OOM的解决方案

    Eclipse的workspace中放入Ext JS卡死或OOM的解决方案 原因:是由于Ext JS 的所有的文件js验证 方法一:关于Eclipse解决Ext JS卡死方案: 打开Eclipse的w ...

  4. iOS开发——使用Autolayout生成动态高度的TableViewCell单元格

    步骤一.TableViewCell中使用Autolayout 要点:Cell的高度必须在Constraints中指明,但不能定死,需要让内部由内容决定高度的View决定动态高度. 如UILabel设置 ...

  5. jdk_配置

    如果不知道jdk安装目录,可以使用如下命令查找到jdk安装目录 linux /usr/libexec/java_home windows版 新建系统变量JAVA_HOME(自己jdk的安装目录)C:\ ...

  6. 阿里云乌班图16配置-PHP环境(包括mysql及apache安装)

    1. 安装apache  apt-get update apt-get install apache2 service apache2 restart   通过浏览器访问服务器的IP,如果出现Apac ...

  7. 鸟哥的linux私房菜服务器架设篇第五章linux常用网络指令

    ifconfig主要可以手动启动观察修改网络接口的相关参数 ifdown,ifup用来启动和关闭接口,后面直接接接口名称 两部主机两点沟通:ping 两主机之间各节点分析 traceroute 查看本 ...

  8. JS使用cookie实现DIV提示框只显示一次的方法

    本文实例讲述了JS使用cookie实现DIV提示框只显示一次的方法.分享给大家供大家参考,具体如下: 这里运用JavaScript的cookie技术,控制网页上的提示DIV只显示一次,也就是当用户是第 ...

  9. 安装Node.js、npm和环境变量的配置

    由于Node.js平台是在后端运行JavaScript代码,所以,必须首先在本机安装Node环境. 一.安装Node.js 首先,从Node.js官网下载对应平台的安装程序,网速慢的童鞋请移步国内镜像 ...

  10. druid.io使用技术简介: Hyperloglog

    druid.io 使用Hyperloglog 估计基数 参照如下连接 http://blog.codinglabs.org/articles/algorithms-for-cardinality-es ...