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 ...
随机推荐
- 概述struts,以及struts如何实现MVC架构的
概述MVC体系结构? 答:MVC包括三类对象,model是应用对象,view是视图,controller是控制器,它定义用户界面对用户输入的响应方式. 在MVC体系中,模型通常被称为”业务逻辑”,是真 ...
- [Atcoder Regular Contest 062] Tutorial
Link: ARC 062 传送门 C: 每次判断增加a/b哪个合法即可 并不用判断两个都合法时哪个更优,因为此时两者答案必定相同 #include <bits/stdc++.h> usi ...
- 5.6(java学习笔记) queue
一.queue接口 queue的中文意思是队列,是一种以先进先出方式处理数据的集合. 队列还提供额外的插入.提取和检查操作.这些方法都以两种形式存在:一种在操作失败时抛出异常,另一种返回特殊值(根据操 ...
- 十. 图形界面(GUI)设计5.布局设计
在界面设计中,一个容器要放置许多组件,为了美观,为组件安排在容器中的位置,这就是布局设计.java.awt中定义了多种布局类,每种布局类对应一种布局的策略.常用的有以下布局类: FlowLayout, ...
- CentOS 6.9系统时间和硬件时间设置(转)
总结一下hwclock,这个容易晕: 1)/etc/sysconfig/clock 文件,只对 hwclock 命令有效,且只在系统启动和关闭的时候才有用(修改了其中的 UTC=true 到 UTC= ...
- mongodb_profier
http://docs.mongodb.org/manual/reference/database-profiler/ 一.获取.设置profile(profile用collection存储数据) d ...
- javascript(JQuery)元素操作
html代码如下: <div id="picK"> <ul> <li style="float:left;width:90px;" ...
- Python 操作mongodb 简单实例
1.建立数据库用户 要读写 mongo数据库,默认没有用户名和密码也可以对数据库进行读写操作,但是从安全的角度,最好给要操作的数据库建立用户名和密码. 打开mongo数据库服务,打开mongo.exe ...
- tomcat环境部署
环境说明 系统版本 CentOS 7.2 x86_64 软件版本 jdk-8u171 tomcat-8.0.27 1.tomcat介绍及软件包准备 Tomcat是Apache软件基金会 ...
- Android SlidingMenu 使用具体解释
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/36677279 非常多APP都有側滑菜单的功能.部分APP左右都是側滑菜单~Sli ...