过河问题
没有找2个方法比较,只用了一个方法。wa了。
全局变量换成局部变量也wa了。换成memset(a,0,sizeof(a));就可以。
/*题意:
有n个人过河,只有一条船,每次只能坐2人,每个人过河所需时间不同,求全部过河所需时间。
解题思路:
当人数等于1时:sum=Num[1];
当人数等于2时:sum=Num[2];
当人数等于3时:sum=Num[3]+Num[2]+Num[1];
当人数大于等于4时:
若设过桥速度最快的那个人过桥时间为a,第二快为b;过桥第二慢的那个人过桥时间为c,最慢为d;
此时有两种过桥方案:
一.最快和次快的人先过,然后最快的回来,然后最慢与次慢的人再过,次快的回来;
二.最快的和最慢的过,快的回来,在和次慢的过,快的再回来;
第一种方法时间为b*2+a+d;
第二种方法时间为c+d+2*a
如果第一种大于第二种 有2*b+a+d>c+d+2*a
化简得
2*b>c+a;*/
Problem F
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 8
Problem 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
1
4
1 2 5 10
Sample Output
17
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; int n,a[1006];//如果定义局部变量就会wa,定义全局变量就ac了。 int main() { int t,i,cnt; scanf("%d",&t); while(t--) { cnt=0; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&a[i]); sort(a,a+n+1); while(n) { if(n==1) { cnt+=a[1]; break; } else if(n==2) { cnt+=a[2]; break; } else if(n==3) { cnt+=a[1]+a[2]+a[3]; break; } else { if(2*a[2]>(a[1]+a[n-1])) cnt+=2*a[1]+a[n-1]+a[n]; else cnt+=2*a[2]+a[1]+a[n]; n-=2; } } printf("%d ",cnt); } return 0; }