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. FreeCodeCamp:Confirm the Ending

    要求: 检查一个字符串(str)是否以指定的字符串(target)结尾. 如果是,返回true;如果不是,返回false. 结果: confirmEnding("Bastian", ...

  2. 杭电oj An easy problem

    </pre><h1 style="color: rgb(26, 92, 200);">An easy problem</h1><stron ...

  3. WEB ICON 的探讨

    一般考虑到webicon 就是cssSprite和自定义font:本文基于下述而总结和进行分析,如有笔误有望指出,谢谢 在线教程:用字体在网页中画ICON图标 http://www.imooc.com ...

  4. MyEclipse中spring MVC的配置

    ---恢复内容开始--- web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app ...

  5. vim插件管理之Vundle

    Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.正是由于其可定制的特性, 许许多多的Vim插件便诞生了.管理这些插件又成为我们最为头疼的问题,最近无 ...

  6. [LeetCode]题解(python):062-Unique Paths

    题目来源: https://leetcode.com/problems/unique-paths/ 题意分析: 给定两个整型m,n.判断从一个m×n的矩阵里面,从(0,0)走到(m-1,n-1)一共有 ...

  7. Web Api Route 注册要放在 Mvc Route 注册前

    今天想研究一下Web Api,写了一个测试Api,打开网站后浏览一下,可是却提示找不到方法,刚开始以为哪里配置错了,可找了半天也没见. 因为我是在一个现有Mvc站点做的Demo,所以打算新建一个Mvc ...

  8. Shell中的正则表达式及字符串处理

    shell里一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式.该模式描述在查找文字主体时待匹配的一个或多个字符串.正则表达式作为一个模板,将某个字符模式与所 ...

  9. Treasure Exploration(二分最大匹配+floyd)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7455   Accepted: 3 ...

  10. Java面试题之一

    下面也将收集一些经典的java面试题,琢磨这些面试题还是非常有好处,可以弄清楚一些容易混淆的知识点,下面面试题的答案部分来自网络,有些来自自己的理解,都是自己精心归纳整理的,有问题的地方,希望大家指出 ...