poj 3404 Bridge over a rough river(过桥问题)
poj 3404 Bridge over a rough river(过桥问题)
题目链接:=3404
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4024 | Accepted: 1644 |
Description
A group of N travelers (1 ≤ N ≤ 50) has approached an old and shabby bridge and wishes to cross the river as soon as possible. However, there can be no more than two persons on the bridge at a time. Besides it's necessary to light the way with a torch for safe crossing but the group has only one torch.
Each traveler needs ti seconds to cross the river on the bridge; i=1, ... , N (ti are integers from 1 to 100). If two travelers are crossing together their crossing time is the time of the slowest traveler.
The task is to determine minimal crossing time for the whole group.
Input
The input consists of two lines: the first line contains the value of N and the second one contains the values of ti (separated by one or several spaces).
Output
The output contains one line with the result.
Sample Input
4 6 7 6 5
Sample Output
29
Source
Northeastern Europe 2001, Western Subregion
思路:
n==1时,直接输出答案
n==2时,输出最大值
n==3时,输出三者和
n>=4时,两种策略,均转化成4人时的情形求最优解。设4人过河速度为A<B<C<D,那么有两种策略:
先AB过,A回,CD过,B回,即temp=A+2*B+D
先AD过,A回,再AC过,A回,即temp=2*A+C+D(忘了这种情况)
然后取其中的最小值即可。也就是说,在2*B<A+C时选方案1,否则选方案2.
代码如下:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define INF 0x3fffffff
//typedef long long LL;
//typedef __int64 LL;
const int M = 1017;
int main()
{int n;int a[M];int i;while(~scanf("%d",&n)){for(i = 1; i <= n; i++){scanf("%d",&a[i]);}if(n == 1){printf("%d\n",a[1]);continue;}sort(a+1,a+n+1);if(n == 2){printf("%d\n",a[2]);continue;}if(n == 3){printf("%d\n",a[1]+a[2]+a[3]);continue;}int sum = 0;while(n){if(2*a[2] < a[1]+a[n-1]){sum+=a[1]+2*a[2]+a[n];n-=2;}else{sum+=2*a[1]+a[n-1]+a[n];n-=2;}if(n <= 3)break;}if(n == 1)sum+=a[1];else if(n == 2)sum+=a[2];else if(n == 3){printf("%d\n",sum+a[1]+a[2]+a[3]);continue;}printf("%d\n",sum);}return 0;
}
- 南宁市计算机技术专业学校,南宁电脑技术学校有哪些
- XML是什么?有什么用?
- Linux用户权限特殊权限
- Annotation定义
- 有关林达华的几个地址
- 关于使用ComponentName连接俩个Activity运行闪退的问题
- 专业人士解释杜邦分析法(二)
- (转)Visual SourceSafe (VSS的使用方法)使用方法
- void* 指针有什么用
- 综合案例:选餐
- VC知识库的一篇文章
- 机器学习几种距离比较:欧拉距离(Euclidean Distance)、曼哈顿距离(Manhattan Distance)和明可夫斯基距离(Minkowski Distance)
- SpringBoot 中定时执行注解(@Scheduled、@EnableScheduling)
- JavaScript弹出框、对话框、提示框、弹窗总结
- Python使用traceback.print
- shiro反序列化漏洞的原理和复现
- Qt QSqlQueryModel实现查询数据库内容