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. 微信小程序开发教程(三)项目目录及文件构成

    第二章我们已经创建了一个Hello WXapplet示例小程序.我们从文件目录结构来了解Hello WXapplet项目的构成. 目录结构显示,在小程序项目的根目录下面包含3个app开头的文件(app ...

  2. ant脚本调用.bat文件

    build.xml内容如下: <project name="example" default="test"> <target name=&qu ...

  3. [BZOJ 2752] 高速公路

    Link: BZOJ 2752 传送门 Solution: 虽然有期望,但实际上就是除了个总数…… 此题计算总代价明显还是要使用对每个$w_i$计算贡献的方式: $w_i的贡献为w_i*(i-l+1) ...

  4. 【动态规划】Gym - 101147H - Commandos

    裸dp,看代码. #include<cstdio> #include<algorithm> #include<cstring> using namespace st ...

  5. iOS开发——检测屏幕旋转

    步骤一.注册通知 1: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrien ...

  6. 从系统相册中选择GIF图片上传到服务器

    -(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)as ...

  7. 可见性-volatile

    出处: http://blog.csdn.net/vking_wang/article/details/9982709

  8. springmvc-框架学习

    https://www.androiddev.net/springmvc-框架学习一-helloworld/

  9. display:flex;多行多列布局学习

    从以前的table布局到现在的div布局,再到未来的flex布局,CSS重构方面对展示行和适应性的要求越来越高: 首先来比较一下布局方式的更新意义: table布局: 优点:1.兼容性好,ie6.ie ...

  10. vertica数据库怎么查看连接数是否已经达到最大值