poj1700 Crossing River

Crossing River

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12585 Accepted: 4787

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

Source

field=source&key=POJ+Monthly--2004.07.18" style="text-decoration:none">POJ Monthly--2004.07.18

题意:n个人一条船,全部人要过河;船每次仅仅能载两个人。用时按最慢的算。求全部人过河的最小时间。

分析:两种情况;设最小的和第二小的为m1,m2,当前

<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 100010

int T,n;
int a[1010];
int sum;

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        CL(a, 0);
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        if(n == 1){printf("%d\n",a[0]); continue;}
        sort(a, a+n);
        int m1 = a[0], m2 = a[1];
        sum = 0;
        for(int i=n-1; i>1; i-=2)
        {
            if(m2*2<m1+a[i-1])
            {
                if(i == 2) sum = sum+m1+m2+a[i];
                else sum = sum+m1+m2*2+a[i];
            }
            else
            {
                sum = sum+m1*2+a[i]+a[i-1];
                if(i == 2) sum -= a[0];
            }
        }
        if(n!=0&&n%2==0) sum += m2;
        printf("%d\n",sum);
    }
    return 0;
}
</span>

最大的和第二大的为n1,n2;

1、m2+m2<m1+n2;m1和m2先过河,然后m1回来,n1和n2过河。m2回来;

2、m2+m2>m1+n2。m1和n1先过河。然后m1回来。m1和n2过河。m1回来;

另外还要注意奇数的情况。

时间: 2024-10-12 12:08:46

poj1700 Crossing River的相关文章

POJ1700 Crossing River(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 13301          Accepted: 5087 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 shu

POJ 1700 Crossing River(贪心)

V - Crossing River Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1700 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. There

poj 1700 Crossing River

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12585   Accepted: 4787 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 arr

[ACM] poj 1700 Crossing River (经典过河问题)

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10212   Accepted: 3855 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 arr

hihocoder-Week175-Robots Crossing River

hihocoder-Week175-Robots Crossing River Robots Crossing River 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Three kinds of robots want to move from Location A to Location B and then from Location B to Location C by boat. The only one boat between A and B an

POJ1700:Crossing River(过河问题)

POJ1700 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

【POJ1700】Crossing River 贪心,附贪心问题的一系列详细解析

题意:一群人过河,船每次只能装两人,每次过河时间为两人权值较大的那个. 题解: 这种题的贪心策略往往不是很好想,这个时候我们就需要依照尽量逼近正解的思路,进行多种贪心,在每种贪心都保证正确的前提下,取多个答案的最值,这样往往就是正解,而即便可以卡,数据也很难出,并不是写个rand+debug拍上两个小时就能拍出来的. 而这种 多线程贪心 可以有两种: 一.单独做每种,然后取最优. 二.每一步转移都用多种贪心取得,然后直接取最后的状态值. 显然后一种的正确性比较高,但是往往因为"贪心"的

Crossing River poj1700贪心

题目描述:N个人过河,只有一只船,最多只能有两人划船,每个人划船速度不同,船速为最慢的人的速度.输入T为case个数,每个case输入N为人数,接下来一行输入的是每个人过河的时间,都不相同.要求输出N个人全部过河的时间 算法思想:采用贪心的方法.有两种划船的方法,一种是最快+最慢,最快回,最快+次慢,最快回,循环下去:第二种是最快+次快,次快回,最慢+次慢,最快回,循环下去.那么当剩余人数N>3的时候,比较第一种和第二种的时间,采用耗时短的方法过河.N=1,2,3的时候单独讨论. #includ

Crossing River

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11814   Accepted: 4476 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 b