POJ 1700 && nyoj 47 Crossing River(贪心)

来源:互联网 发布:举报淘宝店铺会怎么样 编辑:程序博客网 时间:2024/04/29 16:00
Crossing River
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 12089 Accepted: 4591

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

141 2 5 10

Sample Output

17
    题意: 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边。如果不借助手电筒的话,大家是无论如何也不敢过桥去的。不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过。如果各自单独过桥的话,N人所需要的时间已知;而如果两人同时过桥,所需要的时间就是走得比较慢的那个人单独行动时所需的时间。问题是,如何设计一个方案,让这N人尽快过桥。
      
      设时间为t1,t2,t3,...,tn
      n=1时,则只有一种情况
      n=2时,两个人同时过河,时间为两者较大的一个
      n=3时,从大到小已经排好序,最慢的先和最快的过河,然后最快的回来,最快的再和第二块的过河,总时间为t1+t2+t3;
      n>=4时,讨论n=4的情况,
1>    t1和t4先过河,时间为t4,然后t1回来,时间为t1,t1和t3过河,时间为t3,t1回来,时间为t1,t1和t2过河,时间为t2.总时间为t4+t3+2*t1+t2
 2>    t1和t2先过河,时间为t2,然后t1回来,时间为t1,t3和t4过河,时间为t4,t2回来,时间为t2,t1和t2过河,时间为t1.总时间为3*t2+t1+t4
还有几种情况,不过得出的结果都会比这两个结果大,所以只需要讨论这两个结果的大小。
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<ctype.h>#include<stdlib.h>#include<string>#include<algorithm>#include<vector>#include<set>#include<map>#include<list>#include<queue>#include<stack>#include<iomanip>#include<numeric>//#include <istream>     //基本输入流//#include <ostream>     //基本输出流//#include <sstream>     //基于字符串的流//#include <utility>     //STL 通用模板类//#include <complex.h>   //复数处理//#include <fenv.h>    //浮点环境//#include <inttypes.h>  //整数格式转换//#include <stdbool.h>   //布尔环境//#include <stdint.h>   //整型环境//#include <tgmath.h>   //通用类型数学宏#define L(a,b,c) for(int a = b;a >= c;a --)#define M(a,b,c) for(int a = b;a <= c;a ++)#define N(a,b) memset(a,b,sizeof(a));#define MAXX(a,b)   ((a)>(b)?a:b)#define MINN(a,b)   ((a)<(b)?a:b)const int MAX=1<<30;const int MIN=-MAX;using namespace std;int n,a[1010];int main(){    int T;    cin>>T;    while(T--)    {        N(a,0)        cin>>n;        M(i,0,n-1)        cin>>a[i];        sort(a,a+n);        int sum=0;        while(n>3)      ///每次都是以a[0]和a[1]为载体,把最后两个过河所需要的时间最小的相加        {            sum+=min(2*a[0]+a[n-1]+a[n-2],2*a[1]+a[0]+a[n-1]);      ///只是最后两个过河,a[0]和a[1]又回到过河之前的位置,因为接下来讨论的时候还要用到            n-=2;       ///a[n-1]和a[n-2]顺利过河        }        if(n==3)            sum+=a[0]+a[1]+a[2];        else if(n==2)            sum+=a[1];        else if(n==1)            sum+=a[0];        cout<<sum<<endl;    }    return 0;}

0 0
原创粉丝点击