ZOJ 1877 Bridge(贪心)
来源:互联网 发布:自学办公软件视频教程 编辑:程序博客网 时间:2024/06/08 02:31
不要乱猜题目,要分情况讨论,通过验证得出结论,这就是贪心。
题目的主要意思是一群人要过桥,每次至多两个人一起过,过桥需要手电筒,而他们只有一个手电筒,这意味着每次都要有一个人把手电筒送回来。每个人的速度不同,过桥用的时间也不一样,求最短的时间。
主要先理解算法思路:先分析四个人过河的情况,然后再推广到 n 个人。
1. 先讨论四个人的情况,一共是有两种方案的:
假设四个步速为a < b < c < d,
方案一:AB过A回 CD过B回 AB过,时间为 b + a + d + b + b;
方案二:AC过A回 AD过A回 AB过 ,时间为 c + a + d + a + b;
比较两者时间,关键在 2*b 与 a+c 的大小 。
2. 当人数大于四时,四步一组,每次优先将最慢 的两个送到对岸;当人数小于四时,单独计算。
n
people wish to cross a bridge at night. A group of at most two people may cross at any time, and
each group must have a ashlight. Only one ashlight is available among the
n
people, so some sort of
shuttle arrangement must be arranged in order to return the ashlight so that more people may cross.
Each person has a different crossing speed; the speed of a group is determined by the speed of the
slower member. Your job is to determine a strategy that gets all
n
people across the bridge in the
minimum time.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases
following, each of them as described below. This line is followed by a blank line, and there is also a
blank line between two consecutive inputs.
The rst line of input contains
n
, followed by
n
lines giving the crossing times for each of the people.
There are not more than 1000 people and nobody takes more than 100 seconds to cross the bridge.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases
will be separated by a blank line.
The rst line of output must contain the total number of seconds required for all
n
people to cross
the bridge. The following lines give a strategy for achieving this time. Each line contains either one or
two integers, indicating which person or people form the next group to cross. (Each person is indicated
by the crossing time speci ed in the input. Although many people may have the same crossing time
the ambiguity is of no consequence.)
Note that the crossings alternate directions, as it is necessary to return the ashlight so that more
may cross. If more than one strategy yields the minimal time, any one will do.
SampleInput
1
4
1
2
5
10
SampleOutput
17
1 2
1
5 10
2
1 2
AC代码:#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<queue>#include<map>#include<set>#define INF 999999999#define LL long long#define mod 1000003using namespace std;int main(){int t;scanf("%d",&t);while(t--) { int n; scanf("%d",&n); int a[1005]; for(int i=0;i<n;i++) scanf("%d",a+i); int sum=0; sort(a,a+n); int cnt=0; int j=n-1; while(cnt<=n-4) { if(2*a[1]<a[0]+a[j-1]) sum+=a[1]+a[0]+a[j]+a[1]; else sum+=a[j-1]+a[0]+a[j]+a[0]; cnt+=2; j-=2; } switch(n-cnt) { case 1:sum+=a[0];break; case 2:sum+=a[1];break; case 3:sum+=a[0]+a[1]+a[2];break; } printf("%d\n",sum); cnt=0; j=n-1; while(cnt<=n-4) { if(2*a[1]<a[0]+a[j-1]) { printf("%d %d\n%d\n",a[0],a[1],a[0]); printf("%d %d\n%d\n",a[j-1],a[j],a[1]); } else { printf("%d %d\n%d\n",a[0],a[j-1],a[0]); printf("%d %d\n%d\n",a[0],a[j],a[0]); } cnt+=2; j-=2; } switch(n-cnt) { case 1:printf("%d\n",a[0]);break; case 2:printf("%d %d\n",a[0],a[1]);break; case 3:printf("%d %d\n%d\n%d %d\n",a[0],a[1],a[0],a[0],a[2]);break; } if(t!=0) printf("\n"); }}
0 0
- ZOJ 1877 Bridge(贪心)
- ZOJ 1877 Bridge
- ZOJ-1877 Bridge
- ZOJ 1877 Bridge
- UVA 10037 Bridge (贪心)
- ZOJ 2343(贪心)
- ZOJ 3715 (贪心)
- zoj-3721(贪心)
- poj 2573 Bridge (过桥问题 贪心)
- ZOJ _3607_Lazier Salesgirl(贪心)
- ZOJ 2921 Stock(贪心)
- zoj 2921 Stock(贪心)
- ZOJ 3953 Intervals (贪心)
- ZOJ 3987 Numbers (贪心)
- zoj 1579 Bridge
- ZOJ 3472 Play Bridge
- ZOJ 2614 Bridge
- ZOJ 1579 Bridge
- 扫描二维码下载app
- 基于keepalived和ServiceStack.Redis组件的redis链接分析
- Java enum的用法详解
- HDU 5600 N bulbs(机智)
- java常见面试题及答案
- ZOJ 1877 Bridge(贪心)
- Socket相关收集
- 四元数与欧拉教的转换权威材料
- 图片自适应
- Android系统
- jdbc查询数据库返回实体对象集合
- ADO方式连接数据库
- POJ 3667 Hotel 数据结构+维护区间长度+区间赋值+寻找位置
- Hive常见数据压缩