poj-1700 crossing river(贪心题)
程序员文章站
2023-01-13 15:29:14
题目描述: 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 arrangem ......
题目描述:
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
1 4 1 2 5 10
sample output
17
题目大意:过河,速度取慢的
题解:有点像汉诺塔的感觉,贪心算法,直接看代码吧,大水题
#include<iostream> #include<algorithm> #include<cstring> #include<sstream> #include<cmath> #include<cstdlib> #include<queue> using namespace std; #define pi 3.14159265358979323846264338327950 int t,n,a[1003],t; int main() { scanf("%d",&t); while(t--) { t=0; scanf("%d",&n); memset(a,0,sizeof(a)); for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); if(n==1) t=a[0]; else if(n==2) t=a[1]; else if(n==3) t=a[0]+a[1]+a[2]; else { for(int i=n-1;i>0;i=i-2) { if(i>=3) { if(a[0]+a[i-1]>=2*a[1]) t=t+a[0]+2*a[1]+a[i]; else t=t+2*a[0]+a[i]+a[i-1]; } if(i==2) t=t+a[0]+a[1]+a[2]; if(i==1) t=t+a[1]; } } printf("%d\n",t); } }
上一篇: poj-3253 fence repair(贪心题)
下一篇: JS中数组的使用方法解析