当前位置: 代码迷 >> 综合 >> poj 3404 Bridge over a rough river 贪心
  详细解决方案

poj 3404 Bridge over a rough river 贪心

热度:5   发布时间:2024-01-19 05:25:21.0

题意:

有n个人要坐船过河,每个人需要的时间已知,有一条最多容纳两个人的船,两个人坐船的时间为两人中的较大值,求所有人过河的最少时间。

分析:

设4个人分别要a<b<c<d的时间过河,可证明全局最优解中c与d是先过河的(不可能出现d过河然后b,c一起过河,可以d,c一起过河,也可以d通过别人送,c也通过别人送)。

代码:

//poj 3404
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int a[128];int main()
{scanf("%d",&n);for(int i=1;i<=n;++i)scanf("%d",&a[i]);		sort(a+1,a+n+1);int sum=0;while(n>0){if(n==1){sum+=a[1];break;}else if(n==2){sum+=a[2];break;}else if(n==3){sum+=a[1]+a[2]+a[3];break;}else{sum+=min(2*a[1]+a[n-1]+a[n],a[1]+2*a[2]+a[n]);n-=2;	}}printf("%d\n",sum);return 0;	
} 


  相关解决方案