POJ 1700 Crossing River(贪心)

【贪心算法c++】---Crossing River 题目: 几个人过河,每次过两人一人回,速度由慢者决定,问过河所需最短时间。 【输入】 输入t组数据,每组数据第1行输入n,第2行输入n个数,表示每个人过河的时间。 【输出】 输出t行数据,每行1个数,表示每组过河最少时间。 【输入样例】 1 4 1 2 5 10 【输出样例】 17 思路: 我最开始认为是最快的和最慢的一起过河,最快的回来,可是并不得17, 后来发现其实是 1和2过河,1回来 用时... 阅读详情

V - Crossing River
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.

Output

For each test case, print a line containing the total number of seconds required for all the N people to cross the river.

Sample Input

1
4
1 2 5 10

Sample Output

17


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i < b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N  5005

int a[N],n;

int main()
{
	int i,j,t;
	sf(t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
			sf(a[i]);
		if(n==1)
		{
			pf("%d\n",a[0]);
			continue;
		}
		int ans=0;
		sort(a,a+n);
		int e=n-1;
		while(1)
		{
			if(e==1)
			{
				ans+=max(a[0],a[1]);    //两个人
				break;
			}
            if(e==2)
			{
				ans+=a[0]+a[1]+a[2];   //三个人
				break;
			}
			//送最慢两个人过河
			ans+=min(a[e]+a[0]+a[e-1]+a[0],a[1]+a[e]+a[0]+a[1]);   //0 e 去 0回  0 e-1  去,0回
			e-=2;                                                  //0 1 去,0回,e,e-1去,1回
		}
        pf("%d\n",ans);
	}
  return 0;
}








信息学奥赛一本通C++语言——1232:Crossing River 【题目描述】 几个人过河,每次过两人一人回,速度由慢者决定,问过河所需最短时间。 【输入】 输入t组数据,每组数据第1行输入n,第2行输入n个数,表示每个人过河的时间。 【输出】 输出t行数据,每行1个数,表示每组过河最少时间。 【输入样例】 1 4 1 2 5 10 【输出样例】 17 【源代码】 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define 阅读详情

相关推荐

【POJ】POJ-1700 Crossing River 贪心算法

一、题目 A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back ...

m0_46204129的博客 435

信息学奥赛一本通 1232:Crossing River 贪心算法

输入t组数据,每组数据第1行输入n,第2行输入n个数,表示每个人过河的时间。时间限制: 1000 ms 内存限制: 65536 KB。几个人过河,每次过两人一人回,速度由慢者决定,问过河所需最短时间。输出t行数据,每行1个数,表示每组过河最少时间。

长春高老师编程 1005

C程序-——Crossing River

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.

Crossing River / 过河问题(贪心)

贪心算法

善良的小乔的博客 850

对冲策略与马丁结合ea的编程实践(五)核心是把两个品种合成一个新品种

在外汇、数字货币、期货交易甚至赌博中,马丁策略、网格策略是最常用的策略,也是最容易被理解的策略,但是如何克服单边行情、极端行情,是使用马丁策略最为头疼的事情,如加大间距、加大资金量,只能缓解,并不能根本解决这样的情况,所以使用时大部分时候都心里没底。 有没有一种正确的思路,让行情控制在震荡范围以内呢,这是今天我要向大家分享的一个思路,那就是对冲。大家看下图: 大家可以看到欧美(EURUSD)和镑美(GBPUSD),这两个品种,大部分时候走势是一致的,那么我们有没有可能把他们两个品种进行一下合成,比如买0.

omidsky的博客 3738

深度学习二分类实战:用Python+Keras搞定基因表达数据预测(附完整代码)

本文详细介绍了如何使用Python和Keras构建深度学习模型进行基因表达数据的二分类预测。通过数据预处理、特征选择、神经网络构建及优化策略,特别针对小样本场景提供了调参技巧和模型解释方法。文章还包含完整的代码示例,帮助读者快速掌握深度学习在生物信息学中的应用。

weixin_30642561的博客 245

Crossing River 贪心

传送门 参考博客 参考博客 A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row th...

pioneer1 276

Labwindows CVI写上位机与STM32下位机通信(二)

接着上篇,这篇主要谈谈流量计数据读取。我们使用的MEMS流量计采用Modbus协议Modbus 使用RS-485 作为硬件载体。 一. Modbus协议   该协议定义了 Modbus 总线Master(主设备)与Slave(从设备)之间的通讯报文格式,对于主设备来说,Modbus 协议是联系上位机(如PC、PLC、HMI 等)的接口,而且所有的通讯都是“透明的”。控制器通信使用主-从技术,即

yang2011079080010的专栏 1万+

POJ1700 Crossing River【贪心】

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16966   Accepted: 6416 Description A group of N people wishes to go across a river with only o

海岛Blog 1972

从Gcode命令看3D打印机的‘大脑’:Marlin/Klipper固件是如何执行你的指令的?

本文深入解析3D打印机固件Marlin和Klipper如何执行Gcode指令,从语法解析到运动控制算法,再到温度管理,详细揭示了从指令到物理动作的转化过程。通过对比两大固件的设计哲学和性能优化技巧,帮助用户更好地理解和调优3D打印机的核心控制系统。

weixin_26763955的博客 382

SimpleFOC STM32实战:基于F103与HAL库,剖析速度环与电流环的协同控制

本文详细介绍了基于STM32F103与HAL库的SimpleFOC实战应用,重点剖析速度闭环控制与电流环的协同工作原理。通过开发环境搭建、参数调试技巧及性能优化方案,帮助开发者高效实现无刷电机精准控制,适用于机器人、无人机等高精度应用场景。

weixin_29170327的博客 476

POJ 1700 Crossing River【经典贪心】

传送门:POJ 1700 Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22561 Accepted: 8285 Description A group of N people wishes to go across a river with only one boat, which can ...

Waiting For You 227

PCL 点到面的ICP算法【2024最新版】

经典的点到面的ICP算法。博客长期更新,本文最新更新时间为:2024年10月18日。

点云侠的博客 36万+

【POJ1700】Crossing River 贪心,附贪心问题的一系列详细解析

题意:一群人过河,船每次只能装两人,每次过河时间为两人权值较大的那个。 题解:     这种题的贪心策略往往不是很好想,这个时候我们就需要依照尽量逼近正解的思路,进行多种贪心,在每种贪心都保证正确的前提下,取多个答案的最值,这样往往就是正解,而即便可以卡,数据也很难出,并不是写个rand+debug拍上两个小时就能拍出来的。     而这种 多线程贪心 可以有两种: 一、单独做每种,然后取

辗转山河弋流歌 1906

小梅哥Xilinx ZYNQ学习笔记2——38译码器

译码器(Decoder)是一种多输入多输出的组合逻辑电路,负责将二进制代码翻译为特定的对象(如逻辑电平等),功能与编码器相反。译码器一般分为通用译码器和数字显示译码器两大类。本设计的是通用译码器。三八译码器,即是 3 种输入状态翻译成 8 种输出状态。1.case开始开始,endcase结束;2.数字声明时,合法的基数格式有 4 中,包括:十进制('d 或 'D),十六进制('h 或 'H),二进制('b 或 'B),八进制('o 或 'O)。数值可指明位宽,也可不指明位宽。

m0_51430584的博客 1635

可维护的 SAP UI5 Theming 策略:从 CSS 级联与特性选择器到 UI Theme Designer 的工程化落地

摘要:SAP UI5主题化是一个涉及CSS级联、选择器特异性和主题参数的系统工程,与应用逻辑解耦以实现品牌化和无障碍适配。轻量级调整优先使用附加CSS,重大改动才考虑创建新主题。样式冲突需通过特异性原则解决,避免滥用!important。动态添加样式应使用addStyleClass而非直接操作DOM。主题可通过bootstrap参数、URL或运行时API设置,企业级主题推荐使用UI Theme Designer。自定义内容应消费主题变量以实现一致性,避免直接修改控件内部结构或内联样式。工程化落地需规范分层和

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 307

poj1700Crossing River【贪心】

Language: Default Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12387   Accepted: 4702 Description A group of N people wishes to go acro

走一步再走一步 684

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

poj 1700 Crossing River 【贪心】

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11557   Accepted: 4368 Description A group of N people wishes to go across a river with

世界很大 509

深度学习工具包 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")

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

POJ 1700-Crossing River(贪心)

Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24945 Accepted: 9155 Description A group of N people wishes to go across a river with only one boat, which can at most carry...

在读NLP硕士 764

poj 1700 Crossing River(贪心·dp)

题目:http://poj.org/problem?id=1700 Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12177   Accepted: 4613 Description A group of N peopl

theArcticOcean 587

POJ 1700 Crossing River(贪心)

题意:过河问题、有N个人要渡河,但是只有一艘船,船上每次最多只能载两个人,渡河的速度由两个人中较慢的那个决定,小船来回载人直到所有人都渡河,求最短的渡河时间。 题目链接:http://poj.org/problem?id=1700 solution:贪心策略有两种 假设有速度最快的人a、速度次快的人b,速度最慢的人c,速度次慢的人d,把c和d送到终点考虑两种策略: 1、 a和b出发,a回来,c和...

weixin_44014982的博客 216

poj1700 Crossing River 贪心

题目链接:http://poj.org/problem?id=1700 //pku1700 //题目的意思:是若干个人要过河,只有一只船,每次最多只能坐两个人 //船速以较慢的那个算,给出每个人的过河时间,求最少过河时间 //代码: #include&lt;iostream&gt; #include&lt;algorithm&gt; using namespace std; int main(...

java开发指南博客 【转载】 150

POJ1700 Crossing River(贪心)

题意: 经典的过河问题,一群人要过河,只有一艘船,船最多坐两个人,船速为两人中较慢的那个,问把所有人都运到对岸最少要多长时间。 要点: 这题自己想真挺难,看了一下网上的方法,基本上是这样,当人数n=1,2,3时,最小时间是固定的,而n大于3时,可以先考虑把最慢的两个人运过去,有以下两种方法:一:用最快的人先把最慢的运过去,再自己划船回来,再把次慢的运过去,再自己回来;二:最快的和次快的先划船

SeasonJoe的博客 406
上一篇: HDU 1800 Flying to the Mars(字典树)
下一篇: POJ 3253 Fence Repair(贪心)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值