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. Dubbo -- 系统学习 笔记 -- 示例 -- 泛化引用

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 泛化引用 泛接口调用方式主要用于客户端没有API接口及模型类元的情况,参数及返回值 ...

  2. 解决SOCKET通信 ERROR_INSUFFICIENT_BUFFER错误

    错误发生在服务端异步收到一个socket连接,之后使用WSAGetLastError()得到的IO错误码是122 这个错误码在系统中的解释是The data area passed to a syst ...

  3. vmware 安装 Mac OS X 10.9 Mavericks

    This guide shows how to install fresh OS X 10.9 Mavericks on VMware workstation with Windows 7 or Wi ...

  4. Ajax 分析方法

    我们如何查看到 Ajax 请求: 以 https://m.weibo.cn/u/2830678474 这个网页为例,按 F12,加载网页,然后选择资源类型为 XHR 的就可以看到 Ajax 请求了 我 ...

  5. osgearth缓存数据命令

    新建一个.bat文件 中国地区 osgearth_package.exe ReadyMap.earth --tms ReadyMap.earth --out D:\APICenter\EarthDat ...

  6. CentOS7--系统设置语言环境

    设置语言: 系统范围的区域设置存储在/etc/locale.conf文件中,在systemd守护进程提前引导时读取.配置的区域设置/etc/locale.conf由每个服务或用户继承,除非个别程序或个 ...

  7. com.baidu.mapapi.CoordType

    2.2.2升级到3.0.1百度报错了, 一:请检查.jar,.so是否是最新的 二:clear

  8. centos系统-java -jdk 环境配置

    方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ~]# c ...

  9. 一个汉字转拼音的php类

    代码来自网上,可用 <?php function Pinyin($_String, $_Code='gb2312') { $_DataKey = "a|ai|an|ang|ao|ba| ...

  10. iOS9下App Store新应用提审攻略

    博文转载 CocoaChina 文/文公子 公子在第十讲中提到应用更新时需要注意的细节和苹果便捷通道的利用.今天,公子将进一步深扒iTunes Connect的面纱,为大家呈现新应用在提审前需要准备的 ...