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. HDU1251 统计难题(Trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  2. wordpress 404 error on all pages!

    You have to enable mod_rewrite in apache itself or you won't be able to have permalinks the way you ...

  3. Struts2接收参数的几种方式

    一.用Action属性 在action里定义要接收的参数,并提供相应的set和get方法. 如: public class LoginAction extends ActionSupport { pr ...

  4. TeleMCU视频会议之Android版本号WebRTC client支持

    本文原创自 http://blog.csdn.net/voipmaker  转载注明出处. 最新版本号TeleMCU 添加了Android手机端WebRTC视频会议能力,Android手机安装Chro ...

  5. 改写URL的查询字符串QUERY_STRING(转)

    查询字符串是指URL请求中“问号”后面的部分.比如,http://www.nowamagic.net/?foo=bar中粗体部分就是查询字符串,其中变量名是foo,值是bar. 1. 利用QSA转换查 ...

  6. 第一篇:数据工程师眼中的智能电网(Smart Grid)

    前言 想必第一次接触到智能电网这个概念的人,尤其是互联网从业者,都会顾名思义的将之理解为"智能的电网". 然而智能电网中的"智能"是广义上的智能,它就是指更好的 ...

  7. C#使用DataSet类、DataTable类、DataRow类、OleDbConnection类、OleDbDataAdapter类编写简单数据库应用

    //注意:请使用VS2010打开以下的源代码. //源代码地址:http://pan.baidu.com/s/1j9WVR using System; using System.Collections ...

  8. Android(java)学习笔记237:多媒体之图形的变化处理

    1.图形的缩放 (1)布局文件activity_main.xml如下: <LinearLayout xmlns:android="http://schemas.android.com/ ...

  9. U口破解指令介绍.

  10. 如何开通www国际域名个人网站

    欢迎访问我的域名http://www.baiqiantao.xyz 一.准备知识 什么是域名? 所有以www开头的域名,就是国际域名:不以www开头的域名,都是为子域名. 域名都有后缀,后缀是有意义的 ...