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;
    }
  • 相关阅读:
    实现LNMP
    iptabes的用法
    实现LAMP
    实现https
    硬盘信息和磁盘分区管理
    awk
    【BZOJ 2288】 2288: 【POJ Challenge】生日礼物 (贪心+优先队列+双向链表)
    【BZOJ 1150】 1150: [CTSC2007]数据备份Backup (贪心+优先队列+双向链表)
    【BZOJ 1216】 1216: [HNOI2003]操作系统 (模拟+优先队列)
    【BZOJ 1528】 1528: [POI2005]sam-Toy Cars (贪心+堆)
  • 原文地址:https://www.cnblogs.com/--lr/p/7751252.html
Copyright © 2011-2022 走看看