zoukankan      html  css  js  c++  java
  • 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
    ",a[0]);
                }
                else if(n==2)
                {
                    printf("%d
    ",a[1]);
                }
                else if(n==3)
                {
                    printf("%d
    ",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;
    }
  • 相关阅读:
    等待
    QToolBox工具箱
    组合框QGroupBox
    把ui界面加入到工程中
    assistant文档
    日期与时间控件QDate, QTime, QDateTime
    日历控件QCalendarWidget
    液晶数字显示屏QLCDNumbe
    ffpanel --ffmpeg的GUI,让ffmpeg离开黑黑的命令行
    使用Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度
  • 原文地址:https://www.cnblogs.com/--lr/p/7751252.html
Copyright © 2011-2022 走看看