题意:

  有k个整数数组,各包含k个元素,在每个数组中取一个元素加起来,可以得到kk个和,求这些和中最小的k个值

解析:

  从简单的情况开始分析:经典方法,对原题没有思路,那么分析问题的简化版

这是对于两个。。。而对于k个 我们只需要两两处理合并出新数组 然后再与另一个数组合并处理。。依次推理

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int A[maxn][maxn]; 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 mer(int* A, int* B, int* C, int n)
{
priority_queue<node> q;
rap(i, , n)
q.push(node(A[i]+B[], ));
rap(i, , n)
{
node e = q.top(); q.pop();
C[i] = e.s;
int b = e.b;
if(b + <= n)
{
e.s = e.s - B[b] + B[b+];
q.push(node(e.s, b+));
}
}
} int main()
{
int n;
while(cin>> n)
{
rap(i, , n)
{
rap(j, , n)
cin>> A[i][j];
sort(A[i]+, A[i]+n+);
}
rap(i, , n)    //两两合并
mer(A[], A[i], A[], n);
rap(i, , n)
{
if(i != )
cout<< " ";
cout<< A[][i];
}
cout<< endl;
} return ;
}

UVA - 11997(巧妙的优先队列)的更多相关文章

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

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

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

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

  3. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

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

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

  5. UVA 11997 STL 优先队列

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. 优先队列 UVA 11997 K Smallest Sums

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

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

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

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

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

  9. uva 11997 优先队列

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

随机推荐

  1. 探究linux设备驱动模型之——platform虚拟总线(三)最终章

    这篇是最终章了,结束这一章后,对于platform平台总线驱动的使用方法应该是能够无压力掌握.但是这一章涉及的内容会比前面两章多一些. 我们会一步一步地来完善上一章的例子.完善的目的是能够在应用层去控 ...

  2. JavaScript学习笔记(四)——DOM

    第五章 网页交互——文本对象模型[Document object model] 1 简单介绍DOM,dom是将html与javascript进行交互的工具. [使用innerHTML时注意:html中 ...

  3. spring-boot-mybatis搭建

    写在开始 mybatis是一个持久化框架,支持手动sql.存储过程.高级映射.mybatis支持XML方式或注解方式将POJO与数据库表间建立映射. maven依赖 spring-boot.mysql ...

  4. eBay推Winit海外仓 鼓励卖家拓展北美市场

    [亿邦动力网讯]2月11日消息,日前,跨境电商平台eBay与外贸电商服务商万邑通(Winit)合作,针对平台卖家推出了Winit美国海外仓,鼓励卖家拓展北美市场. 亿邦动力网获悉,Winit美国海外仓 ...

  5. hive on hbase 数据表关联

    有时,数据可以容易的存储在hive中,但是要导入到hbase里,可以不用写MR程序来操作,可以使用hive on hbase方式来创建相应的表关联关系来将hive中的数据导入到对应的hbase的表里, ...

  6. JS 数组方法 array数组声明 元素的添加和删除 等

    声明数组 var arr1 = [1,2,3,4,5]; var arr2 = new Array(100); //声明长度为100的arr2数组. arr2=[]; arr2.length = 10 ...

  7. 树状数组怒刷sum!!!(前缀和应用)

    我们知道我们利用树状数组维护的是存到其中的a[ ]数组,但是我们做题需要的是sum[ ]数组,这才是我们真正需要的有用的信息,写这篇博客的目的便是整理一下sum数组是怎么样来应用解题的. 1. Sta ...

  8. C++ map 遍历

    #include <iostream> #include <map> using namespace std; int main(){ map<int,int> m ...

  9. python 为什么没有自增自减符

    >>> b = 5 >>> a = 5 >>> id(a) 162334512 >>> id(b) 162334512 > ...

  10. 智能客服 对话实现--python aiml包

    利用了python的aiml包进行应答 什么是AIML? AIML是Richard Wallace开发的. 他开发了一个叫A.L.I.C.E(Artificial Linguistics Intern ...