英文题目:

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. 高通 display 驱动【转】

    高通display驱动 0. 关键字 MDSS : 高通平台lcd multimedia Display sub system DSI: Display Serial Interface qcom,m ...

  2. Coursera-AndrewNg(吴恩达)机器学习笔记——第三周编程作业(逻辑回归)

    一. 逻辑回归 1.背景:使用逻辑回归预测学生是否会被大学录取. 2.首先对数据进行可视化,代码如下: pos = find(y==); %找到通过学生的序号向量 neg = find(y==); % ...

  3. MySQL SELECT语句中只能输出1000行数据的原因

    同事反映,客户的一套MySQL生产库,执行SELECT.. INTO OUTFILE语句只能导出1000行 最初以为是系统参数被重新设置了,建议他更改系统参数 mysql> set global ...

  4. Orange Pi 3 GPIO 笔记

    这是我写过的最水的文章 设备:Orange pi H6,Pi 3 引脚图: (使用Wiringpi 查看GPIO) +------+-----+----------+------+---+Orange ...

  5. react-native-storage 使用笔记 持续更新

    React-native-storage是在AsyncStorage之上封装的一个缓存操作插件库,刚开始接触这个也遇到了一些问题,在这里简单记录总结一下,碰到了就记下来,持续更新吧 1.安卓下stor ...

  6. Python基础5

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

  7. ElasticSearch + Logstash + Kibana 搭建笔记

    ElasticSearch 安装 1.下载 ElasticSearch,本文使用的版本为 5.5.1. 2.配置 path.data: /data/es #数据路径 path.logs: /data/ ...

  8. shiro实战系列(四)之配置

    Shiro之配置 Shiro 被设计成能够在任何环境下工作,从最简单的命令行应用程序到最大的的企业群集应用.由于环境的多样性,使得许多配置机制适用于它的配置. 一. 许多配置选项 Shiro的Secu ...

  9. pycharm 取消空格,逗号 等符号的自动补全

  10. django博客项目-设置django为中文语言

    找到项目级别里面的setting文件,修改如下配置 """ LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' & ...