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、让最快的人来回载人过去。
2、先让最快的人和次快的人过去,然后让最快的人回来,让最慢和次慢的人过去,让对面最快的人回来。整体效果是最快和次快的人让最慢和次慢的人过去。
于是两者统一,就是让最慢和次慢的人每次渡河。比较两种策略的时间。
直到剩余两个或者三个人,就稍微考虑一下即可。另外不要忘了考虑初始只有一个人的情况。 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10 using namespace std; int n, a[1005]; bool cmp(int x, int y)
{
return x > y;
} void Input()
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
sort(a, a+n, cmp);
} int qt()
{
if (n == 1 || n == 2)
return a[0];
int ans = 0;
int top = 0, rear = n;
while (rear - top > 3)
{
ans += min(a[rear-2]*2+a[rear-1]+a[top], a[top]+a[top+1]+2*a[rear-1]);
top += 2;
}
if (rear - top == 2)
ans += a[rear-2];
else
ans += a[top] + a[rear-1] + a[rear-2];
return ans;
} int main()
{
//freopen("test.txt", "r", stdin);
int T;
scanf("%d", &T);
for (int times = 0; times < T; ++times)
{
Input();
printf("%d\n", qt());
}
return 0;
}

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. RAM和ROM和Flash ROM的区别

    转;http://openedv.com/thread-81182-1-1.html                           http://www.sohu.com/a/112676146 ...

  2. Mysql 免密码登录,修改密码及忘记密码操作

    ----免密码登陆 方式一 my.cnf增加[client]标签 [client] user="root" password="你的密码" 单对定义不同的客户端 ...

  3. 前端要给力之:语句在JavaScript中的值

    文件夹 文件夹 问题是语句有值吗 那么说你骗我咯 有啥米用呢 研究这个是不是闲得那个啥疼 ES5ES6有什么差异呢 结论是ES6是改了规则但更合理 最后不不过if语句 这两天在写语言精髓那本书的第三版 ...

  4. iOS block-base 动画简单用法+关键帧动画设置线性变化速度的问题

    本文转载至 http://www.tuicool.com/articles/aANBF3m 时间 2014-12-07 20:13:37  segmentfault-博客原文  http://segm ...

  5. python 基础 1.3 使用pycharm给python传递参数及pycharm调试模式

    一.通过pycharm 给python传递函数 1. 在pycharm终端中写入要获取的参数,进行获取 1>启动pycharm 中Terminal(终端) 窗口 点击pycharm左下角的图标, ...

  6. PHP部分--图片上传服务器、图片路径存入数据库,并读取

    html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  7. nohup COMMAND > FILE

    nohup  --help nohup(1) - Linux man page https://linux.die.net/man/1/nohup

  8. git功能速查

    http://gitbook.liuhui998.com/index.html git rebase:在本地变基.将本地所有的修改应用到另一个分支上 git merge:在本地合并分支 git bra ...

  9. 【转】数据存储——APP 缓存数据线程安全问题探讨

    http://blog.cnbang.net/tech/3262/ 问题 一般一个 iOS APP 做的事就是:请求数据->保存数据->展示数据,一般用 Sqlite 作为持久存储层,保存 ...

  10. 打开Vs2010时,卡在加载工具箱内容 不动了

    我是直接打开Visual Studio 2010,而不是以打开解决方案的方式打开.然后就在左下角显示“正在从包‘Microsoft.VisualStudio.IDE.ToolboxControlsIn ...