poj1700真正感受到贪心的力量

Posted 不积跬步无以至千里,不积小流无以成江海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj1700真正感受到贪心的力量相关的知识,希望对你有一定的参考价值。

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真正感受到贪心的力量的主要内容,如果未能解决你的问题,请参考以下文章

POJ-3045 Cow Acrobats---贪心

poj - 1700 题解

贪心算法----区间覆盖问题(POJ2376)

阶段性总结-贪心算法

感受Vue3的魔法力量

poj 3253 Fence Repair 贪心