Crossing River POJ过河问题

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

 一个非常巧妙的贪心问题,刚开始的时候以为只要保证让船回来的时候时间最短就好了,即让最小的那一个一直跟着船走,但并不是这样的。。题解: 分两种过程,第一种就是上述过程,第二种情况是我们先让最快的跟次快的过河,然后让最快的回来,再让最慢的跟次慢的过河,再让次快的回来,这两种情况去较小的,前提是至少要有大于等于4个人,如果小于4个人的话,模拟就可以还要注意奇数的情况 ,如果是奇数,那么最后会剩下3个人,即最快 次快 次次快,如果是偶数的话,由于我们每次减少两个,最后只剩下最快于次快;AC代码
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1E5+7;
int arr[N];
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)    scanf("%d",&arr[i]);
        sort(arr+1,arr+1+n);
        int ans=0;
        if(n==1)    ans=arr[1];
        else if(n==2)    ans=arr[2];
        else if(n==3) {
            ans+=arr[3]+arr[1]+arr[2];
        }
        else {
            int sum1,sum2;
            for(int i=n;i>=4;i-=2){
                sum1=arr[i]+arr[i-1]+arr[1]+arr[1];
                sum2=arr[2]+arr[1]+arr[i]+arr[2];
                ans+=min(sum1,sum2);
            }
            if(n&1)
                ans+=arr[3]+arr[1]+arr[2];
            else ans+=arr[2];
        }
        cout<<ans<<endl;
    }
    return 0;
}

这篇博文讲的贼好。

https://blog.csdn.net/Cworld2017/article/details/81503102

原文地址:https://www.cnblogs.com/Accepting/p/11565153.html

时间: 2024-10-28 04:23:28

Crossing River POJ过河问题的相关文章

[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

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

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 arrange

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

poj 1700 Crossing River C++/Java

http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最短时间. 思路: 贪心. 对于每次过河的,有两种情况: //最快和最慢过去,然后最快回来.在和次慢过去.最快回来 int action1=a[i-1] + a[0] + a[i-2] +a[0]; //最快和次慢过去,然后最快回来,在次慢和最慢过去,次慢回来 int action2=a[1] +a[

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

ACM学习历程——POJ 1700 Crossing River(贪心)

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. Eac

poj 1700 Crossing River(贪心)

分析:题意 源岸数量<=3  很好判断 3:num[0]+num[1]+num[2] 2:num[1] ,1:num[0] 源岸数量>3 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main(void) { int n,t ; int i,j; int num[1001]; cin>>