POJ 2573 Bridge 贪心
2014-04-02 21:18
330 查看
Bridge
Description
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 flashlight. Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return
the flashlight 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 first 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
The first 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 specified 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 flashlight so that more may cross. If more than one strategy yields the minimal time, any one will do.
Sample Input
Sample Output
Source
Waterloo local 2000.09.30
百度下才发现是很经典的贪心问题……看来真是刷题太少
POJ 1700,2573,3404 都是一个类型,买一赠二好实惠啊……
kdwycz的网站: http://kdwycz.com/
kdwyz的刷题空间:http://blog.csdn.net/kdwycz
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3220 | Accepted: 1144 | Special Judge |
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 flashlight. Only one flashlight is available among the n people, so some sort of shuttle arrangement must be arranged in order to return
the flashlight 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 first 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
The first 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 specified 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 flashlight so that more may cross. If more than one strategy yields the minimal time, any one will do.
Sample Input
4 1 2 5 10
Sample Output
17 1 2 1 5 10 2 1 2
Source
Waterloo local 2000.09.30
百度下才发现是很经典的贪心问题……看来真是刷题太少
POJ 1700,2573,3404 都是一个类型,买一赠二好实惠啊……
#include <iostream> #include <algorithm> using namespace std; int a[1010],n,i,nn,ans=0; int main() { cin>>n; for (i=1;i<=n;++i) cin>>a[i]; if (n>1){ sort(a+1,a+n+1); for (nn=n;nn>3;nn-=2) ans+=a[1]+a[nn-1]<2*a[2]?2*a[1]+a[nn-1]+a[nn]:a[1]+2*a[2]+a[nn]; if (nn==2) ans+=a[2]; else ans+=a[1]+a[2]+a[3]; cout<<ans<<endl; for (nn=n;nn>3;nn-=2) if (a[1]+a[nn-1]<2*a[2]) cout<<a[1]<<' '<<a[nn-1]<<endl<<a[1]<<endl<<a[1]<<' '<<a[nn]<<endl<<a[1]<<endl; else cout<<a[1]<<' '<<a[2]<<endl<<a[1]<<endl<<a[nn-1]<<' '<<a[nn]<<endl<<a[2]<<endl; if (nn==2) cout<<a[1]<<' '<<a[2]<<endl; else cout<<a[1]<<' '<<a[2]<<endl<<a[1]<<endl<<a[1]<<' '<<a[3]<<endl; } if (n==1) cout<<a[1]<<endl<<a[1]<<endl; return 0; }
kdwycz的网站: http://kdwycz.com/
kdwyz的刷题空间:http://blog.csdn.net/kdwycz
相关文章推荐
- poj 2573 Bridge(贪心:过河问题)
- poj 2573 Bridge (过桥问题 贪心)
- poj 2573 bridge
- poj 3404 Bridge over a rough river 贪心
- POJ 3404 Bridge over a rough river 贪心,每次算最大的两个人去对岸的最少用时
- POJ2573 Bridge 经典的过桥问题
- POJ-2573-Bridge
- poj 2573 Bridge(有A、B、C、D四个人,要在夜里过一座桥……)
- POJ2718(枚举 + 贪心)
- poj 3553 贪心+拓扑排序
- POJ 2456 Aggressive cows(二分+贪心 计算个数)
- POJ1989 The Cow Lineup 额,贪心??
- POJ 1456 supermarket(贪心加并查集)
- poj 3622 Gourmet Grazers 挑剔的美食家(贪心)
- POJ 1017 Packets(贪心)
- POJ 2586 Y2K Accounting Bug贪心
- Poj 3040 Allowance【贪心模拟】
- POJ 1328(贪心)
- poj 3608 Bridge Across Islands
- poj-1017 Packets (贪心)