POJ1700Crossing River(过河问题)
来源:互联网 发布:excel画出数据形状 编辑:程序博客网 时间:2024/06/10 17:38
Crossing River
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 16551 Accepted: 6283
Description
A 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 different rowing 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.
Input
The 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 case is preceded by a blank line. There won't be more than 1000 people and nobody takes more than 100 seconds to cross.
Output
For each test case, print a line containing the total number of seconds required for all the N people to cross the river.
Sample Input
141 2 5 10
Sample Output
17
题意:n个人过河,一次最多过两人,时间取决于最慢的,求全部渡过河的所用最小时间。
思路:
通过总结分析可得当n为1时,用时a[1],当n为2时,取决于二者最大时间,当n为3时,最小时间为三人用时总和。
当n大于等于4时有两种渡河方案:1、最快和次快先过,最快回,然后当前最慢和当前次慢的过,次快回。time=a[2]+a[n]+a[1]+a[2].2、始终最快和当前最慢的过河,最快回。在取时间时,要从两者中选最小。time=a[n]+a[1]+a[n-1]+a[1].
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[1001];int min(int a,int b){ if(a>b) return b; else return a;}int digui(int n){ if(n==1) return a[1]; if(n==2) return a[2]; if(n==3) return a[1]+a[2]+a[3]; int sum=0,i; for(i=2;i<=n;i++) { sum+=a[i]; } return min(sum+a[1]*(n-2),a[1]+2*a[2]+a[n]+digui(n-2));}//最后一个return通过列举连续的例子比较好得出结论。int main(){ int t,n,i,j,temp; scanf("%d",&t); while(t--) { int min_time=0; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } sort(a+1,a+1+n); min_time=digui(n); printf("%d\n",min_time); } return 0;}代码中的递归部分也可以用while循环处理 。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[1001];int min(int a,int b){ if(a>b) return b; else return a;}int main(){ int t,n,i,j,temp; scanf("%d",&t); while(t--) { int time=0; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } sort(a+1,a+1+n); time=0; while(n>0) { if(n==1) { time+=a[1]; break; } else if(n==2) { time+=a[2]; break; } else if(n==3) { time+=a[1]+a[2]+a[3]; break; } else { time=time+min(a[n]+2*a[1]+a[n-1],2*a[2]+a[1]+a[n]); n-=2; // printf("%d\n",time); } } printf("%d\n",time); } return 0;}while循环对本题的解释更明确清晰。
阅读全文
1 0
- POJ1700Crossing River(过河问题)
- poj1700Crossing River(这过河真经典)
- poj1700Crossing River
- poj1700Crossing River【贪心】
- POJ1700 Crossing River 过河问题
- [ACM] poj 1700 Crossing River (经典过河问题)
- POJ 3258 River Hopscotch(牛过河问题,二分)
- POJ--1700--Crossing River--过河问题
- poj 1700 Crossing River 过河问题
- POJ 1700 过河问题(Crossing River)
- poj 1700 Crossing River && nyoj 47 过河问题
- nyoj-47-过河问题&&poj-1700-Crossing River
- NYOJ 47 过河问题&&POJ 1700 Crossing River
- POJ 1700 Crossing River 过河问题 贪心法
- 【NOI OJ】 4.6 贪心 702 Crossing River(过河)
- 压缩路径,动态规划 过河(River)题解
- POJ 1700 解题报告——Crossing River:求过河的最少时间问题
- ZZUOJ 10496: Cross the River (最短路 Dij 青蛙过河)
- Git命令上传和克隆工程
- scala类参数修饰符浅析
- MyBatis 01
- FreeType之字形分析(一)
- 堆
- POJ1700Crossing River(过河问题)
- AsyncTask基本使用
- scala类型系统:1) 类型与类
- 单点登录
- [Android-ARCore开发]ARCore从入门到放弃2-Demo介绍和扩展
- classOf与getClass方法的差异
- CSS 相邻兄弟选择器
- 【设计模式 五】观察者模式
- 18考研-数据结构复习笔记-栈,队列,数组