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. Java编码辅助工具:Lombok —— 避免重复臃肿的代码,提高效率

    在项目开发过程中,经常会涉及到一些调整很少但又必不可少的环节,比如实体类的Getter/Setter方法,ToString方法等.这时可以使用Lombok来避免这种重复的操作,减少非核心代码的臃肿,提 ...

  2. HTML5 2D平台游戏开发#3冲刺

    断断续续地把Demo又写了一阵,终于把角色的冲刺动作完成了.冲刺的作用是使角色能够快速移动,闪避攻击或障碍.其完成效果如下: 首先,仍需要一些变量来表示角色的冲刺状态: //标识角色是否处于冲刺中 v ...

  3. python创建迅雷批量任务

    其实不是真的创建了批量任务,而是用python创建一个文本文件,每行一个要下载的链接,然后打开迅雷,复制文本文件的内容,迅雷监测到剪切板变化,弹出下载全部链接的对话框~~ 实际情况是这样的,因为用py ...

  4. Hadoop起源

    本文来自Doug Cutting为<Hadoop权威指南>所作之序,感觉读一下还是挺有收获的. Hadoop 起源于Nutch项目.我们几个人有一段时间一直在尝试构建一个开源的Web搜索引 ...

  5. firfox浏览器常用快捷键

    Ctrl + 数字键来打开第N个标签页这种还要先数完再到键盘上找数字Ctrl + Page Up = 激活左边一个标签页Ctrl + Page Down = 激活右边一个标签页Ctrl + Tab = ...

  6. 小程序踩坑之不同屏幕下动态改变translate值

    案例还原 小程序做一个进度条,可以通过拽转控制进度 那么肯定有一个进度条,不过小程序自己会做适配宽高 6s下这个div的width 是250 6splus就是276 但是问题来了,我拖拽用的是tran ...

  7. 高特权级代码段转向低特权级代码段(利用 ret(retf) 指令实现 jmp from ring0 to ring3)

    [0]写在前面 0.1)本代码旨在演示 从 ring0 转移到 ring3(即,从高特权级 转移到 低特权级) 0.2)本文 只对 与 门相关的 代码进行简要注释,言简意赅: 0.3)文末的个人总结是 ...

  8. Drupal 主题的表现形式

    1.template.php /**  * Implements hook_theme().  */ function yourtheme_theme($existing, $type, $theme ...

  9. WPF SDK研究 之 数据绑定

    这一章介绍数据绑定.本章共计27个示例,全都在VS2008下.NET3.5测试通过,点击这里下载:ConnectedData.rar 1.ShowDataWithoutBinding注: <?M ...

  10. Error -27728: Step download timeout (120 seconds)的解决方法(转)

    LR中超时问题解决方法 超时错误在LoadRunner录制Web协议脚本回放时超时经常出现. 现象1:Action.c(16): Error -27728: Step download timeout ...