poj--1700
2015-10-15 21:44
381 查看
Crossing River
DescriptionA group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a differentrowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.InputThe first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each caseis preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.OutputFor each test case, print a line containing the total number of seconds required for all the N people to cross the river.Sample Input
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12274 | Accepted: 4647 |
1 4 1 2 5 10Sample Output
17
题目来源:http://poj.org/problem?id=1700
考查知识点:贪心算法
解体思路:这算是一道经典的贪心题目,但我做这道题时,完全没思路,贪心的题要求有很好的思维能力。现在先解释一下为什么样例的结果会是17,而不是19。首先1,2过河,1回来,time=3;再5,10,过河,2回来,time=15;最后1,2过河,time=17。由此可见得,在过河时,可以先把速度最慢的人送过去,但在送过去时有两种方法。一种是反复利用最快的人,一种是在河对岸留一个第二快的人,与第一块的人相互配合。
最佳方案构造法:以下是构造N个人(N≥1)过桥最佳方案的方法: > 1) 如果N=1、2,所有人直接过桥。 > 2) 如果N=3,由最快的人往返一次把其他两人送过河。 > 3) 如果N≥4,设A、B为走得最快和次快的旅行者,过桥所需时间分别为a、b;而Z、Y为走得最慢和次慢的旅行者,过桥所需时间分别为z、y。那么 当2b>a+y时,使用模式一将Z和Y移动过桥; 当2b<a+y时,使用模式二将Z和Y移动过桥;当2b=a+y时,使用模式一将Z和Y移动过桥。这样就使问题转变为N-2个旅行者的情形,从而递归解决之。
代码如下:
#include<stdio.h>#include<algorithm>using namespace std;long long v[1010];int cmp(int a,int b){return a<b;}int main(){int t,n,i;long long sum;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<n;i++)scanf("%lld",&v[i]);sort(v,v+n,cmp);sum=0;// if(n<=2)// printf("%lld\n",v[n-1]);// else if(n==3)// printf("%lld\n",v[0]+v[1]+v[2]);while(n>=4){if(2*v[1]>=v[0]+v[n-2])sum+=2*v[0]+v[n-2]+v[n-1];elsesum+=v[1]+v[0]+v[n-1]+v[1];n-=2;}if(n==3){sum+=v[0]+v[1]+v[2];}else if(n==2){sum+=v[1];}else if(n==1)sum+=v[0];printf("%lld\n",sum);}return 0;}再贴一个别人写的dp,太牛了[/code]
代码如下:
#include<cstdio>#include<algorithm>#include<iostream>using namespace std;int data[10050],dp[10050];int One(int i){return data[i]+data[0];}//利用跑得最快的人int Two(int i){return data[i+1]+data[0]+2*data[1];}//最快于第二快配合int main(){int T,n,i,j,ans,tmp;cin>>T;while(T--){scanf("%d",&n);for(i=0;i<n;i++) scanf("%d",&data[i]);sort(data,data+n);dp[0]=data[0],dp[1]=data[1],dp[2]=data[0]+data[1]+data[2];for(i=3;i<n;i++)dp[i]=min(dp[i-1]+One(i),dp[i-2]+Two(i-1));printf("%d\n",dp[n-1]);}}[/code]
相关文章推荐
- request.getParameterValues()用法
- Tomcat安全配置与性能优化
- EasyUI 关于 panel,window,dialog 通过href加载页面,页面中引用的js不执行的解决方案
- POJ2947 Widget Factory 高斯消元+扩展GCD解同余方程组
- C++设计模式——单例模式
- Spire.DOC生成表格测试
- UVa11300 - Spreading the Wealth
- About NoSql
- 微软将在明年1月停止支持多个旧版本 IE 浏览器
- 奇葩css
- 开源分布式计算系统框架比较
- linux dd、echo 、watch、fuser命令
- 继承作业
- <iframe> 标签的 sandbox 属性
- 录制音频
- 判断是否是变形字符串
- Lesson01UIView 用户界面
- NoSQL数据库是否适用于非互联网行业
- 浅析C++中的重载运算符(重点是重载+=)
- Wing IDE 5.1.8的爆破点