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. 8 -- 深入使用Spring -- 7...3 让Spring管理控制器

    8.7.3 让Spring管理控制器 让Spring容器来管理应用中的控制器,可以充分利用Spring的IoC特性,但需要将配置Struts 2 的控制器部署在Spring容器中,因此导致配置文件冗余 ...

  2. 8 -- 深入使用Spring -- 4...2 使用AspectJ实现AOP

    8.4.2 使用AspectJ实现AOP AspectJ是一个基于Java语言的AOP框架.Spring 4.0 的AOP对AspectJ很好的集成. AspectJ是Java 语言的一个AOP实现, ...

  3. 页面倒计时跳转页面效果,js倒计时效果

    页面倒计时跳转页面效果,js倒计时效果 >>>>>>>>>>>>>>>>>>>> ...

  4. centos 7 安装 gcc-4.9.3.tar.gz

    由于编译新内核需要,更新了GCC编译器,自行获取文件,手动升级. 首先是获取文件:wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gc ...

  5. [SublimeText] 如何创建工程

    Sublime Text 可以很方便地管理多个工程.使用Sublime Text的Projects,可以将不同根目录的文件组织起来成为一个工程,而不用将所有的文件都放到一个根目录下面. 1. 创建工程 ...

  6. PyQt4程序图标

    程序图标就是一个小图片,通常显示在程序图标的左上角(ubuntu gnome在最上侧). #!/usr/bin/python # -*- coding:utf-8 -*- import sys fro ...

  7. JavaScript表达式--掌握最全的表达式,一切尽在掌握中,让表达不再是难事

    一.JavaScript表达式: 算术表达式: 字符串表达式: 关系(比较)表达式: 逻辑表达式 二.JavaScript运算符: ●什么是表达式--表达式是产生一个结果值的式子,常量,变量,运算符. ...

  8. Java面试题全集

    Java面试题全集(上) Java面试题全集(中) Java面试题全集(下) http://www.importnew.com/21445.html

  9. 用Copy命令合并文件

    用Copy命令合并文件 这个文章是在我以前的百度空间里面发表过的,后来因为某个内分泌失调的管理员把我的空间http://hi.baidu.com/kamdy   封了! 旧事不提,还是回到主题吧,这个 ...

  10. VC下遍历文件夹中的所有文件的几种方法

    一.使用::FindFirstFile和::FindNextFile方法 #include "StdAfx.h" #include <windows.h> #inclu ...