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. Nginx 一些常用的URL 重写方法

    url重写应该不陌生,不管是SEO URL 伪静态的需要,还是在非常流行的wordpress里,重写无处不在. 1. 在 Apache 的写法 RewriteCond  %{HTTP_HOST}  n ...

  2. MySql(四):备份与恢复

    一.数据库备份使用场景 下面我就列举一下我个人理解的我们能够需要用到数据库备份的一些比较常见的情况吧. a.数据丢失应用场景 1.人为操作失误造成某些数据被误操作:2.软件BUG 造成数据部分或者全部 ...

  3. C语言 结构体篇

    结构体:是一种构造类型 它是由若干成员组成的 其中每一个成员都可以是一个基本数据类型或者又是一个构造类型 定义结构体变量后,系统就会为其自动分配内存 为了便于更大的程序便于修改和使用  常常将结构体类 ...

  4. 四、Silverlight中使用MVVM(四)——演练

    本来打算用MVVM实现CRUD操作的,这方面例子网上资源还挺多的,毕竟CRUD算是基本功了,因为最近已经开始学习Cailburn框架了,感觉时间 挺紧的,这篇就实现其中的更新操作吧. 功能很明确,当我 ...

  5. linux 跟踪工具

    strace工具,进程诊断.排错.跟踪系统调用和信号量 每行输出都是一个系统调用,包括函数和返回值. strace是Linux环境下的一款程序调试工具,用来监察一个应用程序所使用的系统调用及它所接收的 ...

  6. PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案

    一:PHP服务端如何通过程序将图上传到指定的图片服务器与图片服务器的优化方案 (1) php服务器把图片处理成缩率图或指定大小的图片在通过PHP程序代码 操作FTP 上传到图片服务器. 二:图片服务器 ...

  7. 13-redis主从复制

    一:redis主从复制 (1)配置要求 一台主maste 二台从slave (2)架构图 (3)配置流程 1:分配拷贝2份redis.conf 分别命名为 redis6380.conf redis63 ...

  8. 再看python多线程------threading模块

    现在把关于多线程的能想到的需要注意的点记录一下: 关于threading模块: 1.关于 传参问题 如果调用的子线程函数需要传参,要在参数后面加一个“,”否则会抛参数异常的错误. 如下: for i ...

  9. 广播、多播和IGMP的一点记录

    广播和多播:仅应用于UDP 广播分为: 1.受限的广播(255.255.255.255) 2.指向网络的广播(eg:A类网络 netid.255.255.255)主机号为全1的地址 3.指向子网的广播 ...

  10. Unix环境高级编程第三版中实例代码如何在自己的linux上运行的问题

    学习Linux已经有2个月了,最近被期末考试把进度耽误了,前几天把Unix环境高级编程看了两章,感觉对Linux的整体有了一些思路,今天尝试着对第一章涉及到的一个简单的交互式shell编译运行一下,结 ...