POJ 1700 经典过河问题(贪心)

POJ题目链接:http://poj.org/problem?id=1700

N个人过河,船每次最多只能坐两个人,船载每个人过河的所需时间不同,问最快的过河时间。

思路:

当n=1,2,3时所需要的最小时间很容易求得,现在由n>=4,假设n个人单独过河所需要的时间存储在数组t中,将数组t按升序排序,那么 这时将单独过河所需要时间最多的两个旅行者送到对岸去,有两种方式:

1> 最快的(即所用时间t[0])和次快的过河,然后最快的将船划回来,再次慢的和最慢的过河,然后次快的将船划回来.

即所需时间为:t[0]+2*t[1]+t[n-1]

2> 最快的和最慢的过河,然后最快的将船划回来,再最快的和次慢的过河,然后最快的将船划回来.

即所需时间为:2*t[0]+t[n-2]+t[n-1]

这样就将过河所需时间最大的两个人送过了河,而对于剩下的人,采用同样的处理方式,接下来做的就是判断怎样用的时间最少.

代码如下:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int m,n,t[1001],i,sum;
    cin>>m;
    while(m--)
    {
        cin>>n;
        sum=0;
        for(i=0;i<n;i++)
            cin>>t[i];
        sort(t,t+n);
        for(i=n-1;i>2;i-=2)
            if(t[0]+2*t[1]+t[i]>2*t[0]+t[i-1]+t[i])
                sum+=2*t[0]+t[i-1]+t[i];
            else sum+=t[0]+2*t[1]+t[i];
            if(i==2) sum+=t[0]+t[1]+t[2];
            else if(i==1) sum+=t[1];
            else sum+=t[0];
            cout<<sum<<endl;
    }
    return 0;
}
时间: 2024-08-26 02:29:53

POJ 1700 经典过河问题(贪心)的相关文章

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(过河问题)

此题讲的是N个人过河,每个人都有自己的过河时间,一条船只能承受2个人,所用时间为其中过河时间最多的,所以呢,想到有两种情况,第一种:过河时间最少的人来回接送其他人,第二种:过河时间最少和次少的人来回接送其他人,刚开始就觉得第一种时间必然是最少的,但是仔细想想,不然.因为第一种情况虽然单次过河时间少,但送人的次数要多,如第1个人(过河时间最少)接送第i人 dp[i]=dp[i-1]+time[0]+time[i]: 而第二种情况呢,虽然来回次数多,但是接送的人要多,如第1个人接第i-1个和第i个人

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 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

poj 2586 Y2K Accounting Bug (贪心)

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9632   Accepted: 4806 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.

POJ 1328、Radar Installation 贪心

jQuery的属性操作非常简单,下面以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 添加属性 $('a').attr('href', 'http://www.jquery.com') 添加多个属性 $('a').attr({'href':'http://www.jquery.com', 'title':'jquery.com'}) 获取属性 $('a').attr('href') class属性

POJ 3069 Saruman&#39;s Army(贪心)

Saruman's Army Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3069 Appoint description:  System Crawler  (2015-04-27) Description Saruman the White must lead his army along a straight path from

poj 2393 Yogurt Factory(贪心)

Description The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 <= N <= 10,000) weeks, the price of milk and labor will fluctuate weekly such that it will cost the company C_i (1 <= C_i <= 5,000) c

[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