UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997
|
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 优先队列&&打有序表&&归并的更多相关文章
- UVA 11997 K Smallest Sums 优先队列 多路合并
vjudge 上题目链接:UVA 11997 题意很简单,就是从 k 个数组(每个数组均包含 k 个正整数)中各取出一个整数相加(所以可以得到 kk 个结果),输出前 k 小的和. 这时训练指南上的一 ...
- UVa 11997 K Smallest Sums - 优先队列
题目大意 有k个长度为k的数组,从每个数组中选出1个数,再把这k个数进行求和,问在所有的这些和中,最小的前k个和. 考虑将前i个数组合并,保留前k个和.然后考虑将第(i + 1)个数组和它合并,保留前 ...
- uva 11997 K Smallest Sums 优先队列处理多路归并问题
题意:K个数组每组K个值,每次从一组中选一个,共K^k种,问前K个小的. 思路:优先队列处理多路归并,每个状态含有K个元素.详见刘汝佳算法指南. #include<iostream> #i ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
- 优先队列 UVA 11997 K Smallest Sums
题目传送门 题意:训练指南P189 分析:完全参考书上的思路,k^k的表弄成有序表: 表1:A1 + B1 <= A1 + B2 <= .... A1 + Bk 表2:A2 + B1 &l ...
- 【UVA–11997 K Smallest Sums 】
·哦,这题要用优先队列?那大米饼就扔一个手写堆上去吧! ·英文题,述大意: 输入n个长度为n的序列(题中是k,2<=k<=750).一种结果定义为:从每个序列中都要挑选一个数加 ...
- UVA 11997 K Smallest Sums (多路归并)
从包含k个整数的k个数组中各选一个求和,在所有的和中选最小的k个值. 思路是多路归并,对于两个长度为k的有序表按一定顺序选两个数字组成和,(B表已经有序)会形成n个有序表 A1+B1<=A1+B ...
- 【UVA 11997 K Smallest Sums】优先级队列
来自<训练指南>优先级队列的例题. 题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18702 题意:给定 ...
- uva_11997,K Smallest Sums优先队列
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #inclu ...
随机推荐
- 快速创建Django验证码
# 生成随机验证码图片 import stringfrom random import randint, samplefrom PIL import Image, ImageDraw, ImageFo ...
- 每天一个linux命令13之curl发送http请求
一.get请求 curl "http://www.baidu.com" 如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地 curl -i "http:// ...
- iOS 国际化最新最全教程+如何快速国际化一个现成APP
同学面试时遇到一个问题,面试官问他,有一个现成的APP马上要上线了,怎么在不改原来代码,也不改xib.storyboard里的文字的情况下快速实现国际化.这里应同学请求写下此教程.反正国际化的步骤都要 ...
- UI控件---UIWebView
UIWebView是内置浏览器控件,可以用来浏览网页,文档等,今天就试着做一个简易的浏览器! 定义url的初始化方法和返回,前进,刷新三个方法,实现UIWebViewDelegate协议 @inter ...
- jquery获取下拉列表的值和显示内容的方法
页面的下拉列表: 选择时间段: <select name="timespan" id="timespan" class="Wdate" ...
- Linux使用GPT划分大于2T的分区
在Linux下磁盘分区大于2TB的时候我不能使用常用fdisk命令,这时我们就需要先把大容量的磁盘进行转换为GPT格式,然后试用parted命令来划分大于2T的分区. 方法:1.用parted命令分区 ...
- scrapy-splash抓取动态数据例子十
一.介绍 本例子用scrapy-splash抓取活动行网站给定关键字抓取活动信息. 给定关键字:数字:融合:电视 抓取信息内如下: 1.资讯标题 2.资讯链接 3.资讯时间 4.资讯来源 二.网站信息 ...
- RocketMQ之连接以及连接缓存
发现rabbitmq有一个ConnectionFactory.发现rocketmq好像没这个东西.按道理来说如果每次发送消息都新建一条连接肯定是不可能的. ps:其实之所以是有上面的疑问是因为数据库连 ...
- 解决 The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to have it working
事由: 迁移server的时候须要操作数据库.将数据库也进行迁移,在新server中导入数据的时候提示 The 'InnoDB' feature is disabled; you need MySQL ...
- 表单提交时上传图片 表单ajax提交
页面 <script type="text/javascript" src="js/jquery.form.js"></script>& ...