POJ1700:Crossing River(过河问题)

    POJ1700

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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

题解:

这是一个过河坐船问题,一共有两个策略
①最快和次快过去,最快回;最慢和次慢过去,次快回,最快的和次快的过去,t1=a[1]+a[0]+a[n-1]+a[1]+a[1]。②最快和最慢过去,最快回;最快和次快过去,最快回,最快的和次慢的过去,t2=a[n-1]+a[0]+a[1]+a[0]+a[n-2]。选择两者中用时较少的一个策略执行,判断t1与t2大小,只需要判断2a[1]是否大于a[0]+a[n-2],如此便将最慢和次慢送过河,对剩下n-2个人循环处理。注意当n=1、n=2、n=3时直接相加处理即可.

#include<iostream>
#include<algorithm>
using namespace std;
int n,a[1006];
int main()
{
  int t,i;
  cin>>t;;
  while(t--)
{
  int f=0;//每次f归0
  cin>>n;
  for(i=1; i<=n; i++)
    cin>>a[i];
  sort(a,a+n+1);
  while(n)
{
  if(n==1)
{
    f+=a[1];
      break;
}
  if(n==2)
{
      f+=a[2];
      break;
}
  if(n==3)
{
      f+=a[1]+a[2]+a[3];
      break;
}
  if(n>3)
{
      if(2*a[2]>(a[1]+a[n-1]))
    f+=2*a[1]+a[n]+a[n-1];
else
    f+=2*a[2]+a[1]+a[n];
    n=n-2;//注意循环
}
}
  cout<<f<<endl;
}
  return 0;
}

时间: 2024-10-13 02:08:19

POJ1700:Crossing River(过河问题)的相关文章

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

POJ1700 Crossing River(贪心算法训练)

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 13301          Accepted: 5087 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 shu

[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(贪心)

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

bzoj 1314: River过河 优先队列

1314: River过河 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 26  Solved: 10[Submit][Status][Discuss] Description ZY 带N个小Kid过河,小KID分成两种:高一年级,高二年级,由于存在代沟问题,如果同一条船上高一年级生和高二年级生数量之差超过K,就会发生不和谐的 事件.当然如果一条船上全是同一年级的,就绝对不会发生争执.现在ZY按小KID队列的顺序依次安排上船,并且不能让他们在过河

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

Crossing River POJ过河问题

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

【POJ1700】Crossing River 贪心,附贪心问题的一系列详细解析

题意:一群人过河,船每次只能装两人,每次过河时间为两人权值较大的那个. 题解: 这种题的贪心策略往往不是很好想,这个时候我们就需要依照尽量逼近正解的思路,进行多种贪心,在每种贪心都保证正确的前提下,取多个答案的最值,这样往往就是正解,而即便可以卡,数据也很难出,并不是写个rand+debug拍上两个小时就能拍出来的. 而这种 多线程贪心 可以有两种: 一.单独做每种,然后取最优. 二.每一步转移都用多种贪心取得,然后直接取最后的状态值. 显然后一种的正确性比较高,但是往往因为"贪心"的