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. C函数调用与栈

    这篇blog试图说明这么一个问题,当一个c函数被调用时,一个栈帧(stack frame)是如何被建立,又如何被消除的.这些细节跟操作系统平台及编译器的实现有关,下面的描述是针对运行在Linux的gc ...

  2. Servlet乘法表学习笔记

    一.控制台实现乘法表 package com.shanrengo; import java.io.IOException; import java.io.PrintWriter; import jav ...

  3. 软件源(Software Sources)

    写在前面:浏览了很多国内外的网站,看了很多关于软件源(Software Sources)设置的文章,发现有很多文章中对软件源的设置存在误解,为了让新人能顺利进入Ubuntu的大家庭,特地作此文,详细地 ...

  4. (IOS)Apple 证书相关

    1.私钥 本地钥匙串程序创建<证书请求文件>(.certSigningRequest),用其向苹果申请下载<证书文件>/<私钥>(.cer),并安装到钥匙串: 团队 ...

  5. 7,C++ public, protected, private 继承的区别

    在某处看到一张图,简单明了的说明了三者的关系,很是佩服,遂记录下来. //公有继承 对象访问 成员访问 public --> public Y Y protected --> protec ...

  6. Windows Phone 8初学者开发—第8部分:理解编译和部署

    原文 Windows Phone 8初学者开发—第8部分:理解编译和部署 第8部分:理解编译和部署 原文地址: http://channel9.msdn.com/Series/Windows-Phon ...

  7. vs2010根据字符串内容添加断点

    在vs中我们可以直接用表达式.数值型比较直接用操作符即可. 如i==2,i<2; 但是字符型比较呢? 加入我们有一个名为string的变量,定义如下: char *string="Tw ...

  8. kinect for windows - DepthBasics-D2D详解之一

    Depth在kinect中经常被翻译为深度图,指的是图像到摄像头的距离,这些距离数据能让机器知道物理距离有多远.kinect通过两个红外摄像头来实现这个功能的.在这个例子里,就实现了深度图的提取和现实 ...

  9. 解决warning: incompatible implicit declaration of built-in function &#39;malloc&#39;

    因为代码中使用了malloc函数和字符串函数.编译时出现错误 warning: incompatible implicit declaration of built-in function 'mall ...

  10. MySQL 讨厌哪种类型的查询

    ㈠ 任何查询都讨厌             只要是查询.MySQL都讨厌.执行查询最快的方式就是不去执行        缓存为王.比如Redis或者memcache               ㈡ 查 ...