POJ 1700-Crossing River(贪心+过河问题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/I_believe_CWJ/article/details/82692475

Crossing River

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20240   Accepted: 7546

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

POJ Monthly--2004.07.18

[Submit]   [Go Back]   [Status]   [Discuss]

题目大意:n个人过河,只有一条船,每次最多能载2个人,过河的时间为2个人中最慢的那个人的时间,让你求把所有人渡河所需要的最小时间。

解题思路:这一题是贪心题,贪心策略如下:

1、只有1个人的时候:时间为这个人过河的时间

2、只有2个人的时候:时间为2个人中最慢的那个

3、只有3个人的时候:时间为3个人的时间和(最快的和次快的先过河,然后最快的回来,再带最慢的渡河)

4、人数大于3人时:每次带最慢的两个人过河,又有两种策略:最快a的带最慢和次慢的c, d的两个人过河,时间为c + a + d + a,或者最快a的和次块b的两个先过河,然后最快的回来,再让最慢的和次慢的c, d两个人过河,然后次慢的回来,时间为b + a + d + b,每次过河取这两种策略中最快的那个。

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, b) G.push_back(b);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const LL mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1010;
const double pi = 3.1415926;

int n;
int a[110];

int main()
{
    int T;
    scanf("%d", &T);
    while(T --) {
        scanf("%d", &n);
        for(int i = 0; i < n; i ++) {
            scanf("%d", &a[i]);
        }
        int sum1 = 0, sum2 = 0, sum = 0;
        sort(a, a+n);
        while(n > 3) { //带最慢的两个人过河
            sum1 = 2*a[0] + a[n-1] + a[n-2];
            sum2 = 2*a[1] + a[0] + a[n-1];
            sum += min(sum1, sum2);
            n -= 2;
        }
        if(n == 1 || n == 3) {
            for(int i = 0; i < n; i ++) sum += a[i];
        }else if(n ==  2) sum += a[1];
        printf("%d\n", sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/I_believe_CWJ/article/details/82692475