UVA11997 K Smallest Sums(思维题/优先队列)

全志Longan和Tina系统T113-S3-(Tina Linux 系统介绍) Tina Linux是全志科技基于Linux内核开发的针对智能硬件类产品的嵌入式软件系统。Tina Linux基于openwrt-14.07 版本的软件开发包,包含了 Linux 系统开发用到的内核源码、驱动、工具、系统中间件与应用程序包。Tina系统软件架构如图所示。从下至上分别为Kernel && Driver、Libraries、System Services、Applications 四层。 阅读详情

题目

k(2<=k<=750)个数组,每个数组k个元素,

从每个数组取1个元素,所组成的k元组之和,共有k^{k}种可能

现在要求,输出其中的前k小值

思路来源

https://www.cnblogs.com/yyf0309/p/7797202.html

题解

两两合并,每两个合并的复杂度O(klogk),则总复杂度O(k²logk)

对第二个数组排好序后,考虑两个的合并过程,

优先队列内放入数对(第一个的值,第二个的当前位置0)

那么,第二个数组内位于pos+1的元素被取,

当且仅当在固定第一个数组的值的情况下,第二个数组内pos位置的元素被取

所以,就类似k短路的过程,优先取近的,再把一步可达的加进优先队列,

优先队列,自然是按对答案的贡献重载运算符

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn=755;
int k;
int a[maxn],b[maxn];
struct node
{
	int v,pos;
	node(){}
	node(int vv,int p):v(vv),pos(p){}
}t;
bool operator<(node c,node d)
{
	return c.v+b[c.pos]>d.v+b[d.pos];
}
priority_queue<node>q;
void unite()
{
	while(!q.empty())q.pop();
	for(int j=0;j<k;++j)
	q.push(node(a[j],0));//a的数值 b的下标 
	int cnt=0;
	while(!q.empty()&&cnt<k)
	{
		t=q.top();q.pop();
		//b只有这个下标被取了 下一个才有被取的可能 
		a[cnt++]=t.v+b[t.pos];
		if(t.pos+1<k)q.push(node(t.v,t.pos+1));
	}
}
int main()
{
	while(~scanf("%d",&k))
	{
		for(int j=0;j<k;++j)
		scanf("%d",&a[j]);
		sort(a,a+k);
		for(int i=1;i<k;++i)
		{
			for(int j=0;j<k;++j)
			scanf("%d",&b[j]);
			sort(b,b+k);
			unite();
		}
		for(int i=0;i<k;++i)
		printf("%d%c",a[i],i==k-1?'\n':' ');
	}
	return 0;
}

 

【数据集】CAMS 全球生物质燃烧排放 GFAS(基于火灾辐射功率) CAMS 全球生物质燃烧排放 GFAS(基于火灾辐射功率) 阅读详情

相关推荐

STM8S全解析之系列文章4---STM8S芯片项目升级引导程序(使用中断接收升级数据)

STM8S全解析之系列文章4—STM8S芯片项目升级引导程序(使用中断接收升级数据) 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 例如:第一章 Python 机器学习入门之pandas的使用 文章目录STM8S全解析之系列文章4---STM8S芯片项目升级引导程序(使用中断接收升级数据)前言一、Flash划分boot和App二、使用步骤1.引入库2.读入数据总结 前言 用过STM8S都知道它没有中断向量重定向,也就意味着STM8S的中断向量没办法指向其他的位置,间接说明升级的引导

weixin_39853312的博客 1037

uva 11997(priority_queue)

题意:有k个组,每组k个数,要从每组挑出一个数,k个组相加,一共有k^k种和,要求升序输出k个最小的和。 题解:书上的题解的方法是用优先队列和两两归并,先看两个排好序的数组,(s, b)代表一个元素和,让s = Aa+Bb,那么下一个可能元素和就是(s', b + 1),s' = s - Bb + Bb+1,然后优先队列每次取出队首就是此时队伍中和最小的情况,然后得到s'入队,因为是排好序就不会

俯瞰风景 661

基于FPGA的TDC延迟链优化与码密度校准方法(1)——原理与实现

本文深入探讨了基于FPGA的时间数字转换器(TDC)延迟链非均匀性问题及其码密度校准方法。通过分析工艺偏差、温度电压影响等根源,详细介绍了码密度测试原理、逐位校准技术及气泡消除算法,并分享了自适应校准策略与多场景应用实践,有效提升TDC测量精度至皮秒级。

kiwi8的博客 494

UVa 11997 K个最小和(训练指南)

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

Learn as if you were to live forever 805

从零到一:MIPI D-PHY测试环境的搭建与优化实战指南

本文详细介绍了MIPI D-PHY测试环境的搭建与优化实战指南,涵盖核心设备选型、测试点选取、接地屏蔽技巧、校准流程及自动化测试方案。特别针对D-PHY协议中的关键测试项进行深度解析,并提供疑难问题排查与性能优化建议,帮助硬件工程师高效完成高速差分信号测试。

lll78的博客 637

OpenIddict实战:5步搞定.NET Core应用的OIDC单点登录集成

本文详细介绍了如何使用OpenIddict在.NET Core应用中快速实现OIDC单点登录集成。通过五个关键步骤,包括环境准备、数据库集成、客户端注册、令牌配置和多应用SSO集成,帮助开发者高效构建安全的企业级单点登录解决方案。

lambda的博客 731

靶形数独

我终于AC了!; 毒瘤卡常题 #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pra...

448

FTPClient使用中,碰到SocketTimeoutException 坑

ftpclient设置soTimeout无效

u010379794的博客 1955

【UVA11997】K Smallest Sums 优先队列的多路归并问题

背景 给你个有序列表(假设非降序),将其合并为一个列表(这为《算法导论》上堆部分一道例题) 一种策略是建立一个大小为的小根堆,每个序列第一个元素入堆,标记每个元素所属队列. 依次取出,取出后若对应序列还有元素,则加入堆中否则不加入或者加入. PS:归并排序的归并过程就可以看作是大小为的一个小根堆进行合并的操作. 问题 有个序列,每个序列有个元素。现在要在每个序列里选一个元素出来求和,故...

Hardict的算法日记 414

嵌入式接口通识知识之PCIe接口

PCIe x1接口是最常见的一种,插槽的长度是最短的,仅有25mm。PCIe x4接口有四个数据通道,提供比x1接口更高的带宽,插槽的长度为39mm,分为前后两组,一组22根针脚用于供电,剩下的42根针脚是数据针脚。插槽全长89mm,有164根针脚,分为前后两组,较短的插槽有22根针脚,主要用于供电,较长的插槽142根,主要用于数据传输,具有16通道所带来的高带宽。PCIe x8接口有八个数据通道,插槽全长56mm,有98跟针脚,分为前后两组,较短的22根针脚用于供电,剩下的76根针脚是数据针脚。

2301_76587520的博客 1312

RTCA DO-160G标准详解:航空电子设备的全面环境适应性测试指南

第二章的主要目的是提供标准中使用的术语和定义。了解这些术语对于准确理解标准的要求至关重要,特别是在技术实施过程中。RTCA DO-160G 涉及多个学科的知识,包括航空、电子、电磁学和机械工程,因此需要确保使用的术语在所有学科中具有一致的定义。为了确保测试人员和工程师在执行测试时对所有条件和要求的理解一致,RTCA DO-160G 标准中定义了大量术语。环境应力:指的是设备在操作过程中可能遇到的外部物理或化学条件,包括温度、湿度、振动等。电磁干扰(EMI)

qq_41854911的博客 5301

UVA11997 K Smallest Sums 题解

博客观赏效果更佳 题意简述 一个大小为nnn的方阵,每行都选一个数,得到一个和。一共能得到nnn^nnn个和。求出最小的前nnn个。重复的算多次。n<=700n<=700n<=700。 思路框架 每两行都来一个多路归并即珂。 具体思路 今天去看了刘汝佳的小蓝书。感谢刘汝佳,让我知道优先队列原来还能这么用。 首先,每行里面顺序不重要,先排序再说。 前置知识:n路归并问题 我们应该都...

LightningUZ的博客 262

嵌入式SDIO 总线面试题及参考答案

SDIO 的 4 位总线模式是一种提高数据传输效率的工作模式。在传统的 1 位总线模式下,数据只能通过一根数据线(DAT0)进行传输,这限制了数据传输的带宽和速度。而在 4 位总线模式下,DAT0 - DAT3 这 4 根数据线同时用于数据传输,相当于在同一时间内可以传输 4 倍于 1 位模式的数据量,从而显著提高了数据传输的带宽和速度。要通过 CMD6 命令将 SDIO 总线从当前模式切换到 4 位总线模式,需要遵循以下步骤:第一步,主设备需要确保 SDIO 卡支持 4 位总线模式。

大模型大数据攻城狮的专栏 1725

Uva11997 K Smallest Sums

题目: You’re given k arrays, each array has k integers. There are kk 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 t

Freddddd 448

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

Uva 11997 - K Smallest Sums(优先队列+思维)

题目链接 https://vjudge.net/problem/UVA-11997 【题意】 有k个整数数组,每个数组都包含k个元素,在每个数组中各取出一个元素并求和,可以得到k^k个和(重复的值算多次),求这些和中的最小的k个值分别是多少 【思路】 大白189页例题,这题感觉看着不难,但搞半天搞不出来的那种。其实不用一下子考虑k个数组,我们先只考虑两个数组A和B,长度都是n,在A和B中任...

Change the world by program. 238

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

UVA 11997 K Smallest Sums

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3148 虽然是水题,需要用到优先队列...但还是考一点思维 题意是找出n*n的矩阵中,每行中取一个数求sum的前k小值... 可以很容易想到是求两行中的前k小然后合并....

weixin_30906671的博客 69

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Uva11997——优先队列,多路合并

题意:给出k个长度为k的整数数组,每个数组里面取一个元素加起来,共有k^k个和,输出这些和的前k个(升序排序后)。 经典的多路合并问题,用一个优先队列维护k个值,每次pop出最小值之后将其下一个值加入队列。但是这题有k个数组,我们可以每次合并两个数组,求出前k个(后面的值我们肯定用不着了),接着再和下一个数组合并。 #include #include #include #include

cool_Fires的专栏 1068

UVA 11997--K Smallest Sums+优先队列用于多路归并

题目链接:点击进入 先考虑两个数组A,B的情况,这样总共有n^2种情况;将A,B数组排序后,我们可以将所有情况组织成n张表: 表1: A[1]+B[1]<=A[1]+B[2]<=……<=A[1]+B[n]. 表2: A[2]+B[1]<=A[2]+B[2]<=…….<=A[2]+B[n]. ……. 表n: A[n]+B[1]<=A[n]+B[2]<=……..<=A[n]+B[n] 这n张

荒野孤鹰 1483

【UVA11997】K Smallest Sums

题意   有nn个序列,每个序列有nn个元素。现在要在每个序列里选一个元素出来,求元素总和前nn小的值 解法 优先队列:   最简单的想法就是直接枚举所有可能的方案,然后排序求出前nn小的方案   我们用一个nn元组来表示一种组合   首先我们把每个序列从小到大排好序,那么最小的组合就是(1,1,…,1)了,至于第二小,在某个

清疚的博客 383

Uva-11997-K Smallest Sums

题目要求是从k个数组中各选一个数组成的和中最小的k个。 用优先队列做,注意合并的时候可以做一些适当的优化操作,因为最开始已经对每个数组进行了排序,所以每一次更新只需要从当前优先队列中取出第一个数,然后进行更新操作即可。更新为删除当前相加的值,而加上当前值后面的一个值。 代码: #include #include #include #include #include #define MAX 7

z309241990的专栏 1654

UVA - 11997(巧妙的优先队列)

题意:   有k个整数数组,各包含k个元素,在每个数组中取一个元素加起来,可以得到kk个和,求这些和中最小的k个值 解析:   从简单的情况开始分析:经典方法,对原题没有思路,那么分析问题的简化版 这是对于两个。。。而对于k个 我们只需要两两处理合并出新数组 然后再与另一个数组合并处理。。依次推理 #include <iostream> #include ...

babing2770的博客 213

[uva11997]k个最小和

[uva11997]k个最小和 一个k*k的矩阵,每行选取一个数相加则得到一个和,求最小的前k个和。 k&lt;=750 已知前m行最小的前k个和d[1]…d[k],则前m+1行最小的前k个和都必定是d[i](i&lt;=k)+a[m+1][x]。排序,枚举x,用优先队列处理。 学会了个小技巧: node形式的优先队列,想直接插入元素...

qq_25768269的博客 199

uva11997

此题看英文貌似比书上翻译简单。给k个数组,每个数组k个元素,那么组合就有k^k种方案,然后每个数组中选一个那么就有一个和,求和最小的k个 此题用书上的合并不失为一个好方法。但是贪心应该也可以吧? 我觉得书上说k^2logk是不是少了应该大于这个复杂度,因为优先队列的需要时间把?应该按照堆排的效率来看的吧? 将二维数组只开两个1维估计会节约很多空间,时间也会快些(不用申请那么多空间的时

lyc 894

UVA 11997

K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status]   Description Problem K K Smallest Sums

1690

K Smallest Sums UVA - 11997(优先队列、打表)【详解】

DescribeYou’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 a...

既然弱小,就只顾变强就是了 509
上一篇: poj1723 SOLDIERS(思维题-中位数/货仓选址问题)
下一篇: Educational Codeforces Round 62 (Rated for Div. 2) F. Extending Set of Points(可撤销并查集+线段树分治)
小衣同学
博客等级 码龄9年 1444粉丝 995原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小衣同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址