poj 1700 Crossing River
2016-02-24 17:46
260 查看
⑴当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];
}
else
sum+=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];
else
sum+=a[0];
printf("%d\n",sum);
}
return 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];
}
else
sum+=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];
else
sum+=a[0];
printf("%d\n",sum);
}
return 0;
}
相关文章推荐
- Linux命令-find及xargs的使用
- Eclipse中的Web项目自动部署到Tomcat
- 【CSS3】matrix
- php 中isset 和 empty对于0的判断的区别
- XML 学习介绍 收藏
- 【转】测试思考——测试的价值体现和提升
- 嵌入式介绍
- iphone 各种屏幕尺寸
- 粒子缩放器
- App半透明指引(第一次进去app的新功能或者版本更新之后使用)
- hadoop 集群搭建
- 日志收集
- 深入浅出Java并发包—锁机制(二)
- Android Studio报错:Failed to complete Gradle execution.
- java发送http的get、post请求
- Linux各目录作用
- iOS持久化存储-CoreData简介
- org.springframework.beans.NotWritablePropertyException
- 读书笔记-JavaScript中的全局对象
- Mark《黑客与画家》