1700 Crossing River

题目链接: http://poj.org/problem?id=1700

1. 当1个人时: 直接过河 t[0].

2. 当2个人时: 时间为较慢的那个 t[1].

3. 当3个人时: 时间为 t[0]+t[1]+t[2].

4. 当4个以上的人时, 将t[0] 和 t[1]当作搬运工. 两种方案:

  - 最快和次快的过去, 最快的回来, 最慢和次慢的过去, 次快的回来, 这样就将最慢和次慢的送过去了. 时间: t[0]+2*t[1]+t[i]

  - 最快的依次送最慢和次慢的过去再回来, 时间: 2*t[0]+t[i-1]+t[i]

  以上方案每次就将最慢的和次慢的送过河, 每次送走两个人, 当将其他人送完之后, t[0]和t[1]再过河.

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 bool comp(int a,int b){
 5     return a<b;
 6 }
 7 int main(){
 8     int m,n,t[10001],i,sum;
 9     cin>>m;
10     while(m--){
11         cin>>n;
12         sum = 0;
13         for(i=0;i<n;++i)
14             cin>>t[i];
15         sort(t,t+n,comp);
16         for(i=n-1;i>2;i-=2){
17             //把t[0]和t[1]当成搬运工,最后返回到原地
18             int t1 = t[0]+2*t[1]+t[i];//最快的(即所用时间t[0])和次快的过河,然后最快的将船划回来,再次慢的和最慢的过河,然后次快的将船划回来
19             int t2 = 2*t[0]+t[i-1]+t[i];//最快的和最慢的过河,然后最快的将船划回来,再最快的和次慢的过河,然后最快的将船划回来
20             sum += t1>t2?t2:t1;
21         }
22         if(i==2) //3people left
23             sum += t[0]+t[1]+t[2];
24         else if(i==1) //2 people left
25             sum += t[1];
26         else  // 1 people left
27             sum += t[0];
28         cout<<sum<<endl;
29     }
30     return 0;
31 }
时间: 2024-11-08 20:11:28

1700 Crossing River的相关文章

POJ 1700 Crossing River(贪心)

V - Crossing River Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1700 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. There

poj 1700 Crossing River

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12585   Accepted: 4787 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 arr

[ACM] poj 1700 Crossing River (经典过河问题)

Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10212   Accepted: 3855 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 arr

poj 1700 Crossing River C++/Java

http://poj.org/problem?id=1700 题目大意: 有n个人要过坐船过河,每一个人划船有个时间a[i],每次最多两个人坐一条船过河.且过河时间为两个人中速度慢的,求n个人过河的最短时间. 思路: 贪心. 对于每次过河的,有两种情况: //最快和最慢过去,然后最快回来.在和次慢过去.最快回来 int action1=a[i-1] + a[0] + a[i-2] +a[0]; //最快和次慢过去,然后最快回来,在次慢和最慢过去,次慢回来 int action2=a[1] +a[

ACM学习历程——POJ 1700 Crossing River(贪心)

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. Eac

poj 1700 Crossing River(贪心)

分析:题意 源岸数量<=3  很好判断 3:num[0]+num[1]+num[2] 2:num[1] ,1:num[0] 源岸数量>3 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main(void) { int n,t ; int i,j; int num[1001]; cin>>

POJ 1700 cross river (数学模拟)

 Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10311   Accepted: 3889 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

poj1700 Crossing River

Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12585 Accepted: 4787 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 arrange

hihocoder-Week175-Robots Crossing River

hihocoder-Week175-Robots Crossing River Robots Crossing River 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Three kinds of robots want to move from Location A to Location B and then from Location B to Location C by boat. The only one boat between A and B an