URAL 1826. Minefield 贪心
来源:互联网 发布:java ssh2默认端口 编辑:程序博客网 时间:2024/06/07 01:16
1826. Minefield
Time limit: 0.5 second
Memory limit: 64 MB
Memory limit: 64 MB
To fulfill an assignment, a reconnaissance group of n people must cross the enemy's minefield. Since the group has only one mine detector, the following course of action is taken: two agents cross the field to the enemy's side and then one agent brings the mine detector back to the remaining group. This is repeated until only two agents remain. These two agents then cross the field together.
Each person gets across the field at their own speed. The speed of a pair is determined by the speed of its slower member.
Find the minimal time the whole group needs to get over the minefield.
Input
The first line contains the integer n (2 ≤ n ≤ 100). The i-th of the following n lines specifies the time the i-th member of the group needs to get over the minefield (the time is an integer from 1 to 600).
Output
Output the minimal total time the group needs to cross the minefield.
Sample
411052
17
题目:有n个人在雷区的左边。他们各自有自己跨越雷区的所需时间。他们只有一个扫雷器。为了能安全通过,他们只能两个人或者一个人带着扫雷器在雷区里走。两个人走过雷区的时间,以时间长的算。因为只有一个扫雷器,如果左边还有人,右边的人还要把扫雷器送回到左边。
注意开始只有两个人的时候的特判。
走法:当人数大于不等于四个人的时候。 有两种走法。 挑出 走的最快的a,第二快的b。然后再从 最慢的 z,和第二慢的 y开始。 原则 是a,b来送z和y过去,但是a,b 要回到左边。这里有两种走法,一种只用到a, a和z一起过去,再a一个人回来,再a和y一起过去,再a回来。耗时是a*2+y+z。 第二种走法是,a和b过去,a一个人回来,y和z一起过去,b一个人把扫雷器带回来。 耗时是 b*2+a+z。
再计算 三个人和四个人的情况就好了。见代码
#include <stdio.h>#include <stdlib.h>#include <string.h> #include <string>#include <iostream>#include <algorithm>using namespace std; #include <vector> int main(){int n;int arr[110];while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)cin>>arr[i];sort(arr,arr+n);if(n==2){cout<<arr[1]<<endl;continue;}int j=n-1;int ans=0;for(;j>=4;j-=2){ans+=min(arr[0]*2+arr[j]+arr[j-1],arr[0]+arr[1]*2+arr[j]);//四个人以上的情况}if(n&1)ans+=arr[0]+arr[j]+arr[j-1];//最后三个人elseans+=min(arr[0]*2+arr[1]+arr[j]+arr[j-1],arr[0]+arr[1]*3+arr[j]);//最后四个人cout<<ans<<endl;}return 0;}
0 0
- URAL 1826. Minefield 贪心
- URAL 1826. Minefield(数学 递归)
- URAL 1826 Minefield
- URAL - 1826 Minefield
- ural 1820. Ural Steaks 贪心
- URAL 1389 Roadworks 贪心
- Ural 2041 Nanomatryoshkas(贪心)
- URAL 1820 Ural Steaks (贪心)
- ural 1303 Minimal Coverage【贪心】
- URAL 1901 Space Elevators(贪心)
- URAL 1303. Minimal Coverage 贪心
- URAL 2041 Nanomatryoshkas(贪心)
- URAL 1203 Scientific Conference (贪心)
- URAL 1303 Minimal Coverage (贪心)
- URAL 1826 贪心经典题型
- URAL 1964 - Chinese Dialects(贪心)
- URAL 1203 Scientific Conference(贪心 || DP)
- URAL 1743 Domino Sorting 排序 + 贪心
- 《unix网络编程》(1)源代码的使用方法
- Intent中Component使用
- 数据库登陆失败原因: 未与信任 SQL Server 连接相关联
- 字符串转为整型StoI
- ZeroMQ研究与应用分析
- URAL 1826. Minefield 贪心
- eclipse 中使用tomcat插件部署后访问应用404问题解决
- 黑马程序员-OC学习笔记-----Foundation框架(常用结构体和NSString)
- The Contiki build system
- HDU ACM 1271 整数对
- 将字符串中的cvt(CVT)换成cvte(CVTE)and求树的节点数and斐波那契数列
- 虚拟机(Virtualbox-Ubuntu)与主机(WIN7)共享文件夹设置
- 3月中旬
- 基于注解的前置通知-佟刚老师《Spring4视频教程》学习笔记(17)