POJ1700

题目链接:http://poj.org/problem?id=1700
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
 

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. 1
  2. 4
  3. 1 2 5 10

Sample Output

  1. 17

题解:

  1. 这是一个过河坐船问题,一共有两个策略
  1. ①最快和次快过去,最快回;最慢和次慢过去,次快回,最快的和次快的过去,t1=a[1]+a[0]+a[n-1]+a[1]+a[1]。②最快和最慢过去,最快回;最快和次快过去,最快回,最快的和次慢的过去,t2=a[n-1]+a[0]+a[1]+a[0]+a[n-2]。选择两者中用时较少的一个策略执行,判断t1t2大小,只需要判断2a[1]是否大于a[0]+a[n-2],如此便将最慢和次慢送过河,对剩下n-2个人循环处理。注意当n=1n=2n=3时直接相加处理即可.
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int n,a[];
  5. int main()
  6. {
  7. int t,i;
  8. cin>>t;
  9. while(t--)
  10. {
  11. int f=;
  12. //每次f归0
  13. cin>>n;
  14.  
  15. for(i=;i<=n;i++)
  16. cin>>a[i];
  17. sort(a,a+n+);
  18. while(n)
  19. {
  20. if(n==)
  21. {
  22. f+=a[];
  23. break;
  24. }
  25. if(n==)
  26. {
  27. f+=a[];
  28. break;
  29. }
  30. if(n==)
  31. {
  32. f+=a[]+a[]+a[];
  33. break;
  34. }
  35. if(n>)
  36. {
  37. if(*a[]>(a[]+a[n-]))
  38. f+=*a[]+a[n]+a[n-];
  39. else
  40. f+=*a[]+a[]+a[n];
  41. n=n-;
  42. //注意循环
  43. }
  44. }
  45. cout<<f<<endl;
  46. }
  47. return ;
  48. }

POJ1700:Crossing River(过河问题)的更多相关文章

  1. poj 1700 Crossing River 过河问题。贪心

    Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9887   Accepted: 3737 De ...

  2. poj-1700 crossing river(贪心题)

    题目描述: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...

  3. Crossing River

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

  4. Crossing River(1700poj)

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

  5. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  6. bzoj 1314: River过河 优先队列

    1314: River过河 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 26  Solved: 10[Submit][Status][Discuss ...

  7. poj1700--贪心--Crossing River

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

  8. Crossing River POJ过河问题

    A group of N people wishes to go across a river with only one boat, which can at most carry two pers ...

  9. Crossing River poj1700贪心

    题目描述:N个人过河,只有一只船,最多只能有两人划船,每个人划船速度不同,船速为最慢的人的速度.输入T为case个数,每个case输入N为人数,接下来一行输入的是每个人过河的时间,都不相同.要求输出N ...

随机推荐

  1. JAR WAR EAR包的区别

    jar包和war包的区别: war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块.而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令 ...

  2. angularJs $injector

    一 angularJS中几种注入方式 Spring中使用构造注入或者设值注入的方式,还需要做一些额外的操作,但是angular中只需要在需要的地方声明一下即可,类似模块的引用,因此十分方便. angu ...

  3. C++ 求阶乘 四种方法

    来总结下求阶乘的各种方法哈. 写在最前:①各个代码仅仅是提供了求阶乘的思路,以便在实际须要时再来编码,代码并不健壮!②各个程序都在1到10内測试正确. 代码一: #include<iostrea ...

  4. Android(java)学习笔记246:ContentProvider使用之学习ContentProvider(内容提供者)的目的

    1.使用ContentProvider,把应用程序私有的数据暴露给别的应用程序,让别的应用程序完成对自己私有的数据库数据的增删改查的操作. 2.ContentProvider的应用场景: 获取手机系统 ...

  5. Android(java)学习笔记235:多媒体之计算机图形表示方式

    1.多媒体 很多媒体:文字(TextView,简单不讲),图片,声音,视频等等.   2.图片 计算机如何表示图片的? (1)bmp 高质量保存    256色位图:图片中的每个像素点可以有256种颜 ...

  6. android高效ORM数据库框架greenDao使用

    因为项目中多处用到了数据库,需要对数据库频繁的读写操作,虽然android 自带的SQLiteOpenHelper的.这种方式比较方便易懂,但是在使用过程中需要写很多的sql语句,而且需要及时的关闭和 ...

  7. MVC部署出现HTTP 404 错误

    asp.net mvc部署出现问题,http错误404.0,报错如下图: 在网上找了好多方法都不行.最后我的解决方案是: 打好这个补丁就行了http://support.microsoft.com/k ...

  8. "SOAP WebService " 和 "RESTful WebService" 的定义分别是什么???

    一个个来,要知道区别,首先得知道各自的意义及用途web service采用Xml传输结构化的数据,轻量级跨平台而soap则是一种基于http的应用层通讯协议,同样采用xml编码,只不过soap又更进了 ...

  9. ASP.NET的WebConfig

    转:http://blog.csdn.net/q3498233/article/details/8137364 WebConfig 花了点时间整理了一下ASP.NET Web.config配置文件的基 ...

  10. (转)DEDECMS模板原理、模板标签学习 - .Little Hann

    本文,小瀚想和大家一起来学习一下DEDECMS中目前所使用的模板技术的原理: 什么是编译式模板.解释式模板,它们的区别是什么? 模板标签有哪些种类,它们的区别是什么,都应用在哪些场景? 学习模板的机制 ...