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. Disconf (version : 2.6.21)

    通常我们会做如下配置:(disconf 2.6.21) <!-- 一次扫描 --> <bean id="disconfMgrBean" class="c ...

  2. LeetCode[Array]----3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  3. Jquery Ajax 和json用法

    向您的页面添加 jQuery 库 jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数. 可以通过下面的标记把 jQuery 添加到网页中: <head& ...

  4. 诡异的DataTime.Now.ToString()

    昨天晚上调程序的时候在服务器上出现这种问题 DataTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 居然出现了2014-8-14 8:nn:14: ...

  5. flexbox父盒子justify-content属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Linux关闭Tomcat为什么要用Kill,而不是shutdown.sh

    Linux关闭Tomcat为什么要用Kill,而不是shutdown.sh >>>>>>>>>>>>>>>&g ...

  7. PHP代码审计笔记--文件包含漏洞

    有限制的本地文件包含: <?php include($_GET['file'].".php"); ?> %00截断: ?file=C://Windows//win.in ...

  8. Python模块常用的几种安装方式 【转】

    转自:http://blog.chinaunix.net/uid-23500957-id-3781907.html 一. 单文件模块直接把文件拷贝到 $python_dir/Lib 二. 多文件模块, ...

  9. Spring transaction事务 roll back各种回滚

    Spring的AOP事务管理默认是针对unchecked exception回滚. 也就是默认对RuntimeException()异常极其子类进行事务回滚. Exception作为基类,下面还分ch ...

  10. 《C++标准程序库》笔记之三

    本篇博客笔记顺序大体按照<C++标准程序库(第1版)>各章节顺序编排. ---------------------------------------------------------- ...