Crossing River
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10311   Accepted: 3889

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


思路:(假如 1-n个人时间time[n]。递增排列)
  • 仅仅有一个人的时候:sum=time[1];
  • 二个人的时候:       sum=time[1]+time[2]
  • 三的人的时候:       sum=time[1]+time[2]+time[3]
  • 重点!当大于四个人的时候。为了追求时间最短化。仅仅有两种运送方式:(1) 最快。次快去-->最快回--->最慢。次慢去-->次快   time[2]+time[1]+time[n]+time[n-1]+time[2]
  •                                                                                                          (2) 最快,最慢去-->最快回-->最快。次快去-->最快回     time[n]+time[1]+time[n-1]+time[1]

#include<cstdio>
#include<algorithm> #define maxn 100001
using namespace std;
int time[maxn]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,sum=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",time+i); sort(time+1,time+n+1); while(n)
{
if(n==1)
{
sum+=time[1];
n=0;
}
else if(n==2)
{
sum+=time[2];
n=0;
}
else if(n==3)
{
sum+=time[1]+time[2]+time[3];
n=0;
}
else
{
if(time[2]*2>=time[1]+time[n-1])
sum+=2*time[1]+time[n]+time[n-1];
else
sum+=2*time[2]+time[1]+time[n];
n-=2;
}
} printf("%d\n",sum);
} return 0;
}


POJ 1700 cross river (数学模拟)的更多相关文章

  1. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  2. poj 1700 Crossing River 过河问题。贪心

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9887   Accepted: 3737 De ...

  3. poj 1700 Crossing River C++/Java

    http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最 ...

  4. POJ 1700 - Crossing River

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13982   Accepted: 5349 Description A gr ...

  5. 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 ...

  6. poj 1700

    http://poj.org/problem?id=1700 题目大意就是一条船,有N个人需要过河,求N个人最短过河的时间 #include <stdio.h> int main() { ...

  7. POJ 1700 F(Contest #3)

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

  8. POJ 1700 经典过河问题(贪心)

    POJ题目链接:http://poj.org/problem?id=1700 N个人过河,船每次最多只能坐两个人,船载每个人过河的所需时间不同,问最快的过河时间. 思路: 当n=1,2,3时所需要的最 ...

  9. 1700 Crossing River

    题目链接: http://poj.org/problem?id=1700 1. 当1个人时: 直接过河 t[0]. 2. 当2个人时: 时间为较慢的那个 t[1]. 3. 当3个人时: 时间为 t[0 ...

随机推荐

  1. wpf将表中数据显示到datagrid示例(转)

    原文:http://www.jb51.net/article/47120.htm 这篇文章主要介绍了wpf将表中数据显示到datagrid示例,需要的朋友可以参考下 a.在.xaml文件中拖入一个da ...

  2. centos安装epel源

    用163的源,但是我发现这个源里面,根本没有libmcrypt libmcrypt-devel这二个包,装php扩展mcrypt时,又要用到这二个包,所以我手动装了libmcrypt包,但是给我的感觉 ...

  3. 转:requirejs:让人迷惑的路径解析(~~不错)

    接触过requirejs的童鞋可能都知道,无论是通过define来定义模块,还是通过require来加载模块,模块依赖声明都是很重要的一步.而其中涉及到的模块路径解析,对于新手来说,有的时候会让人觉得 ...

  4. Python web框架有哪些

    简单易学的web.py, 大型的django:文档最完善.市场占有率最高.招聘职位最多. Tornado 具体看:http://feilong.me/2011/01/talk-about-python ...

  5. 1294 - Positive Negative Sign(规律)

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  6. Windows Time服务无法启动 错误5拒绝访问

    接着上次写的文章 XP和Win7设置系统自动同步系统时间方法 本文就把故障出现的过程和解决方法一共写下来,希望大家可以看到本文在解决此项服务的思路.大家以后出现类似的问题和问题可以一样使用此类方法解决 ...

  7. 【Eclipse】修改项目访问名称

    Properties --> Web Project Settings --> Context root --> 输入想要用的名称(默认是项目名)

  8. 写一方法用来计算1+2+3+...n,其中n作为参数输入,返回值可以由方法名返回,也可以由参数返回

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. MacBook外接显示器设置方法(新手入门贴)

    小屏幕的MacBook/MacBook Pro放在桌上长时间使用,眼睛比较累,而且,长时间低头看屏幕,易得颈椎病,绝对有损健康.配一台大屏幕的外置显示器不失为两全其美的好办法. 首先,得买一台中意的大 ...

  10. [Swust OJ 763]--校门外的树 Plus(暴力枚举)

    题目链接:http://acm.swust.edu.cn/problem/0763/ Time limit(ms): 1000 Memory limit(kb): 65535 西南某科技大学的校门外有 ...