POJ1700

题目链接:http://poj.org/problem?id=1700
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
 

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

题解:

这是一个过河坐船问题,一共有两个策略
①最快和次快过去,最快回;最慢和次慢过去,次快回,最快的和次快的过去,t1=a[1]+a[0]+a[n-1]+a[1]+a[1]。②最快和最慢过去,最快回;最快和次快过去,最快回,最快的和次慢的过去,t2=a[n-1]+a[0]+a[1]+a[0]+a[n-2]。选择两者中用时较少的一个策略执行,判断t1与t2大小,只需要判断2a[1]是否大于a[0]+a[n-2],如此便将最慢和次慢送过河,对剩下n-2个人循环处理。注意当n=1、n=2、n=3时直接相加处理即可.
#include<iostream>
#include<algorithm>
using namespace std;
int n,a[];
int main()
{
int t,i;
cin>>t;
while(t--)
{
int f=;
//每次f归0
cin>>n; for(i=;i<=n;i++)
cin>>a[i];
sort(a,a+n+);
while(n)
{
if(n==)
{
f+=a[];
break;
}
if(n==)
{
f+=a[];
break;
}
if(n==)
{
f+=a[]+a[]+a[];
break;
}
if(n>)
{
if(*a[]>(a[]+a[n-]))
f+=*a[]+a[n]+a[n-];
else
f+=*a[]+a[]+a[n];
n=n-;
//注意循环
}
}
cout<<f<<endl;
}
return ;
}

POJ1700:Crossing River(过河问题)的更多相关文章

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

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

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

  3. Crossing River

    Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...

  4. Crossing River(1700poj)

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

  5. POJ 1700 Crossing River (贪心)

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

  6. bzoj 1314: River过河 优先队列

    1314: River过河 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 26  Solved: 10[Submit][Status][Discuss ...

  7. poj1700--贪心--Crossing River

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12260   Accepted: 4641 D ...

  8. Crossing River POJ过河问题

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

  9. Crossing River poj1700贪心

    题目描述:N个人过河,只有一只船,最多只能有两人划船,每个人划船速度不同,船速为最慢的人的速度.输入T为case个数,每个case输入N为人数,接下来一行输入的是每个人过河的时间,都不相同.要求输出N ...

随机推荐

  1. Codeforces Round #263 (Div. 1) C. Appleman and a Sheet of Paper 树状数组暴力更新

    C. Appleman and a Sheet of Paper   Appleman has a very big sheet of paper. This sheet has a form of ...

  2. python多线程机制

    Python中的线程从一开始就是操作系统的原生线程.而Python虚拟机也同样使用一个全局解释器锁(Global Interpreter Lock,GIL)来互斥线程多Python虚拟机的使用. GI ...

  3. 带你一分钟理解 JavaScript 闭包

    什么是闭包? 先看一段代码: function a(){ var n = 0; function inc() { n++; console.log(n); } inc(); inc(); } a(); ...

  4. Xcode use Protocol buffer

    http://stackoverflow.com/questions/10277576/google-protocol-buffers-on-ios http://stackoverflow.com/ ...

  5. do while 与while的区别!

    #include "stdio.h" main() { ,b=; do{ //在这里do while 是先完成{}里的运算在判断while()里的循环// a=a+b; b++; ...

  6. 加载GIF动画方法 iOS

    方法一 使用UIWebView _codeStr为gif网址      如果是本地的gif可以直接使用dataWithContentsOfFile方法 NSData *data = [NSData d ...

  7. 常用的Linux操作二

    1.sudo  说明:以系统管理者的身份执行指令,也就是说,经由 sudo 所执行的指令就好像是 root 亲自执行 . 2.who      说明 : 显示系统中有那些使用者正在上面,显示的资料包含 ...

  8. JAVA大数类

    JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...

  9. 在浏览器地址栏按回车、F5、Ctrl+F5刷新网页的区别--转

    其中,在地址栏按回车又分为两种情况.一是请求的URI在浏览器缓存中未过期,此时,使用Firefox的firebug插件在浏览器里显示的HTTP请求消息头如下: Host    192.168.3.17 ...

  10. ios中xib的使用介绍

    ios中Xib的使用 ios中xib的使用 Nib files are the quintessential(典型的) resource type used to create iOS and Mac ...