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
  2.  
  3. 这个题目第一反应是船回的时候,肯定让当前最快的人划回来,然后还考虑到,去的时候是按照最小速度的人,故让最慢的两个人过去,可能会减少时间。
    于是,考虑两种贪心策略:
    1、让最快的人来回载人过去。
    2、先让最快的人和次快的人过去,然后让最快的人回来,让最慢和次慢的人过去,让对面最快的人回来。整体效果是最快和次快的人让最慢和次慢的人过去。
    于是两者统一,就是让最慢和次慢的人每次渡河。比较两种策略的时间。
    直到剩余两个或者三个人,就稍微考虑一下即可。另外不要忘了考虑初始只有一个人的情况。
  4.  
  5. 代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <vector>
  10. #include <queue>
  11. #include <string>
  12. #define inf 0x3fffffff
  13. #define eps 1e-10
  14.  
  15. using namespace std;
  16.  
  17. int n, a[1005];
  18.  
  19. bool cmp(int x, int y)
  20. {
  21. return x > y;
  22. }
  23.  
  24. void Input()
  25. {
  26. scanf("%d", &n);
  27. for (int i = 0; i < n; ++i)
  28. scanf("%d", &a[i]);
  29. sort(a, a+n, cmp);
  30. }
  31.  
  32. int qt()
  33. {
  34. if (n == 1 || n == 2)
  35. return a[0];
  36. int ans = 0;
  37. int top = 0, rear = n;
  38. while (rear - top > 3)
  39. {
  40. ans += min(a[rear-2]*2+a[rear-1]+a[top], a[top]+a[top+1]+2*a[rear-1]);
  41. top += 2;
  42. }
  43. if (rear - top == 2)
  44. ans += a[rear-2];
  45. else
  46. ans += a[top] + a[rear-1] + a[rear-2];
  47. return ans;
  48. }
  49.  
  50. int main()
  51. {
  52. //freopen("test.txt", "r", stdin);
  53. int T;
  54. scanf("%d", &T);
  55. for (int times = 0; times < T; ++times)
  56. {
  57. Input();
  58. printf("%d\n", qt());
  59. }
  60. return 0;
  61. }

ACM学习历程——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. ACM学习历程—POJ 3764 The xor-longest Path(xor && 字典树 && 贪心)

    题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor( ...

  4. ACM学习历程——POJ 2376 Cleaning Shifts(贪心)

    Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning ...

  5. poj 1700 Crossing River C++/Java

    http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最 ...

  6. POJ 1700 - Crossing River

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13982   Accepted: 5349 Description A gr ...

  7. ACM学习历程——NOJ1113 Game I(贪心 || 线段树)

    Description 尼克发明了这样一个游戏:在一个坐标轴上,有一些圆,这些圆的圆心都在x轴上,现在给定一个x轴上的点,保证该点没有在这些圆内(以及圆上),尼克可以以这个点为圆心做任意大小的圆,他想 ...

  8. ACM学习历程—HDU5422 Rikka with Graph(贪心)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  9. POJ 1700 cross river (数学模拟)

                                                                                                       ...

随机推荐

  1. nginx for windows 配置多域名反向代理

    调试了很久...哦耶 共享出来吧 其实 nginx反向代理同一ip多个域名,给header加上host就可以了 upstream test.test.cn {        server   119. ...

  2. java中类型的隐式转换

    byte+byte=int,低级向高级是隐式类型转换,高级向低级必须强制类型转换,byte<char<short<int<long<float<double

  3. 架构 -- java

    @.sql写在dao层 原文:http://blog.csdn.net/y_dzaichirou/article/details/53673528 @.Java Web项目需要掌握的技能 原文:htt ...

  4. c 字符串 函数

    c编辑 strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#include <string.h> 功能:把src所指由NUL结 ...

  5. NDK以及C语言基础语法(二)

    一.字符串类:(属于类类型) -String (在C++中才有) 使用之前必学引入String 类型: 引入String头文件(系统的头文件): #include <string>   p ...

  6. 关于 Delphi 中流的使用(7) 压缩与解压缩(TCompressionStream、TDecompressionStream)

    unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, For ...

  7. 九度OJ 1034:寻找大富翁 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5925 解决:2375 题目描述:     浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. 输入:     输入包含多组测试用例.   ...

  8. AsyncHttpClien访问网络案例分析

    Android数据存储的四种方式分别是:SharedPreferences存储.File文件存储.Network网络存储和sqlite数据库存储,网络存储需要使用AsyncHttpClient发送请求 ...

  9. MongoDB入门学习(三):MongoDB的增删查改

            对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改.         由于M ...

  10. Pentaho BIServer Community Edtion 6.1 使用教程 第二篇 迁移元数据 [HSQLDB TO MySQL]

    第一部分  迁移原因 Pentaho BI 社区版服务的很多元数据信息存储在数据库汇总,其默认使用HSQLDB 数据库,即借助它存储自身的资料库,比如 Quartz 调度信息.业务资料库连接信息(数据 ...