Crossing River
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9919   Accepted: 3752

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

ps:http://poj.org/problem?id=1700

/* 此题讲的是N个人过河,每个人都有自己的过河时间,一条船只能承受2个人,所用时间为其中过河时间最多的,

所以呢,想到有两种情况,第一种:过河时间最少的人来回接送其他人,第二种:过河时间最少和次少的人来回接送其他人,

刚开始就觉得第一种时间必然是最少的,但是仔细想想,不然。因为第一种情况虽然单次过河时间少,但送人的次数要多,

如第1个人(过河时间最少)接送第i人 dp[i]=dp[i-1]+time[0]+time[i]; 而第二种情况呢,虽然来回次数多,但是接送的人要多,

如第1个人接第i-1个和第i个人,然后让第i-1个和第i个人一块过河,第2个人再接送第1个人。

dp[i]=dp[i-2]+time[0]+time[i]+time[1]*2。所以每次计算出这两个值都应该进行比较,从而AC!*/

#include<stdio.h>
#include<stdlib.h>
#define N 1010 int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
} int a[N];
int dp[N];
int n; int main()
{
int i,j,t,k;
int min1,min2;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<n;i++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(a[]),cmp);
dp[]=a[];
dp[]=a[]; for(i=;i<n;i++)
{
min1=dp[i-]+a[i]+a[];
min2=dp[i-]+a[]+a[]+a[i]+a[];
dp[i]=min1;
if(min1>min2)//中间是嵌套的,哪种快选哪种
dp[i]=min2;
}
printf("%d\n",dp[n-]);
}
return ;
} /*
3
6
1 2 5 10 15 20
5
1 2 5 10 15
4
1 2 5 10
*/

Crossing River(1700poj)的更多相关文章

  1. 702:Crossing River (贪心)

    [题目描述] N个人过河,一次过去2个回来一个,给出每个人所需时间,问最小过河时间. [题目链接] http://noi.openjudge.cn/ch0406/702/ [算法] 一开始想样例是怎么 ...

  2. ACM学习历程——POJ 1700 Crossing River(贪心)

    Description A group of N people wishes to go across a river with only one boat, which can at most ca ...

  3. POJ 1700 cross river (数学模拟)

                                                                                                       ...

  4. poj-1700 crossing river(贪心题)

    题目描述: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...

  5. bzoj1143 祭祀river(最大独立集)

    [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2175  Solved: 1098[Submit][Status] ...

  6. bzoj1143/2718 祭祀river(最大独立集)

    [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2175  Solved: 1098[Submit][Status] ...

  7. BZOJ 1143: [CTSC2008]祭祀river(最大独立集)

    题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1143 一句话题意:给一个DAG(有向无环图),求选出尽量多的点使这些点两两不可达,输出点个 ...

  8. UVA - 12230 Crossing Rivers (期望)

    Description You live in a village but work in another village. You decided to follow the straight pa ...

  9. 【ZJOI2017 Round1练习】D2T1 river(二分图)

    题意: 思路:这道题并没有官方题解 没有羊驼在所有三元组中出现就是NO 现在考虑不少于1只的情况 删去其中一只,我们得到了两组点和一些边 我们只要判断这是否为一张二分图,使用暴力染色的方法就有60分了 ...

随机推荐

  1. [翻译]Elasticsearch重要文章之四:监控每个节点(jvm部分)

    http://zhaoyanblog.com/archives/753.html 操作系统和进程部分 操作系统和进程部分的含义是很清楚的,这里不会描述的很详细.他们列出了基本的资源统计,例如CPU和负 ...

  2. 关于scanf、getchar、getch、getche缓冲区分析——C语言

    缓冲区 根据数据刷新的时机可以将缓冲区的类型分为:全缓冲.行缓冲.无缓冲 (注意:Windows下的输出设备没有缓冲区,意思是printf是无缓冲的,但是在Linux下printf就是行缓冲的,至于为 ...

  3. python -m SimpleHTTPServer 8080

    启动一个简单的 web 服务器 python -m SimpleHTTPServer 8080

  4. 使用python及工具包进行简单的验证码识别

    相信大家利用 Python 写的爬虫应该遇到过要输入验证码的尴尬局面,又或者写了个自动填充表单的小程序,结果就卡在了验证码上. 在ctf中有⼀一些题⽬目,本身有弱验证码识别绕过,那么我们怎么解决呢? ...

  5. 上下文相关的GMM-HMM声学模型续:参数共享

    一.三音素建模存在的问题 问题一:很多三音素在训练数据中没有出现(尤其跨词三音素) 问题二:在训练数据中出现过的三音素有相当一部分出现的频次较少 因此,三音素模型训练时存在较严重的数据不足问题 二.参 ...

  6. centOS7.10 KDE桌面字体设置推荐

    安装完centOS7.10的KDE桌面后,第一次使用觉得字体太难看了,特别是终端,看着很难受,调整多次后觉得如下设置舒服很多,分享出来以供参考. 其中等宽字 这样整体看着就会舒服很多 ******** ...

  7. c++之window.h

    在c++中引入window.h头文件. Sleep函数,此函数接受一个时间参数,单位是ms.即使得程序在一段时间后继续运行.如下: 在hello输出之后3000ms,才会继续输出world字符串. M ...

  8. C++:运算符重载

    运算符重载是一种形式的C++多态.运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义.实际上,很多C++运算符已经被重载.eg:将*运算符用于地址,将得到存储在这个地址中的值,将他用于 ...

  9. flex布局下, 内容改变 不重新渲染问题

    当使用flex布局时,flex内元素包含的内容改变时,浏览器不会进行重新渲染, 答案引用 http://stackoverflow.com/questions/23474191/flexbox-hei ...

  10. Mac下安装Eclipse和Tomcat等

    Mac下做Java开发还是很方便的,不用像.NET开发一样在Parallel Desktop里面安装Windows虚拟机,Mac下面默认已经安装了JDK. 当然,你如果要安装JDK7,请先阅读:htt ...