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. 【AI】Win10-Tensorflow

    一.安装 第一步:先安装anaconda3第二步:pip install --upgrade --ignore-installed tensorflow或者:pip install tensorflo ...

  2. 【Android】ProgressBar

    http://www.cnblogs.com/wangying222/p/5304990.html http://www.cnblogs.com/plokmju/p/android_ProgressB ...

  3. iOS开发-iOS7禁用手势返回

    - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // 禁用 iOS7 返回手势 if ([self.nav ...

  4. Git 单机版

    Git 是一个分布式的开源版本控制系统,也就是说,每台机器都可以充当控制中心,我从本机拉取代码,再提交代码到本机,不需要依赖网络,各自开发各自的 如何创建 git 仓库: [root@localhos ...

  5. drawCall_01

    在屏幕上渲染物体,引擎需要发出一个绘制调用来访问图形API(iOS系统中为OpenGL ES).每个绘制调用需要进行大量的工作来访问图形API,从而导致了CPU方面显著的性能开销.   Unity在运 ...

  6. HTML5实现图片预览功能

    两种方式实现 URL FileReader Index.jsp文件 <%@page contentType="text/html" pageEncoding="UT ...

  7. Matlab 矩阵函数

    clear; clc; A = rand() cond(A) %求矩阵A的条件数 Det(A) %求方阵A的行列式 Dot(A,B) %矩阵A与B的点积 Eig(A) %方阵A的特征值和特征向量 No ...

  8. 查询SQlServer相同表结构差异

    USE [数据库名] GO ); ); ); ); SET @DataName1='库1'; SET @DataName2='库2'; SET @TableName1='表1'; SET @Table ...

  9. IOS设计模式第二篇之单例设计模式

    现在我们的组件已经有组织了.你需要从其他的地方得到数据,你也可以创建一个API类管理数据这个下个设计模式单例里面介绍. 这个单例设计模式确保这个类仅仅拥有一个实例,并且为这个实例提供一个全局的访问点. ...

  10. 分布式实时日志系统(一)环境搭建之 Jstorm 集群搭建过程/Jstorm集群一键安装部署

    最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...