码迷,mamicode.com
首页 > 其他好文 > 详细

poj1700真正感受到贪心的力量

时间:2017-10-29 21:55:41      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:must   input   you   return   这一   end   一点   iostream   提前   

Crossing River

        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


贪心策略
不妨设过河时间a<b<c<d
两种策略
1.a,b过河,a回 c,d过河,b回
2.a,c过河,a回 a,d过河,a回
比较两种策略哪种更好
总人数小于四时提前讨论好,这一点我忘记了
然后再分个奇偶就解决了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <algorithm>
using namespace std;
int a[1010],vis[1010];
int n;
long long sum;
int main()
{
    int t;
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            memset(vis,0,sizeof(vis));
            sum = 0;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
            }
            sort(a,a+n);
            if(n==1)
            {
                printf("%d\n",a[0]);
            }
            else if(n==2)
            {
                printf("%d\n",a[1]);
            }
            else if(n==3)
            {
                printf("%d\n",a[0]+a[1]+a[2]);
            }
            else
            {
                int i1=0,i2=1,i3=n-2,i4=n-1;
                while(i3>=2&&i4>=3)
                {
                    if((a[i1]*2+a[i3]+a[i4])<(a[i1]+a[i4]+a[i2]*2))
                    {
                        sum+=(a[i1]*2+a[i3]+a[i4]);
                        i3-=2;
                        i4-=2;
                    }
                    else
                    {
                        sum+=(a[i1]+a[i4]+a[i2]*2);
                        i3-=2;
                        i4-=2;
                    }
                }
                if(i3==0)
                {
                    sum+=a[1];
                    cout<<sum<<endl;
                }
                else if(i3==1)
                {
                    sum+=(a[0]+a[1]+a[2]);
                    cout<<sum<<endl;
                }
            }
        }
    }
    return 0;
}

 

poj1700真正感受到贪心的力量

标签:must   input   you   return   这一   end   一点   iostream   提前   

原文地址:http://www.cnblogs.com/--lr/p/7751252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!