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. 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<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i < b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N  5005

int a[N],n;

int main()
{
	int i,j,t;
	sf(t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
			sf(a[i]);
		if(n==1)
		{
			pf("%d\n",a[0]);
			continue;
		}
		int ans=0;
		sort(a,a+n);
		int e=n-1;
		while(1)
		{
			if(e==1)
			{
				ans+=max(a[0],a[1]);    //两个人
				break;
			}
            if(e==2)
			{
				ans+=a[0]+a[1]+a[2];   //三个人
				break;
			}
			//送最慢两个人过河
			ans+=min(a[e]+a[0]+a[e-1]+a[0],a[1]+a[e]+a[0]+a[1]);   //0 e 去 0回  0 e-1  去,0回
			e-=2;                                                  //0 1 去,0回,e,e-1去,1回
		}
        pf("%d\n",ans);
	}
  return 0;
}
时间: 2024-08-02 10:42:50

POJ 1700 Crossing River(贪心)的相关文章

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

[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

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

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 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[

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

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

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

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

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