poj 1700 Crossing River

来源:互联网 发布:免费图纸打印软件 编辑:程序博客网 时间:2024/05/17 10:38

⑴当n = 1,直接过河。sum = cost[0]

(2)当n = 2,直接过河。 sum = cost[1]

(3)当n = 3,无论怎么过河, sum = cost[0] + cost[1] + cost[2] 

(4)当n = 4,设从小到大排序后位a=cost[0],b=cost[1],c=cost[2],d=cost[3];

用最小的来送:b + a + c + a + d =2*a+b+c+d= 2*cost[0] + cost[1] + cost[2] + cost[3](a,b过去,a回来,a,c过去,a回来,a,d过去)

两小送两大:b + a + d + b + b =a+3*b+d= cost[0] + 3*cost[1] + cost[3](a,b过去,a回来,c,d过去,b回来,a,b过去)

所以sum = min(2a + b + c + d, a + 3b + d)

(5)当n > 4,设从小到大排序后位a,b,……,c,d,大于4个人,a,b是最小的两个人,c,d是最大的两个人,目标就是把最大的两个人送过去。就要牺牲最小的。

用最小的来送:A=d + a + c + a = 2a + c + d=2*cost[0]+cost[2]+cost[3](a,d过去,a回来,a,c过去,a回来)

两小送两大:B= b + a + d + b = a + 2b + d=cost[0]+2*cost[1]+cost[3];(a,b过去,a回来,c,d过去,b回来)

循环:sum = min(A,B),直到n <= 4时候结束。

#include<algorithm>#include<cstdio>#include<iostream>using namespace std;int main(){int a[1100];int t;int n;scanf("%d",&t);while(t--){int i;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);int sum=0;while(n>=4){if( 2*a[1]+a[0]+a[n-1]>=2*a[0]+a[n-2]+a[n-1] ){sum+=2*a[0]+a[n-2]+a[n-1];}elsesum+=2*a[1]+a[0]+a[n-1];n-=2;}if(n==3)sum+=(a[0]+a[1]+a[2]);else if(n==2)sum+=a[1];elsesum+=a[0];printf("%d\n",sum);}return 0;} 



0 0
原创粉丝点击