英文题目:

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

----------------------------------------------------------------------------------------------------------------------------------------------
中文描述:就是有一条河,只有一条船,有n个人,每个人有自己的过河时间,一条船只能载两个人,过河时间算两个人中最大时间那个。算出最少过河时间。 个人理解:首先我做这道题题目时候,这个题目使用贪心策略;
首先我们来分析四个人的情况,因为3、2、1个人的时候很好办;首先有a(最快)、b(次快)、c(次慢)、d(最慢),分两个策略来解决问题。
1.a和b过河,a回来(保证时间最少),c和d过河,b回去(保证时间最少),保证下一次运输时候,a和b来作为来回运输的人,保证时间最少。
2.a和d过河,a回来,a和c过去,a回来。
所以我们只要在n>3个人的时候来比较这两个策略那一个策略时间比较少,然后n-2;直到人数剩下3人一下(包括3个人)。 我们来代码实现:
 #include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<iterator>
#include<string>
#include<string.h>
#include<ctype.h>
#include<map>
#include<stack>
#include<queue>
#include<iostream>
#include<time.h> using namespace std; #define rep(i ,a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i <= b; i--)
int a[]; int main()
{
int t, n, sum;//最简单的基础贪心问题;
scanf("%d", &t);//过河有两种策略;
while(t--)
{
n = ;
sum = ;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d", &a[i]);
}
sort(a, a+n);
while(n > )
{
sum = min(sum+a[]+a[]+a[n-]+a[],sum+a[n-]+a[]+a[n-]+a[]);
n-=;//保证人数过去时时间最大的两个人。
}
if(n == )
{
sum += a[]+a[]+a[];
}
if(n==)
{
sum +=a[];
}
if(n==)
{
sum += a[];
}
printf("%d\n", sum);
}
return ;
}


贪心Crossing 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. 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. poj1700--贪心--Crossing River

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

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

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

  8. Crossing River poj1700贪心

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

  9. 702:Crossing River (贪心)

    [题目描述] N个人过河,一次过去2个回来一个,给出每个人所需时间,问最小过河时间. [题目链接] http://noi.openjudge.cn/ch0406/702/ [算法] 一开始想样例是怎么 ...

随机推荐

  1. od 转储 二进制文件常用命令

    od :  NAME od - dump files in octal and other formats 常用命令: ➜ Downloads od -t x1 -Ax /etc/ld.so.cach ...

  2. etc/skel目录介绍

    /etc/skel目录的作用: /etc/skel目录是用来存放新用户配置文件的目录,当我们添加新用户时,这个目录下的所有文件会自动被复制到新添加的用户家目录下,默认情况下,/etc/skel 目录下 ...

  3. 【MYSQL】语法复习

    一.数据类型 截图来源: http://www.runoob.com/mysql/mysql-data-types.html 二.基本语句 1.创建数据表 -- 主键自增,T_User CREATE ...

  4. 面向对象程序设计_Task7_Summary

    Summary of the ... 题目链接:第七次作业 终于还是迎来了这学期的最后一次作业,唠叨话还是放最后说,先说说计算器这玩意儿吧 贯穿了整个学期的计算器,要是让我对自己做个评价,顶多只是还好 ...

  5. 第二次项目冲刺(Beta版本)2017/12/6

    一.任务明细 二.燃尽图 三.站立式会议 1.照片(随后上传) 2.会议任务安排 四.总结 第一天冲刺,准备工作不足啊,期末了,最近大家都比较忙吧,开始战斗吧.

  6. 未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider,

    未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft ...

  7. [python]如何理解uiautomator里面的 child, child_by_text, sibling,及使用场景

    如何理解uiautomator里面的 child, child_by_text, sibling,我们借助android原生的uiautomatorviewer抓取的控件来进行理解 以如下图进行详细讲 ...

  8. toFixed四舍五入精度校正

    var a = 2.255; var b = a.toFixed(2); console.log(b); 以上代码,按预期正常四舍五入得到结果应该是2.26,但实际返回值为2.25 js浮点数精度作为 ...

  9. C内存开辟与平移

  10. Python基础5

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...