Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13982   Accepted: 5349

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个人过河时间从小到大排序。p[i];
2.分情况讨论:
⑴当n = 1,直接过河。sum = p[1]
(2)当n = 2,直接过河。 sum = p[2]
(3)当n = 3,无论怎么过河, sum = p[1] + p[2] + p[3] 
(4)当n >= 4,设从小到大排序后位a,b,……,c,d,大于4个人,a,b是最小的两个人,c,d是最大的两个人,目标就是把最大的两个人送过去。就要牺牲最小的。
用最小的来送:A=d + a + c + a = 2a + c + d = 2*p[1] + p[end-1] + p[end];(a,d过去,a回来,a,c过去,a回来)
两小送两大:B= b + b + d + a = a + 2b + d = 2*p[1] + p[end-1] + p[end];(a,b过去,b回来,c,d过去,a回来)
循环:sum = min(A,B),直到n <= 3 时候结束。

刚开始蠢,用函数递归调用,果断超时……

 #include<cstdio>
#include<algorithm>
using namespace std;
int p[];
int min(int a,int b) {return a<b?a:b;}
int time_sum(const int p[],int end)
{
if(end == ) return p[]+p[]+p[];
if(end == ) return p[];
if(end == ) return p[];
int time_1= *p[] + p[end-] + p[end] + time_sum(p,end-);
int time_2= *p[] + p[end] + p[] + time_sum(p,end-);
return min(time_1,time_2);
}
int main()
{
int t,n,time;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&p[i]);
}
sort(p+,p+n+);
if(n == ){
time=p[];
}
else if(n == ){
time=p[];
}
else if(n == ){
time=p[]+p[]+p[];
}
else if(n >= ){
time=time_sum(p,n);
}
printf("%d\n",time);
}
}

后来改了下就好了:

 #include<cstdio>
#include<algorithm>
using namespace std;
int p[];
int min(int a,int b) {return a<b?a:b;}
int main()
{
int t,n,time;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&p[i]);
}
sort(p+,p+n+);
if(n == ){
time=p[];
}
else if(n == ){
time=p[];
}
else if(n == ){
time=p[]+p[]+p[];
}
else if(n >= ){
time=;
int end=n;
while(end >= ){
time+=min( *p[] + p[end-] + p[end] , *p[] + p[end] + p[] );
end-=;
}
if(end == ) time+=p[]+p[]+p[];
else if(end == ) time+=p[];
else if(end == ) time+=p[];
}
printf("%d\n",time);
}
}

POJ 1700 - 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. poj 1700 Crossing River C++/Java

    http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最 ...

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

  5. POJ 1700 cross river (数学模拟)

                                                                                                       ...

  6. 1700 Crossing River

    题目链接: http://poj.org/problem?id=1700 1. 当1个人时: 直接过河 t[0]. 2. 当2个人时: 时间为较慢的那个 t[1]. 3. 当3个人时: 时间为 t[0 ...

  7. Crossing River(1700poj)

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

  8. Crossing River

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

  9. poj1700--贪心--Crossing River

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

随机推荐

  1. PHP常用的缓存技术汇总

    一.数据缓存 这里所说的数据缓存是指数据库查询缓存,每次访问页面的时候,都会先检测相应的缓存数据是否存在,如果不存在,就连接数据库,得到数据,并把查询结果序列化后保存到文件中,以后同样的查询结果就直接 ...

  2. 【代码审计】iZhanCMS_v2.1 前台IndexController.php页面存在SQL注入 漏洞分析

      0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...

  3. PHPMailer命令执行及任意文件读取漏洞

    今天在thinkphp官网闲逛,无意下载了一套eduaskcms,查看了一下libs目录中居然存在PHPMailer-5.2.13,想起了之前看到的PHPMailer的漏洞,可惜这套CMS只提供了一个 ...

  4. 重新生成RF的测试库API文档

    在dos窗口下执行如下命令: 命令:python -m robot.libdoc 库名称  生成的API文件名.html 例如:python -m robot.libdoc MongoDBLibrar ...

  5. Jsoup(二)-- Jsoup查找DOM元素

    一.Jsoup查找DOM元素的方法 getElementById(String id) 根据id 来查询DOM getElementsByTag(String tagName) 根据tag 名称来查询 ...

  6. MongoDB(四)-- 主从配置

    一.前言 虽然MongoDB官方已经不建议使用主从模式了,但是 熟悉下 也是有用的,替代方案是采用副本集的模式.slave默认情况下是不支持读写的,但是master会把数据同步到slave,不支持客户 ...

  7. openjdk源码阅读

    http://rednaxelafx.iteye.com/blog/1549577 http://blog.csdn.net/fancyerii/article/details/7007503 ├—a ...

  8. Memcached 命令行操作

    telnet 用于连接 Memcached: [root@localhost ~]# telnet Trying 127.0.0.1... Connected to 127.0.0.1. Escape ...

  9. Cesium加载影像和地形数据+开启高程遮挡效果+视点定位+定时更新

    // 初始化Cesium var viewer = new Cesium.Viewer('cesiumContainer', { /*imageryProvider : new Cesium.ArcG ...

  10. 第二篇:Hadoop 在Ubuntu Kylin系统上的搭建[图解]

    前言 本文介绍如何在Ubuntu Kylin操作系统上搭建Hadoop平台. 配置 1. 操作系统: Ubuntu Kylin 14.04 2. 编程语言: JDK 1.8 3. 通信协议: SSH ...