• 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;
    }
  • 相关阅读:
    13 Memcached 永久数据被踢现象
    PHP 学习内容
    12 Memcached 缓存无底洞现象
    Memcached 常用的方法
    PHP Memcached 面试题
    11 Memcached 缓存雪崩现象
    JQ报表插件
    (2.1)mysql升级与降级
    基于binlog恢复工具mysqlbinlog_flashback
    如何查看正在执行sql的语句及其父语句调用?如何查看正在执行SQL的具体参数值与执行计划?xml执行计划转为图形计划
  • 原文地址:https://www.cnblogs.com/--lr/p/7751252.html
Copyright © 2020-2023  润新知