博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj-1700 crossing river(贪心题)
阅读量:4979 次
发布时间:2019-06-12

本文共 1629 字,大约阅读时间需要 5 分钟。

题目描述:

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

141 2 5 10

Sample Output

17 题目大意:过河,速度取慢的 题解:有点像汉诺塔的感觉,贪心算法,直接看代码吧,大水题
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define PI 3.14159265358979323846264338327950int T,n,a[1003],t;int main(){ scanf("%d",&T); while(T--) { t=0; scanf("%d",&n); memset(a,0,sizeof(a)); for(int i=0;i
0;i=i-2) { if(i>=3) { if(a[0]+a[i-1]>=2*a[1]) t=t+a[0]+2*a[1]+a[i]; else t=t+2*a[0]+a[i]+a[i-1]; } if(i==2) t=t+a[0]+a[1]+a[2]; if(i==1) t=t+a[1]; } } printf("%d\n",t); }}

 

转载于:https://www.cnblogs.com/smallhester/p/9498705.html

你可能感兴趣的文章
C语言中volatilekeyword的作用
查看>>
Visual Studio 2010 配置Ogre
查看>>
ecstore小记
查看>>
【例3.6】过河卒(Noip2002)
查看>>
Spring 事务入门
查看>>
Codeigniter MongoDB类库
查看>>
Java设计模式——单例模式
查看>>
hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005
查看>>
基于lnmp.org的xdebug安装
查看>>
redisTemplate如何注入到ValueOperations
查看>>
增加列并修改列的顺序
查看>>
国庆七天乐 Day2
查看>>
各种图论模型及其解答(转)
查看>>
kafka消息的可靠性
查看>>
一些软件设计的原则
查看>>
.NET平台开发Mongo基础知识
查看>>
Git:多人推送/抓取分支事项
查看>>
【c++ templates读书笔记】【2】类模板
查看>>
static的用法
查看>>
ubuntu16.04安装网易云音乐
查看>>