zoukankan      html  css  js  c++  java
  • POJ 1700 Crossing River (贪心)


    Crossing River
    Time Limit: 1000MSMemory Limit: 10000K
    Total Submissions: 9585Accepted: 3622

    Description

    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

    141 2 5 10

    Sample Output

    17

    Source


    当人数等于1,2,3的时候:答案很容易得出;
    当人数大于等于4时:

    若设过桥速度最快的那个人过桥时间为a,第二快为b;过桥第二慢的那个人过桥时间为y,最慢为z;
    此时有两种过桥方案:
    一.最快和次快的人先过,然后最快的回来,然后最慢与次慢的人再过,次快的回来;
    二.最快的和最慢的过,快的回来,在和次慢的过,快的再回来;

    第一种方法时间为b*2+a+z
    第二种方法时间为y+z+2*a

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <deque>

    using namespace std;

    int a[1111];

    int main()
    {
        int T;
        cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>a;
        }

        sort(a,a+n);

        deque<int> dq;
        for(int i=0;i<n;i++)
            dq.push_back(a);

        int ans=0;
        while(!dq.empty())
        {
            if(dq.size()==1)
            {
                ans+=dq.front();
                break;
            }
            else if(dq.size()==2)
            {
                ans+=dq.back();
                break;
            }
            else if(dq.size()==3)
            {
                int a=dq.front();
                dq.pop_front();
                int b=dq.front();
                dq.pop_front();
                int c=dq.front();

                ans+=(a+b+c);
                break;
            }
            else if(dq.size()>=4)
            {
                int a,b,c,d;
                a=dq.front();
                dq.pop_front();
                b=dq.front();
                dq.pop_front();
                d=dq.back();
                dq.pop_back();
                c=dq.back();
                dq.pop_back();

                int t1=a+d+2*b;
                int t2=c+d+2*a;

                ans+=min(t1,t2);
                dq.push_front(b);
                dq.push_front(a);
            }
        }
        printf("%d ",ans);
    }

        return 0;
    }

  • 相关阅读:
    new Handler()和new Handler(Looper.getMainLooper())的区别
    Okhttp3日志采集功能
    【ACM打卡】ZOJ 1001 1048
    20180808 阿里巴巴测试开发工程师一面
    20180601 -1
    20180601
    20180601 指针
    20180601 虚函数
    20180601 C++-1
    20180601 C++
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350996.html
Copyright © 2011-2022 走看看